File size: 5,268 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
// Client for the prod-deployment batch-job kind (database → Neon prod,
// web → HuggingFace). These endpoints aren't in the generated OpenAPI types, so
// we use plain fetch (matching the launch helpers in ./batchJobs.ts).
//
// Every endpoint here is admin-gated on the backend (require_admin), so all
// requests carry the bearer token via authHeaders().
import { authHeaders } from '../lib/authHeader'
import { tracedFetch } from '../lib/api'

export type DeploymentStep = {
  key: string
  label: string
  description: string
  target: string
  status: string
  started_at?: string | null
  finished_at?: string | null
  exit_code?: number | null
  note?: string | null
  log: string
  cmd: string
  /** Counts recorded by the step (#823); null when it left no result file. */
  counts?: Record<string, number> | null
  result?: string | null
}

export type StepResultReason = {
  reason: string
  outcome: string
  label: string
  advice: string
  count: number
}

export type StepResultItem = {
  file: string
  video_id?: string | null
  tier?: string | null
  outcome: string
  reason: string
  detail?: string | null
}

export type StepResult = {
  job_id: string
  step: string
  /** False when the step left no result file (older runs, dry runs). */
  available: boolean
  kind: string
  dry_run: boolean
  counts: Record<string, number>
  reasons: StepResultReason[]
  total: number
  rows: StepResultItem[]
}

export type DeploymentJob = {
  job_id: string
  job_type: string
  /** 'load' or 'deploy'. */
  group?: string
  label: string
  dry_run: boolean
  pid?: number | null
  status: string
  started_at?: string | null
  updated_at?: string | null
  finished_at?: string | null
  steps: DeploymentStep[]
  /** True while the orchestrator process is still alive. */
  live: boolean
}

/** ``load`` = tab "2 Load" (analysis files → bronze → dbt gold); ``deploy`` = tab "3 Deploy". */
export type DeploymentGroup = 'load' | 'deploy'

export type DeploymentStepDef = {
  key: string
  label: string
  description: string
  target: string
  group?: DeploymentGroup
}

export type DeploymentsListPayload = {
  jobs: DeploymentJob[]
  available_steps: DeploymentStepDef[]
  /** Whether real (non-dry-run) deploys are enabled (DEPLOYMENTS_ALLOW_LAUNCH). */
  enabled: boolean
}

export type LaunchDeploymentResult = {
  launched: boolean
  job_id: string
  pid?: number | null
  dry_run: boolean
  steps: string[]
  detail: string
}

export type DeploymentLog = {
  job_id: string
  step: string
  path: string
  lines: string[]
}

async function readError(r: Response): Promise<string> {
  let detail = `HTTP ${r.status}`
  try {
    const j = (await r.json()) as { error?: string; detail?: string }
    if (j?.error || j?.detail) detail = (j.error || j.detail) as string
  } catch {
    /* ignore */
  }
  return detail
}

export async function fetchDeployments(
  group?: DeploymentGroup,
): Promise<DeploymentsListPayload> {
  const r = await tracedFetch(`/api/deployments/${group ? `?group=${group}` : ''}`, {
    headers: authHeaders(),
    signal: AbortSignal.timeout(15_000),
  })
  if (!r.ok) throw new Error(await readError(r))
  return (await r.json()) as DeploymentsListPayload
}

export async function launchDeployment(body: {
  steps?: string[]
  dry_run: boolean
  group?: DeploymentGroup
}): Promise<LaunchDeploymentResult> {
  const r = await tracedFetch('/api/deployments/launch', {
    method: 'POST',
    headers: authHeaders({ 'Content-Type': 'application/json' }),
    body: JSON.stringify(body),
    signal: AbortSignal.timeout(15_000),
  })
  if (!r.ok) throw new Error(await readError(r))
  return (await r.json()) as LaunchDeploymentResult
}

export async function stopDeployment(
  jobId: string,
  force = false,
): Promise<{ stopped: boolean; detail: string }> {
  const r = await tracedFetch(
    `/api/deployments/${encodeURIComponent(jobId)}/stop?force=${force ? 'true' : 'false'}`,
    { method: 'POST', headers: authHeaders(), signal: AbortSignal.timeout(15_000) },
  )
  if (!r.ok) throw new Error(await readError(r))
  return (await r.json()) as { stopped: boolean; detail: string }
}

export async function fetchDeploymentLog(
  jobId: string,
  step: string,
): Promise<DeploymentLog> {
  const r = await tracedFetch(
    `/api/deployments/${encodeURIComponent(jobId)}/log?step=${encodeURIComponent(step)}&lines=300`,
    { headers: authHeaders(), signal: AbortSignal.timeout(10_000) },
  )
  if (!r.ok) throw new Error(await readError(r))
  return (await r.json()) as DeploymentLog
}

export async function fetchStepResult(
  jobId: string,
  step: string,
  opts: { outcome?: string; reason?: string; q?: string; limit?: number; offset?: number } = {},
): Promise<StepResult> {
  const p = new URLSearchParams({ step })
  if (opts.outcome) p.set('outcome', opts.outcome)
  if (opts.reason) p.set('reason', opts.reason)
  if (opts.q) p.set('q', opts.q)
  p.set('limit', String(opts.limit ?? 50))
  p.set('offset', String(opts.offset ?? 0))
  const r = await tracedFetch(`/api/deployments/${encodeURIComponent(jobId)}/result?${p.toString()}`, {
    headers: authHeaders(),
    signal: AbortSignal.timeout(15_000),
  })
  if (!r.ok) throw new Error(await readError(r))
  return (await r.json()) as StepResult
}