Files
web-nuxt/app/pages/stage.vue
T
Noel Sabia 233fdbf66c feat: live SSE stage, phone view and presenter control
- proxy Go /api/stream through Nitro; drive /stage, /live, /plan and /forecast from scenario events
- fall back to 5 s polling after SSE failures or heartbeat silence; show Live/Polling/Offline
- authenticate /control and POST /api/scenario in Nitro with server-side control credentials
- add big-screen stage with local QR and a no-login phone recommendation view
- add SSE unit tests and a live smoke test; refresh README/Makefile
2026-11-14 06:59:07 +01:00

561 lines
17 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 {
iconArrowRight,
iconCalendar,
iconCheck,
iconMobileSignal,
iconQrCode,
iconSync,
iconTriangleWarning,
iconUserGroup,
} from '@sit-onyx/icons'
import { scenarioLabel } from '#shared/live'
import type {
ForecastResponse,
ScenarioState,
Sensor,
Shift,
StaffingHour,
StaffingResponse,
} from '#shared/types'
import { formatDateTime, formatEuro, formatPercent, formatShortDate, formatWeekday } from '~/utils/format'
useHead({ title: 'Stage — PulseCast live decision' })
const { data: sensors } = await useFetch<Sensor[]>('/api/sensors')
const { data: initialScenario } = await useFetch<ScenarioState>('/api/scenario')
const sensorId = ref(
sensors.value?.find((sensor) => sensor.id === 'hn-stadtgalerie-01')?.id ??
sensors.value?.[0]?.id ??
'hn-stadtgalerie-01',
)
const live = useLiveScenario(initialScenario.value)
const {
data: planData,
status: planStatus,
error: planError,
refresh: refreshPlan,
} = await useFetch<StaffingResponse>('/api/staffing', {
query: computed(() => ({
sensor: sensorId.value,
scenario: live.scenarioId,
standard: 'standard',
})),
})
const {
data: forecastData,
error: forecastError,
refresh: refreshForecast,
} = await useFetch<ForecastResponse>('/api/forecast', {
query: computed(() => ({
sensor: sensorId.value,
horizon: '7d',
scenario: live.scenarioId,
})),
})
// A new version of the same scenario (new weather/event values) does not
// change the query string, so refresh explicitly. A different scenario id
// already triggers useFetch's own query watcher.
let lastScenarioId = live.scenarioId
watch(
() => live.revision,
() => {
if (live.scenarioId === lastScenarioId) {
void refreshPlan()
void refreshForecast()
}
lastScenarioId = live.scenarioId
},
)
const plan = computed<StaffingResponse | null>(() => planData.value ?? null)
const activeSensor = computed(() => sensors.value?.find((sensor) => sensor.id === sensorId.value))
function maxOf<T>(items: T[], score: (item: T) => number): T | undefined {
let best: T | undefined
for (const item of items) if (!best || score(item) > score(best)) best = item
return best
}
const peakShift = computed<Shift | undefined>(() =>
plan.value ? maxOf(plan.value.shifts, (shift) => shift.staff) : undefined,
)
const peakHour = computed<StaffingHour | undefined>(() =>
plan.value ? maxOf(plan.value.hours, (hour) => hour.required_staff) : undefined,
)
const peakStaff = computed(() => peakShift.value?.staff ?? peakHour.value?.required_staff ?? 0)
const totalStaffHours = computed(() =>
plan.value ? plan.value.hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0) : 0,
)
const forecastPeak = computed(() => {
const points = forecastData.value?.points ?? []
return points.length ? Math.max(...points.map((point) => point.v)) : 0
})
const saving = computed(() =>
plan.value ? Math.max(0, plan.value.summary.flat_schedule_cost_eur - plan.value.summary.cost_eur) : 0,
)
const requestUrl = useRequestURL()
const liveUrl = computed(() => {
const origin = import.meta.client ? window.location.origin : requestUrl.origin
return `${origin}/live`
})
const qrSrc = computed(() => `/api/qr?text=${encodeURIComponent(liveUrl.value)}`)
const headline = computed(() => {
if (!plan.value) return 'Waiting for the next staffing plan.'
const shift = peakShift.value
if (!shift) return 'Next week has no shift blocks yet.'
return `${formatWeekday(shift.date)} peak: ${shift.staff} staff, ${shift.start}–${shift.end}.`
})
</script>
<template>
<div class="page page-stack stage-page">
<header class="stage-hero">
<div class="stage-hero__copy">
<div class="stage-kicker">
<OnyxBadge color="neutral" density="compact">PulseCast stage</OnyxBadge>
<LiveStatusBadge :status="live.status" :last-event-at="live.lastEventAt" />
<OnyxBadge v-if="plan?.cached" color="warning" density="compact">cached forecast</OnyxBadge>
</div>
<h1 class="stage-headline">{{ headline }}</h1>
<p class="stage-sub">
{{ activeSensor?.name ?? 'Stadtgalerie' }} · {{ scenarioLabel(live.scenarioId) }} ·
{{ plan ? `${totalStaffHours} staff-hours` : 'waiting for the API' }} · updates live via SSE
</p>
<div class="stage-scenario">
<OnyxIcon :icon="iconCalendar" size="18px" color="primary" />
<span>
<strong>Scenario version {{ live.version || '—' }}</strong>
<template v-if="live.scenarioAt"> · broadcast {{ formatDateTime(live.scenarioAt) }}</template>
</span>
</div>
</div>
<div class="stage-qr">
<div class="stage-qr__frame">
<img :src="qrSrc" width="220" height="220" :alt="`QR code to ${liveUrl}`" />
</div>
<p class="stage-qr__label">
<OnyxIcon :icon="iconQrCode" size="16px" color="primary" />
Scan for the phone view
</p>
<p class="stage-qr__url mono">{{ liveUrl }}</p>
</div>
</header>
<div v-if="planError" class="callout callout--warning" role="alert">
<OnyxIcon :icon="iconTriangleWarning" size="18px" color="warning" />
<div>
<strong>The plan is unavailable.</strong>
<span class="muted">&nbsp;{{ planError.statusMessage ?? 'Check the Go API and ML service.' }}</span>
</div>
<OnyxButton label="Retry" mode="outline" density="compact" :icon="iconSync" @click="refreshPlan()" />
</div>
<div v-else-if="planStatus === 'pending' && !plan" class="callout">
<OnyxIcon :icon="iconSync" size="18px" color="primary" class="spin-slow" />
<span>Loading the current staffing plan…</span>
</div>
<div v-else-if="plan && !plan.hours.length" class="callout callout--warning" role="status">
<OnyxIcon :icon="iconTriangleWarning" size="18px" color="warning" />
<span>No open hours are available for this sensor yet. Check the data window or pick another store.</span>
</div>
<section class="stage-metrics" aria-label="Decision summary">
<div class="stage-metric stage-metric--primary">
<p class="stage-metric__label"><OnyxIcon :icon="iconUserGroup" size="18px" color="primary" /> Peak staff</p>
<p class="stage-metric__value">{{ plan ? peakStaff : '—' }}</p>
<p class="stage-metric__hint">
<template v-if="peakShift">{{ formatWeekday(peakShift.date) }} {{ peakShift.start }}–{{ peakShift.end }}</template>
<template v-else>Per shift block</template>
</p>
</div>
<div class="stage-metric">
<p class="stage-metric__label">Labour cost</p>
<p class="stage-metric__value">{{ plan ? formatEuro(plan.summary.cost_eur) : '—' }}</p>
<p class="stage-metric__hint">{{ plan ? `€${plan.assumptions.labor_cost_eur_h}/h assumption` : '—' }}</p>
</div>
<div class="stage-metric">
<p class="stage-metric__label">Coverage</p>
<p class="stage-metric__value">{{ plan ? formatPercent(plan.summary.coverage_pct, 1) : '—' }}</p>
<p class="stage-metric__hint">
{{ plan ? `${plan.summary.understaffed_hours} h under` : '—' }}
</p>
</div>
<div class="stage-metric">
<p class="stage-metric__label">Forecast peak</p>
<p class="stage-metric__value">{{ forecastPeak ? Math.round(forecastPeak) : '—' }}</p>
<p class="stage-metric__hint">estimated devices/hour</p>
</div>
<div class="stage-metric">
<p class="stage-metric__label">vs flat schedule</p>
<p class="stage-metric__value">−{{ plan ? formatEuro(saving) : '—' }}</p>
<p class="stage-metric__hint">same coverage, fewer hours</p>
</div>
</section>
<div class="stage-grid">
<section class="stage-panel" aria-labelledby="stage-decision">
<div class="stage-panel__head">
<OnyxHeadline id="stage-decision" is="h2">The decision on stage</OnyxHeadline>
<OnyxBadge color="success" density="compact">{{ scenarioLabel(live.scenarioId) }}</OnyxBadge>
</div>
<p class="stage-decision">
<template v-if="peakShift">
Put <strong>{{ peakShift.staff }} staff</strong> on {{ formatWeekday(peakShift.date) }}
{{ formatShortDate(peakShift.date) }}, {{ peakShift.start }}–{{ peakShift.end }}.
</template>
<template v-else>Pick a store and scenario; the recommendation appears here.</template>
</p>
<p class="stage-why">
<OnyxIcon :icon="iconCheck" size="18px" color="success" />
<template v-if="plan">
{{ plan.summary.understaffed_hours === 0 ? 'Every open hour is covered by the block plan.' : `${plan.summary.understaffed_hours} hours are below requirement.` }}
{{ plan.summary.overstaffed_hours }} hours are intentionally over requirement.
</template>
<template v-else>Nothing to approve yet.</template>
</p>
<div class="stage-actions">
<OnyxButton label="Presenter control" link="/control" color="primary" :icon="iconArrowRight" icon-position="right" />
<OnyxButton label="Open full plan" link="/plan" mode="outline" />
<OnyxButton label="Refresh" mode="plain" :icon="iconSync" @click="refreshPlan()" />
</div>
<p v-if="forecastError" class="small muted">
Forecast refresh failed: {{ forecastError.statusMessage }}
</p>
</section>
<aside class="stage-panel stage-panel--aside">
<div class="stage-panel__head">
<OnyxHeadline is="h2">On every phone</OnyxHeadline>
<OnyxBadge color="info" density="compact">no login</OnyxBadge>
</div>
<div class="stage-phone">
<div class="stage-phone__screen">
<p class="stage-phone__kicker"><OnyxIcon :icon="iconMobileSignal" size="14px" color="primary" /> LIVE</p>
<p class="stage-phone__value">{{ plan ? peakStaff : '—' }} staff</p>
<p class="stage-phone__label">
<template v-if="peakShift">{{ formatWeekday(peakShift.date) }} · {{ peakShift.start }}–{{ peakShift.end }}</template>
<template v-else>Waiting for the plan</template>
</p>
<p class="stage-phone__scenario">{{ scenarioLabel(live.scenarioId) }} · v{{ live.version || '—' }}</p>
</div>
</div>
<p class="small muted">
The audience scans the QR code. The same scenario event refreshes the phone view and this screen within
the SSE round trip.
</p>
</aside>
</div>
<footer class="stage-footer">
<span>
<strong>Assumptions:</strong> {{ plan?.assumptions.visitors_per_staff_hour ?? 25 }} visitors per staff-hour,
×{{ plan?.assumptions.safety_factor ?? 1.15 }} safety, {{ plan?.assumptions.shift_length_h ?? 4 }} h blocks,
store hours {{ plan?.assumptions.store_hours.open ?? '10:00' }}–{{ plan?.assumptions.store_hours.close ?? '20:00' }}.
</span>
<span class="muted">
Estimated devices, not people · 8 sensors in a 580 m cluster · no POS or payroll feed ·
last update {{ plan ? formatDateTime(live.scenarioAt || plan.context_version) : '—' }}
</span>
</footer>
</div>
</template>
<style scoped>
.stage-page {
gap: clamp(1rem, 2vw, 1.75rem);
}
.stage-hero {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: clamp(1rem, 3vw, 2.5rem);
align-items: center;
padding: clamp(1.25rem, 2.5vw, 2.5rem);
border: 1px solid var(--onyx-color-base-primary-200);
border-radius: var(--onyx-radius-component-container-soft);
background:
radial-gradient(circle at 100% 0%, var(--onyx-color-base-primary-200), transparent 45%),
linear-gradient(135deg, var(--onyx-color-base-primary-100), var(--onyx-color-base-neutral-100) 70%);
}
.stage-kicker,
.stage-scenario,
.stage-actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--onyx-density-sm);
}
.stage-headline {
max-width: 24ch;
margin: var(--onyx-density-sm) 0 0;
font-size: clamp(2.1rem, 4.8vw, 4.5rem);
font-weight: 800;
letter-spacing: -0.03em;
line-height: 1.02;
color: var(--onyx-color-text-icons-primary-intense);
}
.stage-sub {
max-width: 70ch;
margin: var(--onyx-density-sm) 0 0;
color: var(--onyx-color-text-icons-neutral-medium);
font-size: clamp(0.95rem, 1.5vw, 1.15rem);
}
.stage-scenario {
margin-top: var(--onyx-density-md);
font-size: 0.9rem;
}
.stage-qr {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--onyx-density-xs);
text-align: center;
}
.stage-qr__frame {
padding: 0.65rem;
border: 1px solid var(--onyx-color-base-neutral-300);
border-radius: var(--onyx-radius-component-container-soft);
background: #fff;
box-shadow: var(--onyx-shadow-soft-bottom);
}
.stage-qr__frame img {
display: block;
width: min(220px, 32vw);
height: auto;
}
.stage-qr__label {
display: flex;
align-items: center;
gap: 0.35rem;
margin: 0;
font-weight: 600;
}
.stage-qr__url {
max-width: 18rem;
margin: 0;
overflow-wrap: anywhere;
color: var(--onyx-color-text-icons-neutral-medium);
}
.stage-metrics {
display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: var(--onyx-density-sm);
}
.stage-metric {
min-width: 0;
padding: var(--onyx-density-md);
border: 1px solid var(--onyx-color-base-neutral-300);
border-radius: var(--onyx-radius-component-container-soft);
background: var(--onyx-color-base-neutral-100);
}
.stage-metric--primary {
border-color: var(--onyx-color-base-primary-400);
background: var(--onyx-color-base-primary-100);
}
.stage-metric__label {
display: flex;
align-items: center;
gap: 0.35rem;
margin: 0;
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.8rem;
font-weight: 600;
}
.stage-metric__value {
margin: var(--onyx-density-xs) 0 0;
font-size: clamp(1.5rem, 2.6vw, 2.5rem);
font-weight: 800;
line-height: 1.05;
}
.stage-metric__hint {
margin: var(--onyx-density-2xs) 0 0;
color: var(--onyx-color-text-icons-neutral-soft);
font-size: 0.75rem;
}
.stage-grid {
display: grid;
grid-template-columns: minmax(0, 1.65fr) minmax(16rem, 1fr);
gap: var(--onyx-density-lg);
align-items: start;
}
.stage-panel {
min-width: 0;
padding: clamp(1rem, 2vw, 1.75rem);
border: 1px solid var(--onyx-color-base-neutral-300);
border-radius: var(--onyx-radius-component-container-soft);
background: var(--onyx-color-base-neutral-100);
}
.stage-panel--aside {
background: var(--onyx-color-base-neutral-200);
}
.stage-panel__head {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: var(--onyx-density-xs);
}
.stage-panel__head h2 {
margin: 0;
}
.stage-decision {
margin: var(--onyx-density-md) 0 0;
font-size: clamp(1.1rem, 2vw, 1.5rem);
line-height: 1.35;
}
.stage-why {
display: flex;
gap: var(--onyx-density-xs);
align-items: flex-start;
margin: var(--onyx-density-sm) 0 0;
color: var(--onyx-color-text-icons-neutral-medium);
}
.stage-actions {
margin-top: var(--onyx-density-lg);
}
.stage-phone {
display: flex;
justify-content: center;
margin-block: var(--onyx-density-md);
}
.stage-phone__screen {
width: min(15rem, 100%);
padding: var(--onyx-density-md);
border: 6px solid #1e293b;
border-radius: 1.5rem;
background: #fff;
box-shadow: var(--onyx-shadow-soft-bottom);
}
.stage-phone__kicker {
display: flex;
align-items: center;
gap: 0.25rem;
margin: 0;
color: var(--onyx-color-text-icons-primary-intense);
font-size: 0.7rem;
font-weight: 700;
letter-spacing: 0.08em;
}
.stage-phone__value {
margin: 0.35rem 0 0;
font-size: 2.2rem;
font-weight: 800;
line-height: 1;
}
.stage-phone__label {
margin: 0.25rem 0 0;
font-size: 0.85rem;
}
.stage-phone__scenario {
margin: 0.75rem 0 0;
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.72rem;
}
.stage-footer {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: var(--onyx-density-sm);
padding-top: var(--onyx-density-sm);
border-top: 1px solid var(--onyx-color-base-neutral-300);
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.75rem;
line-height: 1.45;
}
.stage-footer span {
max-width: 75ch;
}
.spin-slow {
animation: spin 1.4s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.spin-slow {
animation: none;
}
}
@media (max-width: 1100px) {
.stage-metrics {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (max-width: 900px) {
.stage-hero,
.stage-grid {
grid-template-columns: minmax(0, 1fr);
}
.stage-qr {
align-items: flex-start;
text-align: left;
}
.stage-qr__frame img {
width: min(180px, 50vw);
}
}
@media (max-width: 576px) {
.stage-metrics {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.stage-headline {
font-size: clamp(1.8rem, 8vw, 2.6rem);
}
.stage-footnote {
display: none;
}
}
</style>