Skip to content

Commit

Permalink
DBC22-1727: replaced priority with highway ref for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd committed Mar 4, 2024
1 parent 0238411 commit 858513c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/backend/apps/event/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ class EVENT_DIRECTION:

EVENT_DIFF_FIELDS = [
'next_update',
'last_updated',
'priority'
'last_updated'
]

EVENT_UPDATE_FIELDS = [
'highway_segment_names',
'location_description',
'closest_landmark',
'next_update',
'start_point_linear_reference',
'description',
'event_type',
'event_sub_type',
Expand All @@ -115,7 +115,6 @@ class EVENT_DIRECTION:
'closed',
'direction',
'last_updated',
'priority',
'location',
'route_at',
'route_from',
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
17 changes: 17 additions & 0 deletions src/backend/apps/event/migrations/0017_remove_event_priority.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
2 changes: 1 addition & 1 deletion src/backend/apps/event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions src/backend/apps/event/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions src/backend/apps/feed/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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", []):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()
Expand Down
37 changes: 22 additions & 15 deletions src/frontend/src/Components/events/EventsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/pages/EventsListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ export default function EventsListPage() {
{
header: 'Type',
accessorKey: 'display_category',
sortingFn: 'defaultSort',
sortingFn: 'typeSort',
cell: (props) => <EventTypeIcon event={props.row.original} />,
},
{
header: 'Severity',
accessorKey: 'severity',
sortingFn: 'reverseSort',
sortingFn: 'severitySort',
sortDescFirst: true,
cell: (props) => <span>{props.getValue().toLowerCase()}</span>,
},
{
header: 'Road',
accessorKey: 'route_at',
sortingFn: 'defaultSort',
sortingFn: 'routeSort',
},
{
header: 'Direction',
Expand Down

0 comments on commit 858513c

Please sign in to comment.