changeset 52:c81bd512d587

start the stats/ page Ignore-this: 32cd86ef6a9338879d9a47895136259
author drewp@bigasterisk.com
date Thu, 30 May 2019 01:58:36 +0000
parents d75932a2aa47
children a21b87140758
files rdfdb/service.py setup.py
diffstat 2 files changed, 36 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/service.py	Wed May 29 17:24:04 2019 +0000
+++ b/rdfdb/service.py	Thu May 30 01:58:36 2019 +0000
@@ -1,7 +1,9 @@
-import sys, optparse, logging, json, os
+import sys, optparse, logging, json, os, time
 from typing import Dict, List, Set, Optional, Union
 
-from twisted.internet import reactor, defer
+from greplin.scales.cyclonehandler import StatsHandler
+from greplin import scales
+from twisted.internet import reactor, defer, task
 from twisted.internet.inotify import IN_CREATE, INotify
 from twisted.python.failure import Failure
 from twisted.python.filepath import FilePath
@@ -16,6 +18,26 @@
 from rdfdb.patchsender import sendPatch
 from rdfdb.rdflibpatch import patchQuads
 
+# move this out
+procStats = scales.collection('/process',
+                              scales.DoubleStat('time'),
+)
+def updateTimeStat():
+    procStats.time = round(time.time(), 3)
+task.LoopingCall(updateTimeStat).start(.2)
+
+stats = scales.collection('/webServer',
+                          scales.IntStat('plainClients'),
+                          scales.IntStat('websocketClients'),
+                          scales.PmfStat('setAttr'),
+)
+graphStats = scales.collection('/graph',
+                          scales.IntStat('statements'),
+                          scales.RecentFpsStat('patchFps'),
+)
+fileStats = scales.collection('/file',
+                              )
+
 log = logging.getLogger('rdfdb')
 
 
@@ -203,12 +225,15 @@
     def __init__(self, dirUriMap: DirUriMap, addlPrefixes):
         self.clients: List[Union[Client, WsClient]] = []
         self.graph = ConjunctiveGraph()
+        stats.graphLen = len(self.graph)
+        stats.plainClients = len(self.clients)
 
         self.watchedFiles = WatchedFiles(dirUriMap, self.patch,
                                          self.getSubgraph, addlPrefixes)
 
         self.summarizeToLog()
 
+    @graphStats.patchFps.rate()
     def patch(self, patch: Patch, dueToFileChange: bool = False) -> None:
         """
         apply this patch to the master graph then notify everyone about it
@@ -227,10 +252,12 @@
             self.watchedFiles.aboutToPatch(ctx)
 
         patchQuads(self.graph, patch.delQuads, patch.addQuads, perfect=True)
+        stats.graphLen = len(self.graph)
         self._sendPatch(patch)
         if not dueToFileChange:
             self.watchedFiles.dirtyFiles([ctx])
         sendToLiveClients(asJson=patch.jsonRepr)
+        graphStats.statements = len(self.graph)
 
     def _sendPatch(self, p: Patch):
         senderUpdateUri: Optional[URIRef] = getattr(p, 'senderUpdateUri', None)
@@ -249,6 +276,7 @@
         log.info("%r %r - dropping client", c, err.getErrorMessage())
         if c in self.clients:
             self.clients.remove(c)
+        stats.plainClients = len(self.clients)
         self.sendClientsToAllLivePages()
 
     def summarizeToLog(self):
@@ -282,6 +310,7 @@
         sendGraphToClient(self.graph, newClient)
         self.clients.append(newClient)
         self.sendClientsToAllLivePages()
+        stats.plainClients = len(self.clients)
 
     def sendClientsToAllLivePages(self) -> None:
         sendToLiveClients({
@@ -384,11 +413,13 @@
     def connectionMade(self, *args, **kwargs):
         log.info("websocket opened")
         liveClients.add(self)
+        stats.websocketClients = len(liveClients)
         self.settings.db.sendClientsToAllLivePages()
 
     def connectionLost(self, reason):
         log.info("websocket closed")
         liveClients.remove(self)
+        stats.websocketClients = len(liveClients)
 
     def messageReceived(self, message):
         log.info("got message %s" % message)
@@ -396,7 +427,7 @@
 
 
 liveClients: Set[Live] = set()
-
+stats.websocketClients = len(liveClients)
 
 def sendToLiveClients(d=None, asJson=None):
     j = asJson or json.dumps(d)
@@ -451,6 +482,7 @@
             (r'/graphClients', GraphClients),
             (r'/syncedGraph', WebsocketClient),
             (r'/prefixes', Prefixes),
+            (r'/stats/(.*)', StatsHandler, {'serverName': 'rdfdb'}),
             (r'/(.*)', NoExts, {
                 "path": FilePath(__file__).sibling("web").path,
                 "default_filename": "index.html"
--- a/setup.py	Wed May 29 17:24:04 2019 +0000
+++ b/setup.py	Thu May 30 01:58:36 2019 +0000
@@ -11,6 +11,7 @@
         'treq',
         'rdflib-jsonld',
         'service_identity',
+        'scales', # use git+http://github.com/drewp/scales.git@master#egg=scales
         ],
     url='https://projects.bigasterisk.com/rdfdb/rdfdb-0.16.0.tar.gz',
     author='Drew Perttula',