Files
web-nuxt/app/pages/live.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

325 lines
10 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,
iconCircleInformation,
iconMobileSignal,
iconSync,
iconTriangleWarning,
iconUserGroup,
} from '@sit-onyx/icons'
import { scenarioLabel } from '#shared/live'
import type { ScenarioState, Sensor, Shift, StaffingResponse } from '#shared/types'
import { formatDateTime, formatEuro, formatNumber, formatPercent, formatShortDate, formatWeekday } from '~/utils/format'
useHead({ title: 'Live recommendation — PulseCast' })
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',
})),
})
let lastScenarioId = live.scenarioId
watch(
() => live.revision,
() => {
if (live.scenarioId === lastScenarioId) void refreshPlan()
lastScenarioId = live.scenarioId
},
)
const plan = computed(() => 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 peakStaff = computed(() => peakShift.value?.staff ?? 0)
const totalStaffHours = computed(() =>
plan.value ? plan.value.hours.reduce((sum, hour) => sum + hour.scheduled_staff, 0) : 0,
)
const currentReason = computed(() => plan.value?.why[0] ?? '')
const lastUpdate = computed(() => {
if (live.scenarioAt) return live.scenarioAt
return plan.value?.context_version ?? ''
})
</script>
<template>
<div class="page page-stack live-page">
<header class="live-header">
<div>
<p class="eyebrow">Live · no login</p>
<OnyxHeadline is="h1">The plan on your phone.</OnyxHeadline>
<p class="lede">
{{ activeSensor?.name ?? 'Stadtgalerie' }} · {{ scenarioLabel(live.scenarioId) }}. Updates arrive over SSE;
if the stream drops, this page polls every 5 seconds.
</p>
</div>
<div class="live-header__status">
<LiveStatusBadge :status="live.status" :last-event-at="live.lastEventAt" />
<OnyxBadge color="neutral" density="compact">v{{ live.version || '—' }}</OnyxBadge>
</div>
</header>
<div v-if="planError" class="callout callout--warning" role="alert">
<OnyxIcon :icon="iconTriangleWarning" size="18px" color="warning" />
<div>
<strong>The live plan is unavailable.</strong>
<span class="muted">&nbsp;{{ planError.statusMessage ?? 'The API did not answer.' }}</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" />
<span>Loading the current recommendation…</span>
</div>
<div v-else-if="plan && !plan.shifts.length" class="callout callout--warning" role="status">
<OnyxIcon :icon="iconTriangleWarning" size="18px" color="warning" />
<span>No shift blocks are available yet. The presenter may still be preparing the plan.</span>
</div>
<section class="live-hero" aria-labelledby="live-decision">
<p class="live-hero__kicker"><OnyxIcon :icon="iconUserGroup" size="18px" color="primary" /> Current recommendation</p>
<p id="live-decision" class="live-hero__value">
<template v-if="plan">{{ peakStaff }} staff</template>
<template v-else>No plan yet</template>
</p>
<p class="live-hero__shift">
<template v-if="peakShift">
<strong>{{ formatWeekday(peakShift.date) }} {{ formatShortDate(peakShift.date) }}</strong><br />
{{ peakShift.start }}–{{ peakShift.end }} · peak shift
</template>
<template v-else>Waiting for the peak shift</template>
</p>
<p v-if="currentReason" class="live-hero__why">
<OnyxIcon :icon="iconCircleInformation" size="16px" color="primary" />
<span>{{ currentReason }}</span>
</p>
<OnyxButton
label="Open the full plan"
:link="`/plan?scenario=${live.scenarioId}`"
color="primary"
:icon="iconArrowRight"
icon-position="right"
/>
</section>
<section class="live-metrics" aria-label="Plan at a glance">
<div class="live-metric">
<p class="live-metric__label">Staff-hours</p>
<p class="live-metric__value">{{ plan ? formatNumber(totalStaffHours) : '—' }}</p>
</div>
<div class="live-metric">
<p class="live-metric__label">Labour cost</p>
<p class="live-metric__value">{{ plan ? formatEuro(plan.summary.cost_eur) : '—' }}</p>
</div>
<div class="live-metric">
<p class="live-metric__label">Coverage</p>
<p class="live-metric__value">{{ plan ? formatPercent(plan.summary.coverage_pct, 1) : '—' }}</p>
</div>
<div class="live-metric">
<p class="live-metric__label">Understaffed</p>
<p class="live-metric__value">{{ plan ? `${plan.summary.understaffed_hours} h` : '—' }}</p>
</div>
</section>
<section class="live-status-card">
<div class="live-status-card__row">
<span class="live-status-card__label">Scenario</span>
<strong>{{ scenarioLabel(live.scenarioId) }}</strong>
</div>
<div class="live-status-card__row">
<span class="live-status-card__label">Version</span>
<span>{{ live.version || '—' }}</span>
</div>
<div class="live-status-card__row">
<span class="live-status-card__label">Last update</span>
<span>{{ lastUpdate ? formatDateTime(lastUpdate) : '—' }}</span>
</div>
<div class="live-status-card__row">
<span class="live-status-card__label">Transport</span>
<span>{{ live.statusLabel }}<template v-if="live.status === 'polling'"> · 5 s</template></span>
</div>
<p v-if="live.error" class="small muted table-note">{{ live.error }}</p>
</section>
<footer class="live-footer">
<p>
<strong>Decision support, not an optimizer.</strong> The plan uses
{{ 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 shift
blocks and €{{ plan?.assumptions.labor_cost_eur_h ?? 16 }}/h.
</p>
<p class="muted">
Estimated devices, not people · {{ sensors?.length ?? 8 }} sensors in a 580 m cluster · no POS, sales or
payroll data.
</p>
</footer>
</div>
</template>
<style scoped>
.live-page {
max-width: 44rem;
gap: clamp(0.85rem, 2vw, 1.25rem);
}
.live-header {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: space-between;
gap: var(--onyx-density-sm);
}
.live-header h1 {
margin: 0;
}
.live-header__status {
display: flex;
flex-wrap: wrap;
gap: var(--onyx-density-xs);
align-items: center;
}
.live-hero {
padding: clamp(1.1rem, 3vw, 1.75rem);
border: 1px solid var(--onyx-color-base-primary-300);
border-radius: var(--onyx-radius-component-container-soft);
background:
radial-gradient(circle at 100% 0%, var(--onyx-color-base-primary-200), transparent 48%),
linear-gradient(135deg, var(--onyx-color-base-primary-100), var(--onyx-color-base-neutral-100) 72%);
}
.live-hero__kicker {
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;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.live-hero__value {
margin: var(--onyx-density-xs) 0 0;
font-size: clamp(3rem, 16vw, 5.5rem);
font-weight: 800;
letter-spacing: -0.04em;
line-height: 0.95;
color: var(--onyx-color-text-icons-primary-intense);
}
.live-hero__shift {
margin: var(--onyx-density-sm) 0 0;
font-size: clamp(1rem, 3.5vw, 1.25rem);
line-height: 1.4;
}
.live-hero__why {
display: flex;
gap: var(--onyx-density-xs);
align-items: flex-start;
margin: var(--onyx-density-md) 0;
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.9rem;
line-height: 1.45;
}
.live-metrics {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: var(--onyx-density-sm);
}
.live-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);
}
.live-metric__label {
margin: 0;
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.78rem;
font-weight: 600;
}
.live-metric__value {
margin: var(--onyx-density-xs) 0 0;
font-size: clamp(1.35rem, 6vw, 1.9rem);
font-weight: 700;
line-height: 1.1;
}
.live-status-card {
display: grid;
gap: var(--onyx-density-xs);
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);
}
.live-status-card__row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: var(--onyx-density-sm);
}
.live-status-card__label {
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.8rem;
}
.live-footer {
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.5;
}
.live-footer p {
margin: 0 0 var(--onyx-density-xs);
}
@media (min-width: 577px) {
.live-metrics {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
}
</style>