feat: wire Nitro API to Go, real staffing plan and train proxy

- Replace mock Nitro routes with server-side proxies to the Go API
- /plan reads Go /api/staffing; local assumption edits are a labeled preview
- /simulate reads base and scenario staffing from Go instead of local multipliers
- Route train upload and scenario POST through Nitro with server-only credentials
- Keep NUXT_PUBLIC_USE_MOCK=1 as an explicit dev fallback only
- Match Go's global-peak flat schedule and add a captured Go parity fixture
- Run the web runtime as numeric UID/GID 10001:10001
- Add make check, Gitea CI workflow and a proxy smoke script
This commit is contained in:
Noel Sabia committed 2026-11-14 06:13:00 +01:00
1 parent e8d141ca43
commit b33e6ff001
33 files changed
+1147 -317

No files matched your search

+78 -51
View File
@@ -1,25 +1,74 @@
<script setup lang="ts">
import { iconArrowRight, iconCircleInformation, iconFlash } from '@sit-onyx/icons'
import { addDays, applyScenario, buildStaffing, planWeekStart } from '#shared/staffing'
import type { ContextResponse, ForecastResponse, Scenario, ScenarioPreset, Sensor } from '#shared/types'
import { addDays, berlinParts, planWeekStart } from '#shared/staffing'
import type {
ContextResponse,
Scenario,
ScenarioPreset,
Sensor,
StaffingResponse,
} from '#shared/types'
import { formatEuro, formatShortDate, formatWeekday, formatWeekdayShort, weekLabel } from '~/utils/format'
useHead({ title: 'Simulate a scenario — PulseCast' })
const config = useRuntimeConfig()
const demoNow = String(config.public.demoNow)
const weekStart = planWeekStart(demoNow)
const weekEnd = addDays(weekStart, 6)
const sensorId = ref('hn-stadtgalerie-01')
const scenario = ref<Scenario>('sunny-saturday-market')
const { data: sensors } = await useFetch<Sensor[]>('/api/sensors')
const { data: context } = await useFetch<ContextResponse>('/api/context')
const { data: presets } = await useFetch<ScenarioPreset[]>('/api/scenarios')
const { data: baseForecast } = await useFetch<ForecastResponse>('/api/forecast', {
query: computed(() => ({ sensor: sensorId.value, horizon: '7d', scenario: 'base' })),
const sensorId = ref(
sensors.value?.find((sensor) => sensor.id === 'hn-stadtgalerie-01')?.id ??
sensors.value?.[0]?.id ??
'hn-stadtgalerie-01',
)
const scenario = ref<Scenario>('sunny-saturday-market')
const EMPTY_PLAN: StaffingResponse = {
sensor: '',
scenario: 'base',
standard: 'standard',
assumptions: {
visitors_per_staff_hour: 25,
safety_factor: 1.15,
min_staff: 1,
shift_length_h: 4,
labor_cost_eur_h: 16,
store_hours: { open: '10:00', close: '20:00', tz: 'Europe/Berlin' },
},
hours: [],
shifts: [],
summary: {
coverage_pct: 0,
cost_eur: 0,
flat_schedule_cost_eur: 0,
overstaffed_hours: 0,
understaffed_hours: 0,
},
why: [],
cached: false,
data_version: '',
model_version: '',
context_version: '',
}
const { data: baseData } = await useFetch<StaffingResponse>('/api/staffing', {
query: computed(() => ({ sensor: sensorId.value, scenario: 'base', standard: 'standard' })),
})
const { data: scenarioData, status } = await useFetch<StaffingResponse>('/api/staffing', {
query: computed(() => ({ sensor: sensorId.value, scenario: scenario.value, standard: 'standard' })),
})
const basePlan = computed(() => baseData.value ?? EMPTY_PLAN)
const scenarioPlan = computed(() => scenarioData.value ?? basePlan.value)
const weekStart = computed(() => {
const first = basePlan.value.hours[0]
return first ? berlinParts(first.t).date : planWeekStart(demoNow)
})
const weekEnd = computed(() => addDays(weekStart.value, 6))
const activeSensor = computed(() => sensors.value?.find((sensor) => sensor.id === sensorId.value))
const sensorOptions = computed(() =>
@@ -34,31 +83,6 @@ const radioOptions = computed(() =>
})),
)
const scenarioPoints = computed(() =>
applyScenario(baseForecast.value?.points ?? [], scenario.value),
)
const versions = computed(() => ({
data_version: baseForecast.value?.data_version ?? '',
model_version: baseForecast.value?.model_version ?? '',
context_version: baseForecast.value?.context_version ?? '',
}))
function makePlan(points: typeof scenarioPoints.value) {
return buildStaffing({
sensor: sensorId.value,
scenario: scenario.value,
standard: 'standard',
points,
weekStart,
...versions.value,
})
}
const basePlan = computed(() => makePlan(baseForecast.value?.points ?? []).response)
const scenarioBuild = computed(() => makePlan(scenarioPoints.value))
const scenarioPlan = computed(() => scenarioBuild.value.response)
const baseHours = computed(() => basePlan.value.hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0))
const scenarioHours = computed(() =>
scenarioPlan.value.hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0),
@@ -66,18 +90,19 @@ const scenarioHours = computed(() =>
const deltaHours = computed(() => scenarioHours.value - baseHours.value)
const deltaCost = computed(() => scenarioPlan.value.summary.cost_eur - basePlan.value.summary.cost_eur)
function peakStaffByDay(plan: ReturnType<typeof makePlan>) {
function peakStaffByDay(plan: StaffingResponse) {
const map: Record<string, number> = {}
for (const [date, hours] of Object.entries(plan.byDate)) {
map[date] = hours.reduce((peak, hour) => Math.max(peak, hour.required_staff), 0)
for (const hour of plan.hours) {
const date = berlinParts(hour.t).date
map[date] = Math.max(map[date] ?? 0, hour.required_staff)
}
return map
}
const basePeaks = computed(() => peakStaffByDay(makePlan(baseForecast.value?.points ?? [])))
const scenarioPeaks = computed(() => peakStaffByDay(scenarioBuild.value))
const basePeaks = computed(() => peakStaffByDay(basePlan.value))
const scenarioPeaks = computed(() => peakStaffByDay(scenarioPlan.value))
const days = computed(() =>
Object.keys(basePeaks.value)
[...new Set([...Object.keys(basePeaks.value), ...Object.keys(scenarioPeaks.value)])]
.sort()
.map((date) => ({
date,
@@ -111,8 +136,8 @@ const days = computed(() =>
<div class="callout">
<OnyxIcon :icon="iconCircleInformation" size="16px" color="primary" />
Local selection for now. SSE broadcast to <code>/stage</code> and phones is the next slice; this page makes the
recommendation change visible on one screen.
The base and scenario plans are generated by Go `/api/staffing`. SSE broadcast to <code>/stage</code> and
phones is the next slice; this page makes the recommendation change visible on one screen.
</div>
<div class="split-grid">
@@ -121,9 +146,11 @@ const days = computed(() =>
<div class="card-header">
<div>
<OnyxHeadline is="h2">Scenario</OnyxHeadline>
<p class="small muted" style="margin: 0">The contract enum is fixed; perturbation sizes are mock assumptions.</p>
<p class="small muted" style="margin: 0">The contract enum is fixed; the API applies the perturbation.</p>
</div>
<OnyxBadge color="warning" density="compact">mock simulation</OnyxBadge>
<OnyxBadge :color="status === 'pending' ? 'warning' : 'success'" density="compact">
{{ status === 'pending' ? 'loading' : 'Go API' }}
</OnyxBadge>
</div>
<OnyxRadioGroup
v-model="scenario"
@@ -193,7 +220,7 @@ const days = computed(() =>
peak hour before cutting the block.
</template>
<template v-else>
This scenario does not change the shift requirements in the mock. That is a result, not a bug.
This scenario does not change the shift requirements. That is a result, not a bug.
</template>
</p>
</div>
@@ -212,14 +239,14 @@ const days = computed(() =>
<OnyxHeadline is="h3">Scenario rules</OnyxHeadline>
</div>
<ul class="why-list">
<li><span><strong>Sunny Saturday + market</strong> — ×1.20 Saturday footfall.</span></li>
<li><span><strong>Rainy Saturday</strong> — ×0.82 Saturday footfall.</span></li>
<li><span><strong>Wednesday event</strong> — ×1.25 Wed 16:00–20:00.</span></li>
<li><span>Base keeps the latest forecast weather and no event uplift.</span></li>
<li><span><strong>Base week</strong> — latest forecast weather, no scenario uplift.</span></li>
<li><span><strong>Sunny Saturday + market</strong> — Go applies the market/day uplift to next Saturday.</span></li>
<li><span><strong>Rainy Saturday</strong> — Go applies the wet-weather reduction.</span></li>
<li><span><strong>Wednesday event</strong> — Go applies the weekday evening event uplift.</span></li>
</ul>
<p class="small muted table-note">
These multipliers are mock demo assumptions. The Go/ML implementation must publish the same numbers or
the comparison is not comparable.
Scenario perturbations are produced by the Go/ML stack. This page reads the base and scenario plans from
`/api/staffing`; it does not invent its own multipliers.
</p>
</OnyxCard>