Files
pipelines/web/src/views/runs.js
T

354 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The Runs history view: day groups, usage metrics, rows and the filter/sort
// menus. The pure filtering, sorting and formatting live in runs.js; this module
// owns the DOM and the page's run-list state.
import { h } from "../dom.js";
import { statusIcon } from "../statusIcon.js";
import {
RUN_STATES,
RUN_TIME_RANGES,
RUN_SORTS,
runStateOf,
runStatusLabel,
runIdentity,
versionLabel,
progressText,
metaText,
formatDuration,
localDayKey,
filterRuns,
groupRunsByDay,
runComparator,
pipelineFilterOptions,
runListEntries,
runFiltersActiveCount,
DEFAULT_RUN_SORT,
defaultRunFilters,
} from "../runs.js";
export function createRunsView(rt) {
const { state } = rt;
const renderContent = (...args) => rt.renderContent(...args);
const selectRun = (...args) => rt.selectRun(...args);
const renderAttentionBell = () => rt.renderAttentionBell();
function renderRunList(now = Date.now()) {
const pane = h("div", { class: "list runs" });
pane.append(renderRunToolbar());
const scroll = h("div", { class: "run-scroll list-scroll" });
if (state.loading && !state.runs.length) {
scroll.append(h("div", { class: "muted pad", text: "Loading…" }));
pane.append(scroll);
return pane;
}
const filters = state.runFilters;
const today = localDayKey(new Date());
const filtered = filterRuns(state.runs, filters, today);
const groups = groupRunsByDay(filtered, today, undefined, runComparator(state.runSort));
const entries = runListEntries(
state.usage,
state.runs.length === 0,
filtered.length === 0 && state.runs.length > 0,
groups,
"",
);
for (const entry of entries) {
switch (entry.kind) {
case "empty":
scroll.append(renderEmptyState(entry));
break;
case "day":
scroll.append(h("div", { class: "day-header", text: entry.label }));
break;
case "run":
scroll.append(renderRunRow(entry.run, now));
break;
case "error":
scroll.append(h("div", { class: "run-list-error", text: entry.message }));
break;
}
}
pane.append(scroll);
return pane;
}
function renderUsage(usage) {
const metrics = h("div", { class: "metrics" });
metrics.append(metric("Total", String(usage.total ?? 0), "total"));
if ((usage.average_seconds ?? 0) > 0) {
metrics.append(metric("Average", formatDuration(usage.average_seconds), "average"));
}
metrics.append(metric("Succeeded", String(usage.succeeded ?? 0), "succeeded"));
metrics.append(metric("Failed", String(usage.failed ?? 0), "failed"));
metrics.append(metric("Canceled", String(usage.canceled ?? 0), "canceled"));
metrics.append(metric("Interrupted", String(usage.interrupted ?? 0), "interrupted"));
metrics.append(metric("Active", String(usage.active ?? 0), "active"));
return metrics;
}
function metric(label, value, cls) {
return h(
"div",
{ class: `metric metric-${cls}` },
h("span", { class: "metric-value", text: value }),
h("span", { class: "metric-label", text: label }),
);
}
function renderEmptyState(entry) {
const wrap = h("div", { class: "empty-state" }, h("span", { text: entry.message }));
if (entry.actionLabel) {
wrap.append(h("button", { class: "ghost", text: entry.actionLabel, onclick: clearRunFilters }));
}
return wrap;
}
/**
* One run row, in the app's exact hierarchy: status chip (+ spinner while
* executing); primary line = stored title else pipeline name, carrying the
* version chip when there is no title; the pipeline name + version chip on a
* second line only when a title exists; the progress line; the meta line; and
* the diagnostic line, in the error colour only on a failure. Every
* server-provided string is set with textContent.
*/
function renderRunRow(run, now) {
const status = runStateOf(run.status);
const identity = runIdentity(run.title, run.name);
const row = h("button", {
class: `row run-row${state.selectedRunId === run.id ? " selected" : ""}`,
"data-run-id": String(run.id),
"data-animate-key": `run:${run.id}`,
onclick: () => selectRun(run.id),
});
const head = h("span", { class: "run-row-head" });
head.append(statusIcon(status, runStatusLabel(run)));
if (run.cancel_requested || run.cleanup_unconfirmed) {
head.append(h("span", { class: "muted", text: runStatusLabel(run) }));
}
if (run.executing) head.append(h("span", { class: "spinner", "aria-hidden": "true" }));
// The pipeline is the list's anchor: it names what ran, before the task title.
head.append(
h("span", { class: "run-row-pipeline-name", text: identity.pipeline ?? identity.label }),
);
head.append(versionChip(run.version));
row.append(head);
if (identity.pipeline != null) {
row.append(h("span", { class: "run-row-title", text: identity.label }));
}
row.append(h("span", { class: "run-row-progress", text: progressText(run) }));
row.append(
h("span", {
class: "run-row-meta",
"data-run-id": String(run.id),
text: metaText(run, now),
}),
);
if (String(run.diagnostic ?? "").trim()) {
row.append(
h("span", {
class: `run-row-diag${status === "failed" ? " error" : ""}`,
text: run.diagnostic,
}),
);
}
return row;
}
function versionChip(version) {
return h("span", { class: "version-chip", text: versionLabel(version) });
}
function renderRunToolbar() {
const activeCount = runFiltersActiveCount(state.runFilters);
const filterLabel = activeCount > 0 ? `Filter (${activeCount})` : "Filter";
const sortLabel = (RUN_SORTS.find((s) => s.value === state.runSort) ?? RUN_SORTS[0]).label;
const toolbar = h("div", { class: "run-toolbar" });
toolbar.append(
h(
"div",
{ class: "menu-anchor" },
h("button", {
id: "runFilterBtn",
class: `ghost filter-btn${activeCount > 0 ? " active" : ""}`,
title: activeCount > 0 ? `${activeCount} active filter${activeCount === 1 ? "" : "s"}` : "Filter runs",
text: filterLabel,
"data-active-count": String(activeCount),
onclick: () => toggleMenu("filter"),
}),
state.openMenu === "filter" ? renderFilterMenu() : null,
),
h(
"div",
{ class: "menu-anchor" },
h("button", {
id: "runSortBtn",
class: "ghost sort-btn",
title: "Sort runs",
text: `Sort: ${sortLabel}`,
onclick: () => toggleMenu("sort"),
}),
state.openMenu === "sort" ? renderSortMenu() : null,
),
h("span", { class: "spacer" }),
renderAttentionBell(),
);
const pipelineId = state.runFilters.pipelineId;
if (pipelineId != null) {
const name = state.pipelines.find((pipeline) => pipeline.id === pipelineId)?.name ??
state.runs.find((run) => run.pipeline_id === pipelineId)?.name ?? `Pipeline ${pipelineId}`;
const chip = h("button", {
class: "pipeline-filter-chip",
type: "button",
"aria-label": `Clear pipeline filter: ${name}`,
title: `Showing only ${name}`,
onclick: () => setPipelineFilter(null),
}, h("span", { text: name }), h("span", { "aria-hidden": "true", text: "×" }));
toolbar.insertBefore(chip, toolbar.querySelector(".spacer"));
}
return toolbar;
}
/** One small live-stream badge (LiveBadge.kt): a dot plus the honest word. */
function renderFilterMenu() {
const filters = state.runFilters;
const menu = h("div", { class: "menu filter-menu", id: "runFilterMenu" });
menu.append(h("div", { class: "menu-section", text: "Status" }));
for (const status of RUN_STATES) {
menu.append(
filterCheck(status, filters.statuses.has(status), () => toggleStatusFilter(status), {
"data-status": status,
}),
);
}
menu.append(h("div", { class: "menu-divider" }));
menu.append(h("div", { class: "menu-section", text: "Pipeline" }));
menu.append(
filterCheck("Any pipeline", filters.pipelineId == null, () => setPipelineFilter(null), {
"data-pipeline": "any",
}),
);
for (const option of pipelineFilterOptions(state.runs)) {
menu.append(
filterCheck(option.name, filters.pipelineId === option.id, () => setPipelineFilter(option.id), {
"data-pipeline": String(option.id),
}),
);
}
menu.append(h("div", { class: "menu-divider" }));
menu.append(h("div", { class: "menu-section", text: "Time range" }));
for (const range of RUN_TIME_RANGES) {
menu.append(
filterCheck(range.label, filters.timeRange === range.value, () => setTimeRangeFilter(range.value), {
"data-time": range.value,
}),
);
}
menu.append(
h(
"div",
{ class: "menu-actions" },
h("button", { class: "ghost", text: "Clear filters", onclick: clearRunFilters }),
h("button", { class: "ghost", text: "Done", onclick: closeMenu }),
),
);
return menu;
}
function filterCheck(label, checked, onclick, data = {}) {
return h(
"label",
{ class: "filter-check" },
h("input", {
type: "checkbox",
checked: checked ? "checked" : null,
onchange: onclick,
...data,
}),
h("span", { text: label }),
);
}
function renderSortMenu() {
const menu = h("div", { class: "menu sort-menu", id: "runSortMenu" });
for (const option of RUN_SORTS) {
menu.append(
h("button", {
class: `menu-item${state.runSort === option.value ? " selected" : ""}`,
text: option.label,
onclick: () => {
state.runSort = option.value;
closeMenu();
},
}),
);
}
menu.append(h("div", { class: "menu-divider" }));
menu.append(
h("button", {
class: "menu-item",
text: "Reset",
onclick: () => {
state.runSort = DEFAULT_RUN_SORT;
closeMenu();
},
}),
);
return menu;
}
function toggleStatusFilter(status) {
const next = new Set(state.runFilters.statuses);
if (next.has(status)) next.delete(status);
else next.add(status);
state.runFilters = { ...state.runFilters, statuses: next };
renderContent();
}
function setPipelineFilter(pipelineId) {
state.runFilters = { ...state.runFilters, pipelineId };
if (state.selectedRunId == null) rt.pushRoute(pipelineId == null ? { kind: "runs" } : { kind: "runs", pipelineId });
closeMenu();
}
function setTimeRangeFilter(timeRange) {
state.runFilters = { ...state.runFilters, timeRange };
closeMenu();
}
function clearRunFilters() {
state.runFilters = defaultRunFilters();
if (state.selectedRunId == null) rt.pushRoute({ kind: "runs" });
closeMenu();
}
function toggleMenu(name) {
state.openMenu = state.openMenu === name ? null : name;
renderContent();
}
function closeMenu() {
state.openMenu = null;
renderContent();
}
return {
renderRunList,
renderRunToolbar,
renderRunRow,
renderUsage,
renderEmptyState,
toggleMenu,
closeMenu,
clearRunFilters,
};
}