Skip to content

Commit

Permalink
Merge pull request #21 from InspectorIncognito/fix/EDD-3327-speed-dow…
Browse files Browse the repository at this point in the history
…nload-endpoints-and-historic-calc

Fix/edd 3327 speed download endpoints and historic calc
  • Loading branch information
Mrtn-fa authored Oct 3, 2024
2 parents 4b68fcb + cec4cef commit 5843545
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
16 changes: 15 additions & 1 deletion backend/gtfs_rt/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from datetime import datetime
from datetime import datetime, timedelta
from django.utils import timezone
from gtfs_rt.models import GPSPulse

Expand All @@ -24,6 +24,13 @@ def get_temporal_segment(date: datetime, interval: int = 15):
return int(day_minutes / interval)


def get_previous_temporal_segment(date: datetime = timezone.localtime(), interval: int = 15):
previous_datetime = date - timedelta(minutes=interval)
previous_date = previous_datetime.date()
previous_temporal_segment = get_temporal_segment(previous_datetime)
return previous_date, previous_temporal_segment


def get_day_type(dt: datetime):
if dt.tzinfo is None:
raise ValueError("datetime instance must have a tzinfo")
Expand All @@ -34,6 +41,13 @@ def get_day_type(dt: datetime):
return day_type


def get_previous_month():
current_datetime = timezone.localtime()
current_date = current_datetime.replace(day=1)
current_date = current_date - timedelta(days=1)
return current_date.month


def flush_gps_pulses():
print("Calling flush_gps_pulses command...")
GPSPulse.objects.all().delete()
1 change: 1 addition & 0 deletions backend/rest_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
path('speeds/', SpeedViewSet.as_view({"get": 'list'}), name="speeds"),
path('speeds/to_csv/', SpeedViewSet.as_view({"get": "to_csv"}), name="shape-to_csv"),
path('alerts/', AlertViewSet.as_view({"get": 'list'}), name="alerts"),
path('alerts/active/', AlertViewSet.as_view({"get": 'active'}), name="active-alerts"),
path('historicSpeeds/', HistoricSpeedViewSet.as_view({"get": "list"}), name="historicSpeeds"),
path('historicSpeeds/to_csv/', HistoricSpeedViewSet.as_view({"get": "to_csv"}), name="historicSpeeds-to_csv"),
path('stops/', StopViewSet.as_view({"get": "list"}), name="stops"),
Expand Down
57 changes: 45 additions & 12 deletions backend/rest_api/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from datetime import datetime

from django.db.models import F
from django.utils import timezone
from rest_framework import viewsets, mixins
from django.http import JsonResponse, HttpResponse, StreamingHttpResponse
from rest_framework import generics
from rest_framework.permissions import AllowAny

from gtfs_rt.utils import get_previous_temporal_segment, get_previous_month
from processors.models.shapes import shapes_to_geojson
from rest_api.models import Shape, Segment, GTFSShape, Services, Speed, HistoricSpeed, Stop, AlertThreshold, Alert
from rest_api.serializers import ShapeSerializer, SegmentSerializer, GTFSShapeSerializer, ServicesSerializer, \
Expand Down Expand Up @@ -78,9 +81,9 @@ def get_queryset(self):
queryset = self.queryset
start_time = self.request.query_params.get('startTime')
end_time = self.request.query_params.get('endTime')
month = self.request.query_params.get("month")
day_type = self.request.query_params.get("dayType")
temporal_segment = self.request.query_params.get("temporalSegment")
month = self.request.query_params.get('month')
day_type = self.request.query_params.get('dayType')
temporal_segment = self.request.query_params.get('temporalSegment')

if month is not None:
month = int(month)
Expand All @@ -105,7 +108,11 @@ def csv_generator(queryset, fieldnames_dict):
yield ','.join(list(fieldnames_dict.values())) + '\n'
for obj in queryset:
fieldnames = list(fieldnames_dict.keys())
row = [str(obj[field]) for field in fieldnames]
row = []
for field in fieldnames:
if field == 'timestamp':
obj[field] = timezone.localtime(obj[field])
row.append(str(obj[field]))
yield ','.join(row) + '\n'


Expand All @@ -114,27 +121,27 @@ class SpeedViewSet(GenericSpeedViewSet):
queryset = Speed.objects.all().order_by('-temporal_segment')

def to_csv(self, request, *args, **kwargs):
query_params = request.query_params
queryset = self.get_queryset().values(
'segment__shape',
'segment__sequence',
'temporal_segment',
'day_type',
'distance',
'time_secs'
'time_secs',
'timestamp'
)
start_date = request.query_params.get('start_date', None)
if start_date is not None:
start_date = datetime.strptime(start_date, '%Y-%d-%mT%H:%M:%S')
start_date = timezone.make_aware(start_date, timezone.get_current_timezone())
queryset = queryset.filter(timestamp__gte=start_date)

if len(query_params) == 0:
previous_date, previous_temporal_segment = get_previous_temporal_segment()
queryset = queryset.filter(timestamp__date=previous_date, temporal_segment=previous_temporal_segment)
fieldnames_dict = dict(
segment__shape='shape',
segment__sequence='sequence',
temporal_segment='temporal_segment',
day_type='day_type',
distance='distance',
time_secs='time_secs'
time_secs='time_secs',
timestamp='timestamp'
)
response = StreamingHttpResponse(self.csv_generator(queryset, fieldnames_dict), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="segment_speeds.csv"'
Expand All @@ -154,6 +161,9 @@ def to_csv(self, request, *args, **kwargs):
'day_type',
'speed'
)
if len(request.query_params) == 0:
previous_month = get_previous_month()
queryset = queryset.filter(timestamp__month=previous_month)
fieldnames_dict = dict(
segment__shape='shape',
segment__sequence='sequence',
Expand All @@ -171,6 +181,29 @@ class AlertViewSet(viewsets.ModelViewSet):
serializer_class = AlertSerializer
queryset = Alert.objects.all()

def active(self, request, *args, **kwargs):
queryset = self.get_queryset().annotate(
shape=F('segment__shape'),
sequence=F('segment__sequence'),
date=F('timestamp__date')
).values(
'shape',
'sequence',
'detected_speed',
'temporal_segment',
'useful',
'useless',
'date'
)
if len(self.request.query_params) == 0:
previous_date, previous_temporal_segment = get_previous_temporal_segment()
queryset = queryset.filter(timestamp__date=previous_date, temporal_segment=previous_temporal_segment)
response = dict(
count=queryset.count(),
results=queryset
)
return Response(response)


class StopViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/NginxDockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN apk update && \
apk add --no-cache git && \
git config --global url."https://${GIT_PERSONAL_TOKEN}:@github.com/".insteadOf "https://github.com/"
# TODO: replace repository url of ui project
RUN git clone --branch v1.0.2 https://github.com/InspectorIncognito/uoct-frontend.git && \
RUN git clone --branch v1.0.3 https://github.com/InspectorIncognito/uoct-frontend.git && \
cp -r uoct-frontend/. . && \
rm -r uoct-frontend
RUN npm install
Expand Down

0 comments on commit 5843545

Please sign in to comment.