Files
web-nuxt/app/components/TrainModelCard.vue
T

152 lines
4.3 KiB
Vue

<script setup lang="ts">
import { iconUpload } from '@sit-onyx/icons'
interface TrainResponse {
job_id: string
status: string
model: 'small' | 'forecast'
submitted_at: string
mock?: boolean
}
interface TrainJob {
job_id: string
status: 'queued' | 'running' | 'succeeded' | 'failed'
model: 'small' | 'forecast'
metrics: Record<string, number | string> | null
model_version: string | null
error: string | null
}
const file = ref<File | null>(null)
const model = ref<'small' | 'forecast'>('small')
const submitting = ref(false)
const polling = ref(false)
const job = ref<TrainJob | null>(null)
const errorMessage = ref('')
let pollTimer: ReturnType<typeof setInterval> | undefined
const modelOptions = [
{ label: 'Small busy classifier (fast demo)', value: 'small' },
{ label: 'Global forecast model (slower)', value: 'forecast' },
]
const statusColor = computed(() => {
switch (job.value?.status) {
case 'succeeded':
return 'success' as const
case 'failed':
return 'danger' as const
case 'running':
return 'info' as const
default:
return 'neutral' as const
}
})
async function poll(jobId: string) {
polling.value = true
let attempts = 0
pollTimer = setInterval(async () => {
attempts += 1
try {
const result = await $fetch<TrainJob>(`/api/train/${jobId}`)
job.value = result
if (result.status === 'succeeded' || result.status === 'failed') {
clearInterval(pollTimer)
polling.value = false
}
} catch {
if (attempts >= 12) {
clearInterval(pollTimer)
polling.value = false
errorMessage.value = 'Could not read the mock training job status.'
}
}
}, 700)
}
async function submit() {
errorMessage.value = ''
job.value = null
submitting.value = true
try {
const formData = new FormData()
formData.append('model', model.value)
if (file.value) formData.append('file', file.value)
const response = await $fetch<TrainResponse>('/api/train', { method: 'POST', body: formData })
await poll(response.job_id)
} catch {
errorMessage.value = 'Training submission failed. The mock endpoint expects a CSV or model selection.'
} finally {
submitting.value = false
}
}
onBeforeUnmount(() => {
if (pollTimer) clearInterval(pollTimer)
})
</script>
<template>
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h3">Train new data</OnyxHeadline>
<OnyxBadge color="warning" density="compact">P1 mock</OnyxBadge>
</div>
<p class="small muted" style="margin-top: 0">
Posts to the mock <code>/api/train</code> contract. Replace the Nitro route with the Go endpoint when it
exists; the form shape stays the same.
</p>
<form class="form-grid" @submit.prevent="submit">
<OnyxFileUpload
v-model="file"
label="Training CSV"
message="Optional in the mock."
:accept="['text/csv']"
max-size="50MiB"
/>
<p class="small muted" style="margin: 0">The real endpoint expects the challenge CSV schema.</p>
<OnyxSelect
v-model="model"
label="Model"
list-label="Available models"
:options="modelOptions"
/>
<div class="form-actions">
<OnyxButton
type="submit"
label="Start training"
:icon="iconUpload"
:loading="submitting || polling"
/>
<span v-if="submitting" class="small muted">Submitting…</span>
</div>
</form>
<p v-if="errorMessage" class="small" style="color: var(--onyx-color-text-icons-danger-intense)">
{{ errorMessage }}
</p>
<div v-if="job" class="train-status">
<OnyxBadge :color="statusColor">{{ job.status }}</OnyxBadge>
<span class="small muted">Job {{ job.job_id }} · {{ job.model }}</span>
<OnyxBadge v-if="job.model_version" color="neutral" density="compact">
{{ job.model_version }}
</OnyxBadge>
</div>
<dl v-if="job?.metrics" class="metric-list" style="margin-top: var(--onyx-density-md)">
<div v-for="(value, key) in job.metrics" :key="key">
<dt>{{ key }}</dt>
<dd>{{ value }}</dd>
</div>
</dl>
<p v-if="job?.error" class="small" style="color: var(--onyx-color-text-icons-danger-intense)">
{{ job.error }}
</p>
</OnyxCard>
</template>