/
data-source.py
95 lines (74 loc) · 2.82 KB
/
data-source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import math
from datetime import datetime
from calendar import timegm
from bottle import (Bottle, HTTPResponse, run, request, response,
json_dumps as dumps)
FUNCTIONS = {'series A': math.sin, 'series B': math.cos}
app = Bottle()
def convert_to_time_ms(timestamp):
return 1000 * timegm(
datetime.strptime(
timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').timetuple())
def create_data_points(function, start, end, length=1020):
lower = convert_to_time_ms(start)
upper = convert_to_time_ms(end)
return [[function(i), int(i)] for i in [
lower + x*(upper-lower)/length for x in range(length)]]
@app.hook('after_request')
def enable_cors():
print("after_request hook")
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = \
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
@app.route("/", method=['GET', 'OPTIONS'])
def index():
return "OK"
@app.post('/search')
def search():
return HTTPResponse(body=dumps(['series A', 'series B']),
headers={'Content-Type': 'application/json'})
@app.post('/query')
def query():
print(request.json)
if request.json['targets'][0]['type'] == 'table':
series = request.json['targets'][0]['target']
bodies = {'series A': [{
"columns": [
{"text": "Time", "type": "time"},
{"text": "Country", " type": "string"},
{"text": "Number", "type": "number"}
],
"rows": [
[1234567, "SE", 123],
[1234567, "DE", 231],
[1234567, "US", 321]
],
"type": "table"
}],
'series B': [{"columns": [
{"text": "Time", "type": "time"},
{"text": "Country", "type": "string"},
{"text": "Number", "type": "number"}
],
"rows": [
[1234567, "BE", 123],
[1234567, "GE", 231],
[1234567, "PS", 321]
],
"type": "table"
}]}
series = request.json['targets'][0]['target']
body = dumps(bodies[series])
else:
body = []
start, end = request.json['range']['from'], request.json['range']['to']
for target in request.json['targets']:
name = target['target']
datapoints = create_data_points(FUNCTIONS[name], start, end)
body.append({'target': name, 'datapoints': datapoints})
body = dumps(body)
return HTTPResponse(body=body,
headers={'Content-Type': 'application/json'})
if __name__ == '__main__':
run(app=app, host='localhost', port=8081)