/
compile_accelerometer_report.py
122 lines (85 loc) · 4.66 KB
/
compile_accelerometer_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
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.AccelerometerProbe'
class Command(BaseCommand):
def handle(self, *args, **options):
hashes = REPORT_DEVICES
# 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)
# print(start_ts.isoformat())
# print(end_ts.isoformat())
for user_hash in hashes:
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
last_sensor = sys.maxint
base_ts = 0
for payload in payloads[index:end]:
reading_json = json.loads(payload.payload)
ns = []
ss = []
ts = []
xs = []
ys = []
zs = []
has_sensor = False
if 'SENSOR_TIMESTAMP' in reading_json:
has_sensor = True
for s in reading_json['SENSOR_TIMESTAMP']:
ss.append(s)
for t in reading_json['EVENT_TIMESTAMP']:
ts.append(t)
if has_sensor is False:
ss.append(-1)
ns.append(-1)
if has_sensor:
for i in range(0, len(ss)):
sensor_ts = float(ss[i])
normalized_ts = sensor_ts / (1000 * 1000 * 1000)
if normalized_ts < last_sensor:
cpu_time = ts[i]
base_ts = cpu_time - normalized_ts
ns.append(base_ts + normalized_ts)
last_sensor = normalized_ts
for x in reading_json['X']:
xs.append(x)
for y in reading_json['Y']:
ys.append(y)
for z in reading_json['Z']:
zs.append(z)
for i in range(0, len(ts)):
x = xs[i]
y = ys[i]
z = zs[i]
t = ts[i]
s = ss[i]
n = ns[i]
gzf.write(user_hash + '\t' + str(s) + '\t' + str(n) + '\t' + str(t) + '\t' + str(x) + '\t' + str(y) + '\t' + str(z) + '\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 + '-accelerometer.txt.gz', File(temp_file))
report.save()