renderer_specs.ts 8.4 KB

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