renderer_specs.ts 8.2 KB

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