From 5037f93a783bcf0acab9180eb70e0e214d8f7680 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 5 Mar 2018 14:01:55 +0300 Subject: [PATCH] heatmap: sort series before converting to heatmap. This allows to use histogram series from arbitrary datasource and display it properly. --- .../app/plugins/panel/heatmap/heatmap_ctrl.ts | 11 ++++-- .../panel/heatmap/heatmap_data_converter.ts | 34 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts index bb64229dc77..5a3b04905ef 100644 --- a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts +++ b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts @@ -5,7 +5,13 @@ import TimeSeries from 'app/core/time_series2'; import { axesEditor } from './axes_editor'; import { heatmapDisplayEditor } from './display_editor'; import rendering from './rendering'; -import { convertToHeatMap, convertToCards, histogramToHeatmap, calculateBucketSize } from './heatmap_data_converter'; +import { + convertToHeatMap, + convertToCards, + histogramToHeatmap, + calculateBucketSize, + sortSeriesByLabel, +} from './heatmap_data_converter'; let X_BUCKET_NUMBER_DEFAULT = 30; let Y_BUCKET_NUMBER_DEFAULT = 10; @@ -205,9 +211,10 @@ export class HeatmapCtrl extends MetricsPanelCtrl { // Convert histogram to heatmap. Each histogram bucket represented by the series which name is // a top (or bottom, depends of datasource) bucket bound. Further, these values will be used as X axis labels. + this.series.sort(sortSeriesByLabel); bucketsData = histogramToHeatmap(this.series); - tsBuckets = _.map(this.series, 'label'); + tsBuckets = _.map(this.series, 'label'); if (this.datasource && this.datasource.type === 'prometheus') { // Prometheus labels are upper inclusive bounds, so add empty bottom bucket label. tsBuckets = [''].concat(tsBuckets); diff --git a/public/app/plugins/panel/heatmap/heatmap_data_converter.ts b/public/app/plugins/panel/heatmap/heatmap_data_converter.ts index 178eea9ec7f..89b1f1c714e 100644 --- a/public/app/plugins/panel/heatmap/heatmap_data_converter.ts +++ b/public/app/plugins/panel/heatmap/heatmap_data_converter.ts @@ -56,6 +56,39 @@ function histogramToHeatmap(seriesList) { return heatmap; } +/** + * Sort series representing histogram by label value. + */ +function sortSeriesByLabel(s1, s2) { + let label1, label2; + + try { + // fail if not integer. might happen with bad queries + label1 = parseHistogramLabel(s1.label); + label2 = parseHistogramLabel(s2.label); + } catch (err) { + console.log(err); + return 0; + } + + if (label1 > label2) { + return 1; + } + + if (label1 < label2) { + return -1; + } + + return 0; +} + +function parseHistogramLabel(label: string): number { + if (label === '+Inf') { + return +Infinity; + } + return parseInt(label); +} + /** * Convert buckets into linear array of "cards" - objects, represented heatmap elements. * @param {Object} buckets @@ -444,4 +477,5 @@ export { getValueBucketBound, isHeatmapDataEqual, calculateBucketSize, + sortSeriesByLabel, };