Skip to content

Commit

Permalink
Merge pull request #198 from ropable/master
Browse files Browse the repository at this point in the history
Reorder device save, less-noisy logging
  • Loading branch information
ropable authored Nov 21, 2023
2 parents 69ce445 + 75bbd71 commit 6d6c3cc
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 304 deletions.
4 changes: 2 additions & 2 deletions kustomize/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
resources:
- deployment.yaml
- service.yaml
- deployment.yaml
- service.yaml
542 changes: 259 additions & 283 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tool.poetry]
name = "resource_tracking"
version = "1.4.1"
version = "1.4.2"
description = "DBCA internal corporate application to download and serve data from remote tracking devices."
authors = ["DBCA OIM <[email protected]>"]
license = "Apache-2.0"

[tool.poetry.dependencies]
python = "^3.10"
django = "3.2.21"
django = "3.2.23"
psycopg2 = "2.9.6"
dbca-utils = "1.1.6"
python-dotenv = "1.0.0"
Expand Down
3 changes: 2 additions & 1 deletion tracking/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DeviceAdmin(ModelAdmin):
)
list_filter = ("symbol", "district", "source_device_type", "hidden", "internal_only")
search_fields = ("deviceid", "registration", "callsign_display", "rin_display", "symbol", "district_display")
readonly_fields = ("deviceid", "source_device_type", "seen")
readonly_fields = ("deviceid", "source_device_type", "seen", "point")
fieldsets = (
("Vehicle/Device details", {
"description": """<p class="errornote">This is the live tracking database;
Expand All @@ -26,6 +26,7 @@ class DeviceAdmin(ModelAdmin):
"deviceid",
"source_device_type",
"seen",
"point",
"district",
"symbol",
"callsign",
Expand Down
41 changes: 27 additions & 14 deletions tracking/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def harvest_tracking_email(device_type, purge_email=False):
if purge_email:
status, response = email_utils.email_mark_read(imap, uid)
status, response = email_utils.email_delete(imap, uid)
LOGGER.info(f"Flagged {flagged} emails")

LOGGER.info(f"Created {created} tracking points")
LOGGER.info(f"Created {created} tracking points, flagged {flagged} emails")
imap.close()
imap.logout()

Expand All @@ -106,19 +105,21 @@ def save_mp70(message):

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing MP70 message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing MP70 message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
return False

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "mp70"
Expand All @@ -144,19 +145,21 @@ def save_spot(message):

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing Spot message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing Spot message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
return False

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "spot"
Expand All @@ -182,19 +185,21 @@ def save_iriditrak(message):

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing Iriditrak message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing Iriditrak message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
return False

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "iriditrak"
Expand Down Expand Up @@ -222,19 +227,21 @@ def save_dplus(message):

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing DPlus message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing DPlus message from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
return False

device, created = Device.objects.get_or_create(deviceid=data["device_id"])
seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "dplus"
Expand Down Expand Up @@ -308,7 +315,7 @@ def save_dfes_feed():

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing data for DFES device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing data for DFES device {data['device_id']}: {data['latitude']}, {data['longitude']}")
skipped_device += 1
continue

Expand All @@ -334,15 +341,17 @@ def save_dfes_feed():
device.save()

seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
device.save()
updated_device += 1

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "dfes"
Expand All @@ -351,6 +360,8 @@ def save_dfes_feed():
loggedpoint.altitude = data["altitude"]
loggedpoint.save()
logged_points += 1
else:
skipped_device += 1

LOGGER.info(f"Created {created_device}, updated {updated_device}, skipped {skipped_device}, {logged_points} new logged points")

Expand Down Expand Up @@ -382,7 +393,7 @@ def save_tracplus_feed():

# Validate lat/lon values.
if not validate_latitude_longitude(data["latitude"], data["longitude"]):
LOGGER.warning(f"Bad geometry while parsing TracPlus data from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
LOGGER.info(f"Bad geometry while parsing TracPlus data from device {data['device_id']}: {data['latitude']}, {data['longitude']}")
skipped_device += 1
continue

Expand All @@ -400,15 +411,17 @@ def save_tracplus_feed():
device.save()

seen = data["timestamp"]
point = f"POINT({data['longitude']} {data['latitude']})"

if not device.seen or device.seen < seen:
device.seen = seen
device.point = point
device.heading = data["heading"]
device.velocity = data["velocity"]
device.altitude = data["altitude"]
updated_device += 1
device.save()

point = f"POINT({data['longitude']} {data['latitude']})"
loggedpoint, created = LoggedPoint.objects.get_or_create(device=device, seen=seen, point=point)
if created:
loggedpoint.source_device_type = "tracplus"
Expand Down
6 changes: 4 additions & 2 deletions tracking/test_harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@

class HarvestTestCase(TestCase):
"""Unit tests to cover the following harvest formats:
- Email payloads: Iriditrak, MP70, Spot (TODO)
- Email payloads: Iriditrak, MP70
- TracPlus API
- DFES API (TODO)
- DFES API
TODO: Email from Spot, DPlus
"""

def test_validate_latitude_longitude(self):
Expand Down

0 comments on commit 6d6c3cc

Please sign in to comment.