File size: 11,434 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
import { useEffect, useState } from 'react'
import type { BatchJurisdictionRun, BatchJob } from '../api/batchJobs'
import { parseApiDateTime } from './dateTime'

function compareJurisdictions(a: BatchJurisdictionRun, b: BatchJurisdictionRun): number {
  const stA = (a.state_code || '').toUpperCase()
  const stB = (b.state_code || '').toUpperCase()
  if (stA !== stB) return stA.localeCompare(stB)
  const nameA = (a.jurisdiction_name || a.jurisdiction_id || '').toLowerCase()
  const nameB = (b.jurisdiction_name || b.jurisdiction_id || '').toLowerCase()
  const byName = nameA.localeCompare(nameB)
  if (byName !== 0) return byName
  return (a.jurisdiction_id || '').localeCompare(b.jurisdiction_id || '')
}

/** Default table order: state, then jurisdiction name (then id). */
export function sortJurisdictions(jurisdictions: BatchJurisdictionRun[]): BatchJurisdictionRun[] {
  return [...jurisdictions].sort(compareJurisdictions)
}

/** Best timestamp for "last activity" on a jurisdiction row (ISO string). */
export function jurisdictionLastUpdatedIso(j: BatchJurisdictionRun): string | null {
  const candidates: string[] = []
  if (j.updated_at?.trim()) candidates.push(j.updated_at.trim())
  if (j.finished_at?.trim()) candidates.push(j.finished_at.trim())
  if (j.current_video_started_at?.trim()) candidates.push(j.current_video_started_at.trim())
  for (const v of j.videos || []) {
    if (v.finished_at?.trim()) candidates.push(v.finished_at.trim())
  }
  if (j.started_at?.trim()) candidates.push(j.started_at.trim())

  let best: string | null = null
  let bestMs = 0
  for (const iso of candidates) {
    const d = parseApiDateTime(iso)
    if (d && d.getTime() >= bestMs) {
      bestMs = d.getTime()
      best = iso
    }
  }
  return best
}

/** Latest progress timestamp across all batches (ignores batch ``updated_at`` metadata). */
export function latestDashboardActivityIso(batches: BatchJob[]): string | null {
  let bestMs = 0
  let best: string | null = null
  for (const batch of batches) {
    for (const j of batch.jurisdictions || []) {
      const iso = jurisdictionLastUpdatedIso(j)
      const d = parseApiDateTime(iso)
      if (d && d.getTime() >= bestMs) {
        bestMs = d.getTime()
        best = iso
      }
    }
    const summary = batch.summary || {}
    const summaryIso =
      typeof summary.current_video_started_at === 'string'
        ? summary.current_video_started_at.trim()
        : ''
    if (summaryIso) {
      const d = parseApiDateTime(summaryIso)
      if (d && d.getTime() >= bestMs) {
        bestMs = d.getTime()
        best = summaryIso
      }
    }
    for (const iso of [batch.started_at, batch.finished_at]) {
      const trimmed = iso?.trim()
      if (!trimmed) continue
      const d = parseApiDateTime(trimmed)
      if (d && d.getTime() >= bestMs) {
        bestMs = d.getTime()
        best = trimmed
      }
    }
  }
  return best
}

export function jurisdictionStateCodes(jurisdictions: BatchJurisdictionRun[]): string[] {
  const codes = new Set<string>()
  for (const j of jurisdictions) {
    const st = (j.state_code || '').toUpperCase().trim()
    if (st) codes.add(st)
  }
  return [...codes].sort()
}

export function filterJurisdictionsByState(
  jurisdictions: BatchJurisdictionRun[],
  stateFilter: string,
): BatchJurisdictionRun[] {
  const st = stateFilter.trim().toUpperCase()
  if (!st) return jurisdictions
  return jurisdictions.filter((j) => (j.state_code || '').toUpperCase() === st)
}

export type ActiveVideoRun = {
  videoId: string
  title: string
  jurisdictionId: string
  startedAt: string
}

function clockFromRunningJurisdiction(j: BatchJurisdictionRun): ActiveVideoRun | null {
  if (j.status !== 'running') return null

  if (j.current_video_started_at) {
    const started = parseApiDateTime(j.current_video_started_at)
    if (started) {
      return {
        videoId: j.current_video_id || '',
        title: j.current_video_title || '',
        jurisdictionId: j.jurisdiction_id,
        startedAt: j.current_video_started_at,
      }
    }
  }

  const videos = j.videos || []
  if (videos.length > 0) {
    const last = videos[videos.length - 1]
    if (last.finished_at) {
      const started = parseApiDateTime(last.finished_at)
      if (started) {
        return {
          videoId: j.current_video_id || last.video_id || '',
          title: j.current_video_title || last.title || '',
          jurisdictionId: j.jurisdiction_id,
          startedAt: last.finished_at,
        }
      }
    }
  }

  if (j.started_at) {
    const started = parseApiDateTime(j.started_at)
    if (started) {
      return {
        videoId: j.current_video_id || '',
        title: j.current_video_title || '',
        jurisdictionId: j.jurisdiction_id,
        startedAt: j.started_at,
      }
    }
  }

  return null
}

/** Running jurisdiction with an in-flight video (from batch job payload). */
export function findActiveVideoRun(
  jurisdictions: BatchJurisdictionRun[],
): ActiveVideoRun | null {
  let best: ActiveVideoRun | null = null
  let bestStarted = 0
  for (const j of jurisdictions) {
    const run = clockFromRunningJurisdiction(j)
    if (!run) continue
    const t = parseApiDateTime(run.startedAt)?.getTime() ?? 0
    if (!best || t >= bestStarted) {
      best = run
      bestStarted = t
    }
  }
  return best
}

export function resolveRunningFileClock(batch: BatchJob): ActiveVideoRun | null {
  const fromJurisdictions = findActiveVideoRun(batch.jurisdictions)
  if (fromJurisdictions) return fromJurisdictions

  if (batch.status !== 'running') return null
  const s = batch.summary || {}
  const iso = typeof s.current_video_started_at === 'string' ? s.current_video_started_at : ''
  if (!iso || !parseApiDateTime(iso)) return null
  return {
    videoId: typeof s.current_video_id === 'string' ? s.current_video_id : '',
    title: typeof s.current_video_title === 'string' ? s.current_video_title : '',
    jurisdictionId:
      typeof s.current_jurisdiction_id === 'string' ? s.current_jurisdiction_id : '',
    startedAt: iso,
  }
}

export function secondsSince(iso: string | null | undefined, nowMs = Date.now()): number | null {
  const started = parseApiDateTime(iso)
  if (!started) return null
  return Math.max(0, Math.floor((nowMs - started.getTime()) / 1000))
}

export function avgSecondsPerFile(summary: Record<string, unknown>): number | null {
  const fromApi = summary.avg_seconds_per_file
  if (fromApi != null && fromApi !== '') {
    const n = Number(fromApi)
    if (!Number.isNaN(n) && n > 0) return n
  }
  const elapsed = Number(summary.elapsed_seconds) || 0
  const processed = Number(summary.files_processed) || 0
  if (processed > 0 && elapsed > 0) return elapsed / processed
  const videos =
    (Number(summary.videos_ok) || 0) +
    (Number(summary.videos_fail) || 0) +
    (Number(summary.videos_tombstoned) || 0) +
    (Number(summary.videos_empty) || 0) +
    (Number(summary.videos_rate_limit) || 0)
  if (videos > 0 && elapsed > 0) return elapsed / videos
  return null
}

function doneVideosCount(j: BatchJurisdictionRun): number {
  const st = j.stats || {}
  return (
    (Number(st.ok) || 0) +
    (Number(st.fail) || 0) +
    (Number(st.tombstoned) || 0) +
    (Number(st.empty) || 0) +
    (Number(st.rate_limit) || 0)
  )
}

/** Remaining videos for one jurisdiction: pending − done (if pending is known). */
export function remainingVideosForJurisdiction(j: BatchJurisdictionRun): number | null {
  const pending = Number(j.stats?.pending)
  if (!Number.isFinite(pending) || pending <= 0) return null
  return Math.max(0, Math.floor(pending - doneVideosCount(j)))
}

function batchPlannedVideosEstimate(batch: BatchJob): number | null {
  // If every jurisdiction run reported stats.pending, we can be exact.
  // Otherwise, estimate remaining jurisdictions as N videos each.
  const cfgN = Number((batch.config as any)?.n)
  const n = Number.isFinite(cfgN) && cfgN > 0 ? Math.floor(cfgN) : null

  let pendingKnown = 0
  let pendingKnownCount = 0
  for (const j of batch.jurisdictions || []) {
    const p = Number(j.stats?.pending)
    if (!Number.isFinite(p) || p < 0) continue
    pendingKnown += Math.floor(p)
    pendingKnownCount += 1
  }

  const totalJurisdictions =
    Number((batch.summary as any)?.total_jurisdictions) ||
    Number((batch.config as any)?.total_jurisdictions) ||
    0

  if (pendingKnownCount > 0 && pendingKnownCount === (batch.jurisdictions || []).length) {
    return pendingKnown
  }

  if (!n || totalJurisdictions <= 0) {
    // We don't have enough info to estimate total work.
    return pendingKnownCount > 0 ? pendingKnown : null
  }

  const unknown = Math.max(0, totalJurisdictions - pendingKnownCount)
  return pendingKnown + unknown * n
}

/** Remaining videos across a batch (exact when pending counts exist; else estimated from N). */
export function remainingVideosForBatch(batch: BatchJob): number | null {
  const planned = batchPlannedVideosEstimate(batch)
  if (planned == null) return null

  let done = 0
  for (const j of batch.jurisdictions || []) {
    done += doneVideosCount(j)
  }
  return Math.max(0, Math.floor(planned - done))
}

/** Remaining videos across all running batches (best-effort). */
export function remainingVideosForRunningBatches(batches: BatchJob[]): number | null {
  let total = 0
  let any = false
  for (const b of batches) {
    if (b.status !== 'running') continue
    const rem = remainingVideosForBatch(b)
    if (rem == null) continue
    any = true
    total += rem
  }
  return any ? total : null
}

export function aggregateRunningFileTiming(batches: BatchJob[]): {
  avgSecondsPerFile: number | null
  activeVideo: ActiveVideoRun | null
  runningBatchCount: number
  idleRunningBatchCount: number
} {
  let totalElapsed = 0
  let totalFiles = 0
  let activeVideo: ActiveVideoRun | null = null
  let activeStarted = 0

  let runningBatchCount = 0
  let idleRunningBatchCount = 0

  for (const batch of batches) {
    if (batch.status !== 'running') continue
    runningBatchCount += 1
    const s = batch.summary || {}
    const elapsed = Number(s.elapsed_seconds) || 0
    const files =
      Number(s.files_processed) ||
      (Number(s.videos_ok) || 0) +
        (Number(s.videos_fail) || 0) +
        (Number(s.videos_tombstoned) || 0) +
        (Number(s.videos_empty) || 0) +
        (Number(s.videos_rate_limit) || 0)
    totalElapsed += elapsed
    totalFiles += files

    const run = resolveRunningFileClock(batch)
    if (!run) {
      idleRunningBatchCount += 1
      continue
    }
    const t = parseApiDateTime(run.startedAt)?.getTime() ?? 0
    if (!activeVideo || t >= activeStarted) {
      activeVideo = run
      activeStarted = t
    }
  }

  return {
    avgSecondsPerFile:
      totalFiles > 0 && totalElapsed > 0 ? totalElapsed / totalFiles : null,
    activeVideo,
    runningBatchCount,
    idleRunningBatchCount,
  }
}

/** Live counter for the in-flight video (ticks every second while enabled). */
export function useTickingSeconds(
  iso: string | null | undefined,
  enabled: boolean,
): number | null {
  const [now, setNow] = useState(() => Date.now())
  useEffect(() => {
    if (!enabled || !iso) return
    setNow(Date.now())
    const id = window.setInterval(() => setNow(Date.now()), 1000)
    return () => window.clearInterval(id)
  }, [enabled, iso])
  if (!enabled || !iso) return null
  return secondsSince(iso, now)
}