From 6f4232bee383a7782ebbdcd31624f11a2d2bdfab Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 28 Mar 2023 17:16:24 -0500 Subject: [PATCH] HeatMap: Sort y buckets when all bucket names are numeric (#65322) --- .../plugins/datasource/prometheus/result_transformer.ts | 6 +++--- public/app/plugins/panel/heatmap/fields.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/result_transformer.ts b/public/app/plugins/datasource/prometheus/result_transformer.ts index 2a0ab008ee0..30f3150391a 100644 --- a/public/app/plugins/datasource/prometheus/result_transformer.ts +++ b/public/app/plugins/datasource/prometheus/result_transformer.ts @@ -650,13 +650,13 @@ function transformToHistogramOverTime(seriesList: DataFrame[]) { return seriesList; } -function sortSeriesByLabel(s1: DataFrame, s2: DataFrame): number { +export function sortSeriesByLabel(s1: DataFrame, s2: DataFrame): number { let le1, le2; try { // fail if not integer. might happen with bad queries - le1 = parseSampleValue(s1.name ?? ''); - le2 = parseSampleValue(s2.name ?? ''); + le1 = parseSampleValue(s1.name ?? s1.fields[1].name); + le2 = parseSampleValue(s2.name ?? s2.fields[1].name); } catch (err) { console.error(err); return 0; diff --git a/public/app/plugins/panel/heatmap/fields.ts b/public/app/plugins/panel/heatmap/fields.ts index 86bd816a90a..9269026ce0c 100644 --- a/public/app/plugins/panel/heatmap/fields.ts +++ b/public/app/plugins/panel/heatmap/fields.ts @@ -19,6 +19,7 @@ import { readHeatmapRowsCustomMeta, rowsToCellsHeatmap, } from 'app/features/transformers/calculateHeatmap/heatmap'; +import { parseSampleValue, sortSeriesByLabel } from 'app/plugins/datasource/prometheus/result_transformer'; import { CellValues, PanelOptions } from './types'; import { boundedMinMax } from './utils'; @@ -94,6 +95,14 @@ export function prepareHeatmapData( // Everything past here assumes a field for each row in the heatmap (buckets) if (!rowsHeatmap) { if (frames.length > 1) { + let allNamesNumeric = frames.every( + (frame) => !Number.isNaN(parseSampleValue(frame.name ?? frame.fields[1].name)) + ); + + if (allNamesNumeric) { + frames.sort(sortSeriesByLabel); + } + rowsHeatmap = [ outerJoinDataFrames({ frames,