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 } from '../heatmap_data_converter';
  13. // import d3 from 'd3';
  14. describe('grafanaHeatmap', function () {
  15. beforeEach(angularMocks.module('grafana.core'));
  16. function heatmapScenario(desc, func, elementWidth = 500) {
  17. describe(desc, function () {
  18. var ctx: any = {};
  19. ctx.setup = function (setupFunc) {
  20. beforeEach(angularMocks.module(function ($provide) {
  21. $provide.value("timeSrv", new helpers.TimeSrvStub());
  22. }));
  23. beforeEach(angularMocks.inject(function ($rootScope, $compile) {
  24. var ctrl: any = {
  25. colorSchemes: [
  26. {name: 'Oranges', value: 'interpolateOranges', invert: 'dark'},
  27. {name: 'Reds', value: 'interpolateReds', invert: 'dark'},
  28. ],
  29. events: new Emitter(),
  30. height: 200,
  31. panel: {
  32. heatmap: {
  33. },
  34. cards: {
  35. cardPadding: null,
  36. cardRound: null
  37. },
  38. color: {
  39. mode: 'spectrum',
  40. cardColor: '#b4ff00',
  41. colorScale: 'linear',
  42. exponent: 0.5,
  43. colorScheme: 'interpolateOranges',
  44. fillBackground: false
  45. },
  46. xBucketSize: 1000,
  47. xBucketNumber: null,
  48. yBucketSize: 1,
  49. yBucketNumber: null,
  50. xAxis: {
  51. show: true
  52. },
  53. yAxis: {
  54. show: true,
  55. format: 'short',
  56. decimals: null,
  57. logBase: 1,
  58. splitFactor: null,
  59. min: null,
  60. max: null,
  61. removeZeroValues: false
  62. },
  63. tooltip: {
  64. show: true,
  65. seriesStat: false,
  66. showHistogram: false
  67. },
  68. highlightCards: true
  69. },
  70. renderingCompleted: sinon.spy(),
  71. hiddenSeries: {},
  72. dashboard: {
  73. getTimezone: sinon.stub().returns('utc')
  74. },
  75. range: {
  76. from: moment.utc("01 Mar 2017 10:00:00", 'DD MMM YYYY HH:mm:ss'),
  77. to: moment.utc("01 Mar 2017 11:00:00", 'DD MMM YYYY HH:mm:ss'),
  78. },
  79. };
  80. var scope = $rootScope.$new();
  81. scope.ctrl = ctrl;
  82. ctx.series = [];
  83. ctx.series.push(new TimeSeries({
  84. datapoints: [[1, 1422774000000], [2, 1422774060000]],
  85. alias: 'series1'
  86. }));
  87. ctx.series.push(new TimeSeries({
  88. datapoints: [[2, 1422774000000], [3, 1422774060000]],
  89. alias: 'series2'
  90. }));
  91. ctx.data = {
  92. heatmapStats: {
  93. min: 1,
  94. max: 3,
  95. minLog: 1
  96. },
  97. xBucketSize: ctrl.panel.xBucketSize,
  98. yBucketSize: ctrl.panel.yBucketSize
  99. };
  100. setupFunc(ctrl, ctx);
  101. let logBase = ctrl.panel.yAxis.logBase;
  102. let bucketsData = convertToHeatMap(ctx.series, ctx.data.yBucketSize, ctx.data.xBucketSize, logBase);
  103. ctx.data.buckets = bucketsData;
  104. // console.log("bucketsData", bucketsData);
  105. // console.log("series", ctrl.panel.yAxis.logBase, ctx.series.length);
  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. }