diff options
Diffstat (limited to 'ebus/web')
-rw-r--r-- | ebus/web/controller.py | 93 |
1 files changed, 18 insertions, 75 deletions
diff --git a/ebus/web/controller.py b/ebus/web/controller.py index edf174d..406db01 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -4,79 +4,12 @@ import os, time import itty from werkzeug.contrib.cache import SimpleCache from simplejson import dumps as dump_json +from sqlalchemy.orm import sessionmaker from ebus.web import model -def data(name): - return {"name":name} -""" -def data(name): - if name in ( - "solarDaten.tempKollektor", - "solarDaten.tempWarmwasserSolar", - "betriebsdatenRegler1.boilerTemperatur", - "betriebsdatenRegler1.kesselTemperatur", - "betriebsdatenRegler1.aussenTemperatur", - "betriebsdatenFeuerungsautomat.brauchwasserSollwert", - "betriebsdatenFeuerungsautomat.kesselSollwertTemperatur"): - m = Code(''' - function () { - emit(parseInt(this.time/900)*900000, {sum: this.values[field], count:1}); - }''') - r = Code(''' - function (key, values) { - var n = { sum:0, count:0 }; - for (var i = 0; i<values.length; i++) { - n.sum += values[i].sum; - n.count += values[i].count; - }; - return n; - }''') - f = Code(''' - function (who,r) { - r.avg = r.sum/r.count; - return r; - }''') - q={ - "name":name.split(".")[0], - "time":{"$gt": ( time.time() - (72*3600))}, - } - fs=["name", "values.%s"%name.split(".")[1]] - s={ - "field":name.split(".")[1], - } - print "query q=%s fs=%s s=%s" % (q,fs,s) - collection.ensure_index([('time',pymongo.ASCENDING),('name',pymongo.ASCENDING)]) - data = collection.map_reduce(m,r,query=q,finalize=f,fields=fs,scope=s).find().sort('time',pymongo.ASCENDING) - return map(lambda it: [it['_id'], it['value']['avg']], data) - elif name in ( - "betriebsdatenFeuerungsautomat.betriebszustand.reduced", - "solarDaten.solarPumpe.reduced"): - pName = name.split(".")[0] - pField = name.split(".")[1] - q={ - "name":pName, - "time":{"$gt":(time.time()-(72*3600))} - } - fs=["values.%s"%pField, "time"] - collection.ensure_index([('time',pymongo.ASCENDING),('name',pymongo.ASCENDING)]) - data=collection.find(q,fields=fs).sort('time', pymongo.ASCENDING) - #TODO.map(Code('''function(it) { return [it.time, it.values.betriebszustand]; }''')) - data=map(lambda it: [it['time']*1000, it['values'][pField]], data) - - #Fast die ände auf einen Eintrag pro Änderung zusammen - def reduce(data): - prev = None - for item in data: - if item[1] != prev: - prev = item[1] - yield item - - return list(reduce(data)) - else: - return [] -""" +Session = sessionmaker() class CacheDecorator(object): def __init__(self,key,timeout=20): @@ -96,18 +29,27 @@ class CacheDecorator(object): self.func = func return self.cachedFunc +def data(sensor, from_date=None, till_date=None): + session = Session() + values = session.query(model.Value)\ + .join(model.Sensor)\ + .filter(model.Sensor.name == sensor)\ + .all() + return {"sensor":sensor, "from_date":from_date, "till_date":till_date, "values":values} + @itty.get("/") def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") return open(filename,"r").read() -@itty.get('/json/(?P<name>[^/]+)') -@itty.get('/json/(?P<name>[^/]+)/from_date/(?P<from_date>.+)') +@itty.get('/json/(?P<sensor>[^/]+)') +@itty.get('/json/(?P<sensor>[^/]+)/from_date/(?P<from_date>[^/]+)') +@itty.get('/json/(?P<sensor>[^/]+)/from_date/(?P<from_date>[^/]+)/till_date/(?P<till_date>[^/]+)') @CacheDecorator(key=lambda args,kwargs: tuple(kwargs.keys()) + tuple(kwargs.values()), timeout=20) -def json(req,name=None,from_date=None): +def json(req,sensor=None,from_date=None,till_date=None): print "name=%s from_date=%s" % (name,from_date) - return itty.Response(dump_json( {"label":name, "data":data(name)} ), + return itty.Response(dump_json(data(sensor)), content_type="application/json") @@ -117,5 +59,6 @@ def static(request,filename): return itty.Response(file, content_type=itty.content_type(file)) -def run(): - return itty.run_itty(host="0.0.0.0",port=5000) +def run(db_engine,host="0.0.0.0",port=5000): + Session.configure(bind=db_engine) + return itty.run_itty(host=host,port=port) |