Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 16,654 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | import { useQuery } from '@tanstack/react-query'
import { useCallback, useMemo, useState } from 'react'
import {
type DeploymentGroup,
fetchDeployments,
fetchDeploymentLog,
launchDeployment,
stopDeployment,
type DeploymentJob,
type DeploymentStep,
} from '../api/deployments'
import { formatAgoCompact, formatDateTimeAbsolute } from '../utils/dateTime'
import { hasProblems, sumDbtCounts } from '../utils/stepCounts'
import StepResultPanel, { StepCounts } from './StepResultPanel'
const STATUS_STYLE: Record<string, string> = {
completed: 'bg-emerald-100 text-emerald-800 border-emerald-200',
running: 'bg-sky-100 text-sky-800 border-sky-200',
failed: 'bg-red-100 text-red-800 border-red-200',
cancelled: 'bg-amber-100 text-amber-800 border-amber-200',
skipped: 'bg-slate-100 text-slate-500 border-slate-200',
pending: 'bg-slate-100 text-slate-500 border-slate-200',
}
function StatusBadge({ status }: { status: string }) {
const cls = STATUS_STYLE[status] ?? STATUS_STYLE.pending
return (
<span
className={`inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium ${cls}`}
>
{status === 'running' && (
<span className="h-1.5 w-1.5 animate-pulse rounded-full bg-sky-500" />
)}
{status}
</span>
)
}
function StepRow({ job, step }: { job: DeploymentJob; step: DeploymentStep }) {
const [open, setOpen] = useState(false)
const { data: log, isFetching } = useQuery({
queryKey: ['deployment-log', job.job_id, step.key],
queryFn: () => fetchDeploymentLog(job.job_id, step.key),
enabled: open,
// Keep the log fresh while the step is actively running.
refetchInterval: open && step.status === 'running' ? 3_000 : false,
})
return (
<div className="rounded-md border border-slate-200">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className="flex w-full items-center justify-between gap-3 px-3 py-2 text-left hover:bg-slate-50"
>
<span className="flex items-center gap-2">
<span className="text-slate-400">{open ? '▾' : '▸'}</span>
<span className="text-sm font-medium text-slate-800">{step.label}</span>
<span className="text-xs text-slate-500">{step.description}</span>
</span>
<span className="flex items-center gap-3">
<StepCounts counts={step.counts} />
{step.exit_code != null && step.status !== 'completed' && (
<span className="text-xs text-slate-500">exit {step.exit_code}</span>
)}
<StatusBadge status={step.status} />
</span>
</button>
{step.note && (
<p className="border-t border-amber-100 bg-amber-50 px-3 py-2 text-xs text-amber-800">
⚠️ {step.note}
</p>
)}
{open && (
<div className="border-t border-slate-100 bg-slate-50 px-3 py-2">
{(job.group === 'load' || step.counts) && (
<div className="mb-3">
<p className="mb-1 text-xs font-semibold text-slate-700">
{hasProblems(step.counts) ? 'What was skipped or failed' : 'Counts'}
</p>
<StepResultPanel jobId={job.job_id} step={step.key} live={step.status === 'running'} recorded={!!step.counts} />
</div>
)}
{step.cmd && (
<p className="mb-1 font-mono text-[11px] text-slate-500">$ {step.cmd}</p>
)}
{isFetching && !log && (
<p className="text-xs text-slate-400">Loading log…</p>
)}
{log && log.lines.length > 0 ? (
<pre className="max-h-72 overflow-auto whitespace-pre-wrap break-words rounded bg-slate-900 p-2 font-mono text-[11px] leading-relaxed text-slate-100">
{log.lines.join('\n')}
</pre>
) : (
!isFetching && <p className="text-xs text-slate-400">No log output yet.</p>
)}
</div>
)}
</div>
)
}
function stepSummary(job: DeploymentJob): string {
const total = job.steps.length
const done = job.steps.filter((s) => s.status === 'completed').length
const failed = job.steps.filter((s) => s.status === 'failed').length
const parts = [`${total} step${total === 1 ? '' : 's'}`]
if (done) parts.push(`${done} done`)
if (failed) parts.push(`${failed} failed`)
// The headline numbers of the run, when its steps recorded them (#823).
const load = job.steps.find((s) => s.counts && typeof s.counts.read === 'number')?.counts
if (load) {
parts.push(`${(load.read ?? 0).toLocaleString()} files read`, `${(load.loaded ?? 0).toLocaleString()} loaded`)
if (load.skipped) parts.push(`${load.skipped.toLocaleString()} skipped`)
if (load.errors) parts.push(`${load.errors.toLocaleString()} file ${load.errors === 1 ? 'error' : 'errors'}`)
}
const dbt = sumDbtCounts(job.steps)
if (dbt) {
parts.push(`${dbt.success.toLocaleString()} of ${dbt.models.toLocaleString()} models built`)
if (dbt.error) parts.push(`${dbt.error.toLocaleString()} models failed`)
}
return parts.join(' · ')
}
function JobCard({
job,
onStop,
clockMs,
defaultOpen = false,
}: {
job: DeploymentJob
onStop: (jobId: string) => void
clockMs: number
defaultOpen?: boolean
}) {
const [open, setOpen] = useState(defaultOpen)
const when = job.started_at ? formatDateTimeAbsolute(job.started_at) : '—'
const ago = job.started_at ? formatAgoCompact(job.started_at, clockMs) : null
return (
<div className="rounded-lg border border-slate-200 bg-white shadow-sm">
<div className="flex flex-wrap items-center justify-between gap-2 p-4">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className="flex flex-1 items-start gap-2 text-left"
>
<span className="mt-0.5 text-slate-400">{open ? '▾' : '▸'}</span>
<span>
<span className="flex flex-wrap items-center gap-2">
<span className="text-sm font-semibold text-slate-900">{job.label}</span>
{job.dry_run && (
<span className="rounded bg-indigo-100 px-1.5 py-0.5 text-[10px] font-medium text-indigo-700">
DRY RUN
</span>
)}
<StatusBadge status={job.status} />
{!open && (
<span className="text-[11px] text-slate-500">{stepSummary(job)}</span>
)}
</span>
<span className="mt-0.5 block font-mono text-[11px] text-slate-400">
{job.job_id} · started {when}
{ago ? ` (${ago})` : ''}
</span>
</span>
</button>
{job.live && job.status === 'running' && (
<button
type="button"
onClick={() => onStop(job.job_id)}
className="rounded-md border border-red-200 bg-red-50 px-3 py-1.5 text-xs font-medium text-red-700 hover:bg-red-100"
>
Stop
</button>
)}
</div>
{open && (
<div className="space-y-2 border-t border-slate-100 p-4">
{job.steps.map((s) => (
<StepRow key={s.key} job={job} step={s} />
))}
</div>
)}
</div>
)
}
const COPY: Record<
DeploymentGroup,
{ heading: string; blurb: string; realLabel: string; confirm: (steps: string[]) => string; empty: string }
> = {
load: {
heading: 'Load analyses into the database',
blurb:
'Runs the selected steps in order: analysed videos get their event rows, saved analysis files → bronze, then dbt rebuilds the gold tables and ' +
'refreshes the public views the website reads. Local warehouse only; nothing is published. A failing step stops the run.',
realLabel: '▶ Run load',
confirm: (steps) =>
`Run the load (${steps.join(', ')}) against the local warehouse?\n\n` +
'This writes bronze rows and rebuilds gold tables. Nothing is published to production.',
empty: 'No loads yet. Run one above.',
},
deploy: {
heading: 'Launch a prod deployment',
blurb: 'Runs the selected steps in order. A failing step stops the run.',
realLabel: '▶ Deploy to PRODUCTION',
confirm: (steps) =>
`Run a REAL production deployment (${steps.join(', ')})?\n\n` +
'This pushes to the Neon prod database and/or HuggingFace. This cannot be undone.',
empty: 'No deployments yet. Launch one above.',
},
}
export default function DeploymentPanel({ group = 'deploy' }: { group?: DeploymentGroup }) {
const copy = COPY[group]
const { data, isPending, isError, error, refetch } = useQuery({
queryKey: ['deployments', group],
queryFn: () => fetchDeployments(group),
refetchInterval: (q) => {
const live = q.state.data?.jobs?.some((j) => j.live && j.status === 'running')
return live ? 3_000 : 15_000
},
retry: 1,
})
const availableSteps = data?.available_steps ?? []
const enabled = data?.enabled ?? false
const [selected, setSelected] = useState<Set<string>>(new Set())
// A deploy defaults to a dry run; a load only touches the local warehouse, so it runs for real.
const [dryRun, setDryRun] = useState(group === 'deploy')
const [launching, setLaunching] = useState(false)
const [msg, setMsg] = useState<string | null>(null)
const [historyOpen, setHistoryOpen] = useState(false)
// Default selection = every available step, once they load.
const effectiveSelected = useMemo(() => {
if (selected.size > 0) return selected
return new Set(availableSteps.map((s) => s.key))
}, [selected, availableSteps])
const toggleStep = useCallback(
(key: string) => {
setSelected((prev) => {
const base = prev.size > 0 ? new Set(prev) : new Set(availableSteps.map((s) => s.key))
if (base.has(key)) base.delete(key)
else base.add(key)
return base
})
},
[availableSteps],
)
const anyLive = data?.jobs?.some((j) => j.live && j.status === 'running') ?? false
const onLaunch = useCallback(async () => {
const steps = availableSteps.map((s) => s.key).filter((k) => effectiveSelected.has(k))
if (steps.length === 0) {
setMsg('Select at least one step.')
return
}
if (!dryRun) {
const ok = window.confirm(copy.confirm(steps))
if (!ok) return
}
setLaunching(true)
setMsg(null)
try {
const res = await launchDeployment({ steps, dry_run: dryRun, group })
setMsg(res.detail || 'Launched.')
window.setTimeout(() => void refetch(), 1_000)
} catch (e) {
setMsg((e as Error).message || 'Launch failed')
} finally {
setLaunching(false)
}
}, [availableSteps, effectiveSelected, dryRun, refetch, group, copy])
const onStop = useCallback(
async (jobId: string) => {
if (!window.confirm(`Stop deployment ${jobId}? The running step is terminated.`)) return
try {
const res = await stopDeployment(jobId)
setMsg(res.detail)
void refetch()
} catch (e) {
setMsg((e as Error).message || 'Stop failed')
}
},
[refetch],
)
const clockMs = Date.now()
const jobs = data?.jobs ?? []
// Keep any in-flight deployment expanded and pinned to the top; everything
// else is historical and tucked behind a collapsed "History" toggle.
const activeJobs = jobs.filter((j) => j.live && j.status === 'running')
const historyJobs = jobs.filter((j) => !(j.live && j.status === 'running'))
return (
<div className="space-y-4">
{/* Launch control */}
<div className="rounded-lg border border-slate-200 bg-white p-4 shadow-sm">
<h2 className="text-sm font-semibold text-slate-900">{copy.heading}</h2>
<p className="mt-0.5 text-xs text-slate-500">{copy.blurb}</p>
<div className="mt-3 flex flex-wrap gap-2">
{availableSteps.map((s) => {
const on = effectiveSelected.has(s.key)
return (
<button
key={s.key}
type="button"
onClick={() => toggleStep(s.key)}
title={s.description}
className={`rounded-md border px-3 py-1.5 text-xs font-medium ${
on
? 'border-sky-300 bg-sky-50 text-sky-800'
: 'border-slate-200 bg-white text-slate-500'
}`}
>
<span className="mr-1">{on ? '✓' : '○'}</span>
{s.label}
</button>
)
})}
{availableSteps.length === 0 && (
<span className="text-xs text-slate-400">No steps available.</span>
)}
</div>
<div className="mt-3 flex flex-wrap items-center gap-4">
<label className="flex items-center gap-2 text-xs text-slate-700">
<input
type="checkbox"
checked={dryRun}
onChange={(e) => setDryRun(e.target.checked)}
className="h-3.5 w-3.5"
/>
Dry run (log commands, don't execute)
</label>
<button
type="button"
onClick={onLaunch}
disabled={launching || (!dryRun && !enabled) || anyLive}
className={`rounded-md px-4 py-1.5 text-xs font-semibold text-white disabled:cursor-not-allowed disabled:opacity-50 ${
dryRun
? 'bg-indigo-600 hover:bg-indigo-700'
: group === 'load'
? 'bg-emerald-600 hover:bg-emerald-700'
: 'bg-red-600 hover:bg-red-700'
}`}
>
{launching
? 'Launching…'
: dryRun
? '▶ Launch dry run'
: copy.realLabel}
</button>
{anyLive && (
<span className="text-xs text-amber-600">
A run is already in progress — stop it before starting another.
</span>
)}
</div>
{!dryRun && !enabled && group === 'deploy' && (
<p className="mt-2 text-xs text-amber-700">
Real deploys are disabled. Set <code>DEPLOYMENTS_ALLOW_LAUNCH=1</code> on the API to
enable, or keep Dry run on.
</p>
)}
{msg && <p className="mt-2 text-xs text-slate-600">{msg}</p>}
</div>
{/* Recent jobs */}
{isPending && !data && (
<div className="rounded-lg border border-slate-200 bg-white p-8 text-center text-sm text-slate-500">
Loading deployments…
</div>
)}
{isError && (
<div className="rounded-lg border border-red-200 bg-red-50 p-4 text-sm text-red-800">
Could not load deployments: {(error as Error)?.message ?? 'error'}{' '}
<button type="button" className="underline" onClick={() => refetch()}>
Retry
</button>
</div>
)}
{data && jobs.length === 0 && (
<div className="rounded-lg border border-dashed border-slate-200 bg-white p-8 text-center text-sm text-slate-500">
{copy.empty}
</div>
)}
{/* In-flight deployment(s): always expanded, pinned to the top. */}
{activeJobs.map((job) => (
<JobCard key={job.job_id} job={job} onStop={onStop} clockMs={clockMs} defaultOpen />
))}
{/* Past runs: collapsed behind a single toggle so they don't dominate. */}
{historyJobs.length > 0 && (
<div className="space-y-2">
<button
type="button"
onClick={() => setHistoryOpen((v) => !v)}
className="flex w-full items-center gap-2 rounded-lg border border-slate-200 bg-white px-4 py-2.5 text-left text-sm font-medium text-slate-700 shadow-sm hover:bg-slate-50"
>
<span className="text-slate-400">{historyOpen ? '▾' : '▸'}</span>
History
<span className="rounded-full bg-slate-100 px-2 py-0.5 text-[11px] font-medium text-slate-500">
{historyJobs.length}
</span>
{!historyOpen && (
<span className="ml-auto text-[11px] font-normal text-slate-400">
{historyJobs[0]?.status} ·{' '}
{historyJobs[0]?.started_at
? formatAgoCompact(historyJobs[0].started_at, clockMs)
: '—'}
</span>
)}
</button>
{historyOpen &&
historyJobs.map((job) => (
<JobCard key={job.job_id} job={job} onStop={onStop} clockMs={clockMs} />
))}
</div>
)}
</div>
)
}
|