@milabench/bujang 0.1.19 → 0.1.20

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.
@@ -31,7 +31,18 @@ import { SshConnectButton } from '../components/SshConnectDialog';
31
31
  import { JobsTable, fromActive, fromPersisted, mergeRows } from './JobsTable';
32
32
  import { SETTINGS_KEY } from './SettingsView';
33
33
 
34
- type JobsViewProps = Record<string, never>;
34
+ interface JobsViewProps {
35
+ /** organizer filters always applied, not shown in the filter bar ({ projects: 'my-repo' }) */
36
+ filters?: Record<string, string>;
37
+ /** the heading (default "Jobs") */
38
+ title?: React.ReactNode;
39
+ /** the browser tab title (default "Jobs") */
40
+ pageTitle?: string;
41
+ /** shown under the heading */
42
+ header?: React.ReactNode;
43
+ /** where "Submit New Job" goes (default the Submit page) */
44
+ submitPath?: string;
45
+ }
35
46
 
36
47
  const AUTO_REFRESH_KEY = 'bujang-jobs-auto-refresh';
37
48
  const AUTO_REFRESH_MS = 30_000;
@@ -103,8 +114,8 @@ const OrganizerFilters: React.FC<{
103
114
  );
104
115
  };
105
116
 
106
- export const JobsView: React.FC<JobsViewProps> = () => {
107
- usePageTitle('Jobs');
117
+ export const JobsView: React.FC<JobsViewProps> = ({ filters, title, pageTitle, header, submitPath }) => {
118
+ usePageTitle(pageTitle ?? 'Jobs');
108
119
 
109
120
  const queryClient = useQueryClient();
110
121
  const navigate = useNavigate();
@@ -181,7 +192,12 @@ export const JobsView: React.FC<JobsViewProps> = () => {
181
192
 
182
193
  // ?clusters=mila&experiments=lr-sweep: the jobs under every chosen value
183
194
  const [params, setParams] = useSearchParams();
184
- const organizerNames = React.useMemo(() => organizers.map((o) => o.name), [organizers]);
195
+ // the fixed filters are not offered in the filter bar
196
+ const shownOrganizers = React.useMemo(
197
+ () => organizers.filter((o) => !filters?.[o.name]),
198
+ [organizers, filters],
199
+ );
200
+ const organizerNames = React.useMemo(() => shownOrganizers.map((o) => o.name), [shownOrganizers]);
185
201
  const selected = React.useMemo(() => {
186
202
  const out: Record<string, string> = {};
187
203
  for (const name of organizerNames) {
@@ -191,14 +207,14 @@ export const JobsView: React.FC<JobsViewProps> = () => {
191
207
  return out;
192
208
  }, [params, organizerNames]);
193
209
  const allowed = React.useMemo(() => {
194
- const sets = Object.entries(selected).map(([name, value]) => {
210
+ const sets = Object.entries({ ...selected, ...filters }).map(([name, value]) => {
195
211
  const group = organizers.find((o) => o.name === name)?.groups.find((g) => g.value === value);
196
212
  return new Set<string>(group?.jobs ?? []);
197
213
  });
198
214
  if (sets.length === 0) return null;
199
215
  const [first, ...rest] = sets;
200
216
  return new Set<string>([...first].filter((id) => rest.every((set) => set.has(id))));
201
- }, [selected, organizers]);
217
+ }, [selected, filters, organizers]);
202
218
  const setFilter = (name: string, value: string) => {
203
219
  const next = new URLSearchParams(params);
204
220
  if (value) next.set(name, value);
@@ -331,7 +347,7 @@ export const JobsView: React.FC<JobsViewProps> = () => {
331
347
  >
332
348
  <LuStar fill={importantOnly ? 'currentColor' : 'none'} /> Important
333
349
  </Button>
334
- <OrganizerFilters organizers={organizers} selected={selected} onChange={setFilter} onClear={clearFilters} />
350
+ <OrganizerFilters organizers={shownOrganizers} selected={selected} onChange={setFilter} onClear={clearFilters} />
335
351
  </>
336
352
  );
337
353
 
@@ -346,14 +362,14 @@ export const JobsView: React.FC<JobsViewProps> = () => {
346
362
  <HStack justify="space-between" align="center">
347
363
  <HStack gap={3} align="center">
348
364
  <Heading size="xl" fontWeight="bold" color="var(--color-text)">
349
- Jobs
365
+ {title ?? 'Jobs'}
350
366
  </Heading>
351
367
  <SshConnectButton />
352
368
  </HStack>
353
369
 
354
370
  <HStack gap={3}>
355
371
  <Button
356
- onClick={() => navigate(paths.submitJob())}
372
+ onClick={() => navigate(submitPath ?? paths.submitJob())}
357
373
  size="sm"
358
374
  fontWeight="medium"
359
375
  bg="var(--color-primary)"
@@ -418,6 +434,7 @@ export const JobsView: React.FC<JobsViewProps> = () => {
418
434
  </HStack>
419
435
  </HStack>
420
436
  </Box>
437
+ {header}
421
438
 
422
439
 
423
440
  {merged ? (
@@ -0,0 +1,207 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Link, useParams, useSearchParams } from 'react-router-dom';
3
+ import { Box, Button, Heading, HStack, Input, NativeSelect, Spinner, Table, Text } from '@chakra-ui/react';
4
+ import { keepPreviousData, useQuery } from '@tanstack/react-query';
5
+ import { LuChevronLeft, LuChevronRight, LuSearch } from 'react-icons/lu';
6
+ import { getOrganizerValues } from '../api';
7
+ import type { ApiError, OrganizerValuesPage } from '../types';
8
+ import { ORGANIZER_PAGES } from '../components/OrganizerNav';
9
+ import { useBujangPaths } from '../context';
10
+ import { usePageTitle } from '../hooks/usePageTitle';
11
+ import { JobsView } from './JobsView';
12
+ import { CollectionOverview, CollectionTabs, useCollectionTab } from './CollectionView';
13
+
14
+ const PAGE_SIZES = [25, 50, 100];
15
+ type Sort = 'last' | 'name' | 'jobs';
16
+
17
+ const inputStyle = {
18
+ bg: 'var(--color-input-bg)',
19
+ borderColor: 'var(--color-input-border)',
20
+ color: 'var(--color-text)',
21
+ } as const;
22
+
23
+ const when = (ts: number | null) => (ts ? new Date(ts * 1000).toLocaleString() : '—');
24
+
25
+ function ago(ts: number | null): string {
26
+ if (!ts) return '—';
27
+ const s = Date.now() / 1000 - ts;
28
+ if (s < 60) return 'just now';
29
+ if (s < 3600) return `${Math.round(s / 60)} min ago`;
30
+ if (s < 86400) return `${Math.round(s / 3600)} h ago`;
31
+ return `${Math.round(s / 86400)} d ago`;
32
+ }
33
+
34
+ /** The value typed, given to the query a moment after the last key */
35
+ function useDebounced<T>(value: T, ms = 250): T {
36
+ const [out, setOut] = useState(value);
37
+ useEffect(() => {
38
+ const timer = window.setTimeout(() => setOut(value), ms);
39
+ return () => window.clearTimeout(timer);
40
+ }, [value, ms]);
41
+ return out;
42
+ }
43
+
44
+ /**
45
+ * Every experiment (or group): a table with their job count and last activity, searched by name and
46
+ * paginated on the server. The search, sort, page and page size are in the URL.
47
+ */
48
+ export const OrganizerValuesView: React.FC<{ organizer: keyof typeof ORGANIZER_PAGES }> = ({ organizer }) => {
49
+ const page = ORGANIZER_PAGES[organizer];
50
+ usePageTitle(page.label);
51
+ const paths = useBujangPaths();
52
+ const [params, setParams] = useSearchParams();
53
+ const [search, setSearch] = useState(params.get('q') ?? '');
54
+ const query = useDebounced(search.trim());
55
+ const sort = (['last', 'name', 'jobs'].includes(params.get('sort') ?? '') ? params.get('sort') : 'last') as Sort;
56
+ const size = PAGE_SIZES.includes(Number(params.get('size'))) ? Number(params.get('size')) : PAGE_SIZES[0];
57
+ const index = Math.max(0, Number(params.get('page')) || 0);
58
+
59
+ const update = (changes: Record<string, string | null>) => {
60
+ const next = new URLSearchParams(params);
61
+ for (const [k, v] of Object.entries(changes)) {
62
+ if (v === null || v === '') next.delete(k);
63
+ else next.set(k, v);
64
+ }
65
+ setParams(next, { replace: true });
66
+ };
67
+ // a new search starts on the first page
68
+ useEffect(() => {
69
+ if ((params.get('q') ?? '') !== query) update({ q: query, page: null });
70
+ // eslint-disable-next-line react-hooks/exhaustive-deps
71
+ }, [query]);
72
+
73
+ const { data, isLoading, isFetching, error } = useQuery<OrganizerValuesPage>({
74
+ queryKey: ['bujang-organizer-values', organizer, query, sort, index, size],
75
+ queryFn: () => getOrganizerValues(organizer, { search: query, sort, offset: index * size, limit: size }),
76
+ placeholderData: keepPreviousData,
77
+ staleTime: 15_000,
78
+ });
79
+ const total = data?.total ?? 0;
80
+ const pages = Math.max(1, Math.ceil(total / size));
81
+ const Icon = page.icon;
82
+
83
+ const header = (key: Sort, label: string, align?: 'end') => (
84
+ <Table.ColumnHeader textAlign={align} color="var(--color-text-muted)">
85
+ <Button size="xs" variant="ghost" px={1} fontWeight={sort === key ? 'bold' : 'semibold'}
86
+ color={sort === key ? 'var(--color-primary)' : 'var(--color-text-muted)'}
87
+ onClick={() => update({ sort: key === 'last' ? null : key, page: null })}>
88
+ {label}
89
+ </Button>
90
+ </Table.ColumnHeader>
91
+ );
92
+
93
+ return (
94
+ <Box p={6} maxW="1200px">
95
+ <HStack mb={2} gap={3} color="var(--color-text)">
96
+ <Icon size={24} />
97
+ <Heading size="lg">{page.label}</Heading>
98
+ {isFetching && <Spinner size="sm" />}
99
+ </HStack>
100
+ <Text mb={4} color="var(--color-text-muted)">
101
+ Every {page.singular} with its jobs and when they last changed. Open one to see its jobs.
102
+ </Text>
103
+ <HStack mb={3} gap={3} wrap="wrap">
104
+ <HStack flex="1" minW="240px" maxW="420px" gap={2}>
105
+ <LuSearch color="var(--color-text-muted)" />
106
+ <Input {...inputStyle} size="sm" placeholder={`Search the ${page.label.toLowerCase()}`} value={search}
107
+ onChange={(e) => setSearch(e.target.value)} aria-label={`Search the ${page.label.toLowerCase()}`} />
108
+ </HStack>
109
+ <Text fontSize="sm" color="var(--color-text-muted)">
110
+ {total} {total === 1 ? page.singular : page.label.toLowerCase()}
111
+ </Text>
112
+ </HStack>
113
+ {isLoading && <Spinner />}
114
+ {error && <Text color="var(--color-status-failed)">{(error as unknown as ApiError).message || String(error)}</Text>}
115
+ {data && (
116
+ <Box borderWidth="1px" borderColor="var(--color-border)" borderRadius="md" bg="var(--color-bg-card)" overflowX="auto">
117
+ <Table.Root size="sm">
118
+ <Table.Header>
119
+ <Table.Row bg="transparent">
120
+ {header('name', 'Name')}
121
+ {header('jobs', 'Jobs', 'end')}
122
+ {header('last', 'Last activity')}
123
+ <Table.ColumnHeader color="var(--color-text-muted)">First job</Table.ColumnHeader>
124
+ </Table.Row>
125
+ </Table.Header>
126
+ <Table.Body>
127
+ {data.values.length === 0 && (
128
+ <Table.Row bg="transparent">
129
+ <Table.Cell colSpan={4} color="var(--color-text-muted)" fontStyle="italic">
130
+ {query ? `No ${page.singular} matches "${query}".` : `No ${page.singular} yet.`}
131
+ </Table.Cell>
132
+ </Table.Row>
133
+ )}
134
+ {data.values.map((v) => (
135
+ <Table.Row key={v.value} bg="transparent" _hover={{ bg: 'var(--color-bg-hover)' }}>
136
+ <Table.Cell>
137
+ <Link to={page.detail(paths, v.value)}>
138
+ <Text color="var(--color-text)" fontWeight="medium" _hover={{ textDecoration: 'underline' }}>{v.value}</Text>
139
+ </Link>
140
+ </Table.Cell>
141
+ <Table.Cell textAlign="end" color="var(--color-text)">{v.jobs}</Table.Cell>
142
+ <Table.Cell color="var(--color-text)" title={when(v.last)}>{ago(v.last)}</Table.Cell>
143
+ <Table.Cell color="var(--color-text-muted)">{when(v.first)}</Table.Cell>
144
+ </Table.Row>
145
+ ))}
146
+ </Table.Body>
147
+ </Table.Root>
148
+ </Box>
149
+ )}
150
+ {data && total > 0 && (
151
+ <HStack mt={3} gap={2} justify="flex-end" wrap="wrap" color="var(--color-text-muted)" fontSize="sm">
152
+ <Text>rows</Text>
153
+ <NativeSelect.Root size="xs" w="70px">
154
+ <NativeSelect.Field {...inputStyle} value={size} onChange={(e) => update({ size: e.target.value, page: null })}>
155
+ {PAGE_SIZES.map((n) => <option key={n} value={n}>{n}</option>)}
156
+ </NativeSelect.Field>
157
+ <NativeSelect.Indicator />
158
+ </NativeSelect.Root>
159
+ <Text>
160
+ {index * size + 1}–{Math.min(total, (index + 1) * size)} of {total}
161
+ </Text>
162
+ <Button size="xs" variant="ghost" aria-label="Previous page" disabled={index === 0}
163
+ onClick={() => update({ page: index - 1 ? String(index - 1) : null })}>
164
+ <LuChevronLeft />
165
+ </Button>
166
+ <Text>page {index + 1} / {pages}</Text>
167
+ <Button size="xs" variant="ghost" aria-label="Next page" disabled={index + 1 >= pages}
168
+ onClick={() => update({ page: String(index + 1) })}>
169
+ <LuChevronRight />
170
+ </Button>
171
+ </HStack>
172
+ )}
173
+ </Box>
174
+ );
175
+ };
176
+
177
+ /** One experiment (or group): its jobs (the jobs page filtered on it), or their Overview (compared) */
178
+ export const OrganizerValueView: React.FC<{ organizer: keyof typeof ORGANIZER_PAGES }> = ({ organizer }) => {
179
+ const { value = '' } = useParams();
180
+ const page = ORGANIZER_PAGES[organizer];
181
+ const paths = useBujangPaths();
182
+ const [tab, setTab] = useCollectionTab();
183
+ const Icon = page.icon;
184
+ const title = <HStack gap={2} as="span"><Icon /> {value}</HStack>;
185
+ const header = (
186
+ <HStack mt={-3} gap={4} wrap="wrap">
187
+ <CollectionTabs tab={tab} onTab={setTab} />
188
+ <Link to={page.list(paths)}>
189
+ <Text fontSize="sm" color="var(--color-text-muted)" _hover={{ textDecoration: 'underline' }}>
190
+ ← every {page.singular}
191
+ </Text>
192
+ </Link>
193
+ </HStack>
194
+ );
195
+ if (tab === 'overview') {
196
+ return <CollectionOverview organizer={organizer} value={value} title={title} pageTitle={value} header={header} />;
197
+ }
198
+ return (
199
+ <JobsView
200
+ key={`${organizer}:${value}`}
201
+ filters={{ [organizer]: value }}
202
+ pageTitle={value}
203
+ title={title}
204
+ header={header}
205
+ />
206
+ );
207
+ };