singlestat_specs.ts 8.2 KB

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