ChartPlugin.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import time
  2. import itertools
  3. import gevent
  4. from Config import config
  5. from util import helper
  6. from Plugin import PluginManager
  7. from ChartDb import ChartDb
  8. from ChartCollector import ChartCollector
  9. if "db" not in locals().keys(): # Share on reloads
  10. db = ChartDb()
  11. gevent.spawn_later(10 * 60, db.archive)
  12. helper.timer(60 * 60 * 6, db.archive)
  13. collector = ChartCollector(db)
  14. @PluginManager.registerTo("SiteManager")
  15. class SiteManagerPlugin(object):
  16. def load(self, *args, **kwargs):
  17. back = super(SiteManagerPlugin, self).load(*args, **kwargs)
  18. collector.setInitialLastValues(self.sites.values())
  19. return back
  20. def delete(self, address, *args, **kwargs):
  21. db.deleteSite(address)
  22. return super(SiteManagerPlugin, self).delete(address, *args, **kwargs)
  23. @PluginManager.registerTo("UiWebsocket")
  24. class UiWebsocketPlugin(object):
  25. def actionChartDbQuery(self, to, query, params=None):
  26. if not "ADMIN" in self.permissions:
  27. return {"error": "No permission"}
  28. if config.debug or config.verbose:
  29. s = time.time()
  30. rows = []
  31. try:
  32. if not query.strip().upper().startswith("SELECT"):
  33. raise Exception("Only SELECT query supported")
  34. res = db.execute(query, params)
  35. except Exception, err: # Response the error to client
  36. self.log.error("ChartDbQuery error: %s" % err)
  37. return {"error": str(err)}
  38. # Convert result to dict
  39. for row in res:
  40. rows.append(dict(row))
  41. if config.verbose and time.time() - s > 0.1: # Log slow query
  42. self.log.debug("Slow query: %s (%.3fs)" % (query, time.time() - s))
  43. return rows
  44. def actionChartGetPeerLocations(self, to):
  45. if not "ADMIN" in self.permissions:
  46. return {"error": "No permission"}
  47. peers = {}
  48. for site in self.server.sites.values():
  49. peers.update(site.peers)
  50. peer_locations = self.getPeerLocations(peers)
  51. return peer_locations