feat: build the occurrence index
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import type { EntityObject } from '@ChronoManager/models/entity'
|
||||
import type { EventObject } from '@ChronoManager/models/event'
|
||||
import type {
|
||||
CalendarOccurrence,
|
||||
CalendarOccurrenceIndex,
|
||||
} from '../types/occurrence'
|
||||
import type { VisibleDateRange } from '../types'
|
||||
import { shiftLocalDays, startOfLocalDay } from './dateRanges'
|
||||
|
||||
const MINIMUM_DURATION_MS = 1
|
||||
|
||||
function eventTimestamp(value: string | null | undefined): number | null {
|
||||
if (!value) return null
|
||||
|
||||
const timestamp = Date.parse(value)
|
||||
return Number.isFinite(timestamp) ? timestamp : null
|
||||
}
|
||||
|
||||
function occurrenceFromEntity(entity: EntityObject): CalendarOccurrence | null {
|
||||
const event = entity.properties as EventObject
|
||||
const startMs = eventTimestamp(event.startsOn)
|
||||
if (startMs === null) return null
|
||||
|
||||
const parsedEndMs = eventTimestamp(event.endsOn)
|
||||
const endMs = parsedEndMs !== null && parsedEndMs > startMs
|
||||
? parsedEndMs
|
||||
: startMs + MINIMUM_DURATION_MS
|
||||
const dayStartMs = startOfLocalDay(new Date(startMs)).getTime()
|
||||
const lastOccupiedDayMs = startOfLocalDay(new Date(endMs - 1)).getTime()
|
||||
const entityKey = String(entity.identifier)
|
||||
|
||||
return {
|
||||
key: `${entityKey}:master`,
|
||||
entity,
|
||||
occurrenceId: null,
|
||||
startMs,
|
||||
endMs,
|
||||
dayStartMs,
|
||||
durationMs: endMs - startMs,
|
||||
timeless: event.timeless === true || lastOccupiedDayMs !== dayStartMs,
|
||||
multiDay: lastOccupiedDayMs !== dayStartMs,
|
||||
}
|
||||
}
|
||||
|
||||
function appendToDayBuckets(index: CalendarOccurrenceIndex, occurrence: CalendarOccurrence) {
|
||||
const lastOccupiedDayMs = startOfLocalDay(new Date(occurrence.endMs - 1)).getTime()
|
||||
let day = new Date(occurrence.dayStartMs)
|
||||
|
||||
while (day.getTime() <= lastOccupiedDayMs) {
|
||||
const dayKey = day.getTime()
|
||||
const bucket = index.byDay.get(dayKey)
|
||||
if (bucket) bucket.push(occurrence)
|
||||
else index.byDay.set(dayKey, [occurrence])
|
||||
day = startOfLocalDay(shiftLocalDays(day, 1))
|
||||
}
|
||||
}
|
||||
|
||||
function sortOccurrences(occurrences: CalendarOccurrence[]) {
|
||||
occurrences.sort((left, right) => (
|
||||
left.startMs - right.startMs
|
||||
|| right.durationMs - left.durationMs
|
||||
|| left.key.localeCompare(right.key)
|
||||
))
|
||||
}
|
||||
|
||||
export function buildOccurrenceIndex(entities: EntityObject[]): CalendarOccurrenceIndex {
|
||||
const index: CalendarOccurrenceIndex = {
|
||||
byDay: new Map(),
|
||||
byEntity: new Map(),
|
||||
sorted: [],
|
||||
}
|
||||
|
||||
for (const entity of entities) {
|
||||
const occurrence = occurrenceFromEntity(entity)
|
||||
if (!occurrence) continue
|
||||
|
||||
index.sorted.push(occurrence)
|
||||
index.byEntity.set(String(entity.identifier), [occurrence])
|
||||
appendToDayBuckets(index, occurrence)
|
||||
}
|
||||
|
||||
sortOccurrences(index.sorted)
|
||||
for (const bucket of index.byDay.values()) sortOccurrences(bucket)
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
export function occurrencesForDay(
|
||||
index: CalendarOccurrenceIndex,
|
||||
date: Date | number,
|
||||
): CalendarOccurrence[] {
|
||||
const dayKey = startOfLocalDay(new Date(date)).getTime()
|
||||
return index.byDay.get(dayKey) ?? []
|
||||
}
|
||||
|
||||
export function occurrencesForRange(
|
||||
index: CalendarOccurrenceIndex,
|
||||
range: VisibleDateRange,
|
||||
): CalendarOccurrence[] {
|
||||
if (range.endMs <= range.startMs) return []
|
||||
|
||||
const matches = new Map<string, CalendarOccurrence>()
|
||||
let day = startOfLocalDay(new Date(range.startMs))
|
||||
|
||||
while (day.getTime() < range.endMs) {
|
||||
for (const occurrence of index.byDay.get(day.getTime()) ?? []) {
|
||||
if (occurrence.startMs < range.endMs && occurrence.endMs > range.startMs) {
|
||||
matches.set(occurrence.key, occurrence)
|
||||
}
|
||||
}
|
||||
day = startOfLocalDay(shiftLocalDays(day, 1))
|
||||
}
|
||||
|
||||
const occurrences = Array.from(matches.values())
|
||||
sortOccurrences(occurrences)
|
||||
return occurrences
|
||||
}
|
||||
Reference in New Issue
Block a user