/
pdk_foreground_application.py
159 lines (134 loc) · 5.08 KB
/
pdk_foreground_application.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
# pylint: disable=line-too-long, no-member
import datetime
from django.template.loader import render_to_string
from django.utils import timezone
from ..models import DataPoint
def extract_secondary_identifier(properties):
if 'application' in properties:
return properties['application']
return None
def generator_name(identifier): # pylint: disable=unused-argument
return 'Foreground Application'
# def visualization(source, generator):
# context = {}
# context['source'] = source
# context['generator_identifier'] = generator
#
# values = []
#
# end = timezone.now()
# start = end - datetime.timedelta(days=1)
#
# last_value = -1
#
# for point in DataPoint.objects.filter(source=source.identifier, generator_identifier=generator, created__gt=start, created__lte=end).order_by('created'):
# properties = point.fetch_properties()
#
# if last_value != properties['level']:
# value = {}
#
# value['ts'] = properties['passive-data-metadata']['timestamp']
# value['value'] = properties['level']
#
# last_value = properties['level']
#
# values.append(value)
#
# context['values'] = values
#
# context['start'] = time.mktime(start.timetuple())
# context['end'] = time.mktime(end.timetuple())
#
# return render_to_string('pdk_device_battery_template.html', context)
def data_table(source, generator): # pylint: disable=too-many-locals
context = {}
context['source'] = source
context['generator_identifier'] = generator
end = timezone.now()
start = end - datetime.timedelta(days=1)
values = []
last_active = None
last_application = None
last_start = None
cumulative_duration = 0
for point in DataPoint.objects.filter(source=source.identifier, generator_identifier=generator, created__gt=start, created__lte=end).order_by('created'):
properties = point.fetch_properties()
active = None
if 'screen_active' in properties:
active = properties['screen_active']
application = None
if 'application' in properties:
application = properties['application']
update = False
if active != last_active:
update = True
if application != last_application:
update = True
if update:
if last_start != None:
value = {
'screen_active': last_active,
'application': last_application,
'start': last_start,
'duration': datetime.timedelta(seconds=(cumulative_duration / 1000))
}
values.append(value)
last_active = active
last_application = application
last_start = point.created
cumulative_duration = 0
else:
cumulative_duration += properties['duration']
context['values'] = values
return render_to_string('pdk_foreground_application_table_template.html', context)
# def compile_report(generator, sources): # pylint: disable=too-many-locals
# filename = tempfile.gettempdir() + '/pdk_export_' + str(arrow.get().timestamp) + '.zip'
#
# with ZipFile(filename, 'w') as export_file:
# for secondary_identifier in SECONDARY_FIELDS:
# secondary_filename = tempfile.gettempdir() + '/' + generator + '-' + \
# secondary_identifier + '.txt'
#
# with open(secondary_filename, 'w') as outfile:
# writer = csv.writer(outfile, delimiter='\t')
#
# columns = [
# 'Source',
# 'Created Timestamp',
# 'Created Date',
# ]
#
# for column in SECONDARY_FIELDS[secondary_identifier]:
# columns.append(column)
#
# writer.writerow(columns)
#
# for source in sources:
# points = DataPoint.objects.filter(source=source, generator_identifier=generator, secondary_identifier=secondary_identifier).order_by('source', 'created') # pylint: disable=no-member,line-too-long
#
# index = 0
# count = points.count()
#
# while index < count:
# for point in points[index:(index + 5000)]:
# row = []
#
# row.append(point.source)
# row.append(calendar.timegm(point.created.utctimetuple()))
# row.append(point.created.isoformat())
#
# properties = point.fetch_properties()
#
# for column in SECONDARY_FIELDS[secondary_identifier]:
# if column in properties:
# row.append(properties[column])
# else:
# row.append('')
#
# writer.writerow(row)
#
# index += 5000
#
# export_file.write(secondary_filename, secondary_filename.split('/')[-1])
#
# return filename