/
compile_significant_motion_report.py
57 lines (40 loc) · 2.21 KB
/
compile_significant_motion_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
import datetime
import gzip
import json
import pytz
import tempfile
from django.core.files import File
from django.core.management.base import BaseCommand
from django.utils import timezone
from purple_robot.settings import REPORT_DEVICES
from purple_robot_app.models import PurpleRobotReading, PurpleRobotReport
PROBE_NAME = 'edu.northwestern.cbits.purple_robot_manager.probes.builtin.SignificantMotionProbe'
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, 11, 10, 0, 0, 0, 0, tzinfo=pytz.timezone('US/Central'))
end_ts = start_ts + datetime.timedelta(days=1)
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\tTimestamp\n')
index = 0
while index < count:
end = index + 100
if end > count:
end = count
for payload in payloads[index:end]:
reading_json = json.loads(payload.payload)
gzf.write(hash + '\t' + str(reading_json['TIMESTAMP']) + '\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(hash + '-significant-motion.txt.gz', File(temp_file))
report.save()