-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from InspectorIncognito/fix/EDD-3327-speed-dow…
…nload-endpoints-and-historic-calc Fix/edd 3327 speed download endpoints and historic calc
- Loading branch information
Showing
7 changed files
with
134 additions
and
127 deletions.
There are no files selected for viewing
8 changes: 6 additions & 2 deletions
8
backend/gtfs_rt/management/commands/get_last_month_avg_speed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
from django.core.management.base import BaseCommand | ||
from processors.speed.avg_speed import get_last_month_avg_speed | ||
from processors.speed.avg_speed import get_month_commercial_speed | ||
from django.utils import timezone | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
print("Calling get_last_month_avg_speed command...") | ||
get_last_month_avg_speed() | ||
today = timezone.localtime() | ||
this_year = today.year | ||
this_month = today.month | ||
get_month_commercial_speed(year=this_year, month=this_month) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import random | ||
|
||
from django.db.models import Sum | ||
from django.utils import timezone | ||
from processors.speed.avg_speed import get_month_commercial_speed | ||
from rest_api.factories import SpeedFactory, SegmentFactory | ||
from rest_api.models import HistoricSpeed, Speed | ||
from rest_api.tests.tests_views_base import BaseTestCase | ||
|
||
|
||
class HistoricSpeedTest(BaseTestCase): | ||
def setUp(self): | ||
day_minutes = 1440 | ||
temporal_segment_duration = 15 | ||
self.segments = [SegmentFactory(), SegmentFactory()] | ||
self.day_types = ['L', 'S', 'D'] | ||
self.temporal_segments = int(day_minutes / temporal_segment_duration) | ||
|
||
def test_calculate_monthly_speed(self): | ||
for segment in self.segments: | ||
for day_type in self.day_types: | ||
for temporal_segment in range(self.temporal_segments): | ||
for day in range(4): | ||
distance = random.randint(10, 100) | ||
time_secs = random.randint(1, 5) | ||
SpeedFactory(segment=segment, temporal_segment=temporal_segment, day_type=day_type, | ||
distance=distance, time_secs=time_secs) | ||
|
||
today = timezone.localtime() | ||
this_month = today.month | ||
this_year = today.year | ||
get_month_commercial_speed(year=this_year, month=this_month) | ||
|
||
historic_speeds = HistoricSpeed.objects.filter(timestamp__year=this_year, timestamp__month=this_month) | ||
expected_historic_speed_records = len(self.segments) * len(self.day_types) * self.temporal_segments | ||
self.assertEqual(historic_speeds.count(), expected_historic_speed_records) | ||
|
||
for avg_speed in historic_speeds: | ||
segment = avg_speed.segment | ||
temporal_segment = avg_speed.temporal_segment | ||
day_type = avg_speed.day_type | ||
expected_speed = avg_speed.speed | ||
speeds = Speed.objects.filter(timestamp__year=this_year, timestamp__month=this_month, | ||
segment=segment, temporal_segment=temporal_segment, day_type=day_type) | ||
total_distance = speeds.aggregate(Sum('distance'))['distance__sum'] | ||
total_time = speeds.aggregate(Sum('time_secs'))['time_secs__sum'] | ||
actual_speed = round(3.6 * total_distance / total_time, 2) | ||
self.assertAlmostEqual(actual_speed, expected_speed, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from rest_framework.permissions import BasePermission | ||
from .models import DownloadToken | ||
from django.utils import timezone |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters