renderer_specs.ts 8.2 KB

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