annotate service/collector/collector.py @ 1578:807282fb3136

fix deploy; redo stats display page
author drewp@bigasterisk.com
date Thu, 26 Aug 2021 18:03:30 -0700
parents fafe86ae0b03
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
1 """
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
2 requesting /graph/foo returns an SSE patch stream that's the
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
3 result of fetching multiple other SSE patch streams. The result stream
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
4 may include new statements injected by this service.
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
5
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
6 Future:
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
7 - filter out unneeded stmts from the sources
298
8d89da1915df sse_collector now kind of gets concurrent requests right
drewp@bigasterisk.com
parents: 296
diff changeset
8 - give a time resolution and concatenate any patches that come faster than that res
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
9 """
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
10 import collections
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
11 import json
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
12 import logging
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
13 import time
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
14 from typing import (Any, Callable, Dict, List, NewType, Optional, Sequence, Set, Tuple, Union)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
15
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
16 import cyclone.sse
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
17 import cyclone.web
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
18 from docopt import docopt
302
46c5fae89823 factor out patchsource
drewp@bigasterisk.com
parents: 301
diff changeset
19 from patchablegraph import jsonFromPatch
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
20 from patchablegraph.patchsource import PatchSource, ReconnectingPatchSource
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
21 from prometheus_client import Counter, Gauge, Histogram, Summary
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
22 from prometheus_client.exposition import generate_latest
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
23 from prometheus_client.registry import REGISTRY
351
7716b1810d6c reasoning & collector move into docker images
drewp@bigasterisk.com
parents: 316
diff changeset
24 from rdfdb.patch import Patch
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
25 from rdflib import Namespace, URIRef
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
26 from rdflib.term import Node, Statement
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
27 from standardservice.logsetup import enableTwistedLog, log
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
28 from twisted.internet import defer, reactor
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
29
715
fe9cfc088a49 consolidate debug page into ./index.html for now
drewp@bigasterisk.com
parents: 693
diff changeset
30 from collector_config import config
302
46c5fae89823 factor out patchsource
drewp@bigasterisk.com
parents: 301
diff changeset
31
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
32
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
33 #SourceUri = NewType('SourceUri', URIRef) # doesn't work
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
34 class SourceUri(URIRef):
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
35 pass
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
36
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
37
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
38 ROOM = Namespace("http://projects.bigasterisk.com/room/")
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
39 COLLECTOR = SourceUri(URIRef('http://bigasterisk.com/sse_collector/'))
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
40
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
41 GET_STATE_CALLS = Summary("get_state_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
42 LOCAL_STATEMENTS_PATCH_CALLS = Summary("local_statements_patch_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
43 MAKE_SYNC_PATCH_CALLS = Summary("make_sync_patch_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
44 ON_PATCH_CALLS = Summary("on_patch_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
45 SEND_UPDATE_PATCH_CALLS = Summary("send_update_patch_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
46 REPLACE_SOURCE_STATEMENTS_CALLS = Summary("replace_source_statements_calls", 'calls')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
47
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
48
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
49 class Metrics(cyclone.web.RequestHandler):
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
50
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
51 def get(self):
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
52 self.add_header('content-type', 'text/plain')
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
53 self.write(generate_latest(REGISTRY))
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
54
351
7716b1810d6c reasoning & collector move into docker images
drewp@bigasterisk.com
parents: 316
diff changeset
55
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
56 class LocalStatements(object):
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
57 """
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
58 functions that make statements originating from sse_collector itself
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
59 """
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
60
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
61 def __init__(self, applyPatch: Callable[[URIRef, Patch], None]):
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
62 self.applyPatch = applyPatch
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
63 self._sourceState: Dict[SourceUri, URIRef] = {} # source: state URIRef
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
64
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
65 @LOCAL_STATEMENTS_PATCH_CALLS.time()
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
66 def setSourceState(self, source: SourceUri, state: URIRef):
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
67 """
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
68 add a patch to the COLLECTOR graph about the state of this
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
69 source. state=None to remove the source.
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
70 """
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
71 oldState = self._sourceState.get(source, None)
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
72 if state == oldState:
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
73 return
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
74 log.info('source state %s -> %s', source, state)
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
75 if oldState is None:
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
76 self._sourceState[source] = state
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
77 self.applyPatch(COLLECTOR, Patch(addQuads=[
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
78 (COLLECTOR, ROOM['source'], source, COLLECTOR),
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
79 (source, ROOM['state'], state, COLLECTOR),
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
80 ]))
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
81 elif state is None:
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
82 del self._sourceState[source]
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
83 self.applyPatch(COLLECTOR, Patch(delQuads=[
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
84 (COLLECTOR, ROOM['source'], source, COLLECTOR),
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
85 (source, ROOM['state'], oldState, COLLECTOR),
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
86 ]))
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
87 else:
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
88 self._sourceState[source] = state
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
89 self.applyPatch(COLLECTOR, Patch(addQuads=[
300
371af6e92b5e local state statements and self.statements rewrite
drewp@bigasterisk.com
parents: 299
diff changeset
90 (source, ROOM['state'], state, COLLECTOR),
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
91 ], delQuads=[
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
92 (source, ROOM['state'], oldState, COLLECTOR),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
93 ]))
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
94
298
8d89da1915df sse_collector now kind of gets concurrent requests right
drewp@bigasterisk.com
parents: 296
diff changeset
95
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
96 def abbrevTerm(t: Union[URIRef, Node]) -> Union[str, Node]:
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
97 if isinstance(t, URIRef):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
98 return (t.replace('http://projects.bigasterisk.com/room/', 'room:').replace('http://projects.bigasterisk.com/device/',
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
99 'dev:').replace('http://bigasterisk.com/sse_collector/', 'sc:'))
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
100 return t
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
101
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
102
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
103 def abbrevStmt(stmt: Statement) -> str:
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
104 return '(%s %s %s %s)' % (abbrevTerm(stmt[0]), abbrevTerm(stmt[1]), abbrevTerm(stmt[2]), abbrevTerm(stmt[3]))
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
105
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
106
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
107 class PatchSink(cyclone.sse.SSEHandler):
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
108 _handlerSerial = 0
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
109
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
110 def __init__(self, application: cyclone.web.Application, request):
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
111 cyclone.sse.SSEHandler.__init__(self, application, request)
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
112 self.bound = False
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
113 self.created = time.time()
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
114 self.graphClients = self.settings.graphClients
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
115
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
116 self._serial = PatchSink._handlerSerial
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
117 PatchSink._handlerSerial += 1
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
118 self.lastPatchSentTime: float = 0.0
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
119
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
120 def __repr__(self) -> str:
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
121 return '<Handler #%s>' % self._serial
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
122
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
123 def state(self) -> Dict:
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
124 return {
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
125 'created': round(self.created, 2),
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
126 'ageHours': round((time.time() - self.created) / 3600, 2),
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
127 'streamId': self.streamId,
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
128 'remoteIp': self.request.remote_ip, # wrong, need some forwarded-for thing
650
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
129 'foafAgent': self.request.headers.get('X-Foaf-Agent'),
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
130 'userAgent': self.request.headers.get('user-agent'),
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
131 }
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
132
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
133 def bind(self, *args, **kwargs):
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
134 self.streamId = args[0]
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
135
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
136 self.graphClients.addSseHandler(self)
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
137 # If something goes wrong with addSseHandler, I don't want to
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
138 # try removeSseHandler.
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
139 self.bound = True
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
140
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
141 def unbind(self) -> None:
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
142 if self.bound:
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
143 self.graphClients.removeSseHandler(self)
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
144
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
145
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
146 StatementTable = Dict[Statement, Tuple[Set[SourceUri], Set[PatchSink]]]
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
147
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
148
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
149 class PostDeleter(object):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
150
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
151 def __init__(self, statements: StatementTable):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
152 self.statements = statements
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
153
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
154 def __enter__(self):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
155 self._garbage: List[Statement] = []
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
156 return self
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
157
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
158 def add(self, stmt: Statement):
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
159 self._garbage.append(stmt)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
160
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
161 def __exit__(self, type, value, traceback):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
162 if type is not None:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
163 raise NotImplementedError()
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
164 for stmt in self._garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
165 del self.statements[stmt]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
166
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
167
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
168 class ActiveStatements(object):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
169
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
170 def __init__(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
171 # This table holds statements asserted by any of our sources
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
172 # plus local statements that we introduce (source is
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
173 # http://bigasterisk.com/sse_collector/).
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
174 self.table: StatementTable = collections.defaultdict(lambda: (set(), set()))
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
175
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
176 def state(self) -> Dict:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
177 return {
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
178 'len': len(self.table),
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
179 }
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
180
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
181 def postDeleteStatements(self) -> PostDeleter:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
182 return PostDeleter(self.table)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
183
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
184 def pprintTable(self) -> None:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
185 for i, (stmt, (sources, handlers)) in enumerate(sorted(self.table.items())):
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
186 print("%03d. %-80s from %s to %s" % (i, abbrevStmt(stmt), [abbrevTerm(s) for s in sources], handlers))
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
187
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
188 @MAKE_SYNC_PATCH_CALLS.time()
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
189 def makeSyncPatch(self, handler: PatchSink, sources: Set[SourceUri]):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
190 # todo: this could run all handlers at once, which is how we
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
191 # use it anyway
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
192 adds = []
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
193 dels = []
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
194
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
195 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
196 for stmt, (stmtSources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
197 belongsInHandler = not sources.isdisjoint(stmtSources)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
198 handlerHasIt = handler in handlers
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
199 # log.debug("%s belong=%s has=%s",
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
200 # abbrevStmt(stmt), belongsInHandler, handlerHasIt)
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
201 if belongsInHandler and not handlerHasIt:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
202 adds.append(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
203 handlers.add(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
204 elif not belongsInHandler and handlerHasIt:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
205 dels.append(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
206 handlers.remove(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
207 if not handlers and not stmtSources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
208 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
209
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
210 return Patch(addQuads=adds, delQuads=dels)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
211
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
212 def applySourcePatch(self, source: SourceUri, p: Patch):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
213 for stmt in p.addQuads:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
214 sourceUrls, handlers = self.table[stmt]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
215 if source in sourceUrls:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
216 raise ValueError("%s added stmt that it already had: %s" % (source, abbrevStmt(stmt)))
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
217 sourceUrls.add(source)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
218
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
219 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
220 for stmt in p.delQuads:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
221 sourceUrls, handlers = self.table[stmt]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
222 if source not in sourceUrls:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
223 raise ValueError("%s deleting stmt that it didn't have: %s" % (source, abbrevStmt(stmt)))
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
224 sourceUrls.remove(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
225 # this is rare, since some handler probably still has
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
226 # the stmt we're deleting, but it can happen e.g. when
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
227 # a handler was just deleted
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
228 if not sourceUrls and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
229 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
230
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
231 @REPLACE_SOURCE_STATEMENTS_CALLS.time()
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
232 def replaceSourceStatements(self, source: SourceUri, stmts: Sequence[Statement]):
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
233 log.debug('replaceSourceStatements with %s stmts', len(stmts))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
234 newStmts = set(stmts)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
235
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
236 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
237 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
238 if source in sources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
239 if stmt not in stmts:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
240 sources.remove(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
241 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
242 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
243 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
244 if stmt in stmts:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
245 sources.add(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
246 newStmts.discard(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
247
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
248 self.applySourcePatch(source, Patch(addQuads=newStmts, delQuads=[]))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
249
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
250 def discardHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
251 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
252 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
253 handlers.discard(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
254 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
255 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
256
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
257 def discardSource(self, source: SourceUri):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
258 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
259 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
260 sources.discard(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
261 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
262 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
263
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
264
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
265 class GraphClients(object):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
266 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
267 All the active PatchSources and SSEHandlers
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
268
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
269 To handle all the overlapping-statement cases, we store a set of
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
270 true statements along with the sources that are currently
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
271 asserting them and the requesters who currently know them. As
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
272 statements come and go, we make patches to send to requesters.
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
273 """
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
274
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
275 def __init__(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
276 self.clients: Dict[SourceUri, PatchSource] = {} # (COLLECTOR is not listed)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
277 self.handlers: Set[PatchSink] = set()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
278 self.statements: ActiveStatements = ActiveStatements()
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
279
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
280 self._localStatements = LocalStatements(self._onPatch)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
281
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
282 def state(self) -> Dict:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
283 return {
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
284 'clients': sorted([ps.state() for ps in self.clients.values()], key=lambda r: r['reconnectedPatchSource']['url']),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
285 'sseHandlers': sorted([h.state() for h in self.handlers], key=lambda r: (r['streamId'], r['created'])),
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
286 'statements': self.statements.state(),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
287 }
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
288
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
289 def _sourcesForHandler(self, handler: PatchSink) -> List[SourceUri]:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
290 streamId = handler.streamId
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
291 matches = [s for s in config['streams'] if s['id'] == streamId]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
292 if len(matches) != 1:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
293 raise ValueError("%s matches for %r" % (len(matches), streamId))
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
294 return [SourceUri(URIRef(s)) for s in matches[0]['sources']] + [COLLECTOR]
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
295
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
296 @ON_PATCH_CALLS.time()
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
297 def _onPatch(self, source: SourceUri, p: Patch, fullGraph: bool = False):
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
298 if fullGraph:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
299 # a reconnect may need to resend the full graph even
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
300 # though we've already sent some statements
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
301 self.statements.replaceSourceStatements(source, p.addQuads)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
302 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
303 self.statements.applySourcePatch(source, p)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
304
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
305 self._sendUpdatePatch()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
306
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
307 if log.isEnabledFor(logging.DEBUG):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
308 self.statements.pprintTable()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
309
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
310 if source != COLLECTOR:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
311 self._localStatements.setSourceState(source, ROOM['fullGraphReceived'] if fullGraph else ROOM['patchesReceived'])
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
312
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
313 @SEND_UPDATE_PATCH_CALLS.time()
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
314 def _sendUpdatePatch(self, handler: Optional[PatchSink] = None):
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
315 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
316 send a patch event out this handler to bring it up to date with
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
317 self.statements
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
318 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
319 now = time.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
320 selected = self.handlers
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
321 if handler is not None:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
322 if handler not in self.handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
323 log.error("called _sendUpdatePatch on a handler that's gone")
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
324 return
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
325 selected = {handler}
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
326 # reduce loops here- prepare all patches at once
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
327 for h in selected:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
328 period = .9
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
329 if 'Raspbian' in h.request.headers.get('user-agent', ''):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
330 period = 5
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
331 if h.lastPatchSentTime > now - period:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
332 continue
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
333 p = self.statements.makeSyncPatch(h, set(self._sourcesForHandler(h)))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
334 log.debug('makeSyncPatch for %r: %r', h, p.jsonRepr)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
335 if not p.isNoop():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
336 log.debug("send patch %s to %s", p.shortSummary(), h)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
337 # This can be a giant line, which was a problem
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
338 # once. Might be nice for this service to try to break
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
339 # it up into multiple sends, although there's no
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
340 # guarantee at all since any single stmt could be any
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
341 # length.
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
342 h.sendEvent(message=jsonFromPatch(p).encode('utf8'), event=b'patch')
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
343 h.lastPatchSentTime = now
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
344 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
345 log.debug('nothing to send to %s', h)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
346
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
347 def addSseHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
348 log.info('addSseHandler %r %r', handler, handler.streamId)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
349
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
350 # fail early if id doesn't match
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
351 sources = self._sourcesForHandler(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
352
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
353 self.handlers.add(handler)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
354
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
355 for source in sources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
356 if source not in self.clients and source != COLLECTOR:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
357 log.debug('connect to patch source %s', source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
358 self._localStatements.setSourceState(source, ROOM['connect'])
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
359 self.clients[source] = ReconnectingPatchSource(source,
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
360 listener=lambda p, fullGraph, source=source: self._onPatch(source, p, fullGraph),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
361 reconnectSecs=10)
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
362 log.debug('bring new client up to date')
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
363
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
364 self._sendUpdatePatch(handler)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
365
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
366 def removeSseHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
367 log.info('removeSseHandler %r', handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
368 self.statements.discardHandler(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
369 for source in self._sourcesForHandler(handler):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
370 for otherHandler in self.handlers:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
371 if (otherHandler != handler and source in self._sourcesForHandler(otherHandler)):
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
372 # still in use
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
373 break
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
374 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
375 self._stopClient(source)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
376
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
377 self.handlers.remove(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
378
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
379 def _stopClient(self, url: SourceUri):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
380 if url == COLLECTOR:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
381 return
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
382
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
383 self.clients[url].stop()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
384
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
385 self.statements.discardSource(url)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
386
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
387 self._localStatements.setSourceState(url, None)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
388 if url in self.clients:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
389 del self.clients[url]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
390
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
391 self.cleanup()
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
392
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
393 def cleanup(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
394 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
395 despite the attempts above, we still get useless rows in the table
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
396 sometimes
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
397 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
398 with self.statements.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
399 for stmt, (sources, handlers) in self.statements.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
400 if not sources and not any(h in self.handlers for h in handlers):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
401 garbage.add(stmt)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
402
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
403
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
404 class State(cyclone.web.RequestHandler):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
405
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
406 @GET_STATE_CALLS.time()
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
407 def get(self) -> None:
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
408 try:
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
409 state = self.settings.graphClients.state()
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
410 self.write(json.dumps({'graphClients': state}, indent=2, default=lambda obj: '<unserializable>'))
595
7fd9fa5d33aa standardize build. fix /state report
drewp@bigasterisk.com
parents: 470
diff changeset
411 except Exception:
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
412 import traceback
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
413 traceback.print_exc()
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
414 raise
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
415
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
416
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
417 class GraphList(cyclone.web.RequestHandler):
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
418
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
419 def get(self) -> None:
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
420 self.write(json.dumps(config['streams']))
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
421
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
422
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
423 if __name__ == '__main__':
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
424 arg = docopt("""
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
425 Usage: sse_collector.py [options]
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
426
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
427 -v Verbose
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
428 -i Info level only
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
429 """)
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
430
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
431 if arg['-v'] or arg['-i']:
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
432 enableTwistedLog()
693
be2fbdbdf549 collector: add /graphlist, plus logging updates
drewp@bigasterisk.com
parents: 692
diff changeset
433 log.setLevel(logging.DEBUG if arg['-v'] else logging.INFO)
442
ee74dc3b58fb collector build improvements; stats and logging
drewp@bigasterisk.com
parents: 439
diff changeset
434 defer.setDebugging(True)
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
435
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
436 graphClients = GraphClients()
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
437
794
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
438 reactor.listenTCP(9072,
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
439 cyclone.web.Application(handlers=[
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
440 (r"/()", cyclone.web.StaticFileHandler, {
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
441 "path": ".",
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
442 "default_filename": "index.html"
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
443 }),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
444 (r'/state', State),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
445 (r'/graph/', GraphList),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
446 (r'/graph/(.+)', PatchSink),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
447 (r'/metrics', Metrics),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
448 ],
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
449 graphClients=graphClients),
fafe86ae0b03 py3, k8s, prometheus updates
drewp@bigasterisk.com
parents: 748
diff changeset
450 interface='::')
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
451 reactor.run()