Skip to content

Commit

Permalink
Added new visualization options for generators:
Browse files Browse the repository at this point in the history
* Battery
* Foreground Application
* Location
* Fixed display of Light Sensor generator
  • Loading branch information
audaciouscode committed Jun 12, 2017
1 parent d1fa307 commit 7736ecd
Show file tree
Hide file tree
Showing 10 changed files with 609 additions and 50 deletions.
13 changes: 4 additions & 9 deletions generators/pdk_device_battery.py
Expand Up @@ -27,20 +27,15 @@ def visualization(source, generator):
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']
value = {}

last_value = properties['level']
value['ts'] = properties['passive-data-metadata']['timestamp']
value['value'] = properties['level']

values.append(value)
values.append(value)

context['values'] = values

Expand Down
159 changes: 159 additions & 0 deletions generators/pdk_foreground_application.py
@@ -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
132 changes: 132 additions & 0 deletions generators/pdk_location.py
@@ -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
4 changes: 2 additions & 2 deletions templates/pdk_device_battery_table_template.html
@@ -1,7 +1,7 @@
<table id="battery_values_table" class="table-striped" data-toggle="table" data-pagination="true" style="z-index: 10; display: none;">
<table id="battery_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">Created</th>
<th data-sortable="true" data-field="created">Created</th>
<th data-sortable="true">Level</th>
<th data-sortable="true">Status</th>
<th data-sortable="true">Temperature</th>
Expand Down
35 changes: 35 additions & 0 deletions templates/pdk_device_location_table_template.html
@@ -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>

0 comments on commit 7736ecd

Please sign in to comment.