77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
// Merging workflow definitions with their run statistics.
|
|
//
|
|
// A run is evidence the workflow exists: anything with either a definition or
|
|
// statistics gets a row, so a workflow that has run but is no longer listed
|
|
// still appears.
|
|
|
|
/** A workflow's run statistics, as the stats endpoint reports them. */
|
|
export interface WorkflowStats {
|
|
workflow: string;
|
|
runs?: number;
|
|
merged?: number;
|
|
completed?: number;
|
|
avg_tokens?: number;
|
|
avg_steps?: number;
|
|
origin?: string;
|
|
}
|
|
|
|
/** A workflow definition, as the definitions endpoint reports it. */
|
|
export interface WorkflowDefinition {
|
|
id: string;
|
|
name?: string;
|
|
origin?: string;
|
|
enabled?: boolean;
|
|
/** The inputs the workflow declares, by name. A workflow that declares none
|
|
* is started without any, which is every workflow but pr-review today. */
|
|
inputs?: Record<string, { type: string; required?: boolean }>;
|
|
/** none, optional or required; a workflow that declares none needs a repository. */
|
|
repository?: string;
|
|
}
|
|
|
|
/** One table row: a definition, its statistics, or both. */
|
|
export interface WorkflowRow {
|
|
id: string;
|
|
name?: string;
|
|
workflow: string;
|
|
runs?: number;
|
|
merged?: number;
|
|
completed?: number;
|
|
avg_tokens?: number;
|
|
avg_steps?: number;
|
|
origin?: string;
|
|
}
|
|
|
|
/**
|
|
* workflowRows returns one row per known workflow, definitions first and in
|
|
* their declared order, then any workflow seen only in the statistics.
|
|
*/
|
|
export function workflowRows(
|
|
workflows: WorkflowStats[] | null | undefined,
|
|
definitions: WorkflowDefinition[] | null | undefined,
|
|
): WorkflowRow[] {
|
|
const stats = new Map((workflows || []).map((w) => [w.workflow, w]));
|
|
const rows: WorkflowRow[] = [];
|
|
const claimed = new Set<string>();
|
|
|
|
for (const definition of definitions || []) {
|
|
claimed.add(definition.id);
|
|
rows.push({
|
|
...definition,
|
|
...(stats.get(definition.id) || {
|
|
workflow: definition.id,
|
|
runs: 0,
|
|
merged: 0,
|
|
completed: 0,
|
|
}),
|
|
});
|
|
}
|
|
|
|
for (const workflow of workflows || []) {
|
|
if (claimed.has(workflow.workflow)) continue;
|
|
// No definition to describe it, so the id is the only name it has.
|
|
rows.push({ id: workflow.workflow, name: workflow.workflow, ...workflow });
|
|
}
|
|
|
|
return rows;
|
|
}
|