diff service/arduinoNode/arduinoNode.py @ 456:12f9f1838fb5

arduinoNode: build updates, stats support, etc Ignore-this: 39386eb7644e3cf522e0f72a874eadba
author drewp@bigasterisk.com
date Sat, 20 Apr 2019 23:28:29 -0700
parents 13b2a61650c1
children 5c750342604d
line wrap: on
line diff
--- a/service/arduinoNode/arduinoNode.py	Fri Apr 19 14:24:09 2019 -0700
+++ b/service/arduinoNode/arduinoNode.py	Sat Apr 20 23:28:29 2019 -0700
@@ -11,6 +11,8 @@
 from twisted.internet.threads import deferToThread
 from docopt import docopt
 import etcd3
+from greplin import scales
+from greplin.scales.cyclonehandler import StatsHandler
 
 import devices
 import write_arduino_code
@@ -41,7 +43,12 @@
 
 hostname = socket.gethostname()
 CTX = ROOM['arduinosOn%s' % hostname]
-etcd = etcd3.client(host='bang6')
+
+STATS = scales.collection('/root',
+)
+
+
+etcd = etcd3.client(host='bang6', port=9022)
 
 class Config(object):
     def __init__(self, masterGraph, slowMode=False):
@@ -123,7 +130,7 @@
         self.masterGraph = masterGraph
         self.dev = dev
 
-        self.masterGraph.patch(Patch(addQuads=self.staticStmts()))
+        self.masterGraph.setToGraph(self.staticStmts())
 
         # The order of this list needs to be consistent between the
         # deployToArduino call and the poll call.
@@ -181,18 +188,9 @@
                     new = new['latest']
                 else:
                     oneshot = None
-                prev = self._statementsFromInputs.get(i.uri, [])
-                if new or prev:
-                    self._statementsFromInputs[i.uri] = new
-                    # it's important that quads from different devices
-                    # don't clash, since that can lead to inconsistent
-                    # patches (e.g.
-                    #   dev1 changes value from 1 to 2;
-                    #   dev2 changes value from 2 to 3;
-                    #   dev1 changes from 2 to 4 but this patch will
-                    #     fail since the '2' statement is gone)
-                    self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri),
-                                                          inContext(new, i.uri)))
+
+                self._updateMasterWithNewPollStatements(i.uri, new)
+
                 if oneshot:
                     self._sendOneshot(oneshot)
                 self._lastPollTime[i.uri] = now
@@ -218,6 +216,20 @@
             stmts.update(v)
         self._influx.exportToInflux(stmts)
 
+    def _updateMasterWithNewPollStatements(self, dev, new):
+        prev = self._statementsFromInputs.get(dev, set())
+
+        # it's important that quads from different devices
+        # don't clash, since that can lead to inconsistent
+        # patches (e.g.
+        #   dev1 changes value from 1 to 2;
+        #   dev2 changes value from 2 to 3;
+        #   dev1 changes from 2 to 4 but this patch will
+        #     fail since the '2' statement is gone)
+        self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev),
+                                              inContext(new, dev)))
+        self._statementsFromInputs[dev] = new
+              
     def _sendOneshot(self, oneshot):
         body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3())
                          for s,p,o in oneshot)).encode('utf8')
@@ -255,7 +267,7 @@
         if unused:
             log.info("Board %s doesn't care about these statements:", self.uri)
             for s in unused:
-                log.warn("%r", s)
+                log.info("%r", s)
 
     def syncMasterGraphToHostStatements(self, dev):
         hostStmtCtx = URIRef(dev.uri + '/host')
@@ -414,6 +426,7 @@
         (r"/()", cyclone.web.StaticFileHandler, {
             "path": "static", "default_filename": "index.html"}),
         (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}),
+        (r'/stats/(.*)', StatsHandler, {'serverName': 'arduinoNode'}),
         (r'/boards', Boards),
         (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
         (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}),