This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickhouse-proxy.py
executable file
·316 lines (248 loc) · 7.06 KB
/
clickhouse-proxy.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
#import
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import clickhouse_driver
from math import pow as pow
from datetime import datetime
# TODO: check for uint range
SERVER_ADDRESS = ''
SERVER_PORT = int('8000')
CH_ADDRESS = '192.168.119.128'
CH_PORT = ''
CH_LOGIN = 'ionuser'
CH_PASSWORD = 'password'
# Math functions
def get_bit(bits: int, position: int):
return int(bits / pow(2, position)) & 0x01
#
# Decode Bounding Box Integer
#
# Decode hash number into a bound box matches it. Data returned in a four-element array: [minlat, minlon, maxlat, maxlon]
# @param {Number} hashInt
# @param {Number} bitDepth
# @returns {Array}
#
def decode_bbox_int(hashInt: int, bitDepth=52):
maxLat = 90
minLat = -90
maxLon = 180
minLon = -180
latBit = 0
lonBit = 0
step = bitDepth // 2
for i in range(0, step):
lonBit = get_bit(hashInt, ((step - i) * 2) - 1)
latBit = get_bit(hashInt, ((step - i) * 2) - 2)
if latBit == 0:
maxLat = (maxLat + minLat) / 2
else:
minLat = (maxLat + minLat) / 2
if lonBit == 0:
maxLon = (maxLon + minLon) / 2
else:
minLon = (maxLon + minLon) / 2
return [int(minLat), int(minLon), int(maxLat), int(maxLon)]
#
# Decode Integer
#
# Decode a hash number into pair of latitude and longitude. A javascript object is returned with keys `latitude`,
# `longitude` and `error`.
# @param {Number} hash_int
# @param {Number} bitDepth
# @returns {Object}
#
def decode_int(hash_int, bitDepth):
bbox = decode_bbox_int(hash_int, bitDepth)
lat = (bbox[0] + bbox[2]) / 2
lon = (bbox[1] + bbox[3]) / 2
latErr = bbox[2] - lat
lonErr = bbox[3] - lon
return {
"latitude": float(lat),
"longitude": float(lon),
"error": {
"latitude": float(latErr),
"longitude": float(lonErr)
}
}
# Requests handlers
def api_quality(req: dict) -> dict:
resp = dict(req)
return resp
def api_ssareas(req: dict) -> dict:
# Base resp fields
query_id = str(req['query_id'])
# Get current time
t_to = int(datetime.now().timestamp() * 1000)
t_from = t_to - 10000 * 10
req_str = """
select *
from (
SELECT
time,
sat,
ionpoint
FROM
rawdata.satxyz2
WHERE
time BETWEEN %i and %i
) as satxyz
ANY INNER JOIN (
SELECT
time,
sat,
avgNT,
sigNT,
s4
from (
SELECT
toUInt64(floor(time/10000,0)*10000) as time,
sat,
avg(abs(sigNT)) as sigNT
FROM
computed.xz1
WHERE
time BETWEEN %i and %i
group by
time,
sat
) as sigNT
ANY INNER JOIN (
SELECT
time,
sat,
avgNT,
s4
from (
SELECT
toUInt64(floor(time/10000,0)*10000) as time,
sat,
avg(abs(avgNT)) as avgNT
FROM
computed.NTDerivatives
WHERE
time BETWEEN %i and %i
group by
time,
sat
) as avgNT
ANY INNER JOIN (
SELECT
toUInt64(floor(time/10000,0)*10000) as time,
sat,
avg(s4) as s4
FROM
computed.s4
WHERE
time BETWEEN %i and %i
group by
time,
sat
) as S4
USING time, sat
) as avgNT_S4
USING time, sat
) as avgNT_sigNT_S4
USING time, sat
LIMIT 100
""" % (t_from, t_to, t_from, t_to, t_from, t_to, t_from, t_to)
resp = db.execute(req_str)
areas = []
for item in resp:
intensity = item[5]
sats = []
if intensity <= 0.01:
sat_intensity = 1
elif intensity <= 0.05:
sat_intensity = 2
elif intensity <= 0.2:
sat_intensity = 3
elif intensity <= 1.0:
sat_intensity = 4
sats.append(
{
"name": item[1],
"lat": decode_int(item[2], 52)["latitude"],
"lon": decode_int(item[2], 52)["longitude"],
"tec_dev": item[3],
"tec_mean": item[4],
"intensity": sat_intensity
}
)
if intensity <= 0.01:
radius = 50000
elif intensity <= 0.05:
radius = 100000
elif intensity <= 0.2:
radius = 150000
elif intensity <= 1.0:
radius = 200000
# TODO:
area = {}
area['timestamp'] = item[0]
area['Semi_major_axes'] = int(radius / 1000)
area['Semi_minor_axes'] = int(radius * 1.5 / 1000)
area['distance_to_centre'] = 270
area['azimut'] = 43.2
area['angle'] = 23.0
area['sat'] = sats
areas.append(area)
resp_obj = {}
resp_obj['query_id'] = query_id
resp_obj['areas'] = areas
return resp_obj
def api_fadingstats(req: dict) -> dict:
resp = dict(req)
return resp
def api_errors(req: dict) -> dict:
resp = dict(req)
return resp
def api_setreference(req: dict) -> dict:
resp = dict(req)
return resp
def api_status(req: dict) -> dict:
# TODO: add clichouse remote check
resp = {"query_id": req["query_id"], "status": "OK"}
return resp
def api_dcmstats(req: dict) -> dict:
resp = dict(req)
return resp
cases = {
'/api/quality': api_quality,
'/api/ss-areas': api_ssareas,
'/api/fading-stats': api_fadingstats,
'/api/errors': api_errors,
'/api/set-reference': api_setreference,
'/api/status': api_status,
'/api/dcm-stats': api_dcmstats
}
def db_connect():
client = clickhouse_driver.Client(host=CH_ADDRESS, user=CH_LOGIN, password=CH_PASSWORD)
return client
# TODO: change to Threaded*
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def _set_response(self, code: int, content):
self.send_response(code)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(content)
def do_POST(self):
if self.path not in cases:
self._set_response(404, '{}'.encode('utf-8'))
return
content_length = int(self.headers['Content-Length'])
if content_length is 0:
self._set_response(404, '{}'.encode('utf-8'))
return
post_data = self.rfile.read(content_length)
decoded = post_data.decode('utf-8')
resp = cases[self.path](json.loads(decoded))
dumped = json.dumps(resp)
self._set_response(200, dumped.encode('utf-8'))
if __name__ == '__main__':
global db
# TODO: Config parsing
db = db_connect()
httpd = HTTPServer((SERVER_ADDRESS, SERVER_PORT), SimpleHTTPRequestHandler)
httpd.serve_forever()