Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 1092:54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
Ignore-this: a655f4c56db51b09b3f14d7f09e354cb
darcs-hash:4ffd7012f404392375434243104eba065ffb8086
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 09 May 2016 00:32:08 -0700 |
parents | 352ecf3c9aea |
children | e03696277b32 |
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 |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
19 import json, time, traceback, sys |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
20 from logging import getLogger, DEBUG, WARN |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
21 |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
22 from colorlog import ColoredFormatter |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
23 from docopt import docopt |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
24 from rdflib import Namespace, Literal, RDF, Graph |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 from twisted.internet import reactor, task |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
26 from twisted.internet.defer import inlineCallbacks |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
27 import cyclone.web, cyclone.websocket |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
28 |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
29 sys.path.append("scales/src") |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
30 from greplin import scales |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
31 from greplin.scales.cyclonehandler import StatsHandler |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
32 |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
33 from inference import infer, readRules |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
34 from actions import Actions |
1080 | 35 from inputgraph import InputGraph |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
36 from escapeoutputstatements import unquoteOutputStatements |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 sys.path.append("../../lib") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 from logsetup import log |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
40 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 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
|
43 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
|
44 |
1086 | 45 NS = {'': ROOM, 'dev': DEV} |
1088
0f6128740000
fix input/output statement ambiguity problem by allowing quoted output statements
drewp <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
46 |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
47 STATS = scales.collection('/web', |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
48 scales.PmfStat('poll'), |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
49 scales.PmfStat('graphChanged')) |
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
50 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 class Reasoning(object): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 def __init__(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 self.prevGraph = None |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 self.lastPollTime = 0 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 self.lastError = "" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
57 self.actions = Actions(sendToLiveClients) |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
58 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
59 self.rulesN3 = "(not read yet)" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
60 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
|
61 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
62 self.inputGraph = InputGraph([], self.graphChanged) |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
63 self.inputGraph.updateFileData() |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
64 |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
65 @inlineCallbacks |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
66 @STATS.poll.time() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
67 def poll(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
68 try: |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
69 yield self.inputGraph.updateRemoteData() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
70 self.lastPollTime = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
71 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
72 log.error(traceback.format_exc()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
73 self.lastError = str(e) |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
74 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
75 |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
76 def updateRules(self): |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
77 rulesPath = 'rules.n3' |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
78 try: |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
79 t1 = time.time() |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
80 self.rulesN3, self.ruleStore = readRules( |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
81 rulesPath, outputPatterns=[ |
1091
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
82 # Incomplete. See escapeoutputstatements.py for |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
83 # explanation. |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
84 (None, ROOM['brightness'], None), |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
85 (None, ROOM['playState'], None), |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
86 (None, ROOM['powerState'], None), |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
87 (None, ROOM['state'], None), |
352ecf3c9aea
more output statement patterns
drewp <drewp@bigasterisk.com>
parents:
1090
diff
changeset
|
88 ]) |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
89 ruleParseTime = time.time() - t1 |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
90 except ValueError: |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
91 # 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
|
92 # 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
|
93 self.inferred = Graph() |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
94 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
|
95 Literal(traceback.format_exc()))) |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
96 raise |
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
97 return [(ROOM['reasoner'], ROOM['ruleParseTime'], |
1086 | 98 Literal(ruleParseTime))], ruleParseTime |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
99 |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
100 @STATS.graphChanged.time() |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
101 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): |
1090 | 102 """ |
103 If we're getting called for a oneShot event, the oneShotGraph | |
104 statements are already in inputGraph.getGraph(). | |
105 """ | |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
106 log.info("----------------------") |
1090 | 107 log.info("graphChanged:") |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
108 if oneShot: |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
109 for s in oneShotGraph: |
1090 | 110 log.debug(" oneshot stmt %r", s) |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
111 t1 = time.time() |
854 | 112 oldInferred = self.inferred |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
113 try: |
1086 | 114 ruleStatStmts, ruleParseSec = self.updateRules() |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
115 |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
116 self.inferred = self._makeInferred(inputGraph.getGraph()) |
1088
0f6128740000
fix input/output statement ambiguity problem by allowing quoted output statements
drewp <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
117 |
1089
cb7fa2f30df9
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp <drewp@bigasterisk.com>
parents:
1088
diff
changeset
|
118 self.inferred += unquoteOutputStatements(self.inferred) |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
119 |
1090 | 120 self.inferred += ruleStatStmts |
854 | 121 |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
122 if oneShot: |
1090 | 123 # It's possible a oneShotGraph statement didn't |
124 # trigger a rule to do something, but was itself the | |
125 # output statement. Probably we could just mix in the | |
126 # whole inputGraph here and not special-case the | |
127 # oneShotGraph. | |
128 self.inferred += oneShotGraph | |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
129 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
130 t2 = time.time() |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
131 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
|
132 putResultsTime = time.time() - t2 |
854 | 133 finally: |
134 if oneShot: | |
135 self.inferred = oldInferred | |
1086 | 136 log.info("graphChanged took %.1f ms (rule parse %.1f ms, putResults %.1f ms)" % |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
137 ((time.time() - t1) * 1000, |
1086 | 138 ruleParseSec * 1000, |
1045
a328cc370b22
ipv6 fetch support. refactor Actions to new class and file
drewp <drewp@bigasterisk.com>
parents:
1040
diff
changeset
|
139 putResultsTime * 1000)) |
854 | 140 |
141 def _makeInferred(self, inputGraph): | |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
142 t1 = time.time() |
854 | 143 out = infer(inputGraph, self.ruleStore) |
1086 | 144 for p, n in NS.iteritems(): |
145 out.bind(p, n, override=True) | |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
146 inferenceTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
147 |
854 | 148 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
|
149 Literal(inferenceTime))) |
854 | 150 return out |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
151 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
152 |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
153 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
154 class Index(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
155 def get(self): |
1057
c1961da4180a
evtiming in reasoning service
drewp <drewp@bigasterisk.com>
parents:
1056
diff
changeset
|
156 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
157 # 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
|
158 ago = time.time() - self.settings.reasoning.lastPollTime |
1087
806d2f633775
don't break GET=/ page when we're in slow-updates debug mode
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
159 if ago > 15: |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
160 self.set_status(500) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
161 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
|
162 (ago, self.settings.reasoning.lastError)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
163 return |
985 | 164 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
|
165 self.write(open('index.html').read()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
166 |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
167 class ImmediateUpdate(cyclone.web.RequestHandler): |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
168 @inlineCallbacks |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
169 def put(self): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
170 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
171 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
|
172 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
|
173 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
174 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
|
175 everything. |
859
379cefec542e
screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents:
854
diff
changeset
|
176 |
379cefec542e
screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents:
854
diff
changeset
|
177 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
|
178 in very quickly |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
179 """ |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
180 log.info("immediateUpdate from %s %s", |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
181 self.request.headers.get('User-Agent', '?'), |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
182 self.request.headers['Host']) |
1054
bbaf0576f653
fetch all source graphs in parallel
drewp <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
183 yield r.poll() |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
184 self.set_status(202) |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
185 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
186 class OneShot(cyclone.web.RequestHandler): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
187 def post(self): |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
188 """ |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
189 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
|
190 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
|
191 |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 everything appears to be a 'change'. |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
197 """ |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
198 try: |
1086 | 199 dt = self.settings.reasoning.inputGraph.addOneShotFromString( |
200 self.request.body, self.request.headers['content-type']) | |
201 self.set_header('x-graph-ms', str(1000 * dt)) | |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
202 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
|
203 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
|
204 raise |
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
205 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
206 # for reuse |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
207 class GraphResource(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
208 def get(self, which): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
209 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
|
210 r = self.settings.reasoning |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
211 g = {'lastInput': r.inputGraph.getGraph(), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
212 'lastOutput': r.inferred, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
213 }[which] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
214 self.write(self.jsonRdf(g)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
215 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
216 def jsonRdf(self, g): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
217 return json.dumps(sorted(list(g))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
218 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
219 class NtGraphs(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
220 """same as what gets posted above""" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
221 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
222 r = self.settings.reasoning |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
223 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
|
224 inferredNt = r.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
225 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
|
226 self.write(json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
227 "inferred": inferredNt})) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
228 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
229 class Rules(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
230 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
231 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
|
232 self.write(self.settings.reasoning.rulesN3) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
233 |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
234 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
|
235 def get(self): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
236 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
|
237 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
|
238 msg = "" |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 if not msg: |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
243 self.finish("all inputs ok") |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
244 return |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
245 self.set_status(500) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
246 self.finish(msg) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
247 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
248 class Static(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
249 def get(self, p): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
250 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
|
251 |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
252 liveClients = set() |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
253 def sendToLiveClients(d=None, asJson=None): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
254 j = asJson or json.dumps(d) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
255 for c in liveClients: |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
256 c.sendMessage(j) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
257 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
258 class Events(cyclone.websocket.WebSocketHandler): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
259 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
260 def connectionMade(self, *args, **kwargs): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
261 log.info("websocket opened") |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
262 liveClients.add(self) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
263 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
264 def connectionLost(self, reason): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
265 log.info("websocket closed") |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
266 liveClients.remove(self) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
267 |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
268 def messageReceived(self, message): |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
269 log.info("got message %s" % message) |
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
270 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
271 class Application(cyclone.web.Application): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
272 def __init__(self, reasoning): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
273 handlers = [ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
274 (r"/", Index), |
852
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
275 (r"/immediateUpdate", ImmediateUpdate), |
6ccd930834d1
scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents:
851
diff
changeset
|
276 (r"/oneShot", OneShot), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
277 (r'/(jquery.min.js)', Static), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
278 (r'/(lastInput|lastOutput)Graph', GraphResource), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
279 (r'/ntGraphs', NtGraphs), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
280 (r'/rules', Rules), |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
281 (r'/status', Status), |
869
15af9d8c7186
reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents:
859
diff
changeset
|
282 (r'/events', Events), |
1092
54de5144900d
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp <drewp@bigasterisk.com>
parents:
1091
diff
changeset
|
283 (r'/stats/(.*)', StatsHandler, {'serverName': 'reasoning'}), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
284 ] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
285 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
|
286 |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
287 def configLogging(arg): |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
288 log.setLevel(WARN) |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
289 |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
290 if arg['-i'] or arg['-r'] or arg['-o']: |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
291 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s %(name)-6s %(filename)-12s:%(lineno)-3s %(funcName)-20s%(reset)s %(white)s%(message)s", |
1040 | 292 datefmt=None, |
293 reset=True, | |
294 log_colors={ | |
295 'DEBUG': 'cyan', | |
296 'INFO': 'green', | |
297 'WARNING': 'yellow', | |
298 'ERROR': 'red', | |
299 'CRITICAL': 'red,bg_white', | |
300 }, | |
301 secondary_log_colors={}, | |
302 style='%' | |
303 )) | |
304 | |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
305 if arg['-i']: |
984
5da9200418db
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents:
934
diff
changeset
|
306 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
|
307 twisted.python.log.startLogging(sys.stdout) |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
308 |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
309 getLogger('fetch').setLevel(DEBUG if arg['-i'] else WARN) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
310 log.setLevel(DEBUG if arg['-r'] else WARN) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
311 getLogger('output').setLevel(DEBUG if arg['-o'] else WARN) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
312 |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
313 |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
314 if __name__ == '__main__': |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
315 arg = docopt(""" |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
316 Usage: reasoning.py [options] |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
317 |
1085
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
318 -i Verbose log on the input phase (and slow down the polling) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
319 -r Verbose log on the reasoning phase and web stuff |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
320 -o Verbose log on the actions/output phase |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
321 --source=<substr> Limit sources to those with this string. |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
322 """) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
323 |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
324 r = Reasoning() |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
325 configLogging(arg) |
dda3f1524a52
lots of logging updates (patch may be imprecise)
drewp <drewp@bigasterisk.com>
parents:
1080
diff
changeset
|
326 task.LoopingCall(r.poll).start(1.0 if not arg['-i'] else 10) |
1040 | 327 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
|
328 reactor.run() |