Buckets:
| 'use client'; | |
| import { useEffect, useState, useRef, useCallback, useMemo } from 'react'; | |
| import { apiClient } from '@/utils/api'; | |
| import usePollLoop from '@/hooks/usePollLoop'; | |
| export interface LossPoint { | |
| step: number; | |
| wall_time?: number; | |
| value: number | null; | |
| } | |
| type SeriesMap = Record<string, LossPoint[]>; | |
| export default function useJobLossLog(jobID: string, reloadInterval: null | number = null) { | |
| const [series, setSeries] = useState<SeriesMap>({}); | |
| const [keys, setKeys] = useState<string[]>([]); | |
| const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error' | 'refreshing'>('idle'); | |
| const didInitialLoadRef = useRef(false); | |
| const inFlightRef = useRef(false); | |
| // track last step per key so polling is incremental per series | |
| const lastStepByKeyRef = useRef<Record<string, number | null>>({}); | |
| const lossKeys = useMemo(() => { | |
| const base = keys ?? []; | |
| // if keys table is empty early on, fall back to just "loss" | |
| if (base.length === 0) return ['loss']; | |
| return [...base].sort(); | |
| }, [keys]); | |
| const refreshLoss = useCallback(async () => { | |
| if (!jobID) return; | |
| if (inFlightRef.current) return; | |
| inFlightRef.current = true; | |
| const loadStatus: 'loading' | 'refreshing' = didInitialLoadRef.current ? 'refreshing' : 'loading'; | |
| setStatus(loadStatus); | |
| try { | |
| // Step 1: get key list (we can do this by calling endpoint once; it returns keys) | |
| // Keep it cheap: limit=1. | |
| const first = await apiClient | |
| .get(`/api/jobs/${jobID}/loss`, { params: { key: 'loss', limit: 1 } }) | |
| .then(res => res.data as { keys?: string[] }); | |
| const newKeys = first.keys ?? []; | |
| setKeys(newKeys); | |
| const wantedLossKeys = (newKeys.length ? [...newKeys] : ['loss']).sort(); | |
| // Step 2: fetch each loss key incrementally (since_step per key if polling) | |
| const requests = wantedLossKeys.map(k => { | |
| const params: Record<string, any> = { key: k }; | |
| if (reloadInterval && lastStepByKeyRef.current[k] != null) { | |
| params.since_step = lastStepByKeyRef.current[k]; | |
| } | |
| params.limit = 1000000; | |
| return apiClient | |
| .get(`/api/jobs/${jobID}/loss`, { params }) | |
| .then(res => res.data as { key: string; points?: LossPoint[] }); | |
| }); | |
| const results = await Promise.all(requests); | |
| setSeries(prev => { | |
| const next: SeriesMap = { ...prev }; | |
| for (const r of results) { | |
| const k = r.key; | |
| const newPoints = (r.points ?? []).filter(p => p.value !== null); | |
| if (!didInitialLoadRef.current) { | |
| // initial: replace | |
| next[k] = newPoints; | |
| } else if (newPoints.length) { | |
| const existing = next[k] ?? []; | |
| const prevLast = existing.length ? existing[existing.length - 1].step : null; | |
| const filtered = prevLast == null ? newPoints : newPoints.filter(p => p.step > prevLast); | |
| next[k] = filtered.length ? [...existing, ...filtered] : existing; | |
| } else { | |
| // no new points: keep existing | |
| next[k] = next[k] ?? []; | |
| } | |
| // update last step per key | |
| const finalArr = next[k] ?? []; | |
| lastStepByKeyRef.current[k] = finalArr.length | |
| ? finalArr[finalArr.length - 1].step | |
| : (lastStepByKeyRef.current[k] ?? null); | |
| } | |
| // remove stale keys that no longer exist (rare, but keeps UI clean) | |
| for (const existingKey of Object.keys(next)) { | |
| if (!wantedLossKeys.includes(existingKey)) { | |
| delete next[existingKey]; | |
| delete lastStepByKeyRef.current[existingKey]; | |
| } | |
| } | |
| return next; | |
| }); | |
| setStatus('success'); | |
| didInitialLoadRef.current = true; | |
| } catch (err) { | |
| console.error('Error fetching loss logs:', err); | |
| setStatus('error'); | |
| } finally { | |
| inFlightRef.current = false; | |
| } | |
| }, [jobID, reloadInterval]); | |
| // Delete every logged step in [minStep, maxStep] from the on-disk log, then | |
| // drop those points locally so the chart updates without a full reload. | |
| const deleteRange = useCallback( | |
| async (minStep: number, maxStep: number) => { | |
| if (!jobID) return; | |
| await apiClient.delete(`/api/jobs/${jobID}/loss`, { data: { min_step: minStep, max_step: maxStep } }); | |
| setSeries(prev => { | |
| const next: SeriesMap = {}; | |
| for (const k of Object.keys(prev)) { | |
| const kept = prev[k].filter(p => p.step < minStep || p.step > maxStep); | |
| next[k] = kept; | |
| // Re-anchor incremental polling to the new tail so a deleted tail | |
| // isn't treated as already-fetched. | |
| lastStepByKeyRef.current[k] = kept.length ? kept[kept.length - 1].step : null; | |
| } | |
| return next; | |
| }); | |
| }, | |
| [jobID], | |
| ); | |
| // reset when job changes. Declared before the poll loop so the reset runs | |
| // before the first fetch when jobID changes. | |
| useEffect(() => { | |
| didInitialLoadRef.current = false; | |
| lastStepByKeyRef.current = {}; | |
| setSeries({}); | |
| setKeys([]); | |
| setStatus('idle'); | |
| }, [jobID]); | |
| usePollLoop(refreshLoss, reloadInterval, [jobID]); | |
| return { series, keys, lossKeys, status, refreshLoss, deleteRange, setSeries }; | |
| } | |
Xet Storage Details
- Size:
- 5.3 kB
- Xet hash:
- 7afa1344882bc90d557848a00dea73b01c45528c1aad503bfca7b6f7222ce627
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.