Skip to content
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
6 changes: 6 additions & 0 deletions src/datasources/JoreDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class JoreDataSource extends SQLDataSource {
route.date_begin,
route.date_end,
route.date_modified,
route.date_imported,
route.route_length,
trunk_route,
jore.route_mode(route) as mode
Expand All @@ -78,6 +79,7 @@ export class JoreDataSource extends SQLDataSource {
route.date_begin,
route.date_end,
route.date_modified,
route.date_imported,
route.route_length,
trunk_route,
jore.route_mode(route) as mode
Expand Down Expand Up @@ -139,6 +141,7 @@ export class JoreDataSource extends SQLDataSource {
route_segment.timing_stop_type,
route_segment.stop_index,
route_segment.date_modified,
route_segment.date_imported,
jore.route_mode(route) as mode,
stop.lat,
stop.lon,
Expand Down Expand Up @@ -189,6 +192,7 @@ export class JoreDataSource extends SQLDataSource {
stop.name_fi,
stop.stop_radius,
route_segment.date_modified,
route_segment.date_imported,
route_segment.route_id,
route_segment.direction,
route_segment.timing_stop_type,
Expand Down Expand Up @@ -327,6 +331,7 @@ export class JoreDataSource extends SQLDataSource {
route_segment.date_begin,
route_segment.date_end,
route_segment.date_modified,
route_segment.date_imported,
route_segment.duration,
route_segment.stop_index,
route_segment.distance_from_previous,
Expand Down Expand Up @@ -416,6 +421,7 @@ stop.terminal_id,
route_segment.date_begin,
route_segment.date_end,
route_segment.date_modified,
route_segment.date_imported,
route_segment.destination_fi,
route_segment.distance_from_previous,
route_segment.distance_from_start,
Expand Down
62 changes: 45 additions & 17 deletions src/utils/filterByDateGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { groupBy, orderBy } from 'lodash'
import moment from 'moment-timezone'
import { TZ } from '../constants'

export function filterByDateGroups<T extends ValidityRange>(items: T[], date: string): T[] {
let groupedItems = groupBy<T>(items, (item: T) => `${item.date_begin}_${item.date_end}`)
let hasModifiedDate = !!items[0]?.date_modified
let validOnDate = moment.tz(date, TZ)
type ValidityRangeWithImport = ValidityRange & {
date_imported?: string
}

export function filterByDateGroups<T extends ValidityRangeWithImport>(
items: T[],
date: string
): T[] {
const groupedItems = groupBy(items, (item) => `${item.date_begin}_${item.date_end}`)
const validOnDate = moment.tz(date, TZ)

let validGroupEntries = Object.entries<T[]>(groupedItems).filter(([validityRange]) => {
let range = validityRange.split('_')
let rangeStart = moment.tz(range[0], TZ)
let rangeEnd = moment.tz(range[1], TZ)
const validGroupEntries = Object.entries(groupedItems).filter(([validityRange]) => {
const [start, end] = validityRange.split('_')
const rangeStart = moment.tz(start, TZ)
const rangeEnd = moment.tz(end, TZ)

return validOnDate.isBetween(rangeStart, rangeEnd, 'day', '[]')
})
Expand All @@ -27,16 +33,38 @@ export function filterByDateGroups<T extends ValidityRange>(items: T[], date: st
return validGroupEntries[0][1]
}

let orderedGroups = orderBy(
const getLatestAllowedImportUnix = (groupItems: T[]) => {
const allowedImports = groupItems
.map((item) => item.date_imported)
.filter((d): d is string => !!d)
.map((d) => moment.tz(d, TZ))
.filter((importDate) => importDate.isSameOrBefore(validOnDate, 'day'))

if (allowedImports.length === 0) {
return null
}

return Math.max(...allowedImports.map((d) => d.unix()))
}

const hasAnyAllowedImports = validGroupEntries.some(([, groupItems]) => {
return getLatestAllowedImportUnix(groupItems) !== null
})

const orderedGroups = orderBy(
validGroupEntries,
([, items]) => {
// if (hasModifiedDate) {
// return moment(items[0].date_modified).unix()
// }

return moment(items[0].date_begin).unix()
},
'desc'
hasAnyAllowedImports
? [
// First order by the latest allowed date_imported, so that groups with more recent imports that are still valid on the UI date come first.
([, groupItems]) => getLatestAllowedImportUnix(groupItems) ?? 0,
// If there are multiple groups with the same latest allowed import date order those groups by date_begin.
([, groupItems]) => moment.tz(groupItems[0].date_begin, TZ).unix(),
]
: [
// If there are no allowed date_importeds just order by date_begin.
([, groupItems]) => moment.tz(groupItems[0].date_begin, TZ).unix(),
],
hasAnyAllowedImports ? ['desc', 'desc'] : ['desc']
)

return orderedGroups[0][1] // Select the items from the first, ie most valid, group.
Expand Down
Loading