-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
477 lines (396 loc) · 17.4 KB
/
process.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import os
import os.path
import sys
import time
import json
from numpy.core.defchararray import count, index
import pandas as pd
import numpy as np
import unittest as ut
from sqlalchemy import create_engine, text, types
from sqlalchemy.sql.expression import null
# import db configs
from config import *
CAPTURE_FILE_NAME = 'data'
if len(CAPTURES_DIR) <= 0:
print("No captures directory declared in config.py. Exiting.")
sys.exit(1)
if not os.path.exists(CAPTURES_DIR):
os.mkdir(CAPTURES_DIR)
# create db connection
try:
# dialect+driver://username:password@host:port/database
# TODO(rob): use config file https://docs.sqlalchemy.org/en/13/core/engines.html#sqlalchemy.engine_from_config
connection_string = f'mysql+pymysql://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}'
engine = create_engine(connection_string)
except Exception as e:
print(e)
sys.exit(1)
def check_for_unprocessed_captures():
# get ids of unprocessed captures
with engine.connect() as conn:
query = """
SELECT capture_id
FROM captures
WHERE end IS NOT NULL AND processed IS NULL
ORDER BY start
"""
result = conn.execute(query)
ready = list([r[0] for r in result])
return ready
# for each client, aggregate each interaction type and show counts in one session
def aggregate_interaction_type(session_id, interaction_type, request_id):
try:
# aggregate by interaction types
with engine.connect() as conn:
with conn.begin():
query = text("""
DROP TABLE IF EXISTS `aggregate_interaction`;
"""
)
conn.execute(query)
with conn.begin():
query = text("""
CREATE TABLE `aggregate_interaction`
(
client_id int not null,
primary key (client_id),
interaction_count int not null);
"""
)
conn.execute(query)
# insert query results to aggregation_interaction table
with conn.begin():
query = text("""
INSERT INTO aggregate_interaction
SELECT client_id, count(message) as interaction_count
FROM data
WHERE message->'$.interactionType' = :interaction_type and session_id= :session_id
GROUP BY client_id;
"""
)
conn.execute(query,{"session_id":session_id, "interaction_type":interaction_type})
with conn.begin():
query = text("""
SELECT *
FROM aggregate_interaction;
"""
)
result = conn.execute(query)
count = [r[0:] for r in result]
# result to dataframe
df = pd.DataFrame(count, columns = ['client_id','interaction_count'])
filename = str("aggregate_interaction_" + time.strftime('%Y-%m-%d %H-%S') + ".csv")
df.to_csv(filename,index=False)
print("aggregate_interaction csv file downloaded!")
# grab and add file location back to data_request table
file_path = os.path.abspath(filename)
update_data_request(request_id, 1, file_path)
# return True if aggregation function completed and csv got downloaded
return True
except Exception as e:
# return False and print error messages
print(e)
return False
# for each entity_type, show each client's activity in one session
def aggregate_user(session_id, client_id, request_id):
# aggregate by users
try:
with engine.connect()as conn:
with conn.begin():
query = text("""
DROP TABLE IF EXISTS `aggregate_user`;
"""
)
conn.execute(query)
with conn.begin():
query = text("""
CREATE TABLE if not exists `aggregate_user`
(
entity_type varchar(20) not null,
primary key (entity_type),
user_count int not null
);
"""
)
conn.execute(query)
# insert query result to table
with conn.begin():
query = text("""
INSERT INTO aggregate_user
SELECT message->'$.entityType' as entity_type, count(*) as count
FROM data
WHERE message->'$.clientId' = :client_id and session_id = :session_id and `type` = 'sync'
group by entity_type;
"""
)
conn.execute(query,{"session_id":session_id, "client_id":client_id})
with conn.begin():
query = text("""
UPDATE komodo.aggregate_user
SET
entity_type = replace(replace(replace(REPLACE(entity_type, 0, 'head'), 1, 'left_hand'), 2, 'right_hand'), 3 ,'spawned_entity');
"""
)
conn.execute(query)
with conn.begin():
query = text("""
SELECT *
FROM aggregate_user;
"""
)
result = conn.execute(query)
count = [r[0:] for r in result]
# result to dataframe
df = pd.DataFrame(count, columns = ['entity_type','user_count'])
filename = str("aggregate_user_" + time.strftime('%Y-%m-%d %H-%S') + ".csv")
df.to_csv(filename,index=False)
# grab and add file back to data_request table
file_path = os.path.abspath(filename)
print("aggregate_user csv file downloaded!")
update_data_request(request_id, 1, file_path)
# return True if aggregation function completed and csv got downloaded
return True
except Exception as e:
# return False and print error messages
print(e)
return False
# calculate user energy for each entity type
def user_energy(session_id,client_id, entity_type,request_id):
try:
with engine.connect() as conn:
query = text("""
SELECT client_id, session_id, timestamp,entity_type, energy
FROM
(SELECT session_id, client_id, message->'$.entityType' as entity_type,
message->'$.pos' as position,
SQRT(POWER( message->'$.pos.x' - LAG(message->'$.pos.x',1) OVER (order by seq),2)+
POWER( message->'$.pos.y' - LAG(message->'$.pos.y',1) OVER (order by seq),2)+
POWER( message->'$.pos.z' - LAG(message->'$.pos.z',1) OVER (order by seq),2))/(ts - LAG(ts,1) OVER (order by seq)) AS energy,
ts AS timestamp, seq
FROM data
WHERE message->'$.clientId' = :client_id AND session_id = :session_id AND `type` = 'sync'
ORDER BY seq) AS user_energy
WHERE energy IS NOT NULL AND entity_type = :entity_type
ORDER BY entity_type, energy DESC;
"""
)
result = conn.execute(query,{"session_id":session_id, "client_id":client_id, "entity_type":entity_type})
count = [r[0:] for r in result]
# record results in a dataframe
df = pd.DataFrame(count, columns = ['client_d','session_id','timestamp','entity_type','energy'])
filename = str("user_energy_" + time.strftime('%Y-%m-%d %H-%S') + ".csv")
df.to_csv(filename,index=False)
# grab and add file back to data_request table
file_path = os.path.abspath(filename)
print("user energy csv file downloaded!")
update_data_request(request_id, 1, file_path)
return True
except Exception as e:
print(e)
return False
def process_file(id, file):
print("Processing file:", file)
try:
if (not os.path.isfile(file)):
print(f"Error processing file: {file}: file does not exist")
return False
df = pd.read_json(file, dtype={'capture_id': types.String})
# explicitly set capture_id data type because the "_" character is valid syntax for python ints, and will read it as such and omit the "_".
with engine.connect() as conn:
df.to_sql('data', conn, if_exists='append', index=False, dtype={'message': types.JSON}) # explicitly set the message data type, otherwise the insert will fail.
print('Done.')
return True
except Exception as e:
print(f"Error processing file: {file}: {e}")
return False
def mark_as_processed(capture_id, success):
try:
if success:
print("Successfully processed", capture_id)
processed = int(time.time())
else:
print("Failed to process capture:", capture_id)
processed = 0
query = text("UPDATE captures SET processed = :p WHERE capture_id = :ci")
with engine.connect() as conn:
result = conn.execute(query, {'p': processed, 'ci': capture_id})
except Exception as e:
print(e)
def check_for_data_requests_table():
# check if data_requests exist, if not, create one
try:
with engine.connect() as conn:
with conn.begin():
query = """
show tables like 'data_requests';
"""
result = conn.execute(query)
exist = list([r[0] for r in result])
if not (bool(exist)):
with engine.connect()as conn:
with conn.begin():
query = text("""
CREATE TABLE if not exists `data_requests`
(
request_id int not null AUTO_INCREMENT,
processed_capture_id varchar(50) not null,
who_requested int not null,
aggregation_function varchar(50) not null,
is_it_fulfilled int,
url varchar(255),
message JSON,
file_location varchar(255),
primary key (request_id)
);
"""
)
conn.execute(query)
with conn.begin():
query = text("""
INSERT INTO data_requests (`processed_capture_id`, `who_requested`, `aggregation_function`, `is_it_fulfilled`,`message`)
VALUES ('666_9999999999999', 2, 'aggregate_user', 1,'{"sessionId": null, "clientId": 888, "captureId": 777, "type": "test function", "interactionType": 1,"entityType": 0}');
"""
)
conn.execute(query)
print("data_requests table created.")
return True
else:
print("data_requests table exists.")
return True
except Exception as e:
# return False and print error messages
print(e)
return False
def aggregation_file_download():
with engine.connect() as conn:
with conn.begin():
query = text("""
SELECT request_id, aggregation_function, is_it_fulfilled, message->'$.clientId' as client_id,
message->'$.sessionId' as session_id, message->'$.entityType' as entity_type,
message->'$.interactionType' as interaction_type
FROM data_requests
WHERE is_it_fulfilled = 0
ORDER BY request_id;
"""
)
result = conn.execute(query)
count = [r[0:] for r in result]
temp_df = pd.DataFrame(count, columns = ['request_id','aggregation_function','is_it_fulfilled','client_id','session_id','entity_type','interaction_type'])
temp_df.set_index("request_id",inplace = True)
# iterate rows in data_request table
for index, row in temp_df.iterrows():
request_id = index
# parse all inputs
aggregation_function = row['aggregation_function']
is_it_fulfilled = row['is_it_fulfilled']
client_id = row['client_id']
session_id = row['session_id']
entity_type = row['entity_type']
interaction_type = row['interaction_type']
print(aggregation_function)
# direct rows to functions and download CSV
if aggregation_function == "aggregate_interaction_type":
if (session_id != "null" and interaction_type != "null"):
aggregate_interaction_type(session_id,interaction_type,request_id)
else:
print("Argument(s) for aggregate_interaction not valid!")
if aggregation_function == "aggregate_user":
print(session_id, client_id)
if (client_id != "null" and session_id != "null"):
aggregate_user(session_id,client_id,request_id)
else:
print("Argument(s) for aggregate_user not valid!")
if aggregation_function == "user_energy":
if (entity_type != "null" and client_id!= "null"):
label= user_energy(session_id,client_id, entity_type,request_id)
else:
print("Argument(s) for user_energy not valid!")
def update_data_request(request_id,fulfilled_flag,file_location):
# update fulfilled flag to 1, once aggregation function completed and csv files got downloaded
try:
query = text("""
UPDATE `data_requests`
SET is_it_fulfilled = :f, file_location = :fl
WHERE request_id = :ri;
""")
with engine.connect() as conn:
result = conn.execute(query, {'f': fulfilled_flag, 'fl': file_location,'ri':request_id})
except Exception as e:
print(e)
# show how stroke_id, stroke_type were used between timestamps
def drawing_pattern():
try:
with engine.connect() as conn:
with conn.begin():
query = text("""
SELECT ts AS timestamp,
count(message->'$.strokeType') AS stroke_type_count,
count(message->'$.strokeId') AS stroke_id_count
FROM data
GROUP BY ts
ORDER BY stroke_type_count DESC;
"""
)
conn.execute(query)
# get result and return
result = conn.execute(query)
count = [r[0:] for r in result]
return True
except Exception as e:
# return False and print error messages
print(e)
return False
# Get information when multiple users appear within a diameter
def user_proximity(diameter):
try:
with engine.connect() as conn:
with conn.begin():
query = text("""
SELECT ts, client_id, position, distance, capture_id, session_id
FROM(
SELECT client_id, message->'$.pos' AS position,
SQRT(POWER( message->'$.pos.x' - LAG(message->'$.pos.x',1) OVER (order by ts,message->'$.pos'),2)+
POWER( message->'$.pos.y' - LAG(message->'$.pos.y',1) OVER (order by ts,message->'$.pos'),2)+
POWER( message->'$.pos.z' - LAG(message->'$.pos.z',1) OVER (order by ts,message->'$.pos'),2)) AS distance,
capture_id, session_id, ts
FROM data
WHERE ts IN (SELECT ts
FROM data
GROUP BY ts
HAVING count(distinct client_id) > 1)
ORDER BY ts, position
) temp
WHERE distance > 0 AND distance < (:diameter)
ORDER BY distance;
"""
)
conn.execute(query,{"diameter":diameter})
# get result and return
result = conn.execute(query)
return True
except Exception as e:
# return False and print error messages
print(e)
return False
if __name__ == "__main__":
# get result flag for checking data_request table
data_request_flag = check_for_data_requests_table()
# infinite poll & process
while True:
ready = check_for_unprocessed_captures()
if len(ready) > 0:
print("Ready to process:", ready)
for id in ready:
session = id.split("_")[0]
capture = id.split("_")[1]
file = os.path.join(CAPTURES_DIR, session, capture, CAPTURE_FILE_NAME)
success = process_file(id, file)
mark_as_processed(id, success)
else:
print('Nothing to process', time.strftime("%H:%M:%S", time.localtime()))
# rinse & repeat
time.sleep(10)
# check data_request table and direct to respective functions
if data_request_flag:
aggregation_file_download()