renderer_specs.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import { describe, beforeEach, it, sinon, expect, angularMocks } from '../../../../../test/lib/common';
  3. import '../module';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import _ from 'lodash';
  7. import helpers from 'test/specs/helpers';
  8. import TimeSeries from 'app/core/time_series2';
  9. import moment from 'moment';
  10. import { Emitter } from 'app/core/core';
  11. import rendering from '../rendering';
  12. import {convertToHeatMap, convertToCards} from '../heatmap_data_converter';
  13. describe('grafanaHeatmap', function () {
  14. beforeEach(angularMocks.module('grafana.core'));
  15. function heatmapScenario(desc, func, elementWidth = 500) {
  16. describe(desc, function () {
  17. var ctx: any = {};
  18. ctx.setup = function (setupFunc) {
  19. beforeEach(angularMocks.module(function ($provide) {
  20. $provide.value("timeSrv", new helpers.TimeSrvStub());
  21. }));
  22. beforeEach(angularMocks.inject(function ($rootScope, $compile) {
  23. var ctrl: any = {
  24. colorSchemes: [
  25. {name: 'Oranges', value: 'interpolateOranges', invert: 'dark'},
  26. {name: 'Reds', value: 'interpolateReds', invert: 'dark'},
  27. ],
  28. events: new Emitter(),
  29. height: 200,
  30. panel: {
  31. heatmap: {
  32. },
  33. cards: {
  34. cardPadding: null,
  35. cardRound: null
  36. },
  37. color: {
  38. mode: 'spectrum',
  39. cardColor: '#b4ff00',
  40. colorScale: 'linear',
  41. exponent: 0.5,
  42. colorScheme: 'interpolateOranges',
  43. fillBackground: false
  44. },
  45. xBucketSize: 1000,
  46. xBucketNumber: null,
  47. yBucketSize: 1,
  48. yBucketNumber: null,
  49. xAxis: {
  50. show: true
  51. },
  52. yAxis: {
  53. show: true,
  54. format: 'short',
  55. decimals: null,
  56. logBase: 1,
  57. splitFactor: null,
  58. min: null,
  59. max: null,
  60. removeZeroValues: false
  61. },
  62. tooltip: {
  63. show: true,
  64. seriesStat: false,
  65. showHistogram: false
  66. },
  67. highlightCards: true
  68. },
  69. renderingCompleted: sinon.spy(),
  70. hiddenSeries: {},
  71. dashboard: {
  72. getTimezone: sinon.stub().returns('utc')
  73. },
  74. range: {
  75. from: moment.utc("01 Mar 2017 10:00:00", 'DD MMM YYYY HH:mm:ss'),
  76. to: moment.utc("01 Mar 2017 11:00:00", 'DD MMM YYYY HH:mm:ss'),
  77. },
  78. };
  79. var scope = $rootScope.$new();
  80. scope.ctrl = ctrl;
  81. ctx.series = [];
  82. ctx.series.push(new TimeSeries({
  83. datapoints: [[1, 1422774000000], [2, 1422774060000]],
  84. alias: 'series1'
  85. }));
  86. ctx.series.push(new TimeSeries({
  87. datapoints: [[2, 1422774000000], [3, 1422774060000]],
  88. alias: 'series2'
  89. }));
  90. ctx.data = {
  91. heatmapStats: {
  92. min: 1,
  93. max: 3,
  94. minLog: 1
  95. },
  96. xBucketSize: ctrl.panel.xBucketSize,
  97. yBucketSize: ctrl.panel.yBucketSize
  98. };
  99. setupFunc(ctrl, ctx);
  100. let logBase = ctrl.panel.yAxis.logBase;
  101. let bucketsData = convertToHeatMap(ctx.series, ctx.data.yBucketSize, ctx.data.xBucketSize, logBase);
  102. ctx.data.buckets = bucketsData;
  103. let {cards, cardStats} = convertToCards(bucketsData);
  104. ctx.data.cards = cards;
  105. ctx.data.cardStats = cardStats;
  106. let elemHtml = `
  107. <div class="heatmap-wrapper">
  108. <div class="heatmap-canvas-wrapper">
  109. <div class="heatmap-panel" style='width:${elementWidth}px'></div>
  110. </div>
  111. </div>`;
  112. var element = angular.element(elemHtml);
  113. $compile(element)(scope);
  114. scope.$digest();
  115. ctrl.data = ctx.data;
  116. ctx.element = element;
  117. let render = rendering(scope, $(element), [], ctrl);
  118. ctrl.events.emit('render');
  119. }));
  120. };
  121. func(ctx);
  122. });
  123. }
  124. heatmapScenario('default options', function (ctx) {
  125. ctx.setup(function (ctrl) {
  126. ctrl.panel.yAxis.logBase = 1;
  127. });
  128. it('should draw correct Y axis', function () {
  129. var yTicks = getTicks(ctx.element, ".axis-y");
  130. expect(yTicks).to.eql(['1', '2', '3']);
  131. });
  132. it('should draw correct X axis', function () {
  133. var xTicks = getTicks(ctx.element, ".axis-x");
  134. let expectedTicks = [
  135. formatTime("01 Mar 2017 10:00:00"),
  136. formatTime("01 Mar 2017 10:15:00"),
  137. formatTime("01 Mar 2017 10:30:00"),
  138. formatTime("01 Mar 2017 10:45:00"),
  139. formatTime("01 Mar 2017 11:00:00")
  140. ];
  141. expect(xTicks).to.eql(expectedTicks);
  142. });
  143. });
  144. heatmapScenario('when logBase is 2', function (ctx) {
  145. ctx.setup(function (ctrl) {
  146. ctrl.panel.yAxis.logBase = 2;
  147. });
  148. it('should draw correct Y axis', function () {
  149. var yTicks = getTicks(ctx.element, ".axis-y");
  150. expect(yTicks).to.eql(['1', '2', '4']);
  151. });
  152. });
  153. heatmapScenario('when logBase is 10', function (ctx) {
  154. ctx.setup(function (ctrl, ctx) {
  155. ctrl.panel.yAxis.logBase = 10;
  156. ctx.series.push(new TimeSeries({
  157. datapoints: [[10, 1422774000000], [20, 1422774060000]],
  158. alias: 'series3'
  159. }));
  160. ctx.data.heatmapStats.max = 20;
  161. });
  162. it('should draw correct Y axis', function () {
  163. var yTicks = getTicks(ctx.element, ".axis-y");
  164. expect(yTicks).to.eql(['1', '10', '100']);
  165. });
  166. });
  167. heatmapScenario('when logBase is 32', function (ctx) {
  168. ctx.setup(function (ctrl) {
  169. ctrl.panel.yAxis.logBase = 32;
  170. ctx.series.push(new TimeSeries({
  171. datapoints: [[10, 1422774000000], [100, 1422774060000]],
  172. alias: 'series3'
  173. }));
  174. ctx.data.heatmapStats.max = 100;
  175. });
  176. it('should draw correct Y axis', function () {
  177. var yTicks = getTicks(ctx.element, ".axis-y");
  178. expect(yTicks).to.eql(['1', '32', '1.0 K']);
  179. });
  180. });
  181. heatmapScenario('when logBase is 1024', function (ctx) {
  182. ctx.setup(function (ctrl) {
  183. ctrl.panel.yAxis.logBase = 1024;
  184. ctx.series.push(new TimeSeries({
  185. datapoints: [[2000, 1422774000000], [300000, 1422774060000]],
  186. alias: 'series3'
  187. }));
  188. ctx.data.heatmapStats.max = 300000;
  189. });
  190. it('should draw correct Y axis', function () {
  191. var yTicks = getTicks(ctx.element, ".axis-y");
  192. expect(yTicks).to.eql(['1', '1 K', '1.0 Mil']);
  193. });
  194. });
  195. heatmapScenario('when Y axis format set to "none"', function (ctx) {
  196. ctx.setup(function (ctrl) {
  197. ctrl.panel.yAxis.logBase = 1;
  198. ctrl.panel.yAxis.format = "none";
  199. ctx.data.heatmapStats.max = 10000;
  200. });
  201. it('should draw correct Y axis', function () {
  202. var yTicks = getTicks(ctx.element, ".axis-y");
  203. expect(yTicks).to.eql(['0', '2000', '4000', '6000', '8000', '10000', '12000']);
  204. });
  205. });
  206. heatmapScenario('when Y axis format set to "second"', function (ctx) {
  207. ctx.setup(function (ctrl) {
  208. ctrl.panel.yAxis.logBase = 1;
  209. ctrl.panel.yAxis.format = "s";
  210. ctx.data.heatmapStats.max = 3600;
  211. });
  212. it('should draw correct Y axis', function () {
  213. var yTicks = getTicks(ctx.element, ".axis-y");
  214. expect(yTicks).to.eql(['0 ns', '17 min', '33 min', '50 min', '1.11 hour']);
  215. });
  216. });
  217. });
  218. function getTicks(element, axisSelector) {
  219. return element.find(axisSelector).find("text")
  220. .map(function () {
  221. return this.textContent;
  222. }).get();
  223. }
  224. function formatTime(timeStr) {
  225. let format = "HH:mm";
  226. return moment.utc(timeStr, 'DD MMM YYYY HH:mm:ss').format(format);
  227. }