Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 806:6d8f0c088a26
logging and notes
Ignore-this: 341ae9d2e6b7247a05a72646914a48ab
author | drewp@bigasterisk.com |
---|---|
date | Wed, 18 Jul 2012 18:07:49 +0000 |
parents | ce4fffe8e413 |
children | a631e075a5bf |
rev | line source |
---|---|
796 | 1 #!bin/python |
2 """ | |
3 other tools POST themselves to here as subscribers to the graph. They | |
4 are providing a URL we can PUT to with graphs updates. | |
5 | |
6 we immediately PUT them back all the contents of the graph as a bunch | |
7 of adds. | |
8 | |
9 later we PUT them back with updates (add/del lists) when there are | |
10 changes. | |
11 | |
12 If we fail to reach a registered caller, we forget about it for future | |
13 calls. We can PUT empty diffs as a heartbeat to notice disappearing | |
14 callers faster. | |
15 | |
16 A caller can submit add/del changes that should be persisted and | |
17 broadcast. | |
18 | |
19 Global data undo should probably happen within this service. | |
20 | |
21 Maybe some subgraphs are for transient data (e.g. current timecode, | |
22 mouse position in curvecalc) that only some listeners want to hear about. | |
23 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
24 Deletes are graph-specific, so callers may be surprised to delete a |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
25 stmt from one graph but then find that statement is still true. |
796 | 26 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
27 Alternate plan: would it help to insist that every patch is within |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
28 only one subgraph? I think it's ok for them to span multiple ones. |
796 | 29 |
30 Inserts can be made on any subgraphs, and each subgraph is saved in | |
31 its own file. The file might not be in a format that can express | |
32 graphs, so I'm just going to not store the subgraph URI in any file. | |
33 | |
34 I don't support wildcard deletes, and there are race conditions where a | |
35 s-p could end up with unexpected multiple objects. Every client needs | |
36 to be ready for this. | |
37 | |
38 We watch the files and push their own changes back to the clients. | |
39 | |
40 Persist our client list, to survive restarts. In another rdf file? A | |
41 random json one? memcache? Also hold the recent changes. We're not | |
42 logging everything forever, though, since the output files and a VCS | |
43 shall be used for that | |
44 | |
45 Bnodes: this rdfdb graph might be able to track bnodes correctly, and | |
46 they make for more compact n3 files. I'm not sure if it's going to be | |
47 hard to keep the client bnodes in sync though. File rereads would be | |
48 hard,if ever a bnode was used across graphs, so that probably should | |
49 not be allowed. | |
50 | |
51 Our API: | |
52 | |
53 GET / ui | |
54 GET /graph the whole graph (needed? just for ui browsing?) | |
55 PUT /patches clients submit changes | |
56 GET /patches (recent) patches from clients | |
57 POST /graphClients clientUpdate={uri} to subscribe | |
58 GET /graphClients current clients | |
59 | |
60 format: | |
61 json {"adds" : [[quads]...], | |
62 "deletes": [[quads]], | |
63 "from" : tooluri, | |
64 "created":tttt | |
65 } | |
66 maybe use some http://json-ld.org/ in there. | |
67 | |
806 | 68 proposed rule feature: |
69 rdfdb should be able to watch a pair of (sourceFile, rulesFile) and | |
70 rerun the rules when either one changes. Should the sourceFile be able | |
71 to specify its own rules file? That would be easier | |
72 configuration. How do edits work? Not allowed? Patch the source only? | |
73 Also see the source graph loaded into a different ctx, and you can | |
74 edit that one and see the results in the output context? | |
75 | |
796 | 76 Our web ui: |
77 | |
78 registered clients | |
79 | |
80 recent edits, each one says what client it came from. You can reverse | |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
81 them here. We should be able to take patches that are close in time |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
82 and keep updating the same data (e.g. a stream of changes as the user |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
83 drags a slider) and collapse them into a single edit for clarity. |
796 | 84 |
806 | 85 Ways to display patches, using labels and creator/subj icons where possible: |
86 | |
87 <creator> set <subj>'s <p> to <o> | |
88 <creator> changed <subj>'s <pred> from <o1> to <o2> | |
89 <creator> added <o> to <s> <p> | |
90 | |
803 | 91 |
796 | 92 """ |
93 from twisted.internet import reactor | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
94 import twisted.internet.error |
796 | 95 import sys, optparse, logging, json, os |
96 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
97 sys.path.append(".") | |
98 from light9 import networking, showconfig | |
99 from rdflib import ConjunctiveGraph, URIRef, Graph | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
100 from light9.rdfdb.graphfile import GraphFile |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
101 from light9.rdfdb.patch import Patch, ALLSTMTS |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
102 from light9.rdfdb.rdflibpatch import patchQuads |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
103 from light9.rdfdb import syncedgraph |
796 | 104 |
105 from twisted.internet.inotify import INotify | |
106 logging.basicConfig(level=logging.DEBUG) | |
107 log = logging.getLogger() | |
108 | |
109 try: | |
110 import sys | |
111 sys.path.append("../homeauto/lib") | |
112 from cycloneerr import PrettyErrorHandler | |
113 except ImportError: | |
114 class PrettyErrorHandler(object): | |
115 pass | |
116 | |
117 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
118 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
119 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
120 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
121 def __init__(self, updateUri, label, db): |
796 | 122 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
123 self.label = label |
796 | 124 self.updateUri = updateUri |
125 self.sendAll() | |
126 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
127 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
128 return "<%s client at %s>" % (self.label, self.updateUri) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
129 |
796 | 130 def sendAll(self): |
131 """send the client the whole graph contents""" | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
132 log.info("sending all graphs to %s at %s" % |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
133 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
134 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
135 addQuads=self.db.graph.quads(ALLSTMTS), |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
136 delQuads=[])) |
796 | 137 |
138 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
139 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 140 |
141 class Db(object): | |
142 def __init__(self): | |
143 self.clients = [] | |
144 self.graph = ConjunctiveGraph() | |
145 | |
146 notifier = INotify() | |
147 notifier.startReading() | |
148 | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
149 for inFile in [#"show/dance2012/config.n3", |
801 | 150 "show/dance2012/patch.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
151 "show/dance2012/subs/bcools", |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
152 "show/dance2012/subs/bwarm", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
153 "show/dance2012/subs/house", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
154 "demo.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
155 ]: |
796 | 156 self.g = GraphFile(notifier, |
157 inFile, | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
158 URIRef("http://example.com/file/%s" % |
796 | 159 os.path.basename(inFile)), |
160 self.patch, | |
161 self.getSubgraph) | |
162 | |
163 def patch(self, p): | |
164 """ | |
165 apply this patch to the master graph then notify everyone about it | |
166 """ | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
167 log.info("patching graph -%d +%d" % (len(p.delQuads), len(p.addQuads))) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
168 |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
169 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
170 |
796 | 171 self.summarizeToLog() |
172 for c in self.clients: | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
173 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
174 d.addErrback(self.clientErrored, c) |
796 | 175 sendToLiveClients(asJson=p.jsonRepr) |
176 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
177 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
178 err.trap(twisted.internet.error.ConnectError) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
179 log.info("connection error- dropping client %r" % c) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
180 self.clients.remove(c) |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
181 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
182 |
796 | 183 def summarizeToLog(self): |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
184 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 185 for c in self.graph.contexts(): |
186 log.info(" %s: %s statements" % | |
187 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
188 | |
189 def getSubgraph(self, uri): | |
190 # this is returning an empty Graph :( | |
191 #return self.graph.get_context(uri) | |
192 | |
193 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
194 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 195 g.add(s) |
196 return g | |
197 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
198 def addClient(self, updateUri, label): |
796 | 199 [self.clients.remove(c) |
200 for c in self.clients if c.updateUri == updateUri] | |
201 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
202 log.info("new client %s at %s" % (label, updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
203 self.clients.append(Client(updateUri, label, self)) |
796 | 204 self.sendClientsToAllLivePages() |
205 | |
206 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
207 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
208 dict(updateUri=c.updateUri, label=c.label) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
209 for c in self.clients]}) |
796 | 210 |
211 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
212 def get(self): | |
213 self.set_header("Content-Type", "application/xhtml+xml") | |
214 self.write(open("light9/rdfdb.xhtml").read()) | |
215 | |
216 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
217 def get(self): | |
218 pass | |
219 | |
220 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
221 def __init__(self, *args, **kw): | |
222 cyclone.web.RequestHandler.__init__(self, *args, **kw) | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
223 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 224 self.put = lambda: p(self) |
225 | |
226 def get(self): | |
227 pass | |
228 | |
229 | |
230 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
231 def get(self): | |
232 pass | |
233 | |
234 def post(self): | |
235 upd = self.get_argument("clientUpdate") | |
236 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
237 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 238 except: |
239 import traceback | |
240 traceback.print_exc() | |
241 raise | |
242 | |
243 liveClients = set() | |
244 def sendToLiveClients(d=None, asJson=None): | |
245 j = asJson or json.dumps(d) | |
246 for c in liveClients: | |
247 c.sendMessage(j) | |
248 | |
249 class Live(cyclone.websocket.WebSocketHandler): | |
250 | |
251 def connectionMade(self, *args, **kwargs): | |
252 log.info("ws opened") | |
253 liveClients.add(self) | |
254 self.settings.db.sendClientsToAllLivePages() | |
255 | |
256 def connectionLost(self, reason): | |
257 log.info("ws closed") | |
258 liveClients.remove(self) | |
259 | |
260 def messageReceived(self, message): | |
261 log.info("got message %s" % message) | |
262 self.sendMessage(message) | |
263 | |
264 if __name__ == "__main__": | |
265 logging.basicConfig() | |
266 log = logging.getLogger() | |
267 | |
268 parser = optparse.OptionParser() | |
269 parser.add_option('--show', | |
270 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
271 default=showconfig.showUri()) | |
272 parser.add_option("-v", "--verbose", action="store_true", | |
273 help="logging.DEBUG") | |
274 (options, args) = parser.parse_args() | |
275 | |
276 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
277 | |
278 if not options.show: | |
279 raise ValueError("missing --show http://...") | |
280 | |
281 db = Db() | |
282 | |
283 port = 8051 | |
284 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
285 (r'/', Index), | |
286 (r'/live', Live), | |
287 (r'/graph', GraphResource), | |
288 (r'/patches', Patches), | |
289 (r'/graphClients', GraphClients), | |
290 | |
291 (r"/(jquery-1\.7\.2\.min\.js)", cyclone.web.StaticFileHandler, | |
292 dict(path='lib')), | |
293 | |
294 ], db=db)) | |
295 log.info("serving on %s" % port) | |
296 reactor.run() |