Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move data process logic to utils #111

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/gapsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type RawGapsList = {
gtfs_ride_id?: number
}[]

const parseTime = (time: string): Moment | null => {
export const parseTime = (time: string): Moment | null => {
const utcMoment: Moment = moment.utc(time).tz('Asia/Jerusalem')
if (!utcMoment.isValid()) {
return null
Expand Down
53 changes: 53 additions & 0 deletions src/pages/components/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { parseTime } from 'src/api/gapsService'
import { HourlyData, sortByMode } from '.'
import { GapsList } from 'src/model/gaps'
import { convertGapsToHourlyStruct as processData } from 'src/pages/useGapsList'

describe('sortByMode', () => {
it('when mode param is "hour" - should be sorted properly', () => {
Expand Down Expand Up @@ -36,4 +39,54 @@ describe('sortByMode', () => {
/* record with less actual rides should come first */
expect(res[0].actual_rides).toBe(0)
})

it('should convert gapList to HourlyData structure', () => {
const list: GapsList = [
{
gtfsTime: parseTime('2023-10-04T02:00:00'),
siriTime: parseTime('2023-10-04T02:00:00'),
},
]
const [results] = processData(list)
expect(results).toEqual({
actual_rides: 1,
planned_hour: '05:00',
planned_rides: 1,
})
})

it('should convert gapList time entry with null value to - 0', () => {
const list: GapsList = [
{
gtfsTime: parseTime('2023-10-04T02:20:00'),
siriTime: parseTime('null'),
},
]
const [results] = processData(list)

expect(results).toEqual({
planned_hour: '05:20',
planned_rides: 1,
actual_rides: 0,
})
})

it('should convert entries at same time to single entry with sum of actual and planned rides', () => {
const list: GapsList = [
{
gtfsTime: parseTime('2023-10-04T02:00:00'),
siriTime: parseTime('2023-10-04T02:00:00'),
},
{
gtfsTime: parseTime('2023-10-04T02:00:00'),
siriTime: parseTime('2023-10-04T02:00:00'),
},
]
const [results] = processData(list)
expect(results).toEqual({
actual_rides: 2,
planned_hour: '05:00',
planned_rides: 2,
})
})
})
6 changes: 4 additions & 2 deletions src/pages/useGapsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { getGapsAsync } from '../api/gapsService'
import { sortByMode, HourlyData } from './components/utils'
import { GapsList } from 'src/model/gaps'

export const processData = (gapsList: GapsList): HourlyData[] => {
shootermv marked this conversation as resolved.
Show resolved Hide resolved
export type HourlyDataList = HourlyData[]
// Convert gapsList into HourlyDataList structure
export const convertGapsToHourlyStruct = (gapsList: GapsList): HourlyDataList => {
// Convert gapsList data to hourly mapping structure, where hour is a key
const hourlyMapping: Record<string, { planned_rides: number; actual_rides: number }> = {}

Expand Down Expand Up @@ -45,7 +47,7 @@ export const useGapsList = (
const fetchData = async () => {
try {
const gapsList: GapsList = await getGapsAsync(fromDate, toDate, operatorRef, lineRef)
const result = processData(gapsList)
const result = convertGapsToHourlyStruct(gapsList)
setHourlyData(sortByMode(result, sortingMode))
} catch (error) {
console.error('Error fetching data:', error)
Expand Down
Loading