Mercurial > code > home > repos > homeauto
annotate lib/patchablegraph.py @ 1277:ccec6be19b49
py3
Ignore-this: dc6b89c94aaec08f11a07df9fd23beee
darcs-hash:6d1acca51dba077fe1ab8e5bb5aad4be01a89035
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sat, 20 Apr 2019 23:53:04 -0700 |
parents | d40214eaca99 |
children |
rev | line source |
---|---|
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
1 """ |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
2 Design: |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
3 |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
4 1. Services each have (named) graphs, which they patch as things |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
5 change. PatchableGraph is an object for holding this graph. |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
6 2. You can http GET that graph, or ... |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
7 3. You can http GET/SSE that graph and hear about modifications to it |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
8 4. The client that got the graph holds and maintains a copy. The |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
9 client may merge together multiple graphs. |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
10 5. Client queries its graph with low-level APIs or client-side sparql. |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
11 6. When the graph changes, the client knows and can update itself at |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
12 low or high granularity. |
1038
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
13 |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
14 |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
15 See also: |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
16 * http://iswc2007.semanticweb.org/papers/533.pdf RDFSync: efficient remote synchronization of RDF |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
17 models |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
18 * https://www.w3.org/2009/12/rdf-ws/papers/ws07 Supporting Change Propagation in RDF |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
19 * https://www.w3.org/DesignIssues/lncs04/Diff.pdf Delta: an ontology for the distribution of |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
20 differences between RDF graphs |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
21 |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
22 """ |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
23 import sys, json, logging, itertools |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 import cyclone.sse |
1136
1e43ec4a5f23
build and import updates for rdfdb, etc
drewp <drewp@bigasterisk.com>
parents:
1103
diff
changeset
|
25 from rdfdb.grapheditapi import GraphEditApi |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 from rdflib import ConjunctiveGraph |
1277 | 27 from rdfdb.rdflibpatch import patchQuads, inGraph |
1136
1e43ec4a5f23
build and import updates for rdfdb, etc
drewp <drewp@bigasterisk.com>
parents:
1103
diff
changeset
|
28 from rdfdb.patch import Patch |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 from rdflib_jsonld.serializer import from_rdf |
1103
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
30 from rdflib.parser import StringInputSource |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
31 from cycloneerr import PrettyErrorHandler |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
32 from greplin import scales |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 |
1038
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
34 log = logging.getLogger('patchablegraph') |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
35 |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 def writeGraphResponse(req, graph, acceptHeader): |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 if acceptHeader == 'application/nquads': |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 req.set_header('Content-type', 'application/nquads') |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 graph.serialize(req, format='nquads') |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 elif acceptHeader == 'application/ld+json': |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 req.set_header('Content-type', 'application/ld+json') |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 graph.serialize(req, format='json-ld', indent=2) |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 else: |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 req.set_header('Content-type', 'application/x-trig') |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 graph.serialize(req, format='trig') |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
48 def _graphFromQuads2(q): |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 g = ConjunctiveGraph() |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 #g.addN(q) # no effect on nquad output |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 for s,p,o,c in q: |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 #g.store.add((s,p,o), c) # no effect on nquad output |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 return g |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 |
1103
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
56 def jsonFromPatch(p): |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 return json.dumps({'patch': { |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
58 'adds': from_rdf(_graphFromQuads2(p.addQuads)), |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
59 'deletes': from_rdf(_graphFromQuads2(p.delQuads)), |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
60 }}) |
1103
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
61 patchAsJson = jsonFromPatch # deprecated name |
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
62 |
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
63 |
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
64 def patchFromJson(j): |
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
65 body = json.loads(j)['patch'] |
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
66 a = ConjunctiveGraph() |
1277 | 67 a.parse(StringInputSource(json.dumps(body['adds']).encode('utf8')), format='json-ld') |
1103
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
68 d = ConjunctiveGraph() |
1277 | 69 d.parse(StringInputSource(json.dumps(body['deletes']).encode('utf8')), format='json-ld') |
1103
b84e956771fc
sse_collector now kind of gets concurrent requests right
drewp <drewp@bigasterisk.com>
parents:
1038
diff
changeset
|
70 return Patch(addGraph=a, delGraph=d) |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
71 |
1038
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
72 def graphAsJson(g): |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
73 # This is not the same as g.serialize(format='json-ld')! That |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
74 # version omits literal datatypes. |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
75 return json.dumps(from_rdf(g)) |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
76 |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
77 _graphsInProcess = itertools.count() |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
78 class PatchableGraph(GraphEditApi): |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
79 """ |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
80 Master graph that you modify with self.patch, and we get the |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
81 updates to all current listeners. |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
82 """ |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
83 def __init__(self): |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
84 self._graph = ConjunctiveGraph() |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
85 self._observers = [] |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
86 scales.init(self, '/patchableGraph%s' % next(_graphsInProcess)) |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
87 |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
88 _serialize = scales.PmfStat('serialize') |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
89 def serialize(self, to, **kw): |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
90 with self._serialize.time(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
91 return self._graph.serialize(to, **kw) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
92 |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
93 _patch = scales.PmfStat('patch') |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
94 _len = scales.IntStat('statementCount') |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
95 def patch(self, p): |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
96 with self._patch.time(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
97 # assuming no stmt is both in p.addQuads and p.delQuads. |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
98 dels = set([q for q in p.delQuads if inGraph(q, self._graph)]) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
99 adds = set([q for q in p.addQuads if not inGraph(q, self._graph)]) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
100 minimizedP = Patch(addQuads=adds, delQuads=dels) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
101 if minimizedP.isNoop(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
102 return |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
103 patchQuads(self._graph, |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
104 deleteQuads=dels, |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
105 addQuads=adds, |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
106 perfect=False) # true? |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
107 for ob in self._observers: |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
108 ob(patchAsJson(p)) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
109 self._len = len(self._graph) |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
110 |
1038
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
111 def asJsonLd(self): |
ffe6a00c6cef
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents:
1032
diff
changeset
|
112 return graphAsJson(self._graph) |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
113 |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
114 _currentObservers = scales.IntStat('observers/current') |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
115 _observersAdded = scales.IntStat('observers/added') |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
116 def addObserver(self, onPatch): |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
117 self._observers.append(onPatch) |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
118 self._currentObservers = len(self._observers) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
119 self._observersAdded += 1 |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
120 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
121 def removeObserver(self, onPatch): |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
122 try: |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
123 self._observers.remove(onPatch) |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
124 except ValueError: |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
125 pass |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
126 self._currentObservers = len(self._observers) |
1155 | 127 |
128 def setToGraph(self, newGraph): | |
129 self.patch(Patch.fromDiff(self._graph, newGraph)) | |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
130 |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
131 _sendSimpleGraph = scales.PmfStat('serve/simpleGraph') |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
132 _sendFullGraph = scales.PmfStat('serve/events/sendFull') |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
133 _sendPatch = scales.PmfStat('serve/events/sendPatch') |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
134 |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
135 class CycloneGraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
136 def initialize(self, masterGraph): |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
137 self.masterGraph = masterGraph |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
138 |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
139 def get(self): |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
140 with self.masterGraph._sendSimpleGraph.time(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
141 writeGraphResponse(self, self.masterGraph, |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
142 self.request.headers.get('accept')) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
143 |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
144 |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
145 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler): |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
146 """ |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
147 One session with one client. |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
148 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
149 returns current graph plus future patches to keep remote version |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
150 in sync with ours. |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
151 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
152 intsead of turning off buffering all over, it may work for this |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
153 response to send 'x-accel-buffering: no', per |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
154 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
155 """ |
1029
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
156 def __init__(self, application, request, masterGraph): |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
157 cyclone.sse.SSEHandler.__init__(self, application, request) |
4d36cae32a4c
refactor /graph and /graph/events handlers to lib/
drewp <drewp@bigasterisk.com>
parents:
1028
diff
changeset
|
158 self.masterGraph = masterGraph |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
159 |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
160 def bind(self): |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
161 with self.masterGraph._sendFullGraph.time(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
162 graphJson = self.masterGraph.asJsonLd() |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
163 log.debug("send fullGraph event: %s", graphJson) |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
164 self.sendEvent(message=graphJson, event=b'fullGraph') |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
165 self.masterGraph.addObserver(self.onPatch) |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
166 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
167 def onPatch(self, patchJson): |
1276
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
168 with self.masterGraph._sendPatch.time(): |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
169 # throttle and combine patches here- ideally we could see how |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
170 # long the latency to the client is to make a better rate choice |
d40214eaca99
stats support and maybe a no-op filtering logic change snuck in there
drewp <drewp@bigasterisk.com>
parents:
1232
diff
changeset
|
171 self.sendEvent(message=patchJson, event=b'patch') |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
172 |
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
173 def unbind(self): |
1032
69aad813a94e
fix patchablegraph unbind event
drewp <drewp@bigasterisk.com>
parents:
1029
diff
changeset
|
174 self.masterGraph.removeObserver(self.onPatch) |
1028
70d52fa8373a
add new jsonld/SSE support to environment service as a test
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
175 |