| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- import _ from 'lodash';
- import { describe, beforeEach, it, expect } from '../../../../../test/lib/common';
- import TimeSeries from 'app/core/time_series2';
- import {
- convertToHeatMap,
- convertToCards,
- elasticHistogramToHeatmap,
- calculateBucketSize,
- 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];
- let empty = {};
- let emptyValues = _.cloneDeep(ctx.heatmapA);
- emptyValues['1422774000000'].buckets['1'].values = [];
- expect(isHeatmapDataEqual(ctx.heatmapA, ctx.heatmapB)).toBe(true);
- expect(isHeatmapDataEqual(ctx.heatmapB, ctx.heatmapA)).toBe(true);
- expect(isHeatmapDataEqual(ctx.heatmapA, heatmapC)).toBe(true);
- expect(isHeatmapDataEqual(heatmapC, ctx.heatmapA)).toBe(true);
- expect(isHeatmapDataEqual(ctx.heatmapA, heatmapD)).toBe(false);
- expect(isHeatmapDataEqual(heatmapD, ctx.heatmapA)).toBe(false);
- expect(isHeatmapDataEqual(ctx.heatmapA, heatmapE)).toBe(false);
- expect(isHeatmapDataEqual(heatmapE, ctx.heatmapA)).toBe(false);
- expect(isHeatmapDataEqual(empty, ctx.heatmapA)).toBe(false);
- expect(isHeatmapDataEqual(ctx.heatmapA, empty)).toBe(false);
- expect(isHeatmapDataEqual(emptyValues, ctx.heatmapA)).toBe(false);
- expect(isHeatmapDataEqual(ctx.heatmapA, emptyValues)).toBe(false);
- });
- });
- describe('calculateBucketSize', () => {
- let ctx: any = {};
- describe('when logBase is 1 (linear scale)', () => {
- beforeEach(() => {
- ctx.logBase = 1;
- ctx.bounds_set = [
- { bounds: [], size: 0 },
- { bounds: [0], size: 0 },
- { bounds: [4], size: 4 },
- { bounds: [0, 1, 2, 3, 4], size: 1 },
- { bounds: [0, 1, 3, 5, 7], size: 1 },
- { bounds: [0, 3, 7, 9, 15], size: 2 },
- { bounds: [0, 7, 3, 15, 9], size: 2 },
- { bounds: [0, 5, 10, 15, 50], size: 5 },
- ];
- });
- it('should properly calculate bucket size', () => {
- _.each(ctx.bounds_set, b => {
- let bucketSize = calculateBucketSize(b.bounds, ctx.logBase);
- expect(bucketSize).toBe(b.size);
- });
- });
- });
- describe('when logBase is 2', () => {
- beforeEach(() => {
- ctx.logBase = 2;
- ctx.bounds_set = [
- { bounds: [], size: 0 },
- { bounds: [0], size: 0 },
- { bounds: [4], size: 4 },
- { bounds: [1, 2, 4, 8], size: 1 },
- { bounds: [1, Math.SQRT2, 2, 8, 16], size: 0.5 },
- ];
- });
- it('should properly calculate bucket size', () => {
- _.each(ctx.bounds_set, b => {
- let bucketSize = calculateBucketSize(b.bounds, ctx.logBase);
- expect(isEqual(bucketSize, b.size)).toBe(true);
- });
- });
- });
- });
- describe('HeatmapDataConverter', () => {
- let ctx: any = {};
- beforeEach(() => {
- ctx.series = [];
- ctx.series.push(
- new TimeSeries({
- datapoints: [[1, 1422774000000], [1, 1422774000010], [2, 1422774060000]],
- alias: 'series1',
- })
- );
- ctx.series.push(
- new TimeSeries({
- datapoints: [[2, 1422774000000], [2, 1422774000010], [3, 1422774060000]],
- alias: 'series2',
- })
- );
- ctx.series.push(
- new TimeSeries({
- datapoints: [[5, 1422774000000], [3, 1422774000010], [4, 1422774060000]],
- alias: 'series3',
- })
- );
- ctx.xBucketSize = 60000; // 60s
- ctx.yBucketSize = 2;
- 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: {
- '0': {
- y: 0,
- values: [1, 1],
- count: 2,
- bounds: { bottom: 0, top: 2 },
- },
- '2': {
- y: 2,
- values: [2, 2, 3],
- count: 3,
- bounds: { bottom: 2, top: 4 },
- },
- '4': { y: 4, values: [5], count: 1, bounds: { bottom: 4, top: 6 } },
- },
- },
- '1422774060000': {
- x: 1422774060000,
- buckets: {
- '2': {
- y: 2,
- values: [2, 3],
- count: 3,
- bounds: { bottom: 2, top: 4 },
- },
- '4': { y: 4, values: [4], count: 1, bounds: { bottom: 4, top: 6 } },
- },
- },
- };
- let heatmap = convertToHeatMap(ctx.series, ctx.yBucketSize, ctx.xBucketSize, ctx.logBase);
- expect(isHeatmapDataEqual(heatmap, expectedHeatmap)).toBe(true);
- });
- });
- describe.skip('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)).toBe(true);
- });
- });
- });
- describe('ES Histogram converter', () => {
- let ctx: any = {};
- beforeEach(() => {
- ctx.series = [];
- ctx.series.push(
- new TimeSeries({
- datapoints: [[1, 1422774000000], [0, 1422774060000]],
- alias: '1',
- label: '1',
- })
- );
- ctx.series.push(
- new TimeSeries({
- datapoints: [[5, 1422774000000], [3, 1422774060000]],
- alias: '2',
- label: '2',
- })
- );
- ctx.series.push(
- new TimeSeries({
- datapoints: [[0, 1422774000000], [1, 1422774060000]],
- alias: '3',
- label: '3',
- })
- );
- });
- describe('when converting ES histogram', () => {
- beforeEach(() => {});
- it('should build proper heatmap data', () => {
- let expectedHeatmap = {
- '1422774000000': {
- x: 1422774000000,
- buckets: {
- '1': {
- y: 1,
- count: 1,
- values: [],
- points: [],
- bounds: { bottom: 1, top: null },
- },
- '2': {
- y: 2,
- count: 5,
- values: [],
- points: [],
- bounds: { bottom: 2, top: null },
- },
- '3': {
- y: 3,
- count: 0,
- values: [],
- points: [],
- bounds: { bottom: 3, top: null },
- },
- },
- },
- '1422774060000': {
- x: 1422774060000,
- buckets: {
- '1': {
- y: 1,
- count: 0,
- values: [],
- points: [],
- bounds: { bottom: 1, top: null },
- },
- '2': {
- y: 2,
- count: 3,
- values: [],
- points: [],
- bounds: { bottom: 2, top: null },
- },
- '3': {
- y: 3,
- count: 1,
- values: [],
- points: [],
- bounds: { bottom: 3, top: null },
- },
- },
- },
- };
- let heatmap = elasticHistogramToHeatmap(ctx.series);
- expect(heatmap).toEqual(expectedHeatmap);
- });
- });
- });
- describe('convertToCards', () => {
- let buckets = {};
- beforeEach(() => {
- buckets = {
- '1422774000000': {
- x: 1422774000000,
- buckets: {
- '1': { y: 1, values: [1], count: 1, bounds: {} },
- '2': { y: 2, values: [2], count: 1, bounds: {} },
- },
- },
- '1422774060000': {
- x: 1422774060000,
- buckets: {
- '2': { y: 2, values: [2, 3], count: 2, bounds: {} },
- },
- },
- };
- });
- it('should build proper cards data', () => {
- let expectedCards = [
- { x: 1422774000000, y: 1, count: 1, values: [1], yBounds: {} },
- { x: 1422774000000, y: 2, count: 1, values: [2], yBounds: {} },
- { x: 1422774060000, y: 2, count: 2, values: [2, 3], yBounds: {} },
- ];
- let res = convertToCards(buckets);
- expect(res.cards).toMatchObject(expectedCards);
- });
- it('should build proper cards stats', () => {
- let expectedStats = { min: 1, max: 2 };
- let res = convertToCards(buckets);
- expect(res.cardStats).toMatchObject(expectedStats);
- });
- });
- /**
- * Compare two numbers with given precision. Suitable for compare float numbers after conversions with precision loss.
- * @param a
- * @param b
- * @param precision
- */
- function isEqual(a: number, b: number, precision = 0.000001): boolean {
- if (a === b) {
- return true;
- } else {
- return Math.abs(1 - a / b) <= precision;
- }
- }
|