Преглед изворни кода

heatmap: add tests for heatmap_data_converter

Alexander Zobnin пре 8 година
родитељ
комит
114431beed

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

@@ -5,6 +5,16 @@ import _ from 'lodash';
 let VALUE_INDEX = 0;
 let TIME_INDEX = 1;
 
+interface XBucket {
+  x: number;
+  buckets: any;
+}
+
+interface YBucket {
+  y: number;
+  values: number[];
+}
+
 /**
  * Convert set of time series into heatmap buckets
  * @return {Object}    Heatmap object:
@@ -347,11 +357,51 @@ function logp(value, base) {
   return Math.log(value) / Math.log(base);
 }
 
+/**
+ * Compare two heatmap data objects
+ * @param objA
+ * @param objB
+ */
+function isHeatmapDataEqual(objA: any, objB: any): boolean {
+  let is_eql = true;
+
+  _.forEach(objA, (xBucket: XBucket, x) => {
+    if (objB[x]) {
+      _.forEach(xBucket.buckets, (yBucket: YBucket, y) => {
+        if (objB[x].buckets && objB[x].buckets[y]) {
+          if (objB[x].buckets[y].values) {
+            is_eql = _.isEqual(yBucket.values.sort(), objB[x].buckets[y].values.sort());
+            if (!is_eql) {
+              return false;
+            }
+          } else {
+            is_eql = false;
+            return false;
+          }
+        } else {
+          is_eql = false;
+          return false;
+        }
+      });
+
+      if (!is_eql) {
+        return false;
+      }
+    } else {
+      is_eql = false;
+      return false;
+    }
+  });
+
+  return is_eql;
+}
+
 export {
   convertToHeatMap,
   convertToCards,
   removeZeroBuckets,
   mergeZeroBuckets,
   getMinLog,
-  getValueBucketBound
+  getValueBucketBound,
+  isHeatmapDataEqual
 };

+ 125 - 0
public/app/plugins/panel/heatmap/specs/heatmap_data_converter_specs.ts

@@ -0,0 +1,125 @@
+///<reference path="../../../../headers/common.d.ts" />
+
+import _ from 'lodash';
+import { describe, beforeEach, it, sinon, expect, angularMocks } from '../../../../../test/lib/common';
+import TimeSeries from 'app/core/time_series2';
+import { convertToHeatMap, isHeatmapDataEqual } from '../heatmap_data_converter';
+
+describe('isHeatmapDataEqual', () => {
+  let ctx: any = {};
+
+  beforeEach(() => {
+    ctx.heatmapA = {
+      '1422774000000': {
+        x: 1422774000000,
+        buckets: {
+          '1': { y: 1, values: [1, 1.5] },
+          '2': { y: 2, values: [1] }
+        }
+      }
+    };
+
+    ctx.heatmapB = {
+      '1422774000000': {
+        x: 1422774000000,
+        buckets: {
+          '1': { y: 1, values: [1.5, 1] },
+          '2': { y: 2, values: [1] }
+        }
+      }
+    };
+  });
+
+  it('should proper compare objects', () => {
+    let heatmapC = _.cloneDeep(ctx.heatmapA);
+    heatmapC['1422774000000'].buckets['1'].values = [1, 1.5];
+
+    let heatmapD = _.cloneDeep(ctx.heatmapA);
+    heatmapD['1422774000000'].buckets['1'].values = [1.5, 1, 1.6];
+
+    let heatmapE = _.cloneDeep(ctx.heatmapA);
+    heatmapE['1422774000000'].buckets['1'].values = [1, 1.6];
+
+    expect(isHeatmapDataEqual(ctx.heatmapA, ctx.heatmapB)).to.be(true);
+    expect(isHeatmapDataEqual(ctx.heatmapA, heatmapC)).to.be(true);
+    expect(isHeatmapDataEqual(ctx.heatmapA, heatmapD)).to.be(false);
+    expect(isHeatmapDataEqual(ctx.heatmapA, heatmapE)).to.be(false);
+  });
+});
+
+describe('HeatmapDataConverter', () => {
+  let ctx: any = {};
+
+  beforeEach(() => {
+    ctx.series = [];
+    ctx.series.push(new TimeSeries({
+      datapoints: [[1, 1422774000000], [2, 1422774060000]],
+      alias: 'series1'
+    }));
+    ctx.series.push(new TimeSeries({
+      datapoints: [[2, 1422774000000], [3, 1422774060000]],
+      alias: 'series2'
+    }));
+
+    ctx.xBucketSize = 60000; // 60s
+    ctx.yBucketSize = 1;
+    ctx.logBase = 1;
+  });
+
+  describe('when logBase is 1 (linear scale)', () => {
+
+    beforeEach(() => {
+      ctx.logBase = 1;
+    });
+
+    it('should build proper heatmap data', () => {
+      let expectedHeatmap = {
+        '1422774000000': {
+          x: 1422774000000,
+          buckets: {
+            '1': { y: 1, values: [1] },
+            '2': { y: 2, values: [2] }
+          }
+        },
+        '1422774060000': {
+          x: 1422774060000,
+          buckets: {
+            '2': { y: 2, values: [2] },
+            '3': { y: 3, values: [3] }
+          }
+        },
+      };
+
+      let heatmap = convertToHeatMap(ctx.series, ctx.yBucketSize, ctx.xBucketSize, ctx.logBase);
+      expect(isHeatmapDataEqual(heatmap, expectedHeatmap)).to.be(true);
+    });
+  });
+
+  describe('when logBase is 2', () => {
+
+    beforeEach(() => {
+      ctx.logBase = 2;
+    });
+
+    it('should build proper heatmap data', () => {
+      let expectedHeatmap = {
+        '1422774000000': {
+          x: 1422774000000,
+          buckets: {
+            '1': { y: 1, values: [1] },
+            '2': { y: 2, values: [2] }
+          }
+        },
+        '1422774060000': {
+          x: 1422774060000,
+          buckets: {
+            '2': { y: 2, values: [2, 3] }
+          }
+        },
+      };
+
+      let heatmap = convertToHeatMap(ctx.series, ctx.yBucketSize, ctx.xBucketSize, ctx.logBase);
+      expect(isHeatmapDataEqual(heatmap, expectedHeatmap)).to.be(true);
+    });
+  });
+});