From 858513c35c5387f7d623e71adb5a12f9d8a5e4ac Mon Sep 17 00:00:00 2001 From: ray Date: Mon, 4 Mar 2024 11:00:34 -0800 Subject: [PATCH] DBC22-1727: replaced priority with highway ref for sorting --- src/backend/apps/event/enums.py | 5 +-- ...0016_event_start_point_linear_reference.py | 18 +++++++++ .../migrations/0017_remove_event_priority.py | 17 +++++++++ src/backend/apps/event/models.py | 2 +- src/backend/apps/event/tasks.py | 6 +-- src/backend/apps/feed/serializers.py | 10 ++++- .../src/Components/events/EventsTable.js | 37 +++++++++++-------- src/frontend/src/pages/EventsListPage.js | 6 +-- 8 files changed, 73 insertions(+), 28 deletions(-) create mode 100644 src/backend/apps/event/migrations/0016_event_start_point_linear_reference.py create mode 100644 src/backend/apps/event/migrations/0017_remove_event_priority.py diff --git a/src/backend/apps/event/enums.py b/src/backend/apps/event/enums.py index a94e8fe5f..419aef8d1 100644 --- a/src/backend/apps/event/enums.py +++ b/src/backend/apps/event/enums.py @@ -98,8 +98,7 @@ class EVENT_DIRECTION: EVENT_DIFF_FIELDS = [ 'next_update', - 'last_updated', - 'priority' + 'last_updated' ] EVENT_UPDATE_FIELDS = [ @@ -107,6 +106,7 @@ class EVENT_DIRECTION: 'location_description', 'closest_landmark', 'next_update', + 'start_point_linear_reference', 'description', 'event_type', 'event_sub_type', @@ -115,7 +115,6 @@ class EVENT_DIRECTION: 'closed', 'direction', 'last_updated', - 'priority', 'location', 'route_at', 'route_from', diff --git a/src/backend/apps/event/migrations/0016_event_start_point_linear_reference.py b/src/backend/apps/event/migrations/0016_event_start_point_linear_reference.py new file mode 100644 index 000000000..5dbf69e5e --- /dev/null +++ b/src/backend/apps/event/migrations/0016_event_start_point_linear_reference.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.3 on 2024-03-04 17:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('event', '0015_alter_event_closest_landmark_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='event', + name='start_point_linear_reference', + field=models.FloatField(null=True), + ), + ] diff --git a/src/backend/apps/event/migrations/0017_remove_event_priority.py b/src/backend/apps/event/migrations/0017_remove_event_priority.py new file mode 100644 index 000000000..aaa3edea0 --- /dev/null +++ b/src/backend/apps/event/migrations/0017_remove_event_priority.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.3 on 2024-03-04 18:07 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('event', '0016_event_start_point_linear_reference'), + ] + + operations = [ + migrations.RemoveField( + model_name='event', + name='priority', + ), + ] diff --git a/src/backend/apps/event/models.py b/src/backend/apps/event/models.py index 62de2c3e8..523a74e09 100644 --- a/src/backend/apps/event/models.py +++ b/src/backend/apps/event/models.py @@ -14,7 +14,6 @@ class Event(BaseModel): status = models.CharField(max_length=32) severity = models.CharField(max_length=32) closed = models.BooleanField(default=False) - priority = models.PositiveIntegerField(default=0) # Location direction = models.CharField(max_length=32) @@ -28,6 +27,7 @@ class Event(BaseModel): location_description = models.CharField(max_length=256, blank=True, default='') closest_landmark = models.CharField(max_length=256, blank=True, default='') next_update = models.DateTimeField(null=True, blank=True) + start_point_linear_reference = models.FloatField(null=True) # Update status first_created = models.DateTimeField() diff --git a/src/backend/apps/event/tasks.py b/src/backend/apps/event/tasks.py index 63345461f..130d2c446 100644 --- a/src/backend/apps/event/tasks.py +++ b/src/backend/apps/event/tasks.py @@ -68,7 +68,6 @@ def populate_all_event_data(include_closures=True): dit_data_dict = client.get_dit_event_dict() if include_closures else {} open511_data = client.get_event_list()['events'] - priority = 0 active_event_ids = [] for event_data in open511_data: try: @@ -88,11 +87,10 @@ def populate_all_event_data(include_closures=True): event_data["location_description"] = location_description event_data["closest_landmark"] = cars_data.get('closest_landmark', '') event_data["next_update"] = cars_data.get('next_update', None) + event_data["start_point_linear_reference"] = cars_data.get('start_point_linear_reference', None) - # Priority - event_data['priority'] = priority + # Populate db obj populate_event_from_data(event_data) - priority += 1 if id: # Mark event as active active_event_ids.append(id) diff --git a/src/backend/apps/feed/serializers.py b/src/backend/apps/feed/serializers.py index 70dfbf268..87d41aa81 100644 --- a/src/backend/apps/feed/serializers.py +++ b/src/backend/apps/feed/serializers.py @@ -88,6 +88,7 @@ class CarsEventSerializer(serializers.Serializer): location_extent = serializers.CharField(allow_blank=True) closest_landmark = serializers.CharField(allow_blank=True) next_update = serializers.DateTimeField(allow_null=True) + start_point_linear_reference = serializers.FloatField(allow_null=True) def to_internal_value(self, data): data["id"] = data["event-id"] @@ -99,6 +100,7 @@ def to_internal_value(self, data): data["location_extent"] = '' data["closest_landmark"] = '' data["next_update"] = None + data["start_point_linear_reference"] = None # Data under "details" for detail in data.get("details", []): @@ -134,6 +136,12 @@ def to_internal_value(self, data): # Get closest landmark data["closest_landmark"] = template.get('nearby-city-reference', '') + # Data under "open511-event-details" + open511_details = data.get("open511-event-details", {}) + # Get linear ref for ordering + data["start_point_linear_reference"] = float(open511_details.get('start_point_linear_reference')) \ + if 'start_point_linear_reference' in open511_details else None + # Data under "next-update-time" # Get next update time if "next-update-time" in data: @@ -157,7 +165,6 @@ class EventFeedSerializer(serializers.Serializer): source="*", required=False ) - # event_sub_type = serializers.CharField(max_length=32, required=False) # General status status = serializers.CharField(max_length=32) @@ -171,7 +178,6 @@ class EventFeedSerializer(serializers.Serializer): # Update status created = DriveBCDateField('first_created', source="*") updated = DriveBCDateField('last_updated', source="*") - # closed = serializers.SerializerMethodField() # Schedule schedule = serializers.JSONField() diff --git a/src/frontend/src/Components/events/EventsTable.js b/src/frontend/src/Components/events/EventsTable.js index 0a3e757c6..1200e8760 100644 --- a/src/frontend/src/Components/events/EventsTable.js +++ b/src/frontend/src/Components/events/EventsTable.js @@ -30,28 +30,34 @@ export default function EventsTable(props) { const [sorting, setSorting] = useState([{ desc: true, id: 'severity' }]); // Sort functions for react-table - const prioritySortFn = (rowA, rowB) => { - const aPriority = rowA.original.priority; - const bPriority = rowB.original.priority; + const defaultSortFn = (rowA, rowB, columnId) => { + const aValue = rowA.original[columnId]; + const bValue = rowB.original[columnId]; - return aPriority < bPriority ? 1 : -1; + return aValue > bValue ? 1 : -1; } - const defaultSortFn = (rowA, rowB, columnId) => { - const aValue = rowA.getValue(columnId); - const bValue = rowB.getValue(columnId); + // Use highway ref as secondary sort + const routeSortFn = (rowA, rowB, columnId) => { + return defaultSortFn(rowA, rowB, + rowA.getValue(columnId) != rowB.getValue(columnId) ? columnId : 'start_point_linear_reference' + ); + } - if (aValue != bValue) { - return aValue > bValue ? 1 : -1; + const typeSortFn = (rowA, rowB, columnId) => { + // Alphabetical primary sort + if (rowA.getValue(columnId) != rowB.getValue(columnId)) { + return defaultSortFn(rowA, rowB, columnId); - // Equal value, order by reverse priority + // Route secondary sort } else { - return prioritySortFn(rowA, rowB); + return routeSortFn(rowA, rowB, 'route_at'); } } - const reverseSortFn = (rowA, rowB, columnId) => { - return defaultSortFn(rowA, rowB, columnId) * -1; + const severitySortFn = (rowA, rowB, columnId) => { + // Reversed due to desc priority logic + return typeSortFn(rowA, rowB, columnId) * -1; } // react-table @@ -70,8 +76,9 @@ export default function EventsTable(props) { }, onSortingChange: setSorting, sortingFns: { - defaultSort: defaultSortFn, - reverseSort: reverseSortFn, + routeSort: routeSortFn, + severitySort: severitySortFn, + typeSort: typeSortFn, }, getPaginationRowModel: getPaginationRowModel(), getCoreRowModel: getCoreRowModel(), diff --git a/src/frontend/src/pages/EventsListPage.js b/src/frontend/src/pages/EventsListPage.js index 26e4e9f03..59016ceee 100644 --- a/src/frontend/src/pages/EventsListPage.js +++ b/src/frontend/src/pages/EventsListPage.js @@ -65,20 +65,20 @@ export default function EventsListPage() { { header: 'Type', accessorKey: 'display_category', - sortingFn: 'defaultSort', + sortingFn: 'typeSort', cell: (props) => , }, { header: 'Severity', accessorKey: 'severity', - sortingFn: 'reverseSort', + sortingFn: 'severitySort', sortDescFirst: true, cell: (props) => {props.getValue().toLowerCase()}, }, { header: 'Road', accessorKey: 'route_at', - sortingFn: 'defaultSort', + sortingFn: 'routeSort', }, { header: 'Direction',