@milabench/bujang 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +24 -1
- package/dist/context.d.ts +1 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3364 -2835
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +45 -1
- package/dist/views/JobDataOverview.d.ts +9 -0
- package/dist/views/JobMarks.d.ts +17 -0
- package/package.json +5 -2
- package/src/api.ts +59 -0
- package/src/context.tsx +1 -15
- package/src/index.ts +2 -1
- package/src/types.ts +43 -1
- package/src/views/JobDataOverview.tsx +594 -0
- package/src/views/JobLogs.tsx +47 -162
- package/src/views/JobMarks.tsx +233 -0
- package/src/views/JobsView.tsx +36 -35
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Alert,
|
|
4
|
+
Badge,
|
|
5
|
+
Box,
|
|
6
|
+
Button,
|
|
7
|
+
Card,
|
|
8
|
+
Checkbox,
|
|
9
|
+
Code,
|
|
10
|
+
Flex,
|
|
11
|
+
HStack,
|
|
12
|
+
NativeSelect,
|
|
13
|
+
Spinner,
|
|
14
|
+
Table,
|
|
15
|
+
Text,
|
|
16
|
+
VStack,
|
|
17
|
+
Wrap,
|
|
18
|
+
} from '@chakra-ui/react';
|
|
19
|
+
import { useQuery } from '@tanstack/react-query';
|
|
20
|
+
import { LuCopy, LuRefreshCw } from 'react-icons/lu';
|
|
21
|
+
import { getJobData, getJobDataRows, jobDataRowsUrl } from '../api';
|
|
22
|
+
import type { ApiError, JobData, JobDataField } from '../types';
|
|
23
|
+
import { MonacoEditor } from '../components/MonacoEditor';
|
|
24
|
+
import { useColorMode } from '../hooks/useColorMode';
|
|
25
|
+
import { toaster } from '../toast';
|
|
26
|
+
|
|
27
|
+
/** Rows fetched for a chart (the server caps at 500k) */
|
|
28
|
+
const ROW_LIMIT = 50_000;
|
|
29
|
+
/** Fields every row has: the file it comes from */
|
|
30
|
+
const FILE_FIELDS: JobDataField[] = [
|
|
31
|
+
{ name: '_name', type: 'string', count: 0 },
|
|
32
|
+
{ name: '_file', type: 'string', count: 0 },
|
|
33
|
+
];
|
|
34
|
+
const MARKS = ['point', 'line', 'bar', 'tick', 'boxplot', 'area', 'rect'] as const;
|
|
35
|
+
const AGGREGATES = ['', 'mean', 'median', 'sum', 'min', 'max', 'count'] as const;
|
|
36
|
+
/** The envelope events that are not data (the command, its output lines) */
|
|
37
|
+
const NOISE_EVENTS = new Set(['line', 'config', 'meta', 'start', 'end', 'stop', 'error']);
|
|
38
|
+
|
|
39
|
+
interface Builder {
|
|
40
|
+
mark: (typeof MARKS)[number];
|
|
41
|
+
x: string;
|
|
42
|
+
y: string;
|
|
43
|
+
yAggregate: (typeof AGGREGATES)[number];
|
|
44
|
+
color: string;
|
|
45
|
+
column: string;
|
|
46
|
+
row: string;
|
|
47
|
+
logX: boolean;
|
|
48
|
+
logY: boolean;
|
|
49
|
+
/** the number scales start at 0 (always for bar / area, whose length means something) */
|
|
50
|
+
zero: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const EMPTY_BUILDER: Builder = {
|
|
54
|
+
mark: 'point', x: '', y: '', yAggregate: '', color: '', column: '', row: '', logX: false, logY: false, zero: false,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const vlType = (field?: JobDataField) => (field?.type === 'number' ? 'quantitative' : 'nominal');
|
|
58
|
+
|
|
59
|
+
/** A number field holding unix seconds (time, timestamp): shown as dates */
|
|
60
|
+
const isUnixSeconds = (field?: JobDataField) =>
|
|
61
|
+
!!field && field.type === 'number' && /time|stamp|date/i.test(field.name) &&
|
|
62
|
+
(field.min ?? 0) > 1e9 && (field.max ?? 0) < 1e11;
|
|
63
|
+
|
|
64
|
+
/** datum["a"]["b"] of a dotted field, for a Vega expression */
|
|
65
|
+
const datumOf = (name: string) => `datum${name.split('.').map((p) => `[${JSON.stringify(p)}]`).join('')}`;
|
|
66
|
+
|
|
67
|
+
/** Vega-Lite spec of the builder; the rows are the named dataset "rows" */
|
|
68
|
+
const buildSpec = (b: Builder, fields: Map<string, JobDataField>): Record<string, unknown> => {
|
|
69
|
+
const encoding: Record<string, unknown> = {};
|
|
70
|
+
const transform: Record<string, unknown>[] = [];
|
|
71
|
+
const keepsZero = b.zero || b.mark === 'bar' || b.mark === 'area';
|
|
72
|
+
const channel = (name: string, extra: Record<string, unknown> = {}) => {
|
|
73
|
+
const field = fields.get(name);
|
|
74
|
+
if (isUnixSeconds(field)) {
|
|
75
|
+
// seconds -> a date (Vega times are milliseconds)
|
|
76
|
+
const as = `${name.replace(/\./g, '_')}_date`;
|
|
77
|
+
if (!transform.some((t) => t.as === as)) transform.push({ calculate: `${datumOf(name)} * 1000`, as });
|
|
78
|
+
return { field: as, type: 'temporal', title: name, ...extra };
|
|
79
|
+
}
|
|
80
|
+
const type = vlType(field);
|
|
81
|
+
const scale = { ...(type === 'quantitative' && !keepsZero ? { zero: false } : {}), ...((extra.scale as object) || {}) };
|
|
82
|
+
return { field: name, type, ...extra, ...(Object.keys(scale).length ? { scale } : {}) };
|
|
83
|
+
};
|
|
84
|
+
if (b.x) encoding.x = channel(b.x, b.logX ? { scale: { type: 'log' } } : {});
|
|
85
|
+
if (b.y || b.yAggregate === 'count') {
|
|
86
|
+
encoding.y = b.yAggregate === 'count'
|
|
87
|
+
? { aggregate: 'count', type: 'quantitative' }
|
|
88
|
+
: channel(b.y, {
|
|
89
|
+
...(b.yAggregate ? { aggregate: b.yAggregate } : {}),
|
|
90
|
+
...(b.logY ? { scale: { type: 'log' } } : {}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (b.color) encoding.color = channel(b.color, { legend: { labelLimit: 400 } });
|
|
94
|
+
if (b.column) encoding.column = channel(b.column);
|
|
95
|
+
if (b.row) encoding.row = channel(b.row);
|
|
96
|
+
encoding.tooltip = [b.x, b.y, b.color, b.column, b.row, '_name']
|
|
97
|
+
.filter((f, i, all) => f && all.indexOf(f) === i)
|
|
98
|
+
.map((f) => ({ field: f, type: vlType(fields.get(f)) }));
|
|
99
|
+
return {
|
|
100
|
+
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
|
|
101
|
+
data: { name: 'rows' },
|
|
102
|
+
...(transform.length ? { transform } : {}),
|
|
103
|
+
// a composite mark (boxplot) takes no `tooltip` (Altair validates the spec)
|
|
104
|
+
mark: { type: b.mark, ...(b.mark === 'boxplot' ? {} : { tooltip: true }), ...(b.mark === 'line' ? { point: true } : {}) },
|
|
105
|
+
encoding,
|
|
106
|
+
width: b.column ? 260 : 'container',
|
|
107
|
+
height: 320,
|
|
108
|
+
// the legend and the axes inside the container width, not past it
|
|
109
|
+
...(b.column ? {} : { autosize: { type: 'fit-x', contains: 'padding' } }),
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/** Defaults for a set of fields: a category on x, a rate-like number on y, the file as color */
|
|
114
|
+
const guessBuilder = (fields: JobDataField[], multipleFiles: boolean): Builder => {
|
|
115
|
+
const numbers = fields.filter((f) => f.type === 'number' && f.name !== 'time');
|
|
116
|
+
// the categories with the most values first (test names before task: detail / train)
|
|
117
|
+
const categories = fields
|
|
118
|
+
.filter((f) => f.type === 'string' && f.name !== 'event' && f.values && f.values.length > 1 && f.values.length <= 40)
|
|
119
|
+
.sort((a, b) => (b.values?.length || 0) - (a.values?.length || 0));
|
|
120
|
+
const y = numbers.find((f) => /rate|throughput|loss|value/i.test(f.name)) || numbers[0];
|
|
121
|
+
const x = categories[0] || fields.find((f) => f.name === 'time') || numbers.find((f) => f !== y);
|
|
122
|
+
return {
|
|
123
|
+
...EMPTY_BUILDER,
|
|
124
|
+
mark: x?.type === 'string' ? 'boxplot' : 'point',
|
|
125
|
+
x: x?.name || '',
|
|
126
|
+
y: y?.name || '',
|
|
127
|
+
color: multipleFiles ? '_name' : categories[1]?.name || '',
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** The fields a spec reads (to fetch only those): every `field` of it */
|
|
132
|
+
const specFields = (spec: unknown, out = new Set<string>()): Set<string> => {
|
|
133
|
+
if (Array.isArray(spec)) spec.forEach((item) => specFields(item, out));
|
|
134
|
+
else if (spec && typeof spec === 'object') {
|
|
135
|
+
for (const [key, value] of Object.entries(spec)) {
|
|
136
|
+
if (key === 'field' && typeof value === 'string') out.add(value);
|
|
137
|
+
else specFields(value, out);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const isDarkPage = () =>
|
|
144
|
+
typeof document !== 'undefined' &&
|
|
145
|
+
(document.documentElement.classList.contains('dark') || document.documentElement.dataset.theme === 'dark');
|
|
146
|
+
|
|
147
|
+
/** A Vega-Lite spec rendered with vega-embed (loaded on first use), `rows` as the dataset "rows" */
|
|
148
|
+
const VegaChart: React.FC<{ spec: Record<string, unknown>; rows: Record<string, unknown>[] }> = ({ spec, rows }) => {
|
|
149
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
150
|
+
const [error, setError] = useState<string | null>(null);
|
|
151
|
+
const { colorMode } = useColorMode();
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
let cancelled = false;
|
|
155
|
+
let finalize: (() => void) | undefined;
|
|
156
|
+
setError(null);
|
|
157
|
+
import('vega-embed')
|
|
158
|
+
.then(async ({ default: embed }) => {
|
|
159
|
+
if (cancelled || !ref.current) return;
|
|
160
|
+
const full = {
|
|
161
|
+
...spec,
|
|
162
|
+
datasets: { ...((spec.datasets as object) || {}), rows },
|
|
163
|
+
background: 'transparent',
|
|
164
|
+
};
|
|
165
|
+
const result = await embed(ref.current, full as never, {
|
|
166
|
+
actions: { export: true, source: false, compiled: false, editor: false },
|
|
167
|
+
theme: colorMode === 'dark' || isDarkPage() ? 'dark' : undefined,
|
|
168
|
+
// canvas: the CSS reset of the page (font: inherit) would restyle the SVG texts Vega measured
|
|
169
|
+
renderer: 'canvas',
|
|
170
|
+
});
|
|
171
|
+
if (cancelled) result.finalize();
|
|
172
|
+
else finalize = result.finalize;
|
|
173
|
+
})
|
|
174
|
+
.catch((err: unknown) => !cancelled && setError(err instanceof Error ? err.message : String(err)));
|
|
175
|
+
return () => {
|
|
176
|
+
cancelled = true;
|
|
177
|
+
finalize?.();
|
|
178
|
+
};
|
|
179
|
+
}, [spec, rows, colorMode]);
|
|
180
|
+
|
|
181
|
+
return (
|
|
182
|
+
<Box w="100%" minH="340px">
|
|
183
|
+
{error && (
|
|
184
|
+
<Alert.Root status="error" mb={2}>
|
|
185
|
+
<Alert.Indicator />
|
|
186
|
+
<Alert.Content>
|
|
187
|
+
<Alert.Title>Chart error</Alert.Title>
|
|
188
|
+
<Alert.Description fontFamily="mono" fontSize="xs">{error}</Alert.Description>
|
|
189
|
+
</Alert.Content>
|
|
190
|
+
</Alert.Root>
|
|
191
|
+
)}
|
|
192
|
+
<Box ref={ref} w="100%" />
|
|
193
|
+
</Box>
|
|
194
|
+
);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const FieldSelect: React.FC<{
|
|
198
|
+
label: string;
|
|
199
|
+
value: string;
|
|
200
|
+
onChange: (value: string) => void;
|
|
201
|
+
fields: JobDataField[];
|
|
202
|
+
allowEmpty?: boolean;
|
|
203
|
+
}> = ({ label, value, onChange, fields, allowEmpty = true }) => (
|
|
204
|
+
<VStack align="stretch" gap={0}>
|
|
205
|
+
<Text fontSize="xs" color="var(--color-text-muted)">{label}</Text>
|
|
206
|
+
<NativeSelect.Root size="sm" w="190px">
|
|
207
|
+
<NativeSelect.Field value={value} onChange={(e) => onChange(e.target.value)}>
|
|
208
|
+
{allowEmpty && <option value="">—</option>}
|
|
209
|
+
{fields.map((f) => (
|
|
210
|
+
<option key={f.name} value={f.name}>{f.name} ({f.type})</option>
|
|
211
|
+
))}
|
|
212
|
+
</NativeSelect.Field>
|
|
213
|
+
<NativeSelect.Indicator />
|
|
214
|
+
</NativeSelect.Root>
|
|
215
|
+
</VStack>
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const formatRange = (f: JobDataField) => {
|
|
219
|
+
if (f.type === 'number') {
|
|
220
|
+
const fmt = (v?: number) => (v === undefined ? '' : Math.abs(v) >= 1e6 || (v !== 0 && Math.abs(v) < 1e-3) ? v.toExponential(3) : +v.toFixed(4));
|
|
221
|
+
return `${fmt(f.min)} … ${fmt(f.max)}`;
|
|
222
|
+
}
|
|
223
|
+
if (f.values === null) return 'many values';
|
|
224
|
+
if (f.values) return f.values.slice(0, 8).join(', ') + (f.values.length > 8 ? `, … (${f.values.length})` : '');
|
|
225
|
+
return '';
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The Overview tab of a job: the JSONL files of its artifact folders (output/, runs/, results/)
|
|
230
|
+
* plotted with Vega-Lite. A builder writes the spec; the spec can be edited by hand, and is shown
|
|
231
|
+
* with the rows URL as an Altair snippet.
|
|
232
|
+
*/
|
|
233
|
+
export const JobDataOverview: React.FC<{ jrJobId: string }> = ({ jrJobId }) => {
|
|
234
|
+
const { data, isLoading, error, refetch, isFetching } = useQuery<JobData, ApiError>({
|
|
235
|
+
queryKey: ['bujang-job-data', jrJobId],
|
|
236
|
+
queryFn: () => getJobData(jrJobId),
|
|
237
|
+
staleTime: 30_000,
|
|
238
|
+
retry: false,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const files = useMemo(() => (data?.files || []).filter((f) => !f.error), [data]);
|
|
242
|
+
const [selectedFiles, setSelectedFiles] = useState<string[] | null>(null);
|
|
243
|
+
const activeFiles = useMemo(
|
|
244
|
+
() => files.filter((f) => selectedFiles === null || selectedFiles.includes(f.path)),
|
|
245
|
+
[files, selectedFiles],
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
// events of the selected files, data events first
|
|
249
|
+
const events = useMemo(() => {
|
|
250
|
+
const counts = new Map<string, number>();
|
|
251
|
+
activeFiles.forEach((f) => f.events?.forEach((e) => counts.set(e.event, (counts.get(e.event) || 0) + e.count)));
|
|
252
|
+
return [...counts.entries()].sort(
|
|
253
|
+
(a, b) => Number(NOISE_EVENTS.has(a[0])) - Number(NOISE_EVENTS.has(b[0])) || b[1] - a[1],
|
|
254
|
+
);
|
|
255
|
+
}, [activeFiles]);
|
|
256
|
+
const [event, setEvent] = useState<string | null>(null);
|
|
257
|
+
const activeEvent = event !== null && events.some(([e]) => e === event) ? event : events[0]?.[0] ?? '';
|
|
258
|
+
|
|
259
|
+
// fields of the event, merged over the selected files
|
|
260
|
+
const fields = useMemo(() => {
|
|
261
|
+
const merged = new Map<string, JobDataField>();
|
|
262
|
+
activeFiles.forEach((f) =>
|
|
263
|
+
f.events?.filter((e) => e.event === activeEvent).forEach((e) =>
|
|
264
|
+
e.fields.forEach((field) => {
|
|
265
|
+
const prev = merged.get(field.name);
|
|
266
|
+
if (!prev) {
|
|
267
|
+
merged.set(field.name, { ...field, values: field.values ? [...field.values] : field.values });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
prev.count += field.count;
|
|
271
|
+
if (prev.type !== field.type) prev.type = 'mixed';
|
|
272
|
+
if (field.min !== undefined) prev.min = Math.min(prev.min ?? field.min, field.min);
|
|
273
|
+
if (field.max !== undefined) prev.max = Math.max(prev.max ?? field.max, field.max);
|
|
274
|
+
if (prev.values && field.values) prev.values = [...new Set([...prev.values, ...field.values])].sort();
|
|
275
|
+
else if (field.values === null) prev.values = null;
|
|
276
|
+
}),
|
|
277
|
+
),
|
|
278
|
+
);
|
|
279
|
+
return [...merged.values()];
|
|
280
|
+
}, [activeFiles, activeEvent]);
|
|
281
|
+
const fieldMap = useMemo(() => new Map([...fields, ...FILE_FIELDS].map((f) => [f.name, f])), [fields]);
|
|
282
|
+
const fieldOptions = useMemo(() => [...fields, ...FILE_FIELDS], [fields]);
|
|
283
|
+
|
|
284
|
+
// builder: guessed again when the event or the files change, until edited
|
|
285
|
+
const [builder, setBuilder] = useState<Builder>(EMPTY_BUILDER);
|
|
286
|
+
const guessKey = `${activeEvent}|${activeFiles.map((f) => f.path).join(',')}`;
|
|
287
|
+
const lastGuess = useRef<string>('');
|
|
288
|
+
useEffect(() => {
|
|
289
|
+
if (fields.length && lastGuess.current !== guessKey) {
|
|
290
|
+
lastGuess.current = guessKey;
|
|
291
|
+
setBuilder(guessBuilder(fields, activeFiles.length > 1));
|
|
292
|
+
}
|
|
293
|
+
}, [guessKey, fields, activeFiles.length]);
|
|
294
|
+
|
|
295
|
+
// the spec: from the builder, or edited by hand
|
|
296
|
+
const builtSpec = useMemo(() => buildSpec(builder, fieldMap), [builder, fieldMap]);
|
|
297
|
+
const [specText, setSpecText] = useState<string | null>(null);
|
|
298
|
+
const { spec, specError } = useMemo(() => {
|
|
299
|
+
if (specText === null) return { spec: builtSpec, specError: null };
|
|
300
|
+
try {
|
|
301
|
+
return { spec: JSON.parse(specText) as Record<string, unknown>, specError: null };
|
|
302
|
+
} catch (err) {
|
|
303
|
+
return { spec: null, specError: err instanceof Error ? err.message : String(err) };
|
|
304
|
+
}
|
|
305
|
+
}, [specText, builtSpec]);
|
|
306
|
+
// the chart keeps the last valid spec while the one typed does not parse
|
|
307
|
+
const [lastSpec, setLastSpec] = useState<Record<string, unknown> | null>(null);
|
|
308
|
+
useEffect(() => { if (spec) setLastSpec(spec); }, [spec]);
|
|
309
|
+
|
|
310
|
+
// rows: only the fields of the builder's spec; a spec edited by hand gets whole rows (its
|
|
311
|
+
// transforms may read any field)
|
|
312
|
+
const rowsQuery = useMemo(() => {
|
|
313
|
+
const byBuilder = specText === null;
|
|
314
|
+
// the fields of the data (not the ones the spec computes: time_date)
|
|
315
|
+
const wanted = byBuilder && lastSpec
|
|
316
|
+
? [...specFields(lastSpec)].filter((f) => !f.startsWith('_') && fieldMap.has(f))
|
|
317
|
+
: [];
|
|
318
|
+
// only the rows with the x and y of the builder: the others (gpudata, ...) would fill the limit
|
|
319
|
+
const required = byBuilder
|
|
320
|
+
? [builder.x, builder.yAggregate === 'count' ? '' : builder.y].filter((f) => f && !f.startsWith('_') && fieldMap.has(f))
|
|
321
|
+
: [];
|
|
322
|
+
return {
|
|
323
|
+
files: selectedFiles === null ? undefined : activeFiles.map((f) => f.path),
|
|
324
|
+
events: activeEvent ? [activeEvent] : undefined,
|
|
325
|
+
fields: wanted.length ? wanted : undefined,
|
|
326
|
+
require: required.length ? required : undefined,
|
|
327
|
+
limit: ROW_LIMIT,
|
|
328
|
+
};
|
|
329
|
+
}, [lastSpec, specText, builder, fieldMap, selectedFiles, activeFiles, activeEvent]);
|
|
330
|
+
const { data: rows, isFetching: rowsLoading, error: rowsError } = useQuery<Record<string, unknown>[], ApiError>({
|
|
331
|
+
queryKey: ['bujang-job-data-rows', jrJobId, rowsQuery],
|
|
332
|
+
queryFn: () => getJobDataRows(jrJobId, rowsQuery),
|
|
333
|
+
enabled: !!data && activeFiles.length > 0 && !!lastSpec,
|
|
334
|
+
staleTime: 30_000,
|
|
335
|
+
placeholderData: (previous) => previous,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const rowsUrl = useMemo(() => new URL(jobDataRowsUrl(jrJobId, rowsQuery), window.location.href).toString(), [jrJobId, rowsQuery]);
|
|
339
|
+
const altair = useMemo(() => {
|
|
340
|
+
if (!lastSpec) return '';
|
|
341
|
+
const { datasets: _datasets, ...rest } = lastSpec as Record<string, unknown>;
|
|
342
|
+
const forAltair = { ...rest, data: { url: rowsUrl, format: { type: 'json' } } };
|
|
343
|
+
return `import altair as alt\n\nchart = alt.Chart.from_dict(${JSON.stringify(forAltair, null, 4)
|
|
344
|
+
.replace(/\btrue\b/g, 'True').replace(/\bfalse\b/g, 'False').replace(/\bnull\b/g, 'None')})\nchart`;
|
|
345
|
+
}, [lastSpec, rowsUrl]);
|
|
346
|
+
|
|
347
|
+
const copy = (text: string, what: string) => {
|
|
348
|
+
navigator.clipboard?.writeText(text).then(
|
|
349
|
+
() => toaster.create({ title: `${what} copied`, type: 'success', duration: 2000 }),
|
|
350
|
+
() => toaster.create({ title: `Could not copy ${what}`, type: 'error' }),
|
|
351
|
+
);
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
const setB = (patch: Partial<Builder>) => {
|
|
355
|
+
setSpecText(null);
|
|
356
|
+
setBuilder((b) => ({ ...b, ...patch }));
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
if (isLoading) {
|
|
360
|
+
return (
|
|
361
|
+
<VStack py={10}><Spinner size="lg" /><Text>Reading the data files…</Text></VStack>
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
if (error) {
|
|
365
|
+
return (
|
|
366
|
+
<Alert.Root status="error">
|
|
367
|
+
<Alert.Indicator />
|
|
368
|
+
<Alert.Content>
|
|
369
|
+
<Alert.Title>Could not list the data files</Alert.Title>
|
|
370
|
+
<Alert.Description>{error.message}</Alert.Description>
|
|
371
|
+
</Alert.Content>
|
|
372
|
+
</Alert.Root>
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
if (!files.length) {
|
|
376
|
+
return (
|
|
377
|
+
<Card.Root bg="var(--color-bg-card)" border="1px solid" borderColor="var(--color-border)">
|
|
378
|
+
<Card.Body>
|
|
379
|
+
<Text color="var(--color-text-muted)">
|
|
380
|
+
No JSONL data (*.jsonl, *.ndjson, *.data) in {(data?.folders || ['output']).map((f) => `${f}/`).join(', ')} of this job.
|
|
381
|
+
A job writes its results in <Code>output/</Code>.
|
|
382
|
+
</Text>
|
|
383
|
+
<Button mt={3} size="sm" variant="outline" onClick={() => refetch()} loading={isFetching} alignSelf="start">
|
|
384
|
+
<LuRefreshCw /> Check again
|
|
385
|
+
</Button>
|
|
386
|
+
</Card.Body>
|
|
387
|
+
</Card.Root>
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const cardProps = { bg: 'var(--color-bg-card)', border: '1px solid', borderColor: 'var(--color-border)' } as const;
|
|
392
|
+
|
|
393
|
+
return (
|
|
394
|
+
<VStack align="stretch" gap={4} pb={6}>
|
|
395
|
+
{/* Files and event */}
|
|
396
|
+
<Card.Root {...cardProps}>
|
|
397
|
+
<Card.Body py={3}>
|
|
398
|
+
<Flex gap={6} wrap="wrap" align="start">
|
|
399
|
+
<VStack align="stretch" gap={1} flex="1" minW="300px">
|
|
400
|
+
<HStack justify="space-between">
|
|
401
|
+
<Text fontSize="sm" fontWeight="semibold">Files</Text>
|
|
402
|
+
<HStack gap={2}>
|
|
403
|
+
<Button size="2xs" variant="ghost" onClick={() => setSelectedFiles(null)}>all</Button>
|
|
404
|
+
<Button size="2xs" variant="ghost" onClick={() => setSelectedFiles([])}>none</Button>
|
|
405
|
+
<Button size="2xs" variant="ghost" onClick={() => refetch()} loading={isFetching} title="Read the files again">
|
|
406
|
+
<LuRefreshCw />
|
|
407
|
+
</Button>
|
|
408
|
+
</HStack>
|
|
409
|
+
</HStack>
|
|
410
|
+
<Box maxH="140px" overflowY="auto">
|
|
411
|
+
{files.map((f) => {
|
|
412
|
+
const checked = selectedFiles === null || selectedFiles.includes(f.path);
|
|
413
|
+
return (
|
|
414
|
+
<Checkbox.Root
|
|
415
|
+
key={f.path}
|
|
416
|
+
size="sm"
|
|
417
|
+
checked={checked}
|
|
418
|
+
display="flex"
|
|
419
|
+
onCheckedChange={(e) => {
|
|
420
|
+
const current = selectedFiles ?? files.map((x) => x.path);
|
|
421
|
+
setSelectedFiles(e.checked ? [...current, f.path] : current.filter((p) => p !== f.path));
|
|
422
|
+
}}
|
|
423
|
+
>
|
|
424
|
+
<Checkbox.HiddenInput />
|
|
425
|
+
<Checkbox.Control />
|
|
426
|
+
<Checkbox.Label fontFamily="mono" fontSize="xs">
|
|
427
|
+
{f.path} <Text as="span" color="var(--color-text-muted)">· {f.rows} rows</Text>
|
|
428
|
+
</Checkbox.Label>
|
|
429
|
+
</Checkbox.Root>
|
|
430
|
+
);
|
|
431
|
+
})}
|
|
432
|
+
</Box>
|
|
433
|
+
</VStack>
|
|
434
|
+
<VStack align="stretch" gap={1}>
|
|
435
|
+
<Text fontSize="sm" fontWeight="semibold">Event</Text>
|
|
436
|
+
<Wrap gap={1} maxW="360px">
|
|
437
|
+
{events.map(([name, count]) => (
|
|
438
|
+
<Button
|
|
439
|
+
key={name || '(none)'}
|
|
440
|
+
size="xs"
|
|
441
|
+
variant={name === activeEvent ? 'solid' : 'outline'}
|
|
442
|
+
onClick={() => { setEvent(name); setSpecText(null); }}
|
|
443
|
+
>
|
|
444
|
+
{name || '(records)'} <Badge size="xs" variant="subtle">{count}</Badge>
|
|
445
|
+
</Button>
|
|
446
|
+
))}
|
|
447
|
+
</Wrap>
|
|
448
|
+
</VStack>
|
|
449
|
+
</Flex>
|
|
450
|
+
</Card.Body>
|
|
451
|
+
</Card.Root>
|
|
452
|
+
|
|
453
|
+
{/* Chart builder + chart */}
|
|
454
|
+
<Card.Root {...cardProps}>
|
|
455
|
+
<Card.Body py={3}>
|
|
456
|
+
<Wrap gap={3} align="end" mb={3}>
|
|
457
|
+
<VStack align="stretch" gap={0}>
|
|
458
|
+
<Text fontSize="xs" color="var(--color-text-muted)">mark</Text>
|
|
459
|
+
<NativeSelect.Root size="sm" w="110px">
|
|
460
|
+
<NativeSelect.Field value={builder.mark} onChange={(e) => setB({ mark: e.target.value as Builder['mark'] })}>
|
|
461
|
+
{MARKS.map((m) => <option key={m} value={m}>{m}</option>)}
|
|
462
|
+
</NativeSelect.Field>
|
|
463
|
+
<NativeSelect.Indicator />
|
|
464
|
+
</NativeSelect.Root>
|
|
465
|
+
</VStack>
|
|
466
|
+
<FieldSelect label="x" value={builder.x} onChange={(x) => setB({ x })} fields={fieldOptions} />
|
|
467
|
+
<FieldSelect label="y" value={builder.y} onChange={(y) => setB({ y })} fields={fieldOptions} />
|
|
468
|
+
<VStack align="stretch" gap={0}>
|
|
469
|
+
<Text fontSize="xs" color="var(--color-text-muted)">y aggregate</Text>
|
|
470
|
+
<NativeSelect.Root size="sm" w="100px">
|
|
471
|
+
<NativeSelect.Field value={builder.yAggregate} onChange={(e) => setB({ yAggregate: e.target.value as Builder['yAggregate'] })}>
|
|
472
|
+
{AGGREGATES.map((a) => <option key={a} value={a}>{a || '—'}</option>)}
|
|
473
|
+
</NativeSelect.Field>
|
|
474
|
+
<NativeSelect.Indicator />
|
|
475
|
+
</NativeSelect.Root>
|
|
476
|
+
</VStack>
|
|
477
|
+
<FieldSelect label="color" value={builder.color} onChange={(color) => setB({ color })} fields={fieldOptions} />
|
|
478
|
+
<FieldSelect label="column" value={builder.column} onChange={(column) => setB({ column })} fields={fieldOptions} />
|
|
479
|
+
<FieldSelect label="row" value={builder.row} onChange={(row) => setB({ row })} fields={fieldOptions} />
|
|
480
|
+
<Checkbox.Root size="sm" checked={builder.logX} onCheckedChange={(e) => setB({ logX: !!e.checked })}>
|
|
481
|
+
<Checkbox.HiddenInput /><Checkbox.Control /><Checkbox.Label fontSize="xs">log x</Checkbox.Label>
|
|
482
|
+
</Checkbox.Root>
|
|
483
|
+
<Checkbox.Root size="sm" checked={builder.logY} onCheckedChange={(e) => setB({ logY: !!e.checked })}>
|
|
484
|
+
<Checkbox.HiddenInput /><Checkbox.Control /><Checkbox.Label fontSize="xs">log y</Checkbox.Label>
|
|
485
|
+
</Checkbox.Root>
|
|
486
|
+
<Checkbox.Root
|
|
487
|
+
size="sm"
|
|
488
|
+
checked={builder.zero || builder.mark === 'bar' || builder.mark === 'area'}
|
|
489
|
+
disabled={builder.mark === 'bar' || builder.mark === 'area'}
|
|
490
|
+
onCheckedChange={(e) => setB({ zero: !!e.checked })}
|
|
491
|
+
>
|
|
492
|
+
<Checkbox.HiddenInput /><Checkbox.Control /><Checkbox.Label fontSize="xs">include 0</Checkbox.Label>
|
|
493
|
+
</Checkbox.Root>
|
|
494
|
+
</Wrap>
|
|
495
|
+
<HStack gap={3} mb={2} fontSize="xs" color="var(--color-text-muted)">
|
|
496
|
+
{rowsLoading && <Spinner size="xs" />}
|
|
497
|
+
{rows && <Text>{rows.length}{rows.length >= ROW_LIMIT ? '+ (first rows only)' : ''} rows</Text>}
|
|
498
|
+
{specText !== null && <Badge size="xs" colorPalette="orange">spec edited by hand</Badge>}
|
|
499
|
+
</HStack>
|
|
500
|
+
{rowsError && (
|
|
501
|
+
<Alert.Root status="error" mb={2}>
|
|
502
|
+
<Alert.Indicator />
|
|
503
|
+
<Alert.Description>{rowsError.message}</Alert.Description>
|
|
504
|
+
</Alert.Root>
|
|
505
|
+
)}
|
|
506
|
+
{lastSpec && rows && <VegaChart spec={lastSpec} rows={rows} />}
|
|
507
|
+
</Card.Body>
|
|
508
|
+
</Card.Root>
|
|
509
|
+
|
|
510
|
+
{/* Spec + Altair */}
|
|
511
|
+
<Flex gap={4} direction={{ base: 'column', xl: 'row' }}>
|
|
512
|
+
<Card.Root {...cardProps} flex="1" minW={0}>
|
|
513
|
+
<Card.Header py={2}>
|
|
514
|
+
<HStack justify="space-between">
|
|
515
|
+
<Text fontSize="sm" fontWeight="semibold">Vega-Lite spec (data: the dataset "rows")</Text>
|
|
516
|
+
<HStack gap={1}>
|
|
517
|
+
{specText !== null && (
|
|
518
|
+
<Button size="xs" variant="ghost" onClick={() => setSpecText(null)}>
|
|
519
|
+
back to the builder
|
|
520
|
+
</Button>
|
|
521
|
+
)}
|
|
522
|
+
<Button size="xs" variant="ghost" onClick={() => copy(specText ?? JSON.stringify(builtSpec, null, 2), 'Spec')}>
|
|
523
|
+
<LuCopy />
|
|
524
|
+
</Button>
|
|
525
|
+
</HStack>
|
|
526
|
+
</HStack>
|
|
527
|
+
{specError && <Text fontSize="xs" color="red.500">{specError}</Text>}
|
|
528
|
+
</Card.Header>
|
|
529
|
+
<Card.Body pt={0}>
|
|
530
|
+
<MonacoEditor
|
|
531
|
+
value={specText ?? JSON.stringify(builtSpec, null, 2)}
|
|
532
|
+
onChange={(value) => setSpecText(value)}
|
|
533
|
+
language="json"
|
|
534
|
+
height="320px"
|
|
535
|
+
/>
|
|
536
|
+
</Card.Body>
|
|
537
|
+
</Card.Root>
|
|
538
|
+
<Card.Root {...cardProps} flex="1" minW={0}>
|
|
539
|
+
<Card.Header py={2}>
|
|
540
|
+
<HStack justify="space-between">
|
|
541
|
+
<Text fontSize="sm" fontWeight="semibold">Altair</Text>
|
|
542
|
+
<HStack gap={1}>
|
|
543
|
+
<Button size="xs" variant="ghost" onClick={() => copy(rowsUrl, 'Rows URL')}>rows URL</Button>
|
|
544
|
+
<Button size="xs" variant="ghost" onClick={() => copy(altair, 'Altair code')}><LuCopy /></Button>
|
|
545
|
+
</HStack>
|
|
546
|
+
</HStack>
|
|
547
|
+
</Card.Header>
|
|
548
|
+
<Card.Body pt={0}>
|
|
549
|
+
<Box as="pre" fontFamily="mono" fontSize="xs" overflow="auto" maxH="320px" p={2}
|
|
550
|
+
bg="var(--color-bg)" border="1px solid" borderColor="var(--color-border)" borderRadius="md">
|
|
551
|
+
{altair}
|
|
552
|
+
</Box>
|
|
553
|
+
</Card.Body>
|
|
554
|
+
</Card.Root>
|
|
555
|
+
</Flex>
|
|
556
|
+
|
|
557
|
+
{/* Fields */}
|
|
558
|
+
<Card.Root {...cardProps}>
|
|
559
|
+
<Card.Header py={2}>
|
|
560
|
+
<Text fontSize="sm" fontWeight="semibold">
|
|
561
|
+
Fields of {activeEvent ? <Code>{activeEvent}</Code> : 'the records'} · click to plot on y (number) or x
|
|
562
|
+
</Text>
|
|
563
|
+
</Card.Header>
|
|
564
|
+
<Card.Body pt={0} maxH="360px" overflowY="auto">
|
|
565
|
+
<Table.Root size="sm" variant="line">
|
|
566
|
+
<Table.Header>
|
|
567
|
+
<Table.Row>
|
|
568
|
+
<Table.ColumnHeader>field</Table.ColumnHeader>
|
|
569
|
+
<Table.ColumnHeader>type</Table.ColumnHeader>
|
|
570
|
+
<Table.ColumnHeader textAlign="end">count</Table.ColumnHeader>
|
|
571
|
+
<Table.ColumnHeader>range / values</Table.ColumnHeader>
|
|
572
|
+
</Table.Row>
|
|
573
|
+
</Table.Header>
|
|
574
|
+
<Table.Body>
|
|
575
|
+
{fields.map((f) => (
|
|
576
|
+
<Table.Row
|
|
577
|
+
key={f.name}
|
|
578
|
+
cursor="pointer"
|
|
579
|
+
_hover={{ bg: 'var(--color-bg-hover, rgba(127,127,127,0.08))' }}
|
|
580
|
+
onClick={() => setB(f.type === 'number' ? { y: f.name } : { x: f.name })}
|
|
581
|
+
>
|
|
582
|
+
<Table.Cell fontFamily="mono" fontSize="xs">{f.name}</Table.Cell>
|
|
583
|
+
<Table.Cell fontSize="xs">{f.type}</Table.Cell>
|
|
584
|
+
<Table.Cell fontSize="xs" textAlign="end">{f.count}</Table.Cell>
|
|
585
|
+
<Table.Cell fontSize="xs" color="var(--color-text-muted)">{formatRange(f)}</Table.Cell>
|
|
586
|
+
</Table.Row>
|
|
587
|
+
))}
|
|
588
|
+
</Table.Body>
|
|
589
|
+
</Table.Root>
|
|
590
|
+
</Card.Body>
|
|
591
|
+
</Card.Root>
|
|
592
|
+
</VStack>
|
|
593
|
+
);
|
|
594
|
+
};
|