Navigation Menu

Skip to content

Commit

Permalink
Improved support for older databases.
Browse files Browse the repository at this point in the history
* Improved support for event logging.
  • Loading branch information
audaciouscode committed Apr 2, 2017
1 parent 9a51d39 commit e0847a5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
4 changes: 2 additions & 2 deletions admin.py
Expand Up @@ -12,8 +12,8 @@ class DataPointVisualizationsAdmin(admin.OSMGeoAdmin):
class DataPointAdmin(admin.OSMGeoAdmin):
openlayers_url = 'https://openlayers.org/api/2.13.1/OpenLayers.js'

list_display = ('source', 'generator_identifier', 'created', 'recorded',)
list_filter = ('created', 'recorded', 'generator_identifier',)
list_display = ('source', 'generator_identifier', 'secondary_identifier', 'created', 'recorded',)
list_filter = ('created', 'recorded', 'generator_identifier', 'secondary_identifier',)

@admin.register(DataBundle)
class DataBundleAdmin(admin.OSMGeoAdmin):
Expand Down
6 changes: 4 additions & 2 deletions management/commands/process_bundles.py
Expand Up @@ -5,8 +5,8 @@
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

from ..decorators import handle_lock
from ..models import DataPoint, DataBundle, install_supports_jsonfield
from ...decorators import handle_lock
from ...models import DataPoint, DataBundle, install_supports_jsonfield

class Command(BaseCommand):
help = 'Convert unprocessed DataBundle instances into DataPoint instances.'
Expand Down Expand Up @@ -50,6 +50,8 @@ def handle(self, *args, **options):
point.properties = bundle_point
else:
point.properties = json.dumps(bundle_point, indent=2)

point.fetch_secondary_identifier()

point.save()

Expand Down
20 changes: 20 additions & 0 deletions migrations/0016_datapoint_secondary_identifier.py
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-04-02 01:00
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('passive_data_kit', '0015_datafile_identifier'),
]

operations = [
migrations.AddField(
model_name='datapoint',
name='secondary_identifier',
field=models.CharField(blank=True, db_index=True, max_length=1024, null=True),
),
]
22 changes: 22 additions & 0 deletions models.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import json

import importlib
import psycopg2

Expand Down Expand Up @@ -35,6 +37,7 @@ class DataPoint(models.Model):
source = models.CharField(max_length=1024, db_index=True)
generator = models.CharField(max_length=1024, db_index=True)
generator_identifier = models.CharField(max_length=1024, db_index=True, default='unknown-generator')
secondary_identifier = models.CharField(max_length=1024, db_index=True, null=True, blank=True)

created = models.DateTimeField(db_index=True)
generated_at = models.PointField(null=True)
Expand All @@ -45,7 +48,26 @@ class DataPoint(models.Model):
properties = JSONField()
else:
properties = models.TextField(max_length=(32 * 1024 * 1024 * 1024))

def fetch_secondary_identifier(self):
if self.secondary_identifier is not None:
return self.secondary_identifier
else:
if self.generator_identifier == 'pdk-app-event':
props = self.fetch_properties()

self.secondary_identifier = props['event_name']
self.save()

return self.secondary_identifier

return None

def fetch_properties(self):
if install_supports_jsonfield():
return self.properties

return json.loads(self.properties)

class DataBundle(models.Model):
recorded = models.DateTimeField(db_index=True)
Expand Down
28 changes: 23 additions & 5 deletions views.py
Expand Up @@ -12,7 +12,7 @@

from django.contrib.admin.views.decorators import staff_member_required

from .models import DataPoint, DataBundle, DataFile, DataSourceGroup, DataSource, ReportJob, generator_label
from .models import DataPoint, DataBundle, DataFile, DataSourceGroup, DataSource, ReportJob, generator_label, install_supports_jsonfield

@csrf_exempt
def add_data_point(request):
Expand All @@ -31,7 +31,11 @@ def add_data_point(request):
point.source = point['passive-data-metadata']['source']
point.generator = point['passive-data-metadata']['generator']
point.created = datetime.datetime.fromtimestamp(point['passive-data-metadata']['source'], tz=timezone.get_default_timezone())
point.properties = point

if install_supports_jsonfield():
point.properties = point
else:
point.properties = json.dumps(point, indent=2)

point.save()

Expand All @@ -49,7 +53,11 @@ def add_data_point(request):
point.source = point['passive-data-metadata']['source']
point.generator = point['passive-data-metadata']['generator']
point.created = datetime.datetime.fromtimestamp(point['passive-data-metadata']['source'], tz=timezone.get_default_timezone())
point.properties = point

if install_supports_jsonfield():
point.properties = point
else:
point.properties = json.dumps(point, indent=2)

point.save()

Expand Down Expand Up @@ -80,7 +88,12 @@ def add_data_bundle(request):
points = json.loads(request.body)

bundle = DataBundle(recorded=timezone.now())
bundle.properties = points

if install_supports_jsonfield():
bundle.properties = points
else:
bundle.properties = json.dumps(points, indent=2)

bundle.save()

return response
Expand All @@ -95,7 +108,12 @@ def add_data_bundle(request):
points = json.loads(request.POST['payload'])

bundle = DataBundle(recorded=timezone.now())
bundle.properties = points

if install_supports_jsonfield():
bundle.properties = points
else:
bundle.properties = json.dumps(points, indent=2)

bundle.save()
except ValueError:
response = { 'message': 'Unable to parse data bundle.' }
Expand Down

0 comments on commit e0847a5

Please sign in to comment.