Files
windmill/frontend/src/lib/components/RunChart.svelte
T
77fb2eda01 feat: add unit, scale and height controls to the runs chart (#12191)
* feat: add unit, scale and height controls to the runs duration chart

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: point the EE ref at the runs chart usage counters

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep the chart resize arrow keys from moving the selected run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: make the runs chart unit and scale controls smaller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: move the runs chart unit and scale controls into a settings popover

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: place the runs chart settings popover like the other options popovers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: pin the runs chart settings button to the card's top-right corner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref to c476e8dc461f744562a36f240864d70437af1f65

This commit updates the EE repository reference after PR #947 was merged in windmill-ee-private.

Previous ee-repo-ref: 2c7254e714a1b4907055b27b40c825226ce3755c

New ee-repo-ref: c476e8dc461f744562a36f240864d70437af1f65

Automated by sync-ee-ref workflow.

* chore: drop the runs chart usage counters

The display controls sit behind a settings popover now, and their counters would not change a
decision. Points ee-repo-ref at the windmill-ee-private revert of their registration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
2026-11-04 20:29:43 +01:00

308 lines
7.9 KiB
Svelte

<script lang="ts">
import 'chartjs-adapter-date-fns'
import zoomPlugin from 'chartjs-plugin-zoom'
import {
Chart as ChartJS,
Title,
Tooltip,
Legend,
LineElement,
CategoryScale,
LinearScale,
PointElement,
TimeScale,
LogarithmicScale,
type ChartOptions
} from 'chart.js'
import type { CompletedJob } from '$lib/gen'
import { getDbClockNow } from '$lib/forLater'
import { Scatter } from '$lib/components/chartjs-wrappers/chartJs'
import { timeTicksWithDate } from '$lib/components/chartjs-wrappers/timeTicks'
import DarkModeObserver from './DarkModeObserver.svelte'
import { displayDate, msToReadableTime } from '$lib/utils'
import { durationAxisOptions, type DurationScale, type DurationUnit } from './runs/durationAxis'
interface Props {
jobs?: CompletedJob[] | undefined
maxIsNow?: boolean
minTimeSet?: string | null
maxTimeSet?: string | null
selectedIds?: string[]
canSelect?: boolean
unit?: DurationUnit
scale?: DurationScale
onPointClicked: (ids: string[]) => void
onZoom: (zoom: { min: Date; max: Date }) => void
}
let {
jobs = [],
maxIsNow = false,
minTimeSet = null,
maxTimeSet = null,
selectedIds = $bindable([]),
canSelect = true,
unit = 'auto',
scale = 'log',
onPointClicked,
onZoom
}: Props = $props()
const SUCCESS_COLOR = '#4ade80'
// const SUCCESS_COLOR_TRANSPARENT = '#c9b638'
const SUCCESS_COLOR_TRANSPARENT = $derived(mergeColors(SUCCESS_COLOR, getBackgroundColor(), 0.8))
const FAIL_COLOR = '#f87171'
const FAIL_COLOR_TRANSPARENT = $derived(mergeColors(FAIL_COLOR, getBackgroundColor(), 0.8))
ChartJS.register(
Title,
Tooltip,
Legend,
zoomPlugin,
LineElement,
CategoryScale,
LinearScale,
LogarithmicScale,
PointElement,
TimeScale
)
const zoomOptions = {
pan: {
enabled: true,
modifierKey: 'ctrl' as 'ctrl',
onPanComplete: ({ chart }) => {
onZoom({
min: addSeconds(new Date(chart.scales.x.min), -1),
max: addSeconds(new Date(chart.scales.x.max), 1)
})
}
},
zoom: {
drag: {
enabled: true
},
mode: 'x' as 'x',
onZoom: ({ chart }) => {
onZoom({
min: addSeconds(new Date(chart.scales.x.min), -1),
max: addSeconds(new Date(chart.scales.x.max), 1)
})
}
}
}
let darkMode = $state(false)
$effect(() => {
ChartJS.defaults.color = darkMode ? '#ccc' : '#666'
ChartJS.defaults.borderColor = darkMode ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)'
})
function getBackgroundColor(): string {
return darkMode ? '#2e3440' : '#ffffff'
}
function hexToRgb(hexColor: string): number[] {
hexColor = hexColor.replace(/^#/, '')
const r = parseInt(hexColor.substring(0, 2), 16)
const g = parseInt(hexColor.substring(2, 4), 16)
const b = parseInt(hexColor.substring(4, 6), 16)
return [r, g, b]
}
function rgbToHex(rgbColor: number[]): string {
// Convert RGB components to hexadecimal string
return (
'#' +
rgbColor
.map((c) => {
const hex = c.toString(16)
return hex.length === 1 ? '0' + hex : hex
})
.join('')
)
}
function mergeColors(color1: string, color2: string, slider: number): string {
const rgb1 = hexToRgb(color1)
const rgb2 = hexToRgb(color2)
// Blend colors based on percentage
const blendedRgb = rgb1.map((c1, i) => Math.round(c1 * (1 - slider) + rgb2[i] * slider))
return rgbToHex(blendedRgb)
}
function statusLabel(job: CompletedJob): string {
if (job.canceled) return 'Canceled'
if (job.is_skipped) return 'Skipped'
return job.success ? 'Success' : 'Failure'
}
function toPoint(job: CompletedJob) {
return {
x: job.started_at as any,
y: job.duration_ms,
id: job.id,
path: job.script_path,
status: statusLabel(job)
}
}
type DurationPoint = ReturnType<typeof toPoint>
function minJobTime(jobs: CompletedJob[]): Date {
let min: Date = new Date(jobs[0].completed_at!)
for (const job of jobs) {
if (job.completed_at != undefined) {
const date = new Date(job.completed_at)
if (date < min) {
min = date
}
}
}
return min
}
function maxJobTime(jobs: CompletedJob[]): Date {
let max: Date = new Date(jobs[0].completed_at!)
for (const job of jobs) {
if (new Date(job.completed_at!) > max) {
max = new Date(job.completed_at!)
}
}
return max
}
function computeMinMaxTime(
jobs: CompletedJob[] | undefined,
minTimeSet: string | null,
maxTimeSet: string | null
) {
let minTimeSetDate = minTimeSet ? new Date(minTimeSet) : undefined
let maxTimeSetDate = maxTimeSet ? new Date(maxTimeSet) : undefined
if (minTimeSetDate && maxTimeSetDate) {
return { minTime: minTimeSetDate, maxTime: maxTimeSetDate }
}
if (jobs == undefined || jobs?.length == 0) {
const computedMinTime = minTimeSetDate ?? addSeconds(new Date(), -300)
const computedMaxTime = maxTimeSetDate ?? getDbClockNow()
return { minTime: computedMinTime, maxTime: computedMaxTime }
}
const maxJob = maxIsNow ? getDbClockNow() : maxJobTime(jobs)
const minJob = minJobTime(jobs)
const diff = (maxJob.getTime() - minJob.getTime()) / 20000
let computedMinTime = minTimeSetDate ?? addSeconds(minJob, -diff)
let computedMaxTime = maxIsNow
? (maxTimeSetDate ?? maxJob)
: (maxTimeSetDate ?? addSeconds(maxJob, diff))
return { minTime: computedMinTime, maxTime: computedMaxTime }
}
function addSeconds(date: Date, seconds: number): Date {
date.setTime(date.getTime() + seconds * 1000)
return date
}
let success = $derived(jobs?.filter((x) => x.success))
let failed = $derived(jobs?.filter((x) => !x.success))
let data = $derived.by(() => {
const data = {
datasets: [
{
borderColor: 'rgba(99,0,125, 0)',
backgroundColor: FAIL_COLOR as string | string[],
radius: 3,
label: 'Failed',
data: failed?.map(toPoint) ?? []
},
{
borderColor: 'rgba(99,0,125, 0)',
backgroundColor: SUCCESS_COLOR as string | string[],
radius: 3,
label: 'Successful',
data: success?.map(toPoint) ?? []
}
]
}
if (!canSelect || selectedIds.length === 0) {
data.datasets[0].backgroundColor = FAIL_COLOR
data.datasets[1].backgroundColor = SUCCESS_COLOR
} else {
data.datasets[0].backgroundColor = data.datasets[0].data.map((p) =>
selectedIds.includes(p.id) ? FAIL_COLOR : FAIL_COLOR_TRANSPARENT
)
data.datasets[1].backgroundColor = data.datasets[1].data.map((p) =>
selectedIds.includes(p.id) ? SUCCESS_COLOR : SUCCESS_COLOR_TRANSPARENT
)
}
return data
})
const minMaxTime = $derived.by(() => computeMinMaxTime(jobs, minTimeSet, maxTimeSet))
const durations = $derived(jobs?.map((job) => job.duration_ms) ?? [])
let scatterOptions: ChartOptions<'scatter'> = $derived({
responsive: true,
maintainAspectRatio: false,
plugins: {
zoom: zoomOptions,
legend: {
display: false
},
tooltip: {
callbacks: {
title: (items) => {
const raw = items[0]?.raw as DurationPoint | undefined
return raw ? displayDate(raw.x, true) : ''
},
label: (context) => {
const raw = context.raw as DurationPoint
const duration = `${msToReadableTime(raw.y, 2)} · ${raw.status}`
return raw.path ? [raw.path, duration] : duration
}
}
}
},
onClick: (_e: any, u: any) => {
if (canSelect) {
const ids = u.map((j: any) => data.datasets[j.datasetIndex].data[j.index].id)
selectedIds = ids
onPointClicked(ids)
}
},
scales: {
x: {
type: 'time',
grid: {
display: false
},
min: minMaxTime.minTime.getTime(),
max: minMaxTime.maxTime.getTime(),
ticks: timeTicksWithDate(minMaxTime.minTime, minMaxTime.maxTime)
},
y: {
...durationAxisOptions(unit, scale, durations),
grid: {
display: false
},
afterFit: function (axis) {
axis.width = Math.max(axis.width, 65) // min width to prevent layout flickering
}
}
},
animation: false
})
</script>
<DarkModeObserver bind:darkMode />
<div class="relative h-full">
<Scatter {data} options={scatterOptions} />
</div>