Просмотр исходного кода

heatmap: minor refactor, don't repeat cards stats calculation

Alexander Zobnin 8 лет назад
Родитель
Сommit
c7e8b98d14

+ 2 - 8
public/app/plugins/panel/heatmap/heatmap_ctrl.ts

@@ -191,20 +191,14 @@ export class HeatmapCtrl extends MetricsPanelCtrl {
       yBucketSize = 1;
       yBucketSize = 1;
     }
     }
 
 
-    let cardsData = convertToCards(bucketsData);
-    let maxCardsValue = _.max(_.map(cardsData, 'count'));
-    let minCardsValue = _.min(_.map(cardsData, 'count'));
-    let cardStats = {
-      max: maxCardsValue,
-      min: minCardsValue
-    };
+    let {cards, cardStats} = convertToCards(bucketsData);
 
 
     this.data = {
     this.data = {
       buckets: bucketsData,
       buckets: bucketsData,
       heatmapStats: heatmapStats,
       heatmapStats: heatmapStats,
       xBucketSize: xBucketSize,
       xBucketSize: xBucketSize,
       yBucketSize: yBucketSize,
       yBucketSize: yBucketSize,
-      cards: cardsData,
+      cards: cards,
       cardStats: cardStats
       cardStats: cardStats
     };
     };
   }
   }

+ 11 - 1
public/app/plugins/panel/heatmap/heatmap_data_converter.ts

@@ -51,6 +51,7 @@ function elasticHistogramToHeatmap(seriesList) {
  * @return {Array}          Array of "card" objects
  * @return {Array}          Array of "card" objects
  */
  */
 function convertToCards(buckets) {
 function convertToCards(buckets) {
+  let min = 0, max = 0;
   let cards = [];
   let cards = [];
   _.forEach(buckets, xBucket => {
   _.forEach(buckets, xBucket => {
     _.forEach(xBucket.buckets, yBucket=> {
     _.forEach(xBucket.buckets, yBucket=> {
@@ -62,10 +63,19 @@ function convertToCards(buckets) {
         count: yBucket.count,
         count: yBucket.count,
       };
       };
       cards.push(card);
       cards.push(card);
+
+      if (cards.length === 1) {
+        min = yBucket.count;
+        max = yBucket.count;
+      }
+
+      min = yBucket.count < min ? yBucket.count : min;
+      max = yBucket.count > max ? yBucket.count : max;
     });
     });
   });
   });
 
 
-  return cards;
+  let cardStats = {min, max};
+  return {cards, cardStats};
 }
 }
 
 
 /**
 /**

+ 2 - 8
public/app/plugins/panel/heatmap/specs/renderer_specs.ts

@@ -114,14 +114,8 @@ describe('grafanaHeatmap', function () {
           let bucketsData = convertToHeatMap(ctx.series, ctx.data.yBucketSize, ctx.data.xBucketSize, logBase);
           let bucketsData = convertToHeatMap(ctx.series, ctx.data.yBucketSize, ctx.data.xBucketSize, logBase);
           ctx.data.buckets = bucketsData;
           ctx.data.buckets = bucketsData;
 
 
-          let cardsData = convertToCards(bucketsData);
-          let maxCardsValue = _.max(_.map(cardsData, 'count'));
-          let minCardsValue = _.min(_.map(cardsData, 'count'));
-          let cardStats = {
-            max: maxCardsValue,
-            min: minCardsValue
-          };
-          ctx.data.cards = cardsData;
+          let {cards, cardStats} = convertToCards(bucketsData);
+          ctx.data.cards = cards;
           ctx.data.cardStats = cardStats;
           ctx.data.cardStats = cardStats;
 
 
           let elemHtml = `
           let elemHtml = `