Mercurial > code > home > repos > homeauto
annotate service/reasoning/oneShot @ 795:c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
author | drewp@bigasterisk.com |
---|---|
date | Sun, 27 Dec 2020 03:29:18 -0800 |
parents | 0996e3a91c85 |
children |
rev | line source |
---|---|
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
1 #!/usr/bin/python3 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
2 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
3 send a statement to the reasoning server for one update cycle. Args |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
4 are s/p/o in n3 notation, with many prefixes predefined here. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
5 """ |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
6 import json |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
7 import os |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
8 import subprocess |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
9 import sys |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
10 import time |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
11 |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
12 import requests |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
13 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
14 s, p, o = sys.argv[1:] |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
15 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
16 prefixes = { |
328 | 17 '': 'http://projects.bigasterisk.com/room/', |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
18 'room': 'http://projects.bigasterisk.com/room/', |
321
79efb6fdcb95
rules updates for storage and changing
drewp@bigasterisk.com
parents:
250
diff
changeset
|
19 'shuttle': 'http://bigasterisk.com/room/livingRoom/shuttlepro/', |
79efb6fdcb95
rules updates for storage and changing
drewp@bigasterisk.com
parents:
250
diff
changeset
|
20 'sensor': 'http://bigasterisk.com/homeauto/sensor/', |
79efb6fdcb95
rules updates for storage and changing
drewp@bigasterisk.com
parents:
250
diff
changeset
|
21 } |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
23 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
24 def expand(term): |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
47
diff
changeset
|
25 if ':' not in term or term.startswith(('<', '"', "'")): |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
26 return term |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
27 left, right = term.split(':', 1) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
28 if left in prefixes: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
29 return '<%s%s>' % (prefixes[left], right) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
30 return term |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
31 |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
32 |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
33 pod = json.loads(subprocess.check_output(["kubectl", "get", "pod", "--selector=app=reasoning", "-o", "json"])) |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
34 ip = pod['items'][0]['status']['podIP'] |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
35 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
36 stmt = '%s %s %s .' % (expand(s), expand(p), expand(o)) |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
37 print("Sending: %s" % stmt) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
diff
changeset
|
38 |
250 | 39 t1 = time.time() |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
40 ret = requests.post('http://%s/oneShot' % os.environ.get('REASONING', f'{ip}:9071'), |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
41 headers={"content-type": "text/n3"}, |
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
42 data=stmt.encode('ascii')) |
250 | 43 g = float(ret.headers['x-graph-ms']) |
795
c8562ace4917
big updates for k8s, py3, drop FuXi, use prometheus for metrics.
drewp@bigasterisk.com
parents:
611
diff
changeset
|
44 print("%.1f ms for graph update; %.1f ms other overhead" % (g, 1000 * (time.time() - t1) - g)) |