annotate service/collector/sse_collector.py @ 692:b1258d252ef0

reformat Ignore-this: 823c997b62c14b717c614c3205c23bd
author drewp@bigasterisk.com
date Wed, 29 Jan 2020 01:04:09 -0800
parents 22751570eda1
children be2fbdbdf549
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
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
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
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
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,
650
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
123 'remoteIp': self.request.remote_ip, # wrong, need some forwarded-for thing
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
124 'foafAgent': self.request.headers.get('X-Foaf-Agent'),
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
125 'userAgent': self.request.headers.get('user-agent'),
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
126 }
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
127
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
128 def bind(self, *args, **kwargs):
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
129 self.streamId = args[0]
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
130
301
29f593aee67b big rewrites in sse_collector
drewp@bigasterisk.com
parents: 300
diff changeset
131 self.graphClients.addSseHandler(self)
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
132 # 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
133 # try removeSseHandler.
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
134 self.bound = True
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
135
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
136 def unbind(self) -> None:
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
137 if self.bound:
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
138 self.graphClients.removeSseHandler(self)
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
139
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
140
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
141 StatementTable = Dict[StatementType, Tuple[Set[SourceUri], Set[PatchSink]]]
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
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
144 class PostDeleter(object):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
145 def __init__(self, statements: StatementTable):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
146 self.statements = statements
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 def __enter__(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
149 self._garbage: List[StatementType] = []
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
150 return self
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
151
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
152 def add(self, stmt: StatementType):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
153 self._garbage.append(stmt)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
154
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
155 def __exit__(self, type, value, traceback):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
156 if type is not None:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
157 raise
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
158 for stmt in self._garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
159 del self.statements[stmt]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
160
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
161
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
162 class ActiveStatements(object):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
163 def __init__(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
164 # This table holds statements asserted by any of our sources
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
165 # plus local statements that we introduce (source is
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
166 # http://bigasterisk.com/sse_collector/).
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
167 self.table: StatementTable = collections.defaultdict(
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
168 lambda: (set(), set()))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
169
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
170 def state(self) -> Dict:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
171 return {
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
172 'len': len(self.table),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
173 }
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
174
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
175 def postDeleteStatements(self) -> PostDeleter:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
176 return PostDeleter(self.table)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
177
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
178 def pprintTable(self) -> None:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
179 for i, (stmt, (sources, handlers)) in enumerate(
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
180 sorted(self.table.items())):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
181 print("%03d. %-80s from %s to %s" % (
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
182 i,
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
183 abbrevStmt(stmt),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
184 [abbrevTerm(s) for s in sources],
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
185 handlers))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
186
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
187 @STATS.makeSyncPatch.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
188 def makeSyncPatch(self, handler: PatchSink, sources: Set[SourceUri]):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
189 # 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
190 # use it anyway
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
191 adds = []
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
192 dels = []
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
193
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
194 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
195 for stmt, (stmtSources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
196 belongsInHandler = not sources.isdisjoint(stmtSources)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
197 handlerHasIt = handler in handlers
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
198 #log.debug("%s belong=%s has=%s",
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
199 # abbrevStmt(stmt), belongsInHandler, handlerHasIt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
200 if belongsInHandler and not handlerHasIt:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
201 adds.append(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
202 handlers.add(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
203 elif not belongsInHandler and handlerHasIt:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
204 dels.append(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
205 handlers.remove(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
206 if not handlers and not stmtSources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
207 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
208
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
209 return Patch(addQuads=adds, delQuads=dels)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
210
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
211 def applySourcePatch(self, source: SourceUri, p: Patch):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
212 for stmt in p.addQuads:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
213 sourceUrls, handlers = self.table[stmt]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
214 if source in sourceUrls:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
215 raise ValueError("%s added stmt that it already had: %s" %
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
216 (source, abbrevStmt(stmt)))
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:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
223 raise ValueError("%s deleting stmt that it didn't have: %s" %
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
224 (source, abbrevStmt(stmt)))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
225 sourceUrls.remove(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
226 # this is rare, since some handler probably still has
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
227 # 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
228 # a handler was just deleted
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
229 if not sourceUrls and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
230 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
231
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
232 @STATS.replaceSourceStatements.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
233 def replaceSourceStatements(self, source: SourceUri,
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
234 stmts: Sequence[StatementType]):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
235 log.debug('replaceSourceStatements with %s stmts', len(stmts))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
236 newStmts = set(stmts)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
237
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
238 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
239 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
240 if source in sources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
241 if stmt not in stmts:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
242 sources.remove(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
243 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
244 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
245 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
246 if stmt in stmts:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
247 sources.add(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
248 newStmts.discard(stmt)
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 self.applySourcePatch(source, Patch(addQuads=newStmts, delQuads=[]))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
251
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
252 def discardHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
253 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
254 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
255 handlers.discard(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
256 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
257 garbage.add(stmt)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
258
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
259 def discardSource(self, source: SourceUri):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
260 with self.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
261 for stmt, (sources, handlers) in self.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
262 sources.discard(source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
263 if not sources and not handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
264 garbage.add(stmt)
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
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
267
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
268 class GraphClients(object):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
269 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
270 All the active PatchSources and SSEHandlers
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
271
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
272 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
273 true statements along with the sources that are currently
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
274 asserting them and the requesters who currently know them. As
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
275 statements come and go, we make patches to send to requesters.
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
276 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
277 def __init__(self):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
278 self.clients: Dict[SourceUri, PatchSource] = {} # (COLLECTOR is not listed)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
279 self.handlers: Set[PatchSink] = set()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
280 self.statements: ActiveStatements = ActiveStatements()
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
281
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
282 self._localStatements = LocalStatements(self._onPatch)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
283
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
284 def state(self) -> Dict:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
285 return {
650
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
286 'clients': sorted([ps.state() for ps in self.clients.values()],
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
287 key=lambda r: r['reconnectedPatchSource']['url']),
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
288 'sseHandlers': sorted([h.state() for h in self.handlers],
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
289 key=lambda r: (r['streamId'], r['created'])),
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
290 'statements': self.statements.state(),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
291 }
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
292
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
293 def _sourcesForHandler(self, handler: PatchSink) -> List[SourceUri]:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
294 streamId = handler.streamId
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
295 matches = [s for s in config['streams'] if s['id'] == streamId]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
296 if len(matches) != 1:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
297 raise ValueError("%s matches for %r" % (len(matches), streamId))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
298 return [SourceUri(URIRef(s)) for s in matches[0]['sources']] + [
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
299 COLLECTOR]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
300
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
301 @STATS.onPatch.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
302 def _onPatch(self, source: SourceUri, p: Patch, fullGraph: bool=False):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
303 if fullGraph:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
304 # a reconnect may need to resend the full graph even
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
305 # though we've already sent some statements
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
306 self.statements.replaceSourceStatements(source, p.addQuads)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
307 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
308 self.statements.applySourcePatch(source, p)
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 self._sendUpdatePatch()
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 log.isEnabledFor(logging.DEBUG):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
313 self.statements.pprintTable()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
314
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
315 if source != COLLECTOR:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
316 self._localStatements.setSourceState(
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
317 source,
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
318 ROOM['fullGraphReceived'] if fullGraph else
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
319 ROOM['patchesReceived'])
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 @STATS.sendUpdatePatch.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
322 def _sendUpdatePatch(self, handler: Optional[PatchSink]=None):
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 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
325 self.statements
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
326 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
327 now = time.time()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
328 selected = self.handlers
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
329 if handler is not None:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
330 if handler not in self.handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
331 log.error("called _sendUpdatePatch on a handler that's gone")
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
332 return
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
333 selected = {handler}
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
334 # reduce loops here- prepare all patches at once
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
335 for h in selected:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
336 period = .9
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
337 if 'Raspbian' in h.request.headers.get('user-agent', ''):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
338 period = 5
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
339 if h.lastPatchSentTime > now - period:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
340 continue
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
341 p = self.statements.makeSyncPatch(h, set(self._sourcesForHandler(h)))
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
342 log.debug('makeSyncPatch for %r: %r', h, p.jsonRepr)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
343 if not p.isNoop():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
344 log.debug("send patch %s to %s", p.shortSummary(), h)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
345 # This can be a giant line, which was a problem
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
346 # once. Might be nice for this service to try to break
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
347 # it up into multiple sends, although there's no
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
348 # guarantee at all since any single stmt could be any
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
349 # length.
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
350 h.sendEvent(message=jsonFromPatch(p).encode('utf8'),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
351 event=b'patch')
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
352 h.lastPatchSentTime = now
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
353 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
354 log.debug('nothing to send to %s', h)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
355
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
356 def addSseHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
357 log.info('addSseHandler %r %r', handler, handler.streamId)
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 # fail early if id doesn't match
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
360 sources = self._sourcesForHandler(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
361
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
362 self.handlers.add(handler)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
363
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
364 for source in sources:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
365 if source not in self.clients and source != COLLECTOR:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
366 log.debug('connect to patch source %s', source)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
367 self._localStatements.setSourceState(source, ROOM['connect'])
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
368 self.clients[source] = ReconnectingPatchSource(
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
369 source,
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
370 listener=lambda p, fullGraph, source=source: self._onPatch(
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
371 source, p, fullGraph),
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
372 reconnectSecs=10)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
373 log.debug('bring new client up to date')
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
374
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
375 self._sendUpdatePatch(handler)
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 def removeSseHandler(self, handler: PatchSink):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
378 log.info('removeSseHandler %r', handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
379 self.statements.discardHandler(handler)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
380 for source in self._sourcesForHandler(handler):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
381 for otherHandler in self.handlers:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
382 if (otherHandler != handler and
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
383 source in self._sourcesForHandler(otherHandler)):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
384 # still in use
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
385 break
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
386 else:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
387 self._stopClient(source)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
388
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
389 self.handlers.remove(handler)
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 def _stopClient(self, url: SourceUri):
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
392 if url == COLLECTOR:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
393 return
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
394
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
395 self.clients[url].stop()
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
396
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
397 self.statements.discardSource(url)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
398
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
399 self._localStatements.setSourceState(url, None)
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
400 if url in self.clients:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
401 del self.clients[url]
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
402
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
403 self.cleanup()
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
404
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
405 def cleanup(self):
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 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
408 sometimes
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
409 """
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
410 with self.statements.postDeleteStatements() as garbage:
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
411 for stmt, (sources, handlers) in self.statements.table.items():
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
412 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
413 garbage.add(stmt)
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
414
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
415
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
416 class State(cyclone.web.RequestHandler):
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
417 @STATS.getState.time()
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
418 def get(self) -> None:
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
419 try:
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
420 state = self.settings.graphClients.state()
595
7fd9fa5d33aa standardize build. fix /state report
drewp@bigasterisk.com
parents: 470
diff changeset
421 self.write(json.dumps({'graphClients': state}, indent=2,
7fd9fa5d33aa standardize build. fix /state report
drewp@bigasterisk.com
parents: 470
diff changeset
422 default=lambda obj: '<unserializable>'))
7fd9fa5d33aa standardize build. fix /state report
drewp@bigasterisk.com
parents: 470
diff changeset
423 except Exception:
306
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
424 import traceback; traceback.print_exc()
6aad04b34231 sse_collector stats page
drewp@bigasterisk.com
parents: 303
diff changeset
425 raise
650
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
426
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
427 if __name__ == '__main__':
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
428 arg = docopt("""
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
429 Usage: sse_collector.py [options]
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
430
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
431 -v Verbose
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
432 """)
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
433
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
434 if arg['-v']:
449
ef7eba0551f2 collector partial py3+types update. WIP
drewp@bigasterisk.com
parents: 446
diff changeset
435 enableTwistedLog()
442
ee74dc3b58fb collector build improvements; stats and logging
drewp@bigasterisk.com
parents: 439
diff changeset
436 log.setLevel(logging.DEBUG)
ee74dc3b58fb collector build improvements; stats and logging
drewp@bigasterisk.com
parents: 439
diff changeset
437 defer.setDebugging(True)
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
438
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
439
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
440 graphClients = GraphClients()
442
ee74dc3b58fb collector build improvements; stats and logging
drewp@bigasterisk.com
parents: 439
diff changeset
441 #exporter = InfluxExporter(... to export some stats values
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
442
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
443 reactor.listenTCP(
451
17a556ddc5ac add types to sse_collector.py. Surprisingly few bugs found.
drewp@bigasterisk.com
parents: 449
diff changeset
444 9072,
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
445 cyclone.web.Application(
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
446 handlers=[
650
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
447 (r"/()", cyclone.web.StaticFileHandler, {
22751570eda1 new index page table
drewp@bigasterisk.com
parents: 595
diff changeset
448 "path": "static", "default_filename": "index.html"}),
692
b1258d252ef0 reformat
drewp@bigasterisk.com
parents: 650
diff changeset
449 (r'/static/(.*)',cyclone.web.StaticFileHandler, {"path": "static"}),
439
124c921ad52d stats->state to make room for greplin stats
drewp@bigasterisk.com
parents: 353
diff changeset
450 (r'/state', State),
470
91ab9f926aa1 py3 updates. some other refactors.
drewp@bigasterisk.com
parents: 451
diff changeset
451 (r'/graph/(.*)', PatchSink),
442
ee74dc3b58fb collector build improvements; stats and logging
drewp@bigasterisk.com
parents: 439
diff changeset
452 (r'/stats/(.*)', StatsHandler, {'serverName': 'collector'}),
296
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
453 ],
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
454 graphClients=graphClients),
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
455 interface='::')
233b81cf2712 start sse_collector
drewp@bigasterisk.com
parents:
diff changeset
456 reactor.run()