|
@@ -1,5 +1,6 @@
|
|
|
import json
|
|
|
import gzip
|
|
|
+import pytz
|
|
|
from datetime import datetime
|
|
|
|
|
|
from django.shortcuts import render
|
|
@@ -10,6 +11,7 @@ from django.core.cache import cache
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
|
from django.contrib.gis.geos import Point
|
|
|
+from django.db.models import Q
|
|
|
|
|
|
from .models import *
|
|
|
from .workers.points2trips import TripConverter
|
|
@@ -25,14 +27,16 @@ class MarkerCreateView(PermissionRequiredMixin, View):
|
|
|
permission_required = ("gps-logger.markers.change")
|
|
|
|
|
|
def post(self, request):
|
|
|
+ utctz = pytz.timezone("UTC")
|
|
|
insert_count = censor_count = replace_count = 0
|
|
|
data = json.loads(request.body)
|
|
|
+ minDate = datetime.utcnow().replace(tzinfo=utctz)
|
|
|
for file in data:
|
|
|
if len(data[file]) == 0:
|
|
|
continue
|
|
|
objects = [Marker(
|
|
|
timestamp=datetime.strptime(x['timestamp'].replace("Z","-0000"), r"%Y-%m-%dT%H:%M:%S.%f%z"),
|
|
|
- location = Point(x['lng'], x['lat']),
|
|
|
+ location = Point(x['lng'], x['lat'], srid=4326),
|
|
|
alt = x['alt'],
|
|
|
hdop = x.get('hdop', None),
|
|
|
speed = x.get('speed', None)
|
|
@@ -40,8 +44,10 @@ class MarkerCreateView(PermissionRequiredMixin, View):
|
|
|
start_date = objects[0].timestamp
|
|
|
end_date = objects[-1].timestamp
|
|
|
oldMarkers = Marker.objects.filter(timestamp__range=(start_date, end_date)).all()
|
|
|
- identical = len(oldMarkers) == len(objects)
|
|
|
-
|
|
|
+ identical = oldMarkers.count() == len(objects)
|
|
|
+
|
|
|
+ minDate = min(minDate, start_date)
|
|
|
+
|
|
|
if identical:
|
|
|
for i, marker in enumerate(oldMarkers):
|
|
|
if marker.timestamp != objects[i].timestamp:
|
|
@@ -52,27 +58,34 @@ class MarkerCreateView(PermissionRequiredMixin, View):
|
|
|
for i, marker in enumerate(oldMarkers):
|
|
|
objects[i].id = marker.id
|
|
|
Marker.objects.bulk_update(objects, ['location', 'alt', 'hdop', 'speed'])
|
|
|
- replace_count += len(oldMarkers)
|
|
|
+ replace_count += oldMarkers.count()
|
|
|
else:
|
|
|
- insert_count += len(objects) - len(oldMarkers)
|
|
|
+ insert_count += len(objects) - oldMarkers.count()
|
|
|
oldMarkers.delete()
|
|
|
Marker.objects.bulk_create(objects)
|
|
|
|
|
|
censor_count = censor()
|
|
|
|
|
|
+ minDate = minDate.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
+ print("regenerating trips after", minDate)
|
|
|
+ tc = TripConverter(minDate)
|
|
|
+ tc.run()
|
|
|
+ cache.delete('trips')
|
|
|
+
|
|
|
return JsonResponse({
|
|
|
- 'censored': censor_count,
|
|
|
- 'replaced': replace_count,
|
|
|
- 'inserted': insert_count - censor_count
|
|
|
+ 'markers_censored': censor_count,
|
|
|
+ 'markers_replaced': replace_count,
|
|
|
+ 'markers_inserted': insert_count - censor_count,
|
|
|
+ 'trips_created': tc.created,
|
|
|
+ 'trips_updated': tc.updated,
|
|
|
+ 'trips_skipped': tc.skipped
|
|
|
}, status=201)
|
|
|
|
|
|
class CreateTripsView(PermissionRequiredMixin, View):
|
|
|
permission_required = ("gps-logger.trips.change")
|
|
|
|
|
|
def get(self, request):
|
|
|
- data = list(Marker.objects.all())
|
|
|
-
|
|
|
- tc = TripConverter(data)
|
|
|
+ tc = TripConverter()
|
|
|
tc.run()
|
|
|
cache.delete('trips')
|
|
|
return JsonResponse({
|
|
@@ -123,10 +136,14 @@ class DeleteCensoredView(PermissionRequiredMixin, View):
|
|
|
return JsonResponse({'censored': counter})
|
|
|
|
|
|
def censor() -> int:
|
|
|
- counter = 0
|
|
|
locations = CensoredLocation.objects.all()
|
|
|
+ filter = None
|
|
|
for location in locations:
|
|
|
- counter += location.delete_markers()
|
|
|
- if(cache.get('markers')):
|
|
|
- cache.delete('markers')
|
|
|
+ f = Q(location__distance_lte=(location.location, distance_to_decimal_degrees(D(m=location.radius), location.location.y)))
|
|
|
+ filter = f if filter is None else filter | f
|
|
|
+
|
|
|
+ matches = Marker.objects.filter(filter)
|
|
|
+ counter, _ = matches.delete()
|
|
|
+
|
|
|
+ cache.delete('markers')
|
|
|
return counter
|