ZeroNet/plugins/Chart/ChartPlugin.py

58 lines
1.9 KiB
Python
Raw Permalink Normal View History

2018-01-19 02:13:17 +01:00
import time
import itertools
import gevent
from Config import config
from util import helper
2019-08-26 03:20:07 +02:00
from util.Flag import flag
2018-01-19 02:13:17 +01:00
from Plugin import PluginManager
2019-03-15 21:06:59 +01:00
from .ChartDb import ChartDb
from .ChartCollector import ChartCollector
2018-01-19 02:13:17 +01:00
if "db" not in locals().keys(): # Share on reloads
db = ChartDb()
gevent.spawn_later(10 * 60, db.archive)
2018-01-19 02:13:17 +01:00
helper.timer(60 * 60 * 6, db.archive)
collector = ChartCollector(db)
@PluginManager.registerTo("SiteManager")
class SiteManagerPlugin(object):
def load(self, *args, **kwargs):
back = super(SiteManagerPlugin, self).load(*args, **kwargs)
collector.setInitialLastValues(self.sites.values())
return back
def delete(self, address, *args, **kwargs):
db.deleteSite(address)
return super(SiteManagerPlugin, self).delete(address, *args, **kwargs)
@PluginManager.registerTo("UiWebsocket")
class UiWebsocketPlugin(object):
2019-08-26 03:20:07 +02:00
@flag.admin
2018-01-19 02:13:17 +01:00
def actionChartDbQuery(self, to, query, params=None):
if config.debug or config.verbose:
s = time.time()
rows = []
try:
if not query.strip().upper().startswith("SELECT"):
raise Exception("Only SELECT query supported")
res = db.execute(query, params)
2019-03-15 21:06:59 +01:00
except Exception as err: # Response the error to client
2018-01-19 02:13:17 +01:00
self.log.error("ChartDbQuery error: %s" % err)
return {"error": str(err)}
# Convert result to dict
for row in res:
rows.append(dict(row))
if config.verbose and time.time() - s > 0.1: # Log slow query
self.log.debug("Slow query: %s (%.3fs)" % (query, time.time() - s))
return rows
2019-08-26 03:20:07 +02:00
@flag.admin
2018-01-19 02:13:17 +01:00
def actionChartGetPeerLocations(self, to):
peers = {}
for site in self.server.sites.values():
peers.update(site.peers)
peer_locations = self.getPeerLocations(peers)
return peer_locations