singlestat_specs.ts 10 KB

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