changeset 1168:808503d8c5cc

xidle support graph/events streams, use actmod module for the input now Ignore-this: 925a9bb8419fe9f81df40ad54d082c darcs-hash:14ff4f89a20c11a21853bb0ef486ddb012b6b49e
author drewp <drewp@bigasterisk.com>
date Sun, 09 Sep 2018 04:06:59 -0700
parents 03e132cd351b
children d9c2b59e760d
files service/xidle/xidle.py
diffstat 1 files changed, 22 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/service/xidle/xidle.py	Sun Sep 09 04:06:00 2018 -0700
+++ b/service/xidle/xidle.py	Sun Sep 09 04:06:59 2018 -0700
@@ -29,9 +29,13 @@
 host = socket.gethostname()
 client = InfluxDBClient('bang6', 9060, 'root', 'root', 'main')
 
+os.environ['DISPLAY'] = ':0.0'
+
+actmon.get_idle_time() # fail if we can't get the display or something
+
 class Root(cyclone.web.RequestHandler):
     def get(self):
-        xss.get_info() # fail if we can't get the display or something
+        actmon.get_idle_time() # fail if we can't get the display or something
         self.write('''
       Get the <a href="idle">X idle time</a> on %s.
       <a href="graph">rdf graph</a> available.''' % host)
@@ -39,21 +43,8 @@
 class Idle(cyclone.web.RequestHandler):
     def get(self):
         self.set_header('Content-type', 'application/json')
-        self.write(json.dumps({"idleMs" : xss.get_info().idle}))
+        self.write(json.dumps({"idleMs" : actmon.get_idle_time()}))
         
-class Graph(cyclone.web.RequestHandler):
-    def get(self):
-        self.set_header('Content-type', 'application/x-trig')
-
-        g = StateGraph(ctx=DEV['xidle/%s' % host])
-
-        ms = xss.get_info().idle
-        subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host)
-        g.add((subj, ROOM['idleTimeMs'], Literal(ms)))
-        g.add((subj, ROOM['idleTimeMinutes'], Literal(ms / 1000 / 60)))
-
-        self.write(g.asTrig())
-
 class Poller(object):
     def __init__(self):
         self.points = []
@@ -62,7 +53,13 @@
         task.LoopingCall(self.poll).start(5)
         
     def poll(self):
-        ms = xss.get_info().idle
+        ms = actmon.get_idle_time()
+        ctx = DEV['xidle/%s' % host]
+        subj = URIRef("http://bigasterisk.com/host/%s/xidle" % host)
+        masterGraph.patchObject(ctx, subj, ROOM['idleTimeMs'], Literal(ms))
+        masterGraph.patchObject(ctx, subj, ROOM['idleTimeMinutes'],
+                                Literal(ms / 1000 / 60))
+        
         lastMinActive = ms < 60 * 1000
         now = int(time.time())
         if self.lastSent != lastMinActive or now > self.lastSentTime + 3600:
@@ -73,15 +70,22 @@
             self.lastSent = lastMinActive
             self.lastSentTime = now
 
-            client.write_points(self.points, time_precision='s')
+            try:
+                client.write_points(self.points, time_precision='s')
+            except influxdb.exceptions.InfluxDBServerError as e:
+                print repr(e)
+                reactor.crash()
             self.points = []
 
+            
+masterGraph = PatchableGraph()
 poller = Poller()
             
 reactor.listenTCP(9107, cyclone.web.Application([
     (r'/', Root),
     (r'/idle', Idle),
-    (r'/graph', Graph),
+    (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}),
+    (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
 ]), interface='::')
 
 reactor.run()