Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 1057:c1961da4180a
evtiming in reasoning service
Ignore-this: aa4aba730ca916bda2b8e99c743cac2a
darcs-hash:5d6fb7aa4ef06bf359751d069cc071348833ca98
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 12 Feb 2016 02:42:29 -0800 |
parents | d2007482aec5 |
children | 32cc1eda8389 |
rev | line source |
---|---|
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 #!bin/python |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 """ |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
3 Graph consists of: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
4 input/* (read at startup) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
5 webinput/* (new files are noticed in here) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
6 any number of remote graphs, specified in the other graph as objects of (:reasoning, :source, *), reread constantly |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
7 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 gather subgraphs from various services, run them through a rules |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 engine, and make http requests with the conclusions. |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
11 E.g. 'when drew's phone is near the house, and someone is awake, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 unlock the door when the door's motion sensor is activated' |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 When do we gather? The services should be able to trigger us, perhaps |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 with PSHB, that their graph has changed. |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 from twisted.internet import reactor, task |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
20 from twisted.internet.defer import inlineCallbacks, gatherResults |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
21 from twisted.python.filepath import FilePath |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
22 import time, traceback, sys, json, logging |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
23 from rdflib import Graph, ConjunctiveGraph |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
24 from rdflib import Namespace, URIRef, Literal, RDF |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
25 from rdflib.parser import StringInputSource |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
26 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
27 import cyclone.web, cyclone.websocket |
1040 | 28 from inference import infer |
29 from rdflibtrig import addTrig | |
853 | 30 from graphop import graphEqual |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
31 from docopt import docopt |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
32 from actions import Actions |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
33 from FuXi.Rete.RuleStore import N3RuleStore |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 sys.path.append("../../lib") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 from logsetup import log |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
37 log.setLevel(logging.WARN) |
854 | 38 outlog = logging.getLogger('output') |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
39 outlog.setLevel(logging.WARN) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
41 sys.path.append('../../../ffg/ffg') |
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
42 import evtiming |
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
43 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 DEV = Namespace("http://projects.bigasterisk.com/device/") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
47 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
48 class InputGraph(object): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
49 def __init__(self, inputDirs, onChange): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
50 """ |
854 | 51 this has one Graph that's made of: |
52 - all .n3 files from inputDirs (read at startup) | |
53 - all the remote graphs, specified in the file graphs | |
54 | |
55 call updateFileData or updateRemoteData to reread those | |
56 graphs. getGraph to access the combined graph. | |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
57 |
854 | 58 onChange(self) is called if the contents of the full graph |
59 change (in an interesting way) during updateFileData or | |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
60 updateRemoteData. Interesting means statements other than the |
854 | 61 ones with the predicates on the boring list. onChange(self, |
62 oneShot=True) means: don't store the result of this change | |
63 anywhere; it needs to be processed only once | |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
64 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
65 self.inputDirs = inputDirs |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
66 self.onChange = onChange |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
67 self._fileGraph = Graph() |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
68 self._remoteGraph = None |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
69 self._combinedGraph = None |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
70 self._oneShotAdditionGraph = None |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
71 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
72 def updateFileData(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
73 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
74 make sure we contain the correct data from the files in inputDirs |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
75 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
76 # this sample one is actually only needed for the output, but I don't |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
77 # think I want to have a separate graph for the output |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
78 # handling |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
79 log.debug("read file graphs") |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
80 for fp in FilePath("input").walk(): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
81 if fp.isdir(): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
82 continue |
916
e8cce78a79d6
switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents:
882
diff
changeset
|
83 if fp.splitext()[1] != '.n3': |
e8cce78a79d6
switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents:
882
diff
changeset
|
84 continue |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
85 log.debug("read %s", fp) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
86 # todo: if this fails, leave the report in the graph |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
87 self._fileGraph.parse(fp.open(), format="n3") |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
88 self._combinedGraph = None |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
89 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
90 self.onChange(self) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
91 |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
92 @inlineCallbacks |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
93 def updateRemoteData(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
94 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
95 read all remote graphs (which are themselves enumerated within |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
96 the file data) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
97 """ |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
98 t1 = time.time() |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
99 log.debug("read remote graphs") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
100 g = ConjunctiveGraph() |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
101 |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
102 @inlineCallbacks |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
103 def fetchOne(source): |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
104 try: |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
105 # this part could be parallelized |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
106 fetchTime = yield addTrig(g, source) |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
107 except Exception, e: |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
108 log.error(" can't add source %s: %s", source, e) |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
109 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e)))) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
110 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad'])) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
111 else: |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
112 g.add((URIRef(source), ROOM['graphLoadMs'], |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
113 Literal(round(fetchTime * 1000, 1)))) |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
114 |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
115 fetchDone = [] |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
116 for source in self._fileGraph.objects(ROOM['reasoning'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
117 ROOM['source']): |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
118 fetchDone.append(fetchOne(source)) |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
119 yield gatherResults(fetchDone, consumeErrors=True) |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
120 log.debug("loaded all in %.1f ms", 1000 * (time.time() - t1)) |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
121 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
122 prevGraph = self._remoteGraph |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
123 self._remoteGraph = g |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
124 self._combinedGraph = None |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
125 if (prevGraph is None or |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
126 not graphEqual(g, prevGraph, ignorePredicates=[ |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
127 ROOM['signalStrength'], |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
128 # perhaps anything with a number-datatype for its |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
129 # object should be filtered out, and you have to make |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
130 # an upstream quantization (e.g. 'temp high'/'temp |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
131 # low') if you want to do reasoning on the difference |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
132 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"), |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
133 URIRef("http://bigasterisk.com/map#lastSeenAgo"), |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
134 ROOM['usingPower'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
135 ROOM['idleTimeMinutes'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
136 ROOM['idleTimeMs'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
137 ROOM['graphLoadMs'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
138 ROOM['localTimeToSecond'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
139 ROOM['history'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
140 ROOM['temperatureF'], |
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
141 ROOM['connectedAgo'], |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
142 ])): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
143 log.debug(" remote graph changed") |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
144 self.onChange(self) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
145 else: |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
146 log.debug(" remote graph has no changes to trigger rules") |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
147 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
148 def addOneShot(self, g): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
149 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
150 add this graph to the total, call onChange, and then revert |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
151 the addition of this graph |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
152 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
153 self._oneShotAdditionGraph = g |
854 | 154 self._combinedGraph = None |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
155 try: |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
156 self.onChange(self, oneShot=True, oneShotGraph=g) |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
157 finally: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
158 self._oneShotAdditionGraph = None |
854 | 159 self._combinedGraph = None |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
160 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
161 def getGraph(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
162 """rdflib Graph with the file+remote contents of the input graph""" |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
163 # this could be much faster with the combined readonly graph |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
164 # view from rdflib |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
165 if self._combinedGraph is None: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
166 self._combinedGraph = Graph() |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
167 if self._fileGraph: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
168 for s in self._fileGraph: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
169 self._combinedGraph.add(s) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
170 if self._remoteGraph: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
171 for s in self._remoteGraph: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
172 self._combinedGraph.add(s) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
173 if self._oneShotAdditionGraph: |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
174 for s in self._oneShotAdditionGraph: |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
175 self._combinedGraph.add(s) |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
176 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
177 return self._combinedGraph |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
178 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
179 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
180 class Reasoning(object): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
181 def __init__(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
182 self.prevGraph = None |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
183 self.lastPollTime = 0 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
184 self.lastError = "" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
185 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
186 self.actions = Actions(sendToLiveClients) |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
187 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
188 self.rulesN3 = "(not read yet)" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
189 self.inferred = Graph() # gets replaced in each graphChanged call |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
190 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
191 self.inputGraph = InputGraph([], self.graphChanged) |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
192 self.inputGraph.updateFileData() |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
193 |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
194 @evtiming.serviceLevel.timed('readRules') |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
195 def readRules(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
196 self.rulesN3 = open('rules.n3').read() # for web display |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
197 self.ruleStore = N3RuleStore() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
198 self.ruleGraph = Graph(self.ruleStore) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
199 self.ruleGraph.parse('rules.n3', format='n3') # for inference |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
200 |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
201 @inlineCallbacks |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
202 def poll(self): |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
203 t1 = time.time() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
204 try: |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
205 yield self.inputGraph.updateRemoteData() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
206 self.lastPollTime = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
207 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
208 log.error(traceback.format_exc()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
209 self.lastError = str(e) |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
210 evtiming.serviceLevel.addData('poll', time.time() - t1) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
211 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
212 def updateRules(self): |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
213 try: |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
214 t1 = time.time() |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
215 self.readRules() |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
216 ruleParseTime = time.time() - t1 |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
217 except ValueError: |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
218 # this is so if you're just watching the inferred output, |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
219 # you'll see the error too |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
220 self.inferred = Graph() |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
221 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
222 Literal(traceback.format_exc()))) |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
223 raise |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
224 return [(ROOM['reasoner'], ROOM['ruleParseTime'], |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
225 Literal(ruleParseTime))] |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
226 |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
227 evtiming.serviceLevel.timed('graphChanged') |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
228 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
229 t1 = time.time() |
854 | 230 oldInferred = self.inferred |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
231 try: |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
232 ruleStmts = self.updateRules() |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
233 |
854 | 234 g = inputGraph.getGraph() |
235 self.inferred = self._makeInferred(g) | |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
236 [self.inferred.add(s) for s in ruleStmts] |
854 | 237 |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
238 if oneShot: |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
239 # unclear where this should go, but the oneshot'd |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
240 # statements should be just as usable as inferred |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
241 # ones. |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
242 for s in oneShotGraph: |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
243 self.inferred.add(s) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
244 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
245 t2 = time.time() |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
246 self.actions.putResults(self.inputGraph.getGraph(), self.inferred) |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
247 putResultsTime = time.time() - t2 |
854 | 248 finally: |
249 if oneShot: | |
250 self.inferred = oldInferred | |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
251 log.info("graphChanged %.1f ms (putResults %.1f ms)" % |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
252 ((time.time() - t1) * 1000, |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
253 putResultsTime * 1000)) |
854 | 254 |
255 def _makeInferred(self, inputGraph): | |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
256 t1 = time.time() |
854 | 257 out = infer(inputGraph, self.ruleStore) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
258 inferenceTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
259 |
854 | 260 out.add((ROOM['reasoner'], ROOM['inferenceTime'], |
916
e8cce78a79d6
switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents:
882
diff
changeset
|
261 Literal(inferenceTime))) |
854 | 262 return out |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
263 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
264 |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
265 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
266 class Index(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
267 def get(self): |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
268 print evtiming.serviceLevel.serviceJsonReport() |
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
269 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
270 # make sure GET / fails if our poll loop died |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
271 ago = time.time() - self.settings.reasoning.lastPollTime |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
272 if ago > 2: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
273 self.set_status(500) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
274 self.finish("last poll was %s sec ago. last error: %s" % |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
275 (ago, self.settings.reasoning.lastError)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
276 return |
985 | 277 self.set_header("Content-Type", "text/html") |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
278 self.write(open('index.html').read()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
279 |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
280 class ImmediateUpdate(cyclone.web.RequestHandler): |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
281 @inlineCallbacks |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
282 def put(self): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
283 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
284 request an immediate load of the remote graphs; the thing we |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
285 do in the background anyway. No payload. |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
286 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
287 Using PUT because this is idempotent and retryable and |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
288 everything. |
859
379cefec542e
screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents:
854
diff
changeset
|
289 |
379cefec542e
screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents:
854
diff
changeset
|
290 todo: this should do the right thing when many requests come |
379cefec542e
screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents:
854
diff
changeset
|
291 in very quickly |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
292 """ |
854 | 293 print self.request.headers |
294 log.info("immediateUpdate from %s", | |
295 self.request.headers.get('User-Agent', '?')) | |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
296 yield r.poll() |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
297 self.set_status(202) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
298 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
299 def parseRdf(text, contentType): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
300 g = Graph() |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
301 g.parse(StringInputSource(text), format={ |
1055
e1693cc0b992
fix oneshot. more time reportin
drewp <drewp@bigasterisk.com>
parents:
1054
diff
changeset
|
302 'text/n3': 'n3', |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
303 }[contentType]) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
304 return g |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
305 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
306 class OneShot(cyclone.web.RequestHandler): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
307 def post(self): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
308 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
309 payload is an rdf graph. The statements are momentarily added |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
310 to the input graph for exactly one update. |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
311 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
312 todo: how do we go from a transition like doorclosed-to-open |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
313 to a oneshot event? the upstream shouldn't have to do it. Do |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
314 we make those oneshot events here? for every object change? |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
315 there are probably special cases regarding startup time when |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
316 everything appears to be a 'change'. |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
317 """ |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
318 try: |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
319 g = parseRdf(self.request.body, self.request.headers['content-type']) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
320 for s in g: |
1056
d2007482aec5
start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents:
1055
diff
changeset
|
321 log.debug("oneshot stmt %r", s) |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
322 if not len(g): |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
323 log.warn("incoming oneshot graph had no statements: %r", self.request.body) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
324 return |
1055
e1693cc0b992
fix oneshot. more time reportin
drewp <drewp@bigasterisk.com>
parents:
1054
diff
changeset
|
325 t1 = time.time() |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
326 self.settings.reasoning.inputGraph.addOneShot(g) |
1055
e1693cc0b992
fix oneshot. more time reportin
drewp <drewp@bigasterisk.com>
parents:
1054
diff
changeset
|
327 self.set_header('x-graph-ms', str(1000 * (time.time() - t1))) |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
328 except Exception as e: |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
329 log.error(e) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
330 raise |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
331 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
332 # for reuse |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
333 class GraphResource(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
334 def get(self, which): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
335 self.set_header("Content-Type", "application/json") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
336 r = self.settings.reasoning |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
337 g = {'lastInput': r.inputGraph.getGraph(), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
338 'lastOutput': r.inferred, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
339 }[which] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
340 self.write(self.jsonRdf(g)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
341 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
342 def jsonRdf(self, g): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
343 return json.dumps(sorted(list(g))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
344 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
345 class NtGraphs(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
346 """same as what gets posted above""" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
347 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
348 r = self.settings.reasoning |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
349 inputGraphNt = r.inputGraph.getGraph().serialize(format="nt") |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
350 inferredNt = r.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
351 self.set_header("Content-Type", "application/json") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
352 self.write(json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
353 "inferred": inferredNt})) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
354 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
355 class Rules(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
356 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
357 self.set_header("Content-Type", "text/plain") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
358 self.write(self.settings.reasoning.rulesN3) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
359 |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
360 class Status(cyclone.web.RequestHandler): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
361 def get(self): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
362 self.set_header("Content-Type", "text/plain") |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
363 g = self.settings.reasoning.inputGraph.getGraph() |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
364 msg = "" |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
365 for badSource in g.subjects(RDF.type, ROOM['FailedGraphLoad']): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
366 msg += "GET %s failed (%s). " % ( |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
367 badSource, g.value(badSource, ROOM['graphLoadError'])) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
368 if not msg: |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
369 self.finish("all inputs ok") |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
370 return |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
371 self.set_status(500) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
372 self.finish(msg) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
373 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
374 class Static(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
375 def get(self, p): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
376 self.write(open(p).read()) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
377 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
378 liveClients = set() |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
379 def sendToLiveClients(d=None, asJson=None): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
380 j = asJson or json.dumps(d) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
381 for c in liveClients: |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
382 c.sendMessage(j) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
383 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
384 class Events(cyclone.websocket.WebSocketHandler): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
385 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
386 def connectionMade(self, *args, **kwargs): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
387 log.info("websocket opened") |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
388 liveClients.add(self) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
389 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
390 def connectionLost(self, reason): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
391 log.info("websocket closed") |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
392 liveClients.remove(self) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
393 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
394 def messageReceived(self, message): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
395 log.info("got message %s" % message) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
396 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
397 class Application(cyclone.web.Application): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
398 def __init__(self, reasoning): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
399 handlers = [ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
400 (r"/", Index), |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
401 (r"/immediateUpdate", ImmediateUpdate), |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
402 (r"/oneShot", OneShot), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
403 (r'/(jquery.min.js)', Static), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
404 (r'/(lastInput|lastOutput)Graph', GraphResource), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
405 (r'/ntGraphs', NtGraphs), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
406 (r'/rules', Rules), |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
407 (r'/status', Status), |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
408 (r'/events', Events), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
409 ] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
410 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
411 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
412 if __name__ == '__main__': |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
413 |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
414 arg = docopt(""" |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
415 Usage: reasoning.py [options] |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
416 |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
417 -v Verbose (and slow updates) |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
418 """) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
419 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
420 r = Reasoning() |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
421 if arg['-v']: |
1040 | 422 from colorlog import ColoredFormatter |
423 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s", | |
424 datefmt=None, | |
425 reset=True, | |
426 log_colors={ | |
427 'DEBUG': 'cyan', | |
428 'INFO': 'green', | |
429 'WARNING': 'yellow', | |
430 'ERROR': 'red', | |
431 'CRITICAL': 'red,bg_white', | |
432 }, | |
433 secondary_log_colors={}, | |
434 style='%' | |
435 )) | |
436 | |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
437 import twisted.python.log |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
438 twisted.python.log.startLogging(sys.stdout) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
439 log.setLevel(logging.DEBUG) |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
440 outlog.setLevel(logging.DEBUG) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
441 |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
442 task.LoopingCall(r.poll).start(1.0 if not arg['-v'] else 10) |
1040 | 443 reactor.listenTCP(9071, Application(r), interface='::') |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
444 reactor.run() |