renderer_specs.ts 7.8 KB

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