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