Skip to content

Commit

Permalink
refactor: move sortByMode to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shootermv committed Oct 10, 2023
1 parent 91060ba commit 0091eec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/pages/components/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const bySeverityHandler = (a: HourlyData, b: HourlyData) => {
return b.planned_rides - a.planned_rides
}

export const sortByMode = (hourlyData: HourlyData[] = [], sortingMode: string) => {
return hourlyData.toSorted(sortingMode === 'hour' ? byHourHandler : bySeverityHandler)
}

export const mapColorByExecution = (planned: number, actual: number) => {
const misses = planned - actual
const percentageMisses = (misses / planned) * 100
Expand Down
74 changes: 33 additions & 41 deletions src/pages/useGapsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,51 @@ import { useEffect, useState } from 'react'
import { Moment } from 'moment'
import { getGapsAsync } from '../api/gapsService'

import { HourlyData, byHourHandler, bySeverityHandler } from './components/utils'
import { sortByMode, HourlyData } from './components/utils'
import { GapsList } from 'src/model/gaps'

export const processData = (gapsList: GapsList): HourlyData[] => {
// Convert gapsList data to hourly mapping structure, where hour is a key
const hourlyMapping: Record<string, { planned_rides: number; actual_rides: number }> = {}

for (const ride of gapsList) {
if (ride.gtfsTime === null) {
continue
}
const plannedHour = ride.gtfsTime.format('HH:mm')

if (!hourlyMapping[plannedHour]) {
hourlyMapping[plannedHour] = { planned_rides: 0, actual_rides: 0 }
}

hourlyMapping[plannedHour].planned_rides += 1
if (ride.siriTime) {
hourlyMapping[plannedHour].actual_rides += 1
}
}
// coverts hourlyMapping data to objects array where hour is a field
return Object.entries(hourlyMapping).map(([hour, data]) => ({
planned_hour: hour,
actual_rides: data.actual_rides,
planned_rides: data.planned_rides,
}))
}

export const useGapsList = (
fromDate: Moment,
toDate: Moment,
operatorRef: string,
lineRef: number,
sortingMode: string,
) => {
): HourlyData[] => {
const [hourlyData, setHourlyData] = useState<HourlyData[]>([])

const sortData = (hourlyData: HourlyData[] = [], sortingMode: string) => {
const orderedData = [...hourlyData]
if (sortingMode === 'hour') {
orderedData.sort(byHourHandler)
} else if (sortingMode === 'severity') {
orderedData.sort(bySeverityHandler)
}
return orderedData
}
useEffect(() => {
const fetchData = async () => {
try {
const gapsList = await getGapsAsync(fromDate, toDate, operatorRef, lineRef)

// Convert gapsList data into hourly mapping as needed
const hourlyMapping: Record<string, { planned_rides: number; actual_rides: number }> = {}

for (const ride of gapsList) {
if (ride.gtfsTime === null) {
continue
}
const plannedHour = ride.gtfsTime.format('HH:mm')

if (!hourlyMapping[plannedHour]) {
hourlyMapping[plannedHour] = { planned_rides: 0, actual_rides: 0 }
}

hourlyMapping[plannedHour].planned_rides += 1
if (ride.siriTime) {
hourlyMapping[plannedHour].actual_rides += 1
}
}

const result: HourlyData[] = Object.entries(hourlyMapping).map(([hour, data]) => ({
planned_hour: hour,
actual_rides: data.actual_rides,
planned_rides: data.planned_rides,
}))

const orderedData = sortData(result, sortingMode)

setHourlyData(orderedData)
const gapsList: GapsList = await getGapsAsync(fromDate, toDate, operatorRef, lineRef)
const result = processData(gapsList)
setHourlyData(sortByMode(result, sortingMode))
} catch (error) {
console.error('Error fetching data:', error)
}
Expand Down

0 comments on commit 0091eec

Please sign in to comment.