renderer_specs.ts 8.1 KB

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