Files
web-nuxt/shared/insights.ts

100 lines
3.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { berlinParts, isInWeek } from './staffing'
import type { ContextResponse, Scenario, StaffingResponse, WeatherPoint } from './types'
export type WeatherKind = 'sunny' | 'partly-cloudy' | 'cloudy' | 'rain'
export interface DailyWeather {
date: string
tempMin: number
tempMax: number
precipMm: number
cloudPct: number
kind: WeatherKind
}
export function weatherKind(point: Pick<WeatherPoint, 'precip_mm' | 'cloud_pct'>): WeatherKind {
if (point.precip_mm >= 0.5) return 'rain'
if (point.cloud_pct <= 35) return 'sunny'
if (point.cloud_pct <= 70) return 'partly-cloudy'
return 'cloudy'
}
/** Group hourly weather into local Heilbronn days. */
export function dailyWeather(forecast: WeatherPoint[]): Record<string, DailyWeather> {
const days: Record<string, DailyWeather> = {}
for (const point of forecast) {
const { date, hour } = berlinParts(point.t)
const day = (days[date] ??= {
date,
tempMin: point.temp_c,
tempMax: point.temp_c,
precipMm: 0,
cloudPct: 0,
kind: 'cloudy',
})
day.tempMin = Math.min(day.tempMin, point.temp_c)
day.tempMax = Math.max(day.tempMax, point.temp_c)
day.precipMm = Math.max(day.precipMm, point.precip_mm)
// Cloud cover at midday is a better summary than the daily maximum.
if (hour >= 11 && hour <= 14) day.cloudPct = Math.max(day.cloudPct, point.cloud_pct)
}
for (const day of Object.values(days)) {
day.kind = weatherKind({ precip_mm: day.precipMm, cloud_pct: day.cloudPct })
}
return days
}
/** Plain-language drivers for the "why this plan" card. */
export function planDrivers(
plan: StaffingResponse,
context: ContextResponse,
weekStart: string,
scenario: Scenario = 'base',
): string[] {
const reasons: string[] = []
const weather = dailyWeather(context.weather.forecast)
const weekWeather = Object.values(weather).filter((day) => isInWeek(day.date, weekStart))
if (plan.hours.length) {
const peak = plan.hours.reduce((best, hour) => (hour.footfall > best.footfall ? hour : best))
const parts = berlinParts(peak.t)
const weekday = new Intl.DateTimeFormat('en-GB', { weekday: 'long', timeZone: 'Europe/Berlin' }).format(
new Date(peak.t),
)
reasons.push(
`${weekday} ${String(parts.hour).padStart(2, '0')}:00 is the forecast peak: ${peak.footfall} estimated visitors/hour needs ${peak.required_staff} staff.`,
)
}
if (scenario === 'base') {
const bestWeather = weekWeather
.filter((day) => day.precipMm < 0.5)
.sort((a, b) => b.tempMax - a.tempMax)[0]
if (bestWeather) {
const weekday = new Intl.DateTimeFormat('en-GB', { weekday: 'long', timeZone: 'Europe/Berlin' }).format(
new Date(`${bestWeather.date}T12:00:00Z`),
)
reasons.push(
`Weather driver: ${weekday} is the mildest dry day (${Math.round(bestWeather.tempMax)} °C, ${bestWeather.precipMm.toFixed(1)} mm rain) in the current Open-Meteo mock.`,
)
}
} else {
reasons.push(
`Scenario “${scenario}” applies a mock footfall perturbation. The weather strip stays the base context; it is not recomputed per scenario.`,
)
}
const holiday = context.holidays.find((entry) => isInWeek(entry.date, weekStart))
reasons.push(
holiday
? `Public holiday in this week: ${holiday.name} (${holiday.date}). Expect a different footfall shape.`
: 'No Baden-Württemberg public holiday falls inside this plan week.',
)
reasons.push(
`Rule: ${plan.assumptions.visitors_per_staff_hour} visitors per staff-hour × ${plan.assumptions.safety_factor} safety factor, minimum ${plan.assumptions.min_staff} staff, ${plan.assumptions.shift_length_h} h shift blocks.`,
)
return reasons
}