comparison service/reasoning/reasoning.py @ 280:c192d37b2bc8

lots of logging updates (patch may be imprecise) Ignore-this: cfe81299fe9a1e2b6b1140e9ec46aaf7
author drewp@bigasterisk.com
date Fri, 06 May 2016 17:32:41 -0700
parents d3733587e749
children 9728288c7f2f
comparison
equal deleted inserted replaced
279:57c9dbd2fdd0 280:c192d37b2bc8
14 When do we gather? The services should be able to trigger us, perhaps 14 When do we gather? The services should be able to trigger us, perhaps
15 with PSHB, that their graph has changed. 15 with PSHB, that their graph has changed.
16 """ 16 """
17 17
18 18
19 import json, time, traceback, sys
20 from logging import getLogger, DEBUG, WARN
21
22 from FuXi.Rete.RuleStore import N3RuleStore
23 from colorlog import ColoredFormatter
24 from docopt import docopt
25 from rdflib import Namespace, Literal, RDF, Graph
19 from twisted.internet import reactor, task 26 from twisted.internet import reactor, task
20 from twisted.internet.defer import inlineCallbacks, gatherResults 27 from twisted.internet.defer import inlineCallbacks
21 import time, traceback, sys, json, logging
22 from rdflib import Graph, ConjunctiveGraph
23 from rdflib import Namespace, URIRef, Literal, RDF
24 from rdflib.parser import StringInputSource
25
26 import cyclone.web, cyclone.websocket 28 import cyclone.web, cyclone.websocket
29
27 from inference import infer 30 from inference import infer
28 from docopt import docopt
29 from actions import Actions 31 from actions import Actions
30 from inputgraph import InputGraph 32 from inputgraph import InputGraph
31 from FuXi.Rete.RuleStore import N3RuleStore
32 33
33 sys.path.append("../../lib") 34 sys.path.append("../../lib")
34 from logsetup import log 35 from logsetup import log
35 log.setLevel(logging.WARN) 36
36 outlog = logging.getLogger('output')
37 outlog.setLevel(logging.WARN)
38 fetchlog = logging.getLogger('fetch')
39 fetchlog.setLevel(logging.WARN)
40 37
41 sys.path.append('../../../ffg/ffg') 38 sys.path.append('../../../ffg/ffg')
42 import evtiming 39 import evtiming
43 40
44 ROOM = Namespace("http://projects.bigasterisk.com/room/") 41 ROOM = Namespace("http://projects.bigasterisk.com/room/")
92 return [(ROOM['reasoner'], ROOM['ruleParseTime'], 89 return [(ROOM['reasoner'], ROOM['ruleParseTime'],
93 Literal(ruleParseTime))] 90 Literal(ruleParseTime))]
94 91
95 evtiming.serviceLevel.timed('graphChanged') 92 evtiming.serviceLevel.timed('graphChanged')
96 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): 93 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None):
94 log.info("----------------------")
95 log.info("graphChanged oneShot=%s", oneShot)
96 if oneShot:
97 for s in oneShotGraph:
98 log.debug("oneshot stmt %r", s)
97 t1 = time.time() 99 t1 = time.time()
98 oldInferred = self.inferred 100 oldInferred = self.inferred
99 try: 101 try:
100 ruleStmts = self.updateRules() 102 ruleStmts = self.updateRules()
101 103
156 everything. 158 everything.
157 159
158 todo: this should do the right thing when many requests come 160 todo: this should do the right thing when many requests come
159 in very quickly 161 in very quickly
160 """ 162 """
161 print self.request.headers 163 log.info("immediateUpdate from %s %s",
162 log.info("immediateUpdate from %s", 164 self.request.headers.get('User-Agent', '?'),
163 self.request.headers.get('User-Agent', '?')) 165 self.request.headers['Host'])
164 yield r.poll() 166 yield r.poll()
165 self.set_status(202) 167 self.set_status(202)
166 168
167 def parseRdf(text, contentType): 169 def parseRdf(text, contentType):
168 g = Graph() 170 g = Graph()
275 (r'/status', Status), 277 (r'/status', Status),
276 (r'/events', Events), 278 (r'/events', Events),
277 ] 279 ]
278 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning) 280 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning)
279 281
280 if __name__ == '__main__': 282 def configLogging(arg):
281 283 log.setLevel(WARN)
282 arg = docopt("""
283 Usage: reasoning.py [options]
284
285 -v Verbose (and slow updates)
286 -f Verbose log for fetching
287 --source=<substr> Limit sources to those with this string.
288 """)
289 284
290 r = Reasoning() 285 if arg['-i'] or arg['-r'] or arg['-o']:
291 if arg['-v']: 286 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s %(name)-6s %(filename)-12s:%(lineno)-3s %(funcName)-20s%(reset)s %(white)s%(message)s",
292 from colorlog import ColoredFormatter
293 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s",
294 datefmt=None, 287 datefmt=None,
295 reset=True, 288 reset=True,
296 log_colors={ 289 log_colors={
297 'DEBUG': 'cyan', 290 'DEBUG': 'cyan',
298 'INFO': 'green', 291 'INFO': 'green',
302 }, 295 },
303 secondary_log_colors={}, 296 secondary_log_colors={},
304 style='%' 297 style='%'
305 )) 298 ))
306 299
300 if arg['-i']:
307 import twisted.python.log 301 import twisted.python.log
308 twisted.python.log.startLogging(sys.stdout) 302 twisted.python.log.startLogging(sys.stdout)
309 log.setLevel(logging.DEBUG) 303
310 outlog.setLevel(logging.DEBUG) 304 getLogger('fetch').setLevel(DEBUG if arg['-i'] else WARN)
311 305 log.setLevel(DEBUG if arg['-r'] else WARN)
312 if arg['-f']: 306 getLogger('output').setLevel(DEBUG if arg['-o'] else WARN)
313 fetchlog.setLevel(logging.DEBUG) 307
314 308
315 task.LoopingCall(r.poll).start(1.0 if not arg['-v'] else 10) 309 if __name__ == '__main__':
310 arg = docopt("""
311 Usage: reasoning.py [options]
312
313 -i Verbose log on the input phase (and slow down the polling)
314 -r Verbose log on the reasoning phase and web stuff
315 -o Verbose log on the actions/output phase
316 --source=<substr> Limit sources to those with this string.
317 """)
318
319 r = Reasoning()
320 configLogging(arg)
321 task.LoopingCall(r.poll).start(1.0 if not arg['-i'] else 10)
316 reactor.listenTCP(9071, Application(r), interface='::') 322 reactor.listenTCP(9071, Application(r), interface='::')
317 reactor.run() 323 reactor.run()