annotate service/collector/sse_collector.py @ 595:7fd9fa5d33aa

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