singlestat_specs.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import {
  2. describe,
  3. beforeEach,
  4. afterEach,
  5. it,
  6. sinon,
  7. expect,
  8. angularMocks
  9. } from "test/lib/common";
  10. import helpers from "test/specs/helpers";
  11. import { SingleStatCtrl } from "../module";
  12. import moment from "moment";
  13. describe("SingleStatCtrl", function() {
  14. var ctx = new helpers.ControllerTestContext();
  15. var epoch = 1505826363746;
  16. var clock;
  17. function singleStatScenario(desc, func) {
  18. describe(desc, function() {
  19. ctx.setup = function(setupFunc) {
  20. beforeEach(angularMocks.module("grafana.services"));
  21. beforeEach(angularMocks.module("grafana.controllers"));
  22. beforeEach(
  23. angularMocks.module(function($compileProvider) {
  24. $compileProvider.preAssignBindingsEnabled(true);
  25. })
  26. );
  27. beforeEach(ctx.providePhase());
  28. beforeEach(ctx.createPanelController(SingleStatCtrl));
  29. beforeEach(function() {
  30. setupFunc();
  31. ctx.ctrl.onDataReceived(ctx.data);
  32. ctx.data = ctx.ctrl.data;
  33. });
  34. };
  35. func(ctx);
  36. });
  37. }
  38. singleStatScenario("with defaults", function(ctx) {
  39. ctx.setup(function() {
  40. ctx.data = [{ target: "test.cpu1", datapoints: [[10, 1], [20, 2]] }];
  41. });
  42. it("Should use series avg as default main value", function() {
  43. expect(ctx.data.value).to.be(15);
  44. expect(ctx.data.valueRounded).to.be(15);
  45. });
  46. it("should set formatted falue", function() {
  47. expect(ctx.data.valueFormatted).to.be("15");
  48. });
  49. });
  50. singleStatScenario("showing serie name instead of value", function(ctx) {
  51. ctx.setup(function() {
  52. ctx.data = [{ target: "test.cpu1", datapoints: [[10, 1], [20, 2]] }];
  53. ctx.ctrl.panel.valueName = "name";
  54. });
  55. it("Should use series avg as default main value", function() {
  56. expect(ctx.data.value).to.be(0);
  57. expect(ctx.data.valueRounded).to.be(0);
  58. });
  59. it("should set formatted value", function() {
  60. expect(ctx.data.valueFormatted).to.be("test.cpu1");
  61. });
  62. });
  63. singleStatScenario("showing last iso time instead of value", function(ctx) {
  64. ctx.setup(function() {
  65. ctx.data = [
  66. { target: "test.cpu1", datapoints: [[10, 12], [20, 1505634997920]] }
  67. ];
  68. ctx.ctrl.panel.valueName = "last_time";
  69. ctx.ctrl.panel.format = "dateTimeAsIso";
  70. });
  71. it("Should use time instead of value", function() {
  72. expect(ctx.data.value).to.be(1505634997920);
  73. expect(ctx.data.valueRounded).to.be(1505634997920);
  74. });
  75. it("should set formatted value", function() {
  76. expect(ctx.data.valueFormatted).to.be(
  77. moment(1505634997920).format("YYYY-MM-DD HH:mm:ss")
  78. );
  79. });
  80. });
  81. singleStatScenario("showing last us time instead of value", function(ctx) {
  82. ctx.setup(function() {
  83. ctx.data = [
  84. { target: "test.cpu1", datapoints: [[10, 12], [20, 1505634997920]] }
  85. ];
  86. ctx.ctrl.panel.valueName = "last_time";
  87. ctx.ctrl.panel.format = "dateTimeAsUS";
  88. });
  89. it("Should use time instead of value", function() {
  90. expect(ctx.data.value).to.be(1505634997920);
  91. expect(ctx.data.valueRounded).to.be(1505634997920);
  92. });
  93. it("should set formatted value", function() {
  94. expect(ctx.data.valueFormatted).to.be(
  95. moment(1505634997920).format("MM/DD/YYYY h:mm:ss a")
  96. );
  97. });
  98. });
  99. singleStatScenario("showing last time from now instead of value", function(
  100. ctx
  101. ) {
  102. beforeEach(() => {
  103. clock = sinon.useFakeTimers(epoch);
  104. });
  105. ctx.setup(function() {
  106. ctx.data = [
  107. { target: "test.cpu1", datapoints: [[10, 12], [20, 1505634997920]] }
  108. ];
  109. ctx.ctrl.panel.valueName = "last_time";
  110. ctx.ctrl.panel.format = "dateTimeFromNow";
  111. });
  112. it("Should use time instead of value", function() {
  113. expect(ctx.data.value).to.be(1505634997920);
  114. expect(ctx.data.valueRounded).to.be(1505634997920);
  115. });
  116. it("should set formatted value", function() {
  117. expect(ctx.data.valueFormatted).to.be("2 days ago");
  118. });
  119. afterEach(() => {
  120. clock.restore();
  121. });
  122. });
  123. singleStatScenario(
  124. "MainValue should use same number for decimals as displayed when checking thresholds",
  125. function(ctx) {
  126. ctx.setup(function() {
  127. ctx.data = [
  128. { target: "test.cpu1", datapoints: [[99.999, 1], [99.99999, 2]] }
  129. ];
  130. });
  131. it("Should be rounded", function() {
  132. expect(ctx.data.value).to.be(99.999495);
  133. expect(ctx.data.valueRounded).to.be(100);
  134. });
  135. it("should set formatted value", function() {
  136. expect(ctx.data.valueFormatted).to.be("100");
  137. });
  138. }
  139. );
  140. singleStatScenario("When value to text mapping is specified", function(ctx) {
  141. ctx.setup(function() {
  142. ctx.data = [{ target: "test.cpu1", datapoints: [[9.9, 1]] }];
  143. ctx.ctrl.panel.valueMaps = [{ value: "10", text: "OK" }];
  144. });
  145. it("value should remain", function() {
  146. expect(ctx.data.value).to.be(9.9);
  147. });
  148. it("round should be rounded up", function() {
  149. expect(ctx.data.valueRounded).to.be(10);
  150. });
  151. it("Should replace value with text", function() {
  152. expect(ctx.data.valueFormatted).to.be("OK");
  153. });
  154. });
  155. singleStatScenario(
  156. "When range to text mapping is specified for first range",
  157. function(ctx) {
  158. ctx.setup(function() {
  159. ctx.data = [{ target: "test.cpu1", datapoints: [[41, 50]] }];
  160. ctx.ctrl.panel.mappingType = 2;
  161. ctx.ctrl.panel.rangeMaps = [
  162. { from: "10", to: "50", text: "OK" },
  163. { from: "51", to: "100", text: "NOT OK" }
  164. ];
  165. });
  166. it("Should replace value with text OK", function() {
  167. expect(ctx.data.valueFormatted).to.be("OK");
  168. });
  169. }
  170. );
  171. singleStatScenario(
  172. "When range to text mapping is specified for other ranges",
  173. function(ctx) {
  174. ctx.setup(function() {
  175. ctx.data = [{ target: "test.cpu1", datapoints: [[65, 75]] }];
  176. ctx.ctrl.panel.mappingType = 2;
  177. ctx.ctrl.panel.rangeMaps = [
  178. { from: "10", to: "50", text: "OK" },
  179. { from: "51", to: "100", text: "NOT OK" }
  180. ];
  181. });
  182. it("Should replace value with text NOT OK", function() {
  183. expect(ctx.data.valueFormatted).to.be("NOT OK");
  184. });
  185. }
  186. );
  187. describe("When table data", function() {
  188. const tableData = [
  189. {
  190. columns: [
  191. { text: "Time", type: "time" },
  192. { text: "test1" },
  193. { text: "mean" },
  194. { text: "test2" }
  195. ],
  196. rows: [[1492759673649, "ignore1", 15, "ignore2"]],
  197. type: "table"
  198. }
  199. ];
  200. singleStatScenario("with default values", function(ctx) {
  201. ctx.setup(function() {
  202. ctx.data = tableData;
  203. ctx.ctrl.panel.tableColumn = "mean";
  204. });
  205. it("Should use first rows value as default main value", function() {
  206. expect(ctx.data.value).to.be(15);
  207. expect(ctx.data.valueRounded).to.be(15);
  208. });
  209. it("should set formatted value", function() {
  210. expect(ctx.data.valueFormatted).to.be("15");
  211. });
  212. });
  213. singleStatScenario("When table data has multiple columns", function(ctx) {
  214. ctx.setup(function() {
  215. ctx.data = tableData;
  216. ctx.ctrl.panel.tableColumn = "";
  217. });
  218. it("Should set column to first column that is not time", function() {
  219. expect(ctx.ctrl.panel.tableColumn).to.be("test1");
  220. });
  221. });
  222. singleStatScenario(
  223. "MainValue should use same number for decimals as displayed when checking thresholds",
  224. function(ctx) {
  225. ctx.setup(function() {
  226. ctx.data = tableData;
  227. ctx.data[0].rows[0] = [1492759673649, "ignore1", 99.99999, "ignore2"];
  228. ctx.ctrl.panel.tableColumn = "mean";
  229. });
  230. it("Should be rounded", function() {
  231. expect(ctx.data.value).to.be(99.99999);
  232. expect(ctx.data.valueRounded).to.be(100);
  233. });
  234. it("should set formatted falue", function() {
  235. expect(ctx.data.valueFormatted).to.be("100");
  236. });
  237. }
  238. );
  239. singleStatScenario("When value to text mapping is specified", function(
  240. ctx
  241. ) {
  242. ctx.setup(function() {
  243. ctx.data = tableData;
  244. ctx.data[0].rows[0] = [1492759673649, "ignore1", 9.9, "ignore2"];
  245. ctx.ctrl.panel.tableColumn = "mean";
  246. ctx.ctrl.panel.valueMaps = [{ value: "10", text: "OK" }];
  247. });
  248. it("value should remain", function() {
  249. expect(ctx.data.value).to.be(9.9);
  250. });
  251. it("round should be rounded up", function() {
  252. expect(ctx.data.valueRounded).to.be(10);
  253. });
  254. it("Should replace value with text", function() {
  255. expect(ctx.data.valueFormatted).to.be("OK");
  256. });
  257. });
  258. singleStatScenario(
  259. "When range to text mapping is specified for first range",
  260. function(ctx) {
  261. ctx.setup(function() {
  262. ctx.data = tableData;
  263. ctx.data[0].rows[0] = [1492759673649, "ignore1", 41, "ignore2"];
  264. ctx.ctrl.panel.tableColumn = "mean";
  265. ctx.ctrl.panel.mappingType = 2;
  266. ctx.ctrl.panel.rangeMaps = [
  267. { from: "10", to: "50", text: "OK" },
  268. { from: "51", to: "100", text: "NOT OK" }
  269. ];
  270. });
  271. it("Should replace value with text OK", function() {
  272. expect(ctx.data.valueFormatted).to.be("OK");
  273. });
  274. }
  275. );
  276. singleStatScenario(
  277. "When range to text mapping is specified for other ranges",
  278. function(ctx) {
  279. ctx.setup(function() {
  280. ctx.data = tableData;
  281. ctx.data[0].rows[0] = [1492759673649, "ignore1", 65, "ignore2"];
  282. ctx.ctrl.panel.tableColumn = "mean";
  283. ctx.ctrl.panel.mappingType = 2;
  284. ctx.ctrl.panel.rangeMaps = [
  285. { from: "10", to: "50", text: "OK" },
  286. { from: "51", to: "100", text: "NOT OK" }
  287. ];
  288. });
  289. it("Should replace value with text NOT OK", function() {
  290. expect(ctx.data.valueFormatted).to.be("NOT OK");
  291. });
  292. }
  293. );
  294. singleStatScenario("When value is string", function(ctx) {
  295. ctx.setup(function() {
  296. ctx.data = tableData;
  297. ctx.data[0].rows[0] = [1492759673649, "ignore1", 65, "ignore2"];
  298. ctx.ctrl.panel.tableColumn = "test1";
  299. });
  300. it("Should replace value with text NOT OK", function() {
  301. expect(ctx.data.valueFormatted).to.be("ignore1");
  302. });
  303. });
  304. singleStatScenario("When value is zero", function(ctx) {
  305. ctx.setup(function() {
  306. ctx.data = tableData;
  307. ctx.data[0].rows[0] = [1492759673649, "ignore1", 0, "ignore2"];
  308. ctx.ctrl.panel.tableColumn = "mean";
  309. });
  310. it("Should return zero", function() {
  311. expect(ctx.data.value).to.be(0);
  312. });
  313. });
  314. });
  315. });