annotate collector.py @ 13:bfd95926be6e default tip

initial port to starlette. missing some disconnect & cleanup functionality
author drewp@bigasterisk.com
date Sat, 26 Nov 2022 14:13:51 -0800
parents 032e59be8fe9
children
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 """
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
10 import asyncio
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
11 import json
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
12 import logging
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
13 import time
12
032e59be8fe9 refactor to separate the nonweb stuff a bit, in prep for cyclone->starlette
drewp@bigasterisk.com
parents: 9
diff changeset
14 from typing import Dict, List, Optional, Set, Union
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
15
9
36471461685f py server doesn't do static files now
drewp@bigasterisk.com
parents: 6
diff changeset
16 from patchablegraph.patchablegraph import jsonFromPatch
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
17 from prometheus_client import Summary
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
18 from rdfdb.patch import Patch
6
a3b6b06fc699 cleanup and type fixes
drewp@bigasterisk.com
parents: 1
diff changeset
19 from rdflib import Namespace, URIRef
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
20
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
21 from starlette.applications import Starlette
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
22 from starlette.requests import Request
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
23 from starlette.responses import JSONResponse
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
24 from starlette.routing import Route
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
25 from starlette_exporter import PrometheusMiddleware, handle_metrics
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
26
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
27 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
28 from merge import SourceUri, ActiveStatements, LocalStatements
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
29 from patchsink import PatchSink, PatchSinkResponse
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
30 from patchsource import PatchSource
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
31 logging.basicConfig(level=logging.DEBUG)
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
32 log=logging.getLogger()
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
33 ROOM = Namespace("http://projects.bigasterisk.com/room/")
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
34 COLLECTOR = SourceUri(URIRef('http://bigasterisk.com/sse_collector/'))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
35
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
36 GET_STATE_CALLS = Summary("get_state_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
37 ON_PATCH_CALLS = Summary("on_patch_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
38 SEND_UPDATE_PATCH_CALLS = Summary("send_update_patch_calls", 'calls')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
39
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
40
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
41 class GraphClients(object):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
42 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
43 All the active PatchSources and SSEHandlers
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
44
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
45 To handle all the overlapping-statement cases, we store a set of
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
46 true statements along with the sources that are currently
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
47 asserting them and the requesters who currently know them. As
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
48 statements come and go, we make patches to send to requesters.
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
49 """
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 def __init__(self):
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
52 self.clients: Dict[SourceUri, PatchSource] = {} # (COLLECTOR is not listed)
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
53 self.handlers: Set[PatchSinkResponse] = set()
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
54 self.statements: ActiveStatements = ActiveStatements()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
55
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
56 self._localStatements = LocalStatements(self._onPatch)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
57
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
58 def state(self) -> Dict:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
59 return {
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
60 'clients': sorted([ps.state() for ps in self.clients.values()], key=lambda r: r['url']),
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
61 '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
62 'statements': self.statements.state(),
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
63 }
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
64
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
65 def _sourcesForHandler(self, handler: PatchSinkResponse) -> List[SourceUri]:
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
66 streamId = handler.streamId
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
67 matches = [s for s in config['streams'] if s['id'] == streamId]
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
68 if len(matches) != 1:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
69 raise ValueError("%s matches for %r" % (len(matches), streamId))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
70 return [SourceUri(URIRef(s)) for s in matches[0]['sources']] + [COLLECTOR]
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 @ON_PATCH_CALLS.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
73 def _onPatch(self, source: SourceUri, p: Patch, fullGraph: bool = False):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
74 if fullGraph:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
75 # a reconnect may need to resend the full graph even
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
76 # though we've already sent some statements
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
77 self.statements.replaceSourceStatements(source, p.addQuads)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
78 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
79 self.statements.applySourcePatch(source, p)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
80
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
81 self._sendUpdatePatch()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
82
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
83 if 0 and log.isEnabledFor(logging.DEBUG):
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
84 self.statements.pprintTable()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
85
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
86 if source != COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
87 self._localStatements.setSourceState(source, ROOM['fullGraphReceived'] if fullGraph else ROOM['patchesReceived'])
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
88
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
89 @SEND_UPDATE_PATCH_CALLS.time()
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
90 def _sendUpdatePatch(self, handler: Optional[PatchSinkResponse] = None):
0
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 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
93 self.statements
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
94 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
95 now = time.time()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
96 selected = self.handlers
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
97 if handler is not None:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
98 if handler not in self.handlers:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
99 log.error("called _sendUpdatePatch on a handler that's gone")
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
100 return
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
101 selected = {handler}
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
102 # reduce loops here- prepare all patches at once
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
103 for h in selected:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
104 period = .9
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
105 if 'Raspbian' in h.user_agent:
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
106 period = 5
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
107 if h.lastPatchSentTime > now - period:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
108 continue
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
109 p = self.statements.makeSyncPatch(h, set(self._sourcesForHandler(h)))
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
110 log.debug('makeSyncPatch for %r: %r', h, p.jsonRepr)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
111 if not p.isNoop():
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
112 log.debug("send patch %s to %s", p.shortSummary(), h)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
113 # This can be a giant line, which was a problem
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
114 # once. Might be nice for this service to try to break
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
115 # it up into multiple sends, although there's no
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
116 # guarantee at all since any single stmt could be any
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
117 # length.
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
118 h.sendEvent(message=jsonFromPatch(p), event='patch')
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
119 h.lastPatchSentTime = now
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
120 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
121 log.debug('nothing to send to %s', h)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
122
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
123 def addSseHandler(self, handler: PatchSinkResponse):
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
124 log.info('addSseHandler %r %r', handler, handler.streamId)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
125
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
126 # fail early if id doesn't match
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
127 sources = self._sourcesForHandler(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
128
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
129 self.handlers.add(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
130
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
131 for source in sources:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
132 if source not in self.clients and source != COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
133 log.debug('connect to patch source %s', source)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
134 self._localStatements.setSourceState(source, ROOM['connect'])
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
135 self.clients[source] = PatchSource(source,
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
136 listener=lambda p, fullGraph, source=source: self._onPatch(source, p, fullGraph),
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
137 reconnectSecs=10)
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
138 log.debug('bring new client up to date')
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
139
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
140 self._sendUpdatePatch(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
141
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
142 def removeSseHandler(self, handler: PatchSinkResponse):
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
143 log.info('removeSseHandler %r', handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
144 self.statements.discardHandler(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
145 for source in self._sourcesForHandler(handler):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
146 for otherHandler in self.handlers:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
147 if (otherHandler != handler and source in self._sourcesForHandler(otherHandler)):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
148 # still in use
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
149 break
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
150 else:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
151 self._stopClient(source)
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 self.handlers.remove(handler)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
154
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
155 def _stopClient(self, url: SourceUri):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
156 if url == COLLECTOR:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
157 return
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
158
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
159 self.clients[url].stop()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
160
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
161 self.statements.discardSource(url)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
162
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
163 self._localStatements.setSourceState(url, None)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
164 if url in self.clients:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
165 del self.clients[url]
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.cleanup()
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 cleanup(self):
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
170 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
171 despite the attempts above, we still get useless rows in the table
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
172 sometimes
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
173 """
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
174 with self.statements.postDeleteStatements() as garbage:
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
175 for stmt, (sources, handlers) in self.statements.table.items():
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
176 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
177 garbage.add(stmt)
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
178
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
179
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
180 @GET_STATE_CALLS.time()
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
181 def State(request: Request) -> JSONResponse:
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
182 state = request.app.state.graphClients.state()
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
183 msg = json.dumps({'graphClients': state}, indent=2, default=lambda obj: '<unserializable>')
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
184 log.info(msg)
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
185 return JSONResponse({'graphClients': state})
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
186
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
187
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
188 def GraphList(request: Request) -> JSONResponse:
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
189 return JSONResponse(config['streams'])
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
190
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
191 def main():
0
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
192 graphClients = GraphClients()
e2d855c00e57 initial move from homeauto/ repo
drewp@bigasterisk.com
parents:
diff changeset
193
13
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
194 app = Starlette(
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
195 debug=True,
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
196 routes=[
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
197 Route('/state', State),
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
198 Route('/graph/', GraphList),
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
199 Route('/graph/{stream_id:str}', PatchSink),
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
200 ])
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
201 app.state.graphClients = graphClients
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
202
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
203 app.add_middleware(PrometheusMiddleware, app_name='collector')
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
204 app.add_route("/metrics", handle_metrics)
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
205 return app
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
206
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
207
bfd95926be6e initial port to starlette. missing some disconnect & cleanup functionality
drewp@bigasterisk.com
parents: 12
diff changeset
208 app = main()