annotate collector.py @ 12:032e59be8fe9

refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
author drewp@bigasterisk.com
date Fri, 25 Nov 2022 20:58:08 -0800
parents 36471461685f
children bfd95926be6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
1 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
2 requesting /graph/foo returns an SSE patch stream that's the
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
3 result of fetching multiple other SSE patch streams. The result stream
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
4 may include new statements injected by this service.
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
5
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
6 Future:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
7 - filter out unneeded stmts from the sources
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
8 - give a time resolution and concatenate any patches that come faster than that res
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
9 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
10 import json
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
11 import logging
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
12 import time
12
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
13 from typing import Dict, List, Optional, Set, Union
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
14
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
15 import cyclone.sse
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
16 import cyclone.web
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
17 from docopt import docopt
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
18 from patchablegraph.patchablegraph import jsonFromPatch
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
19 from patchablegraph.patchsource import PatchSource, ReconnectingPatchSource
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
20 from prometheus_client import Summary
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
21 from prometheus_client.exposition import generate_latest
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
22 from prometheus_client.registry import REGISTRY
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
23 from rdfdb.patch import Patch
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
24 from rdflib import Namespace, URIRef
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
25 from standardservice.logsetup import enableTwistedLog, log
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
26 from twisted.internet import defer, reactor
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
27
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
28 from collector_config import config
12
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
29 from merge import SourceUri, ActiveStatements, LocalStatements
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
30 from patchsink import PatchSink
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
31
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
32 import cyclone.sse
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
33 def py3_sendEvent(self, message, event=None, eid=None, retry=None):
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
34
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
35 if isinstance(message, dict):
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
36 message = cyclone.sse.escape.json_encode(message)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
37 if isinstance(message, str):
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
38 message = message.encode("utf-8")
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
39 assert isinstance(message, bytes)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
40 if eid:
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
41 self.transport.write(b"id: %s\n" % eid)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
42 if event:
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
43 self.transport.write(b"event: %s\n" % event)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
44 if retry:
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
45 self.transport.write(b"retry: %s\n" % retry)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
46 self.transport.write(b"data: %s\n\n" % message)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
47
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
48
12
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
49 cyclone.sse.SSEHandler.sendEvent = py3_sendEvent
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
50
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
51
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
52 ROOM = Namespace("http://projects.bigasterisk.com/room/")
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
53 COLLECTOR = SourceUri(URIRef('http://bigasterisk.com/sse_collector/'))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
54
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
55 GET_STATE_CALLS = Summary("get_state_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
56 ON_PATCH_CALLS = Summary("on_patch_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
57 SEND_UPDATE_PATCH_CALLS = Summary("send_update_patch_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
58
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
59
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
60 class Metrics(cyclone.web.RequestHandler):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
61
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
62 def get(self):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
63 self.add_header('content-type', 'text/plain')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
64 self.write(generate_latest(REGISTRY))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
65
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
66
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
67
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
68 class GraphClients(object):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
69 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
70 All the active PatchSources and SSEHandlers
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
71
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
72 To handle all the overlapping-statement cases, we store a set of
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
73 true statements along with the sources that are currently
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
74 asserting them and the requesters who currently know them. As
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
75 statements come and go, we make patches to send to requesters.
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
76 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
77
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
78 def __init__(self):
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
79 self.clients: Dict[SourceUri, Union[PatchSource, ReconnectingPatchSource]] = {} # (COLLECTOR is not listed)
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
80 self.handlers: Set[PatchSink] = set()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
81 self.statements: ActiveStatements = ActiveStatements()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
82
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
83 self._localStatements = LocalStatements(self._onPatch)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
84
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
85 def state(self) -> Dict:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
86 return {
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
87 'clients': sorted([ps.state() for ps in self.clients.values()], key=lambda r: r['reconnectedPatchSource']['url']),
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
88 'sseHandlers': sorted([h.state() for h in self.handlers], key=lambda r: (r['streamId'], r['created'])),
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
89 'statements': self.statements.state(),
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
90 }
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
91
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
92 def _sourcesForHandler(self, handler: PatchSink) -> List[SourceUri]:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
93 streamId = handler.streamId
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
94 matches = [s for s in config['streams'] if s['id'] == streamId]
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
95 if len(matches) != 1:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
96 raise ValueError("%s matches for %r" % (len(matches), streamId))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
97 return [SourceUri(URIRef(s)) for s in matches[0]['sources']] + [COLLECTOR]
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
98
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
99 @ON_PATCH_CALLS.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
100 def _onPatch(self, source: SourceUri, p: Patch, fullGraph: bool = False):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
101 if fullGraph:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
102 # a reconnect may need to resend the full graph even
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
103 # though we've already sent some statements
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
104 self.statements.replaceSourceStatements(source, p.addQuads)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
105 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
106 self.statements.applySourcePatch(source, p)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
107
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
108 self._sendUpdatePatch()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
109
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
110 if log.isEnabledFor(logging.DEBUG):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
111 self.statements.pprintTable()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
112
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
113 if source != COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
114 self._localStatements.setSourceState(source, ROOM['fullGraphReceived'] if fullGraph else ROOM['patchesReceived'])
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
115
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
116 @SEND_UPDATE_PATCH_CALLS.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
117 def _sendUpdatePatch(self, handler: Optional[PatchSink] = None):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
118 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
119 send a patch event out this handler to bring it up to date with
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
120 self.statements
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
121 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
122 now = time.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
123 selected = self.handlers
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
124 if handler is not None:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
125 if handler not in self.handlers:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
126 log.error("called _sendUpdatePatch on a handler that's gone")
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
127 return
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
128 selected = {handler}
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
129 # reduce loops here- prepare all patches at once
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
130 for h in selected:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
131 period = .9
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
132 if 'Raspbian' in h.request.headers.get('user-agent', ''):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
133 period = 5
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
134 if h.lastPatchSentTime > now - period:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
135 continue
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
136 p = self.statements.makeSyncPatch(h, set(self._sourcesForHandler(h)))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
137 log.debug('makeSyncPatch for %r: %r', h, p.jsonRepr)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
138 if not p.isNoop():
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
139 log.debug("send patch %s to %s", p.shortSummary(), h)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
140 # This can be a giant line, which was a problem
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
141 # once. Might be nice for this service to try to break
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
142 # it up into multiple sends, although there's no
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
143 # guarantee at all since any single stmt could be any
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
144 # length.
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
145 h.sendEvent(message=jsonFromPatch(p), event=b'patch')
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
146 h.lastPatchSentTime = now
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
147 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
148 log.debug('nothing to send to %s', h)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
149
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
150 def addSseHandler(self, handler: PatchSink):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
151 log.info('addSseHandler %r %r', handler, handler.streamId)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
152
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
153 # fail early if id doesn't match
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
154 sources = self._sourcesForHandler(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
155
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
156 self.handlers.add(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
157
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
158 for source in sources:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
159 if source not in self.clients and source != COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
160 log.debug('connect to patch source %s', source)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
161 self._localStatements.setSourceState(source, ROOM['connect'])
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
162 self.clients[source] = ReconnectingPatchSource(source,
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
163 listener=lambda p, fullGraph, source=source: self._onPatch(source, p, fullGraph),
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
164 reconnectSecs=10)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
165 log.debug('bring new client up to date')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
166
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
167 self._sendUpdatePatch(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
168
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
169 def removeSseHandler(self, handler: PatchSink):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
170 log.info('removeSseHandler %r', handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
171 self.statements.discardHandler(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
172 for source in self._sourcesForHandler(handler):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
173 for otherHandler in self.handlers:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
174 if (otherHandler != handler and source in self._sourcesForHandler(otherHandler)):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
175 # still in use
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
176 break
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
177 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
178 self._stopClient(source)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
179
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
180 self.handlers.remove(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
181
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
182 def _stopClient(self, url: SourceUri):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
183 if url == COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
184 return
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
185
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
186 self.clients[url].stop()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
187
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
188 self.statements.discardSource(url)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
189
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
190 self._localStatements.setSourceState(url, None)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
191 if url in self.clients:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
192 del self.clients[url]
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
193
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
194 self.cleanup()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
195
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
196 def cleanup(self):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
197 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
198 despite the attempts above, we still get useless rows in the table
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
199 sometimes
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
200 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
201 with self.statements.postDeleteStatements() as garbage:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
202 for stmt, (sources, handlers) in self.statements.table.items():
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
203 if not sources and not any(h in self.handlers for h in handlers):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
204 garbage.add(stmt)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
205
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
206
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
207 class State(cyclone.web.RequestHandler):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
208
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
209 @GET_STATE_CALLS.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
210 def get(self) -> None:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
211 try:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
212 state = self.settings.graphClients.state()
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
213 msg = json.dumps({'graphClients': state}, indent=2, default=lambda obj: '<unserializable>')
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
214 log.info(msg)
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
215 self.write(msg)
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
216 except Exception:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
217 import traceback
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
218 traceback.print_exc()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
219 raise
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
220
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
221
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
222 class GraphList(cyclone.web.RequestHandler):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
223
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
224 def get(self) -> None:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
225 self.write(json.dumps(config['streams']))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
226
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
227
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
228 if __name__ == '__main__':
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
229 arg = docopt("""
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
230 Usage: sse_collector.py [options]
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
231
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
232 -v Verbose
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
233 -i Info level only
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
234 """)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
235
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
236 if True:
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
237 enableTwistedLog()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
238 log.setLevel(logging.DEBUG if arg['-v'] else logging.INFO)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
239 defer.setDebugging(True)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
240
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
241 graphClients = GraphClients()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
242
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
243 reactor.listenTCP(
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
244 9072,
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
245 cyclone.web.Application( #
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
246 handlers=[
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
247 (r'/state', State),
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
248 (r'/graph/', GraphList),
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
249 (r'/graph/(.+)', PatchSink),
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
250 (r'/metrics', Metrics),
12
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
251 ], graphClients=graphClients),
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
252 interface='::')
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
253 reactor.run()