Changeset - f427801da9f6
[Not reviewed]
default
0 3 0
Drew Perttula - 9 years ago 2016-06-07 10:51:23
drewp@bigasterisk.com
collector properly merges repeated attr settings in the same message
Ignore-this: a03dcc65eb0d9300cecf3153cd2a58d3
3 files changed with 34 insertions and 6 deletions:
0 comments (0 inline, 0 general)
bin/collector
Show inline comments
 
@@ -7,6 +7,7 @@ from txzmq import ZmqEndpoint, ZmqFactor
 
import json
 
import logging
 
import klein
 
import optparse
 
from greplin import scales
 
from greplin.scales.twistedweb import StatsResource
 

	
 
@@ -75,8 +76,11 @@ def launch(graph):
 
    
 
    
 
def main():
 
    log.setLevel(logging.DEBUG)
 

	
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
                      help="logging.DEBUG")
 
    (options, args) = parser.parse_args()
 
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 
    graph = SyncedGraph(networking.rdfdb.url, "collector")
 

	
light9/collector/collector.py
Show inline comments
 
@@ -61,6 +61,15 @@ class Collector(object):
 
        for c in staleClients:
 
            del self.lastRequest[c]
 

	
 
    def resolvedSettingsDict(self, settingsList):
 
        out = {}
 
        for d, a, v in settingsList:
 
            if (d, a) in out:
 
                out[(d, a)] = resolve(d, a, [out[(d, a)], v])
 
            else:
 
                out[(d, a)] = v
 
        return out
 

	
 
    def setAttrs(self, client, clientSession, settings):
 
        """
 
        settings is a list of (device, attr, value). These attrs are
 
@@ -80,14 +89,13 @@ class Collector(object):
 
                prevClientSettings = {}
 
        else:
 
            prevClientSettings = {}
 
        for d, a, v in settings:
 
            prevClientSettings[(d, a)] = v
 
        prevClientSettings.update(self.resolvedSettingsDict(settings))
 
        self.lastRequest[client] = (clientSession, now, prevClientSettings)
 

	
 

	
 
        deviceAttrs = {} # device: {attr: value}
 
        for _, _, settings in self.lastRequest.itervalues():
 
            for (device, attr), value in settings.iteritems():
 
        for _, _, lastSettings in self.lastRequest.itervalues():
 
            for (device, attr), value in lastSettings.iteritems():
 
                attrs = deviceAttrs.setdefault(device, {})
 
                if attr in attrs:
 
                    value = resolve(device, attr, [attrs[attr], value])
light9/collector/collector_test.py
Show inline comments
 
@@ -195,3 +195,19 @@ class TestCollector(unittest.TestCase):
 

	
 
        self.assertEqual([[0, 255, 0, 215], 'flush'], self.udmx.updates)
 
        self.assertEqual([[127], 'flush', [255], 'flush', [255], 'flush'], self.dmx0.updates)
 

	
 
    def testRepeatedAttributesInOneRequestGetResolved(self):
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 
        
 
        c.setAttrs('client1', 'sess1', [
 
            (DEV['inst1'], L9['brightness'], .5),
 
            (DEV['inst1'], L9['brightness'], .3),
 
        ])
 
        self.assertEqual([[127], 'flush'], self.dmx0.updates)
 

	
 
        c.setAttrs('client1', 'sess1', [
 
            (DEV['inst1'], L9['brightness'], .3),
 
            (DEV['inst1'], L9['brightness'], .5),
 
        ])
 
        self.assertEqual([[127], 'flush', [127], 'flush'], self.dmx0.updates)
 

	
0 comments (0 inline, 0 general)