Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new visualization options for generators:
* Battery * Foreground Application * Location * Fixed display of Light Sensor generator
- Loading branch information
1 parent
d1fa307
commit 7736ecd
Showing
10 changed files
with
609 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# pylint: disable=line-too-long, no-member | ||
|
||
import datetime | ||
import time | ||
|
||
from django.conf import settings | ||
from django.template.loader import render_to_string | ||
from django.utils import timezone | ||
|
||
from ..models import DataPoint | ||
|
||
def extract_secondary_identifier(properties): | ||
if 'status' in properties: | ||
return properties['status'] | ||
|
||
return None | ||
|
||
def generator_name(identifier): # pylint: disable=unused-argument | ||
return 'Device Location' | ||
|
||
def visualization(source, generator): | ||
context = {} | ||
context['source'] = source | ||
context['generator_identifier'] = generator | ||
|
||
values = [] | ||
|
||
end = timezone.now() | ||
start = end - datetime.timedelta(days=1) | ||
|
||
min_latitude = 90 | ||
max_latitude = -90 | ||
|
||
min_longitude = 180 | ||
max_longitude = -180 | ||
|
||
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() | ||
|
||
values.append(properties) | ||
|
||
latitude = properties['latitude'] | ||
longitude = properties['longitude'] | ||
|
||
if latitude < min_latitude: | ||
min_latitude = latitude | ||
|
||
if latitude > max_latitude: | ||
max_latitude = latitude | ||
|
||
if longitude < min_longitude: | ||
min_longitude = longitude | ||
|
||
if longitude > max_longitude: | ||
max_longitude = longitude | ||
|
||
context['values'] = values | ||
|
||
context['center_latitude'] = (min_latitude + max_latitude) / 2 | ||
context['center_longitude'] = (min_longitude + max_longitude) / 2 | ||
|
||
context['start'] = time.mktime(start.timetuple()) | ||
context['end'] = time.mktime(end.timetuple()) | ||
|
||
context['google_api_key'] = settings.PDK_GOOGLE_MAPS_API_KEY | ||
|
||
return render_to_string('pdk_device_location_template.html', context) | ||
|
||
def data_table(source, generator): | ||
context = {} | ||
context['source'] = source | ||
context['generator_identifier'] = generator | ||
|
||
end = timezone.now() | ||
start = end - datetime.timedelta(days=1) | ||
|
||
context['values'] = DataPoint.objects.filter(source=source.identifier, generator_identifier=generator, created__gt=start, created__lte=end).order_by('created') | ||
|
||
return render_to_string('pdk_device_location_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<table id="location_values_table" class="table-striped" data-toggle="table" data-sort-name="created" data-sort-order="desc" data-pagination="true" style="z-index: 10; display: none;"> | ||
<thead> | ||
<tr> | ||
<th data-sortable="true" data-field="created">Created</th> | ||
<th data-sortable="true">Latitude</th> | ||
<th data-sortable="true">Longitude</th> | ||
<th data-sortable="true">Accuracy</th> | ||
<th data-sortable="true">Provider</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for row in values %} | ||
{% with props=row.fetch_properties %} | ||
<tr> | ||
<td> | ||
<span style="display: none;"> | ||
{{ row.created.isoformat }} | ||
</span> | ||
{{ row.created }} | ||
</td> | ||
<td>{{ props.latitude }}</td> | ||
<td>{{ props.longitude }}</td> | ||
<td>{{ props.accuracy }}</td> | ||
<td>{{ props.provider }}</td> | ||
</tr> | ||
{% endwith %} | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
|
||
<script> | ||
window.showValues = function() { | ||
$("#location_values_table").show(); | ||
}; | ||
</script> |
Oops, something went wrong.