diff options
-rwxr-xr-x | bin/ebus_web | 5 | ||||
-rw-r--r-- | ebus/web/__init__.py | 0 | ||||
-rw-r--r-- | ebus/web/controller.py | 49 | ||||
-rw-r--r-- | ebus/web/model.py | 6 | ||||
-rw-r--r-- | ebus/web/static/index.html (renamed from static/index.html) | 0 | ||||
-rw-r--r-- | ebus/web/static/jquery-flot.js (renamed from static/jquery-flot.js) | 0 | ||||
-rw-r--r-- | ebus/web/static/jquery.js (renamed from static/jquery.js) | 0 | ||||
-rw-r--r-- | test.py | 15 |
8 files changed, 50 insertions, 25 deletions
diff --git a/bin/ebus_web b/bin/ebus_web new file mode 100755 index 0000000..057d50e --- /dev/null +++ b/bin/ebus_web @@ -0,0 +1,5 @@ +#!/usr/bin/env python +import sys +from ebus.web import controller + +sys.exit(controller.run()) diff --git a/ebus/web/__init__.py b/ebus/web/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ebus/web/__init__.py diff --git a/ebus/web/controller.py b/ebus/web/controller.py index f3fa438..edf174d 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -1,11 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import sys, os, time -sys.path.insert(0,"lib") -from itty import itty +import os, time +import itty from werkzeug.contrib.cache import SimpleCache from simplejson import dumps as dump_json +from ebus.web import model + + +def data(name): + return {"name":name} """ def data(name): if name in ( @@ -74,25 +78,44 @@ def data(name): return [] """ +class CacheDecorator(object): + def __init__(self,key,timeout=20): + self.cache = SimpleCache(default_timeout=timeout) + self.func = None + self.key = key + + def cachedFunc(self, *args, **kwargs): + key = self.key(args, kwargs) + ret = self.cache.get(key) + if ret is None: + ret = self.func(*args, **kwargs) + self.cache.set(key, ret) + return ret + + def __call__(self, func): + self.func = func + return self.cachedFunc @itty.get("/") def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") return open(filename,"r").read() -json_cache = SimpleCache(default_timeout=20) -@itty.get('/json/<name>') -def json(req,name): - value = json_cache.get(name) - if value is None: - value = deny.Response(dump_json(dict(map(lambda r: (r, {"label":r, "data":data(r)}), name.split(",")))), mimetype='application/json') - json_cache.set(name, value) - return value +@itty.get('/json/(?P<name>[^/]+)') +@itty.get('/json/(?P<name>[^/]+)/from_date/(?P<from_date>.+)') +@CacheDecorator(key=lambda args,kwargs: tuple(kwargs.keys()) + tuple(kwargs.values()), + timeout=20) +def json(req,name=None,from_date=None): + print "name=%s from_date=%s" % (name,from_date) + return itty.Response(dump_json( {"label":name, "data":data(name)} ), + content_type="application/json") + @itty.get("/static/(?P<filename>.+)") def static(request,filename): file = itty.static_file(filename, root=os.path.join(os.path.dirname(__file__), 'static')) return itty.Response(file, content_type=itty.content_type(file)) -if __name__ == '__main__': - sys.exit(itty.run_itty(host="0.0.0.0",port=5000)) + +def run(): + return itty.run_itty(host="0.0.0.0",port=5000) diff --git a/ebus/web/model.py b/ebus/web/model.py index b8405dc..e7744df 100644 --- a/ebus/web/model.py +++ b/ebus/web/model.py @@ -3,9 +3,9 @@ from sqlalchemy import Column, Integer, Float, DateTime, String, ForeignKey from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base -Base = declarative_base() +ModelBase = declarative_base() -class Sensor(Base): +class Sensor(ModelBase): __tablename__ = 'sensor' id = Column(Integer, primary_key=True) @@ -22,7 +22,7 @@ class Sensor(Base): -class Value(Base): +class Value(ModelBase): __tablename__ = 'value' id = Column(Integer, primary_key=True) diff --git a/static/index.html b/ebus/web/static/index.html index 3c8445a..3c8445a 100644 --- a/static/index.html +++ b/ebus/web/static/index.html diff --git a/static/jquery-flot.js b/ebus/web/static/jquery-flot.js index 6534a46..6534a46 100644 --- a/static/jquery-flot.js +++ b/ebus/web/static/jquery-flot.js diff --git a/static/jquery.js b/ebus/web/static/jquery.js index 9263574..9263574 100644 --- a/static/jquery.js +++ b/ebus/web/static/jquery.js @@ -1,11 +1,11 @@ +from ebus.web import model -import model from sqlalchemy import create_engine -#engine = create_engine('sqlite:///:memory:', echo=True) -engine = create_engine("postgresql://yvesf:iii@10.2.2.13:5432/yvesf", echo=True) +engine = create_engine('sqlite:///:memory:', echo=True) +#engine = create_engine("postgresql://yvesf:iii@10.2.2.13:5432/yvesf", echo=True) -model.Base.metadata.create_all(engine) +model.ModelBase.metadata.create_all(engine) from sqlalchemy.orm import sessionmaker Session = sessionmaker() @@ -17,14 +17,11 @@ session = Session() t1_sensor = model.Sensor("foosensor", "blatest") session.add(t1_sensor) -t1_value1 = model.Value(t1_sensor, 0.5) -session.add(t1_value1) +for i in xrange(10000): + session.add( model.Value(t1_sensor, 0.5) ) session.commit() - - - session.close() |