Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions v5/client/src/api/machines.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useMemo } from 'react'
import { queryOptions, useQuery } from '@tanstack/react-query'
import { machineLabel } from '../labels'
import { api, unwrap, type Schemas } from './client'
import { queryKeys } from './keys'
import type { paths } from './schema'
Expand All @@ -9,6 +11,9 @@ type Machine = Schemas['Machine']
/** The longest name a machine can have (D5's column). */
export const MACHINE_NAME_LENGTH = 256

/** The longest display name a machine can have (D5). */
export const DISPLAY_NAME_LENGTH = 256

type MachineListQuery = NonNullable<
paths['/api/projects/{project}/suites/{suite}/machines']['get']['parameters']['query']
>
Expand All @@ -32,8 +37,6 @@ export function machinesQuery(suite: SuiteRef, query: MachineListQuery = {}) {
})
}

const namesOf = (machines: Machine[]) => machines.map((machine) => machine.name)

/** Whether `machines`, as the API listed them, include the machine `name`. */
export function hasMachine(machines: readonly Machine[], name: string): boolean {
return machines.some((machine) => machine.name === name)
Expand All @@ -43,16 +46,15 @@ export function hasMachine(machines: readonly Machine[], name: string): boolean
const RECENTLY_ACTIVE: MachineListQuery = { tracked: true, sort: '-last_run_at' }

/**
* The names of the `count` tracked machines that most recently had a run, most recent first:
* machines the user did not pick (D5), such as the Dashboard's (DA2). Machines without runs are
* left out.
* The `count` tracked machines that most recently had a run, most recent first: machines the user
* did not pick (D5), such as the Dashboard's (DA2). Machines without runs are left out.
*/
export function useRecentlyActiveMachines(suite: SuiteRef, count: number, enabled = true) {
return useQuery({
...machinesQuery(suite, RECENTLY_ACTIVE),
enabled,
select: (machines) =>
namesOf(machines.filter((machine) => machine.last_run_at !== null).slice(0, count)),
machines.filter((machine) => machine.last_run_at !== null).slice(0, count),
})
}

Expand All @@ -64,14 +66,39 @@ export function useMachineList(suite: SuiteRef, enabled = true) {
return useQuery({ ...machinesQuery(suite), enabled })
}

/** The label of the machine a name names (AR23). */
export type MachineLabels = (name: string) => string

// A plain object rather than a Map, so that the query keeps it, and the lookup below, the same
// from one fetch of the machines to the next while no label changes.
const labelsByName = (machines: readonly Machine[]): Record<string, string> =>
Object.fromEntries(machines.map((machine) => [machine.name, machineLabel(machine)]))

/** The lookup of `labels`, which names a machine they do not hold, if any, by its name. */
function lookup(labels: Record<string, string> | undefined): MachineLabels {
return (name) => (labels !== undefined && Object.hasOwn(labels, name) ? labels[name] : name)
}

/**
* The label of each of the suite's machines, by name (AR23), for what refers to a machine by its
* name alone, such as a run (I4). A machine is named by its name while the suite's machines are
* being fetched, if they cannot be, or if they do not include it: nothing waits for them, and
* their failure is not reported, since the name still names the machine.
*/
export function useMachineLabels(suite: SuiteRef, enabled = true): MachineLabels {
const { data } = useQuery({ ...machinesQuery(suite), enabled, select: labelsByName })
return useMemo(() => lookup(data), [data])
}

/** The query key of the machine `name`, which a change to it updates. */
export function machineKey(suite: SuiteRef, name: string) {
return [...queryKeys.machines(suite), 'detail', name] as const
}

/** The machine `name` (E2). */
export function useMachine(suite: SuiteRef, name: string) {
/** The machine `name` (E2), unless `enabled` is false. */
export function useMachine(suite: SuiteRef, name: string, enabled = true) {
return useQuery({
enabled,
queryKey: machineKey(suite, name),
queryFn: ({ signal }) =>
unwrap(
Expand Down
15 changes: 12 additions & 3 deletions v5/client/src/components/checked-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useMemo } from 'react'
import { skipToken, useQueries } from '@tanstack/react-query'
import { combineResults, type Failures } from '../api/failures'
import { machinesQuery } from '../api/machines'
import { machineLabel } from '../labels'
import { findSuite, fullName, suiteRef, type Suite, type SuiteRef } from '../api/suites'
import { commitLookupKey, commitLookupQuery } from './commit-lookup'

Expand Down Expand Up @@ -40,6 +41,8 @@ export interface CheckedCommit {
schema: Suite | undefined
/** Whether its machine is among its suite's, once they are listed. */
machineKnown: boolean | undefined
/** The label of its machine (AR23), or its name until its suite's machines are listed. */
machineLabel: string
/** Whether the machine has runs at the commit, once that is known. */
found: boolean | undefined
/** The display value of the commit (AR6), or the commit string until it is known. */
Expand Down Expand Up @@ -80,15 +83,20 @@ export function useCheckedCommits(
queries: named.map((suite) => ({ ...machinesQuery(suite), staleTime: Infinity })),
combine: combineResults,
})
const machineKnown = useMemo(
// Each one's machine, as its suite lists it: null if it does not, undefined until that is known.
const machines = useMemo(
() =>
distinct.map((entry, i) => {
if (schemas[i] === undefined) return undefined
const list = machineLists.data[named.indexOf(entry.suite)]
return list?.some((machine) => machine.name === entry.machine)
return list && (list.find((machine) => machine.name === entry.machine) ?? null)
}),
[distinct, schemas, named, machineLists.data],
)
const machineKnown = useMemo(
() => machines.map((machine) => (machine === undefined ? undefined : machine !== null)),
[machines],
)

const lookups = useQueries({
queries: distinct.map(({ suite, machine, commit }, i) => {
Expand All @@ -112,6 +120,7 @@ export function useCheckedCommits(
return {
schema: schemas[i],
machineKnown: machineKnown[i],
machineLabel: machines[i] ? machineLabel(machines[i]) : entry.machine,
found: lookup === undefined ? undefined : lookup !== null,
display: lookup?.text ?? entry.commit,
unusable:
Expand All @@ -120,7 +129,7 @@ export function useCheckedCommits(
lookup === null,
}
}),
[commits, at, suites, schemas, machineKnown, lookups.data],
[commits, at, suites, schemas, machines, machineKnown, lookups.data],
)
return {
states,
Expand Down
10 changes: 9 additions & 1 deletion v5/client/src/components/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import clsx from 'clsx'
import { errorMessage } from '../api/client'
import { FieldLabel } from './field-label'
import { Note } from './note'
import { Popover } from './popover'
import { sectioned } from './sectioned'
import type { Suggestion, Suggestions } from './suggestions'
Expand Down Expand Up @@ -324,7 +325,14 @@ function List({
className={clsx(shared.option, virtualized && styles.virtualOption)}
>
{/* Rendered once shown, rather than for every suggestion, as React Aria lists them all. */}
{renderSuggestion ? () => renderSuggestion(item) : item.text}
{renderSuggestion
? () => renderSuggestion(item)
: () => (
<>
{item.text}
{item.note !== undefined && <Note>{item.note}</Note>}
</>
)}
</ListBoxItem>
),
[renderSuggestion, virtualized],
Expand Down
20 changes: 20 additions & 0 deletions v5/client/src/components/machine-name.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { machineLabel, type MachineNames } from '../labels'
import { Note } from './note'

/**
* Where the user finds a machine among others, in a picker or on the Machines tab: its label,
* followed by its name in muted text when it has a display name (AR23).
*/
export function LabelWithName({ machine }: { machine: MachineNames }) {
return (
<>
{machineLabel(machine)}
<SecondaryName machine={machine} />
</>
)
}

/** The muted name that follows a machine's label when it has a display name (AR23), or nothing. */
export function SecondaryName({ machine }: { machine: MachineNames }) {
return machine.display_name === null ? null : <Note>{machine.name}</Note>
}
28 changes: 27 additions & 1 deletion v5/client/src/components/machine-suggestions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ const LISTED = [
]

describe('machine pickers (AR18)', () => {
it('order the tracked machines first, then the untracked ones, each as the API lists them', () => {
it('order the tracked machines first, then the untracked ones, each by label', () => {
expect(inPickerOrder(LISTED).map((m) => m.name)).toEqual([
'b-linux',
'd-macos',
'a-retired',
'c-O3',
])
// AR23: by label, case-insensitively, then by name.
const labelled = [
machine('x', { display_name: 'zeta' }),
machine('b', { display_name: 'Alpha' }),
machine('a', { display_name: 'alpha' }),
machine('m'),
]
expect(inPickerOrder(labelled).map((m) => m.name)).toEqual(['a', 'b', 'm', 'x'])
})

it('suggest the untracked machines under their heading, leaving out those excluded', () => {
Expand All @@ -26,4 +34,22 @@ describe('machine pickers (AR18)', () => {
{ key: 'a-retired', text: 'a-retired', section: 'Untracked' },
])
})

it('show a machine by its label, followed by its name, by which it is found too (AR23)', () => {
const named = [
machine('linux-1', { display_name: 'Linux / x86' }),
machine('linux-2', { display_name: 'Linux / x86', tracked: false }),
]
// Two machines with the same label stay two suggestions, told apart by their names.
expect(machineSuggestions(named)).toEqual([
{ key: 'linux-1', text: 'Linux / x86', aliases: ['linux-1'], note: 'linux-1' },
{
key: 'linux-2',
text: 'Linux / x86',
aliases: ['linux-2'],
note: 'linux-2',
section: 'Untracked',
},
])
})
})
34 changes: 22 additions & 12 deletions v5/client/src/components/machine-suggestions.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { useMemo } from 'react'
import { useMachineList } from '../api/machines'
import { useMachineLabels, useMachineList } from '../api/machines'
import type { SuiteRef } from '../api/suites'
import type { Schemas } from '../api/client'
import { compareMachines, machineLabel, type MachineNames } from '../labels'
import { useLocalSuggestions, type Suggestion } from './suggestions'

/** The heading the untracked machines are listed under in a machine picker (AR18). */
export const UNTRACKED_SECTION = 'Untracked'

/**
* `machines`, as listed by the API, in the order a machine picker offers them (AR18): the tracked
* ones first, then the untracked ones, each in the API's order, which is by name.
* `machines` in the order a machine picker offers them (AR18): the tracked ones first, then the
* untracked ones, each by label (AR23).
*/
export function inPickerOrder<M extends { tracked: boolean }>(machines: readonly M[]): M[] {
export function inPickerOrder<M extends MachineNames & { tracked: boolean }>(
machines: readonly M[],
): M[] {
const sorted = [...machines].sort(compareMachines)
return [
...machines.filter((machine) => machine.tracked),
...machines.filter((machine) => !machine.tracked),
...sorted.filter((machine) => machine.tracked),
...sorted.filter((machine) => !machine.tracked),
]
}

/**
* The suggestions of a combobox picking one of `machines`, but none of `exclude` (AR18): the
* tracked ones, then the untracked ones under their heading.
* tracked ones, then the untracked ones under their heading, each shown by its label, followed by
* its name when it has a display name, and found and picked by either (AR23).
*/
export function machineSuggestions(
machines: readonly Schemas['Machine'][],
Expand All @@ -30,23 +35,28 @@ export function machineSuggestions(
.filter((machine) => !exclude.includes(machine.name))
.map((machine) => ({
key: machine.name,
text: machine.name,
text: machineLabel(machine),
...(machine.display_name === null ? {} : { aliases: [machine.name], note: machine.name }),
...(machine.tracked ? {} : { section: UNTRACKED_SECTION }),
}))
}

/** The suite's machines, and the suggestions for a combobox that picks one of them. */
/**
* The suite's machines, the suggestions for a combobox that picks one of them, and their labels.
*/
export type Machines = ReturnType<typeof useMachineSuggestions>

/**
* The suite's machines, unless `enabled` is false, and the suggestions of a combobox picking one of
* them, filtered locally as the user types, since a suite has few machines (D5).
* The suite's machines, unless `enabled` is false, the suggestions of a combobox picking one of
* them, filtered locally as the user types, since a suite has few machines (D5), and the label of
* each, for the text of the one picked (AR23).
*/
export function useMachineSuggestions(suite: SuiteRef, enabled = true) {
const machines = useMachineList(suite, enabled)
const labelOfMachine = useMachineLabels(suite, enabled)
const suggestions = useLocalSuggestions(
useMemo(() => machines.data && machineSuggestions(machines.data), [machines.data]),
machines.error,
)
return { machines, suggestions }
return { machines, suggestions, labelOfMachine }
}
5 changes: 5 additions & 0 deletions v5/client/src/components/note.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* What is said of an option, or of a machine, after its label: muted, and plain after bold text. */
.note {
color: var(--lnt-muted);
font-weight: normal;
}
14 changes: 14 additions & 0 deletions v5/client/src/components/note.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import styles from './note.module.css'

/**
* Muted text said of what it follows, after a space, such as what a dropdown says of an option, or
* a machine's name after its label (AR23).
*/
export function Note({ children }: { children: string }) {
return (
<>
{' '}
<span className={styles.note}>{children}</span>
</>
)
}
21 changes: 13 additions & 8 deletions v5/client/src/components/run-columns.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Link } from 'react-router'
import type { Schemas } from '../api/client'
import { refOf, type Suite } from '../api/suites'
import type { MachineLabels } from '../api/machines'
import { refOf, type Suite, type SuiteRef } from '../api/suites'
import { formatTimestamp } from '../format'
import { machinePath, runPath } from '../paths'
import type { Commit } from '../schema'
Expand All @@ -12,19 +13,14 @@ type Run = Schemas['Run']

/**
* The columns a table of runs picks from, so that every such table shows runs the same way: the
* run, its machine and its commit, each linked to its own page, the commit with its card (AR21), and
* when it was measured or submitted.
* run and its commit, each linked to its own page, the commit with its card (AR21), and when it was
* measured or submitted. `machineColumn` adds the machine.
* `commits` resolves the commits' display values, and their cards' contents (see `fetchRunPage`).
*/
export function runColumns(schema: Suite, commits: Map<string, Commit>) {
const suite = refOf(schema)
return {
run: uuidColumn<Run>('run', 'Run', (uuid) => runPath(suite, uuid)),
machine: {
id: 'machine',
header: 'Machine',
cell: (run) => <Link to={machinePath(suite, run.machine)}>{run.machine}</Link>,
},
commit: {
id: 'commit',
header: 'Commit',
Expand All @@ -46,3 +42,12 @@ export function runColumns(schema: Suite, commits: Map<string, Commit>) {
},
} satisfies Record<string, Column<Run>>
}

/** The column of a table of runs naming each run's machine by its label (AR23), linking to it. */
export function machineColumn(suite: SuiteRef, labelOfMachine: MachineLabels): Column<Run> {
return {
id: 'machine',
header: 'Machine',
cell: (run) => <Link to={machinePath(suite, run.machine)}>{labelOfMachine(run.machine)}</Link>,
}
}
5 changes: 0 additions & 5 deletions v5/client/src/components/select.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,3 @@
.checkOption[data-disabled] input {
visibility: hidden;
}

/* What is said of an option after its label. */
.note {
color: var(--lnt-muted);
}
Loading