/
compile_gyroscope_report.py
123 lines (86 loc) · 4.69 KB
/
compile_gyroscope_report.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
# pylint: disable=line-too-long, no-member
import datetime
import gzip
import json
import pytz
import sys
import tempfile
from django.core.files import File
from django.core.management.base import BaseCommand
from django.utils import timezone
from purple_robot_app.models import PurpleRobotReading, PurpleRobotReport
from purple_robot.settings import REPORT_DEVICES
PROBE_NAME = 'edu.northwestern.cbits.purple_robot_manager.probes.builtin.GyroscopeProbe'
class Command(BaseCommand):
def handle(self, *args, **options):
hashes = REPORT_DEVICES # PurpleRobotPayload.objects.order_by().values('user_id').distinct()
# start = datetime.datetime.now() - datetime.timedelta(days=120)
start_ts = datetime.datetime(2015, 7, 3, 5, 0, 0, 0, tzinfo=pytz.timezone('US/Central'))
end_ts = start_ts + datetime.timedelta(hours=1)
for user_hash in hashes:
# hash = hash['user_id']
payloads = PurpleRobotReading.objects.filter(user_id=user_hash, probe=PROBE_NAME, logged__gte=start_ts, logged__lt=end_ts).order_by('logged')
count = payloads.count()
if count > 0:
temp_file = tempfile.TemporaryFile()
gzf = gzip.GzipFile(mode='wb', fileobj=temp_file)
gzf.write('User ID\tSensor Timestamp\tNormalized Timestamp\tCPU Timestamp\tX\tY\tZ\n')
index = 0
last_sensor = sys.maxint
base_ts = 0
while index < count:
end = index + 100
if end > count:
end = count
for payload in payloads[index:end]:
reading_json = json.loads(payload.payload)
normal_times = []
sensor_times = []
cpu_times = []
x_readings = []
y_readings = []
z_readings = []
has_sensor = False
if 'SENSOR_TIMESTAMP' in reading_json:
has_sensor = True
for sensor_time in reading_json['SENSOR_TIMESTAMP']:
sensor_times.append(sensor_time)
for event_time in reading_json['EVENT_TIMESTAMP']:
cpu_times.append(event_time)
if has_sensor is False:
sensor_times.append(-1)
normal_times.append(-1)
if has_sensor:
for i in range(0, len(sensor_times)):
try:
sensor_ts = float(sensor_times[i])
normalized_ts = sensor_ts / (1000 * 1000 * 1000)
if normalized_ts < last_sensor:
cpu_time = cpu_times[i]
base_ts = cpu_time - normalized_ts
normal_times.append(base_ts + normalized_ts)
last_sensor = normalized_ts
except TypeError:
print('compile_gyroscope_report[Invalid Value/' + str(payload.pk) + ']: ' + str(sensor_times[i]))
for x_reading in reading_json['X']:
x_readings.append(x_reading)
for y_reading in reading_json['Y']:
y_readings.append(y_reading)
for z_reading in reading_json['Z']:
z_readings.append(z_reading)
for i in range(0, len(cpu_times)):
x_reading = x_readings[i]
y_reading = y_readings[i]
z_reading = z_readings[i]
cpu_time = cpu_times[i]
sensor_time = sensor_times[i]
normal_time = normal_times[i]
gzf.write(user_hash + '\t' + str(sensor_time) + '\t' + str(normal_time) + '\t' + str(cpu_time) + '\t' + str(x_reading) + '\t' + str(y_reading) + '\t' + str(z_reading) + '\n')
index += 100
gzf.flush()
gzf.close()
temp_file.seek(0)
report = PurpleRobotReport(generated=timezone.now(), mime_type='application/x-gzip', probe=PROBE_NAME, user_id=user_hash)
report.save()
report.report_file.save(user_hash + '-gyroscope.txt.gz', File(temp_file))
report.save()