Files
web-nuxt/app/pages/forecast.vue
T

275 lines
9.8 KiB
Vue
Raw 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.
<script setup lang="ts">
import { iconCalendar, iconChart } from '@sit-onyx/icons'
import { dailyWeather } from '#shared/insights'
import { berlinParts, planWeekStart, STANDARD_VISITORS_PER_STAFF_HOUR } from '#shared/staffing'
import type { ContextResponse, ForecastResponse, MetaResponse, Point, Scenario, ScenarioPreset, Sensor } from '#shared/types'
import { formatDate, formatNumber, formatPercent, formatShortDate, formatWeekday, formatWeekdayShort, weekLabel } from '~/utils/format'
useHead({ title: 'Forecast — PulseCast' })
const config = useRuntimeConfig()
const demoNow = String(config.public.demoNow)
const weekStart = planWeekStart(demoNow)
const weekEnd = new Date(new Date(`${weekStart}T12:00:00Z`).getTime() + 6 * 86400000)
.toISOString()
.slice(0, 10)
const sensorId = ref('hn-stadtgalerie-01')
const scenario = ref<Scenario>('base')
const { data: sensors } = await useFetch<Sensor[]>('/api/sensors')
const { data: context } = await useFetch<ContextResponse>('/api/context')
const { data: meta } = await useFetch<MetaResponse>('/api/meta')
const { data: presets } = await useFetch<ScenarioPreset[]>('/api/scenarios')
const { data: forecast } = await useFetch<ForecastResponse>('/api/forecast', {
query: computed(() => ({ sensor: sensorId.value, horizon: '7d', scenario: scenario.value })),
})
const activeSensor = computed(() => sensors.value?.find((sensor) => sensor.id === sensorId.value))
const sensorOptions = computed(() =>
(sensors.value ?? []).map((sensor) => ({ value: sensor.id, label: sensor.name })),
)
const scenarioOptions = computed(() =>
(presets.value ?? []).map((preset) => ({ value: preset.id, label: preset.label })),
)
interface DayForecast {
date: string
points: Point[]
mean: number
peak: number
peakHour: number
weather?: ReturnType<typeof dailyWeather>[string]
requiredAtPeak: number
}
const days = computed<DayForecast[]>(() => {
const grouped: Record<string, Point[]> = {}
for (const point of forecast.value?.points ?? []) {
const { date } = berlinParts(point.t)
;(grouped[date] ??= []).push(point)
}
const weather = dailyWeather(context.value?.weather.forecast ?? [])
return Object.keys(grouped)
.sort()
.map((date) => {
const points = grouped[date]!
const peakPoint = points.reduce((best, point) => (point.v > best.v ? point : best))
const mean = points.reduce((sum, point) => sum + point.v, 0) / points.length
return {
date,
points,
mean: Math.round(mean),
peak: peakPoint.v,
peakHour: berlinParts(peakPoint.t).hour,
weather: weather[date],
requiredAtPeak: Math.max(
1,
Math.ceil((peakPoint.v / STANDARD_VISITORS_PER_STAFF_HOUR.standard) * 1.15),
),
}
})
})
const weekPeak = computed(() =>
days.value.length ? days.value.reduce((best, day) => (day.peak > best.peak ? day : best)) : undefined,
)
const dayMax = computed(() => Math.max(...days.value.map((day) => day.peak), 1))
const weekWeather = computed(() =>
Object.values(dailyWeather(context.value?.weather.forecast ?? []))
.filter((day) => day.date >= weekStart && day.date <= weekEnd)
.sort((a, b) => a.date.localeCompare(b.date)),
)
const total = computed(() => days.value.reduce((sum, day) => sum + day.mean, 0))
</script>
<template>
<div class="page page-stack">
<header class="page-header">
<div>
<p class="eyebrow">Forecast · {{ weekLabel(weekStart, weekEnd) }}</p>
<OnyxHeadline is="h1">Next week peaks on {{ weekPeak ? formatWeekday(weekPeak.date) : '—' }}.</OnyxHeadline>
<p class="lede">
A 168-hour mock forecast for <strong>{{ activeSensor?.name ?? 'Stadtgalerie' }}</strong>, aggregated by
day. The decision, shift blocks and hourly coverage are on <NuxtLink to="/plan">Plan</NuxtLink>.
</p>
</div>
<div style="min-width: 14rem">
<OnyxSelect
v-model="sensorId"
label="Sensor"
list-label="Inner-city sensors"
:options="sensorOptions"
density="compact"
/>
</div>
</header>
<OnyxCard>
<div class="card-header">
<div>
<OnyxHeadline is="h2">Daily shape</OnyxHeadline>
<p class="small muted" style="margin: 0">
Peak estimated devices per hour. Weather is context, not a causal claim.
</p>
</div>
<div style="min-width: 15rem">
<OnyxSelect
v-model="scenario"
label="Scenario"
list-label="Demo scenarios"
:options="scenarioOptions"
density="compact"
/>
</div>
</div>
<div class="forecast-bars" role="img" aria-label="Daily peak forecast">
<div v-for="day in days" :key="day.date" class="forecast-bar">
<span class="small muted">{{ formatNumber(day.peak) }}</span>
<div class="forecast-bar__track">
<div class="forecast-bar__fill" :style="{ height: `${Math.max(4, (day.peak / dayMax) * 100)}%` }" />
</div>
<strong>{{ formatWeekdayShort(day.date) }}</strong>
<span class="small muted">{{ formatShortDate(day.date) }}</span>
</div>
</div>
<OnyxTable striped with-page-scrolling :table-attrs="{ style: 'min-width: 46rem' }" style="margin-top: var(--onyx-density-lg)">
<template #head>
<tr>
<th scope="col">Day</th>
<th scope="col">Mean</th>
<th scope="col">Peak</th>
<th scope="col">Peak hour</th>
<th scope="col">80% band at peak</th>
<th scope="col">Weather</th>
<th scope="col">Staff at peak</th>
</tr>
</template>
<tr v-for="day in days" :key="day.date">
<th scope="row" style="text-align: left; font-weight: 600">
{{ formatWeekday(day.date) }}
<span class="muted" style="font-weight: 400">{{ formatShortDate(day.date) }}</span>
<OnyxBadge v-if="day.date === weekPeak?.date" color="warning" density="compact" style="margin-left: 0.35rem">peak</OnyxBadge>
</th>
<td>{{ formatNumber(day.mean) }}</td>
<td>{{ formatNumber(day.peak) }}</td>
<td>{{ String(day.peakHour).padStart(2, '0') }}:00</td>
<td>{{ formatNumber(day.points.find((point) => point.v === day.peak)?.lo ?? 0) }}–{{ formatNumber(day.points.find((point) => point.v === day.peak)?.hi ?? 0) }}</td>
<td>
<template v-if="day.weather">
{{ Math.round(day.weather.tempMin) }}–{{ Math.round(day.weather.tempMax) }} °C · {{ day.weather.precipMm.toFixed(1) }} mm
</template>
<span v-else class="muted">outside mock window</span>
</td>
<td><OnyxBadge color="neutral" density="compact">{{ day.requiredAtPeak }}</OnyxBadge></td>
</tr>
</OnyxTable>
<p class="small muted table-note">
Scroll the table sideways on a phone for the uncertainty band and staffing columns. Weekly mean
{{ formatNumber(total) }} estimated devices/hour across open+closed hours. Staff at peak uses 25
visitors/staff-hour × 1.15; the plan can use a different standard.
</p>
</OnyxCard>
<div class="split-grid">
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h2">Weather strip</OnyxHeadline>
<OnyxBadge color="neutral" density="compact">Open-Meteo mock</OnyxBadge>
</div>
<WeatherStrip :days="weekWeather" />
<p v-if="!weekWeather.length" class="small muted table-note">
The mock weather window does not cover the plan week.
</p>
<p class="small muted table-note">
Weather is the main accuracy driver in the model. Holidays are context and a smaller feature.
</p>
</OnyxCard>
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h2">Read it honestly</OnyxHeadline>
<OnyxIcon :icon="iconChart" size="24px" color="primary" />
</div>
<ul class="why-list">
<li>
<OnyxIcon :icon="iconCalendar" size="16px" color="primary" />
<span>168 hourly points from the mock 7-day horizon, not a live stream.</span>
</li>
<li>
<OnyxIcon :icon="iconChart" size="16px" color="primary" />
<span>
Mean WAPE {{ meta ? formatPercent(meta.metrics.wape, 1) : '—' }} vs
{{ meta ? formatPercent(meta.metrics.baseline_wape, 1) : '—' }} seasonal naive in the mock meta;
per-hour uncertainty is real.
</span>
</li>
<li>
<OnyxIcon :icon="iconCalendar" size="16px" color="primary" />
<span>
Forecast covers {{ formatDate(days[0]?.date ?? weekStart) }} →
{{ formatDate(days[days.length - 1]?.date ?? weekEnd) }} in Europe/Berlin.
</span>
</li>
</ul>
<OnyxButton label="Turn this into a plan" link="/plan" color="primary" style="margin-top: var(--onyx-density-md)" />
</OnyxCard>
</div>
</div>
</template>
<style scoped>
.forecast-bars {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
gap: var(--onyx-density-xs);
align-items: end;
min-height: 13rem;
}
.forecast-bar {
display: flex;
min-width: 0;
flex-direction: column;
align-items: center;
gap: 0.2rem;
text-align: center;
}
.forecast-bar strong {
font-size: 0.8125rem;
}
.forecast-bar__track {
display: flex;
width: 100%;
height: 8rem;
align-items: flex-end;
border-radius: 3px;
background: var(--onyx-color-base-neutral-200);
}
.forecast-bar__fill {
width: 100%;
border-radius: 3px 3px 0 0;
background: var(--onyx-color-base-primary-500);
}
@media (max-width: 576px) {
.forecast-bars {
gap: 2px;
}
.forecast-bar strong {
font-size: 0.7rem;
}
.forecast-bar .small {
font-size: 0.65rem;
}
}
</style>