Skip to content

Commit

Permalink
fix(frontend): fixed calculate speed start_time and end_time logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtn-fa committed Nov 17, 2024
1 parent 167e652 commit 8cd540b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
7 changes: 2 additions & 5 deletions backend/gtfs_rt/processors/speed.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import time
import datetime
import pandas as pd
from django.utils import timezone
from gtfs_rt.utils import get_last_temporal_range
from rest_api.models import Segment, Speed
from rest_api.util.temporal_segment import get_last_temporal_segment_dates
from velocity.grid import GridManager
from velocity.vehicle import VehicleManager
from velocity.segment import FiveHundredMeterSegmentCriteria
Expand All @@ -13,9 +12,7 @@
def calculate_speed(start_date: datetime.datetime = None, end_date: datetime.datetime = None):
print("Calling calculate_speed command...")
if start_date is None or end_date is None:
start_date, end_date = get_last_temporal_segment_dates()
print("Start date:", start_date)
print("End date:", end_date)
start_date, end_date = get_last_temporal_range()
today_weekday = start_date.weekday()
today_weekday = "L" if today_weekday < 5 else "S" if today_weekday == 5 else "D"

Expand Down
13 changes: 6 additions & 7 deletions backend/gtfs_rt/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

import numpy as np
from datetime import datetime, timedelta
from django.utils import timezone
Expand Down Expand Up @@ -36,11 +34,12 @@ def get_temporal_range(temporal_segment):
return start_time, end_time


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_last_temporal_range():
delta = timedelta(minutes=15)
now = timezone.localtime()
last_15_minutes = now - delta
last_temporal_segment = get_temporal_segment(last_15_minutes)
return get_temporal_range(last_temporal_segment)


def get_day_type(dt: datetime):
Expand Down
35 changes: 16 additions & 19 deletions backend/rest_api/util/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.utils import timezone

from rest_api.models import Segment, Speed, HistoricSpeed, Alert, AlertThreshold
from gtfs_rt.utils import get_temporal_segment, get_day_type
from gtfs_rt.utils import get_temporal_segment, get_day_type, get_last_temporal_range

logger = logging.getLogger(__name__)
DAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
Expand Down Expand Up @@ -195,35 +195,31 @@ def create_alert_data(segment: Segment, speed: Speed):
return alert_data


def create_alerts():
now = timezone.localtime()
end_time = now.replace(second=0, microsecond=0)
delta = timedelta(minutes=15)
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
def create_alerts(start_time: datetime = None, end_time: datetime = None):
if start_time is None or end_time is None:
start_time, end_time = get_last_temporal_range()

start_time = end_time - delta
day_type = get_day_type(start_time)
temporal_segment = get_temporal_segment(start_time)
day_type = get_day_type(start_time)

segments = Segment.objects.all()
alert_threshold = AlertThreshold.objects.first().threshold

for segment in segments:
speed = Speed.objects.filter(segment=segment, day_type=day_type, timestamp__date=today,
temporal_segment=temporal_segment).first()
if speed is None:
continue
speed_obj = Speed.objects.filter(segment=segment, timestamp__date=start_time.date(),
temporal_segment=temporal_segment).first()
historic_speed = HistoricSpeed.objects.filter(segment=segment, day_type=day_type,
temporal_segment=temporal_segment).order_by("-timestamp").first()
if historic_speed is None:
if speed_obj is None or historic_speed is None:
continue
speed_value = speed.get_speed()
speed_value = speed_obj.get_speed()
historic_speed_value = historic_speed.speed
alert_condition = speed_value < historic_speed_value / alert_threshold

if alert_condition:
alert_obj_data = {
"segment": segment,
"detected_speed": speed,
"detected_speed": speed_obj,
"temporal_segment": temporal_segment,
}
Alert.objects.create(**alert_obj_data)
Expand All @@ -242,14 +238,15 @@ def search_alert_obj(segment_uuid):
return Alert.objects.filter(segment__segment_id=segment_uuid).first()


def update_alerts(site_manager: TranSappSiteManager):
def update_alerts(site_manager: TranSappSiteManager, start_time: datetime = None, end_time: datetime = None):
print("Calling update_alerts command...")
end_time = timezone.localtime()
start_time = end_time - timedelta(minutes=15)
temporal_segment = get_temporal_segment(start_time)
if not start_time or not end_time:
start_time, end_time = get_last_temporal_range()

temporal_segment = get_temporal_segment(start_time)
alerts = Alert.objects.filter(timestamp__date=start_time.date(), temporal_segment=temporal_segment)
alert_data = site_manager.get_all_alerts()

for alert in alerts:
segment_uuid = alert.segment.segment_id
site_alert = search_alert_by_uuid(alert_data, segment_uuid)
Expand Down
9 changes: 6 additions & 3 deletions backend/rest_api/util/process.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from gtfs_rt.processors.speed import calculate_speed
from gtfs_rt.utils import get_last_temporal_range
from rest_api.util.alert import create_alerts, update_alerts, TranSappSiteManager


def calculate_speed_and_check_alerts():
calculate_speed()
start_time, end_time = get_last_temporal_range()

calculate_speed(start_date=start_time, end_date=end_time)
site_manager = TranSappSiteManager()
create_alerts()
update_alerts(site_manager)
create_alerts(start_time=start_time, end_time=end_time)
update_alerts(site_manager, start_time=start_time, end_time=end_time)

0 comments on commit 8cd540b

Please sign in to comment.