Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add threshold line to timeseries charts [MA-3442] #1825

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
:legend-position="legendPosition"
:show-annotations="showAnnotationsToggle"
:show-legend-values="showLegendValuesToggle"
:threshold="1250"
:timeseries-zoom="timeSeriesZoomToggle"
tooltip-title="tooltip title"
@zoom-time-range="eventLog += 'Zoomed to ' + JSON.stringify($event) + '\n'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
:metric-unit="computedMetricUnit"
:stacked="chartOptions.stacked"
:synthetics-data-key="syntheticsDataKey"
:threshold="threshold"
:time-range-ms="timeRangeMs"
:tooltip-title="tooltipTitle"
:type="(chartOptions.type as ('timeseries_line' | 'timeseries_bar'))"
Expand Down Expand Up @@ -183,6 +184,11 @@ const props = defineProps({
required: false,
default: true,
},
threshold: {
type: Number,
required: false,
default: undefined,
},
timeseriesZoom: {
type: Boolean,
required: false,
Expand All @@ -207,6 +213,7 @@ const computedChartData = computed(() => {
{
fill: props.chartOptions.stacked,
colorPalette: props.chartOptions.chartDatasetColors || defaultStatusCodeColors,
threshold: props.threshold || undefined,
},
toRef(props, 'chartData'),
).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Dataset, KChartData, ExploreToDatasetDeps, DatasetLabel } from '..
import { parseISO } from 'date-fns'
import { isNullOrUndef } from 'chart.js/helpers'
import composables from '../composables'
import { KUI_COLOR_BACKGROUND_NEUTRAL } from '@kong/design-tokens'

const range = (start: number, stop: number, step: number = 1): number[] =>
Array(Math.ceil((stop - start) / step)).fill(start).map((x, y) => x + y * step)
Expand Down Expand Up @@ -180,6 +181,22 @@ export default function useExploreResultToTimeDataset(
// sort by total, descending
datasets.sort((a, b) => (Number(a.total) < Number(b.total) ? -1 : 1))

// Draw an optional threshold line
if (deps.threshold) {
datasets.push({
type: 'line',
borderColor: KUI_COLOR_BACKGROUND_NEUTRAL,
borderWidth: 3,
borderDash: [12, 8],
fill: false,
order: -1, // Display above all other datasets
borderJoinStyle: 'miter',
stack: 'custom', // Never stack this dataset
data: zeroFilledTimeSeries.map(ts => {
return { x: ts, y: deps.threshold }
}),
} as Dataset)
}
return {
datasets,
colorMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,4 +726,57 @@ describe('useVitalsExploreDatasets', () => {
expect(result.value.datasets[3].backgroundColor).toEqual('#ffd5b1')
expect(result.value.datasets[4].backgroundColor).toEqual('#ffb6b6')
})

it('displays a static threshold line on timeseries charts', () => {
const exploreResult: ComputedRef<ExploreResultV4> = computed(() => ({
data: [
{
timestamp: START_FOR_DAILY_QUERY.toISOString(),
event: {
metric1: 2,
metric2: 1,
},
} as GroupByResult,
{
timestamp: END_FOR_DAILY_QUERY.toISOString(),
event: {
metric1: 2,
metric2: 1,
},
} as GroupByResult,
],
meta: {
start_ms: Math.trunc(START_FOR_DAILY_QUERY.getTime()),
end_ms: Math.trunc(END_FOR_DAILY_QUERY.getTime()),
granularity_ms: 86400000,
metric_names: ['metric1', 'metric2'] as any as ExploreAggregations[],
display: {},
query_id: '',
metric_units: { metric1: 'units' } as MetricUnit,
},
}))

const staticThreshold = 1.24

const result = useExploreResultToTimeDataset(
{
fill: false,
threshold: staticThreshold,
},
exploreResult,
)

expect(result.value.datasets[2].data).toEqual(
[
{
x: START_FOR_DAILY_QUERY.getTime(),
y: staticThreshold,
},
{
x: END_FOR_DAILY_QUERY.getTime(),
y: staticThreshold,
},
],
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ import type { AnalyticsChartColors } from './chart-data'
export interface ExploreToDatasetDeps {
colorPalette?: AnalyticsChartColors | string[]
fill?: boolean
threshold?: number
}