query_ctrl.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import { StackdriverQueryCtrl } from '../query_ctrl';
  2. describe('StackdriverQueryCtrl', () => {
  3. let ctrl;
  4. let result;
  5. describe('when initializing query editor', () => {
  6. beforeEach(() => {
  7. const existingFilters = ['key1', '=', 'val1', 'AND', 'key2', '=', 'val2'];
  8. ctrl = createCtrlWithFakes(existingFilters);
  9. });
  10. it('should initialize filter segments using the target filter values', () => {
  11. expect(ctrl.filterSegments.length).toBe(8);
  12. expect(ctrl.filterSegments[0].type).toBe('key');
  13. expect(ctrl.filterSegments[1].type).toBe('operator');
  14. expect(ctrl.filterSegments[2].type).toBe('value');
  15. expect(ctrl.filterSegments[3].type).toBe('condition');
  16. expect(ctrl.filterSegments[4].type).toBe('key');
  17. expect(ctrl.filterSegments[5].type).toBe('operator');
  18. expect(ctrl.filterSegments[6].type).toBe('value');
  19. expect(ctrl.filterSegments[7].type).toBe('plus-button');
  20. });
  21. });
  22. describe('group bys', () => {
  23. beforeEach(() => {
  24. ctrl = createCtrlWithFakes();
  25. });
  26. describe('when labels are fetched', () => {
  27. beforeEach(async () => {
  28. ctrl.metricLabels = { 'metric-key-1': ['metric-value-1'] };
  29. ctrl.resourceLabels = { 'resource-key-1': ['resource-value-1'] };
  30. result = await ctrl.getGroupBys();
  31. });
  32. it('should populate group bys segments', () => {
  33. expect(result.length).toBe(3);
  34. expect(result[0].value).toBe('metric.label.metric-key-1');
  35. expect(result[1].value).toBe('resource.label.resource-key-1');
  36. expect(result[2].value).toBe('-- remove group by --');
  37. });
  38. });
  39. describe('when a group by label is selected', () => {
  40. beforeEach(async () => {
  41. ctrl.metricLabels = {
  42. 'metric-key-1': ['metric-value-1'],
  43. 'metric-key-2': ['metric-value-2'],
  44. };
  45. ctrl.resourceLabels = {
  46. 'resource-key-1': ['resource-value-1'],
  47. 'resource-key-2': ['resource-value-2'],
  48. };
  49. ctrl.target.aggregation.groupBys = ['metric.label.metric-key-1', 'resource.label.resource-key-1'];
  50. result = await ctrl.getGroupBys();
  51. });
  52. it('should not be used to populate group bys segments', () => {
  53. expect(result.length).toBe(3);
  54. expect(result[0].value).toBe('metric.label.metric-key-2');
  55. expect(result[1].value).toBe('resource.label.resource-key-2');
  56. expect(result[2].value).toBe('-- remove group by --');
  57. });
  58. });
  59. describe('when a group by is selected', () => {
  60. beforeEach(() => {
  61. const removeSegment = { fake: true, value: '-- remove group by --' };
  62. const segment = { value: 'groupby1' };
  63. ctrl.groupBySegments = [segment, removeSegment];
  64. ctrl.groupByChanged(segment);
  65. });
  66. it('should be added to group bys list', () => {
  67. expect(ctrl.target.aggregation.groupBys.length).toBe(1);
  68. });
  69. });
  70. describe('when a selected group by is removed', () => {
  71. beforeEach(() => {
  72. const removeSegment = { fake: true, value: '-- remove group by --' };
  73. const segment = { value: 'groupby1' };
  74. ctrl.groupBySegments = [segment, removeSegment];
  75. ctrl.groupByChanged(removeSegment);
  76. });
  77. it('should be added to group bys list', () => {
  78. expect(ctrl.target.aggregation.groupBys.length).toBe(0);
  79. });
  80. });
  81. });
  82. describe('filters', () => {
  83. beforeEach(() => {
  84. ctrl = createCtrlWithFakes();
  85. });
  86. describe('when values for a condition filter part are fetched', () => {
  87. beforeEach(async () => {
  88. const segment = { type: 'condition' };
  89. result = await ctrl.getFilters(segment, 0);
  90. });
  91. it('should populate group bys segments', () => {
  92. expect(result.length).toBe(1);
  93. expect(result[0].value).toBe('AND');
  94. });
  95. });
  96. describe('when values for a operator filter part are fetched', () => {
  97. beforeEach(async () => {
  98. const segment = { type: 'operator' };
  99. result = await ctrl.getFilters(segment, 0);
  100. });
  101. it('should populate group bys segments', () => {
  102. expect(result.length).toBe(4);
  103. expect(result[0].value).toBe('=');
  104. expect(result[1].value).toBe('!=');
  105. expect(result[2].value).toBe('=~');
  106. expect(result[3].value).toBe('!=~');
  107. });
  108. });
  109. describe('when values for a key filter part are fetched', () => {
  110. beforeEach(async () => {
  111. ctrl.metricLabels = {
  112. 'metric-key-1': ['metric-value-1'],
  113. 'metric-key-2': ['metric-value-2'],
  114. };
  115. ctrl.resourceLabels = {
  116. 'resource-key-1': ['resource-value-1'],
  117. 'resource-key-2': ['resource-value-2'],
  118. };
  119. const segment = { type: 'key' };
  120. result = await ctrl.getFilters(segment, 0);
  121. });
  122. it('should populate group bys segments', () => {
  123. expect(result.length).toBe(5);
  124. expect(result[0].value).toBe('metric.label.metric-key-1');
  125. expect(result[1].value).toBe('metric.label.metric-key-2');
  126. expect(result[2].value).toBe('resource.label.resource-key-1');
  127. expect(result[3].value).toBe('resource.label.resource-key-2');
  128. expect(result[4].value).toBe('-- remove filter --');
  129. });
  130. });
  131. describe('when values for a value filter part are fetched', () => {
  132. beforeEach(async () => {
  133. ctrl.metricLabels = {
  134. 'metric-key-1': ['metric-value-1'],
  135. 'metric-key-2': ['metric-value-2'],
  136. };
  137. ctrl.resourceLabels = {
  138. 'resource-key-1': ['resource-value-1'],
  139. 'resource-key-2': ['resource-value-2'],
  140. };
  141. ctrl.filterSegments = [{ type: 'key', value: 'metric.label.metric-key-1' }, { type: 'operator', value: '=' }];
  142. const segment = { type: 'value' };
  143. result = await ctrl.getFilters(segment, 2);
  144. });
  145. it('should populate group bys segments', () => {
  146. expect(result.length).toBe(1);
  147. expect(result[0].value).toBe('metric-value-1');
  148. });
  149. });
  150. describe('when a filter is created by clicking on plus button', () => {
  151. describe('and there are no other filters', () => {
  152. beforeEach(() => {
  153. const segment = { value: 'filterkey1', type: 'plus-button' };
  154. ctrl.filterSegments = [segment];
  155. ctrl.filterSegmentUpdated(segment, 0);
  156. });
  157. it('should transform the plus button segment to a key segment', () => {
  158. expect(ctrl.filterSegments[0].type).toBe('key');
  159. });
  160. it('should add an operator, value segment and plus button segment', () => {
  161. expect(ctrl.filterSegments.length).toBe(3);
  162. expect(ctrl.filterSegments[1].type).toBe('operator');
  163. expect(ctrl.filterSegments[2].type).toBe('value');
  164. });
  165. });
  166. });
  167. describe('when has one existing filter', () => {
  168. describe('and user clicks on key segment', () => {
  169. beforeEach(() => {
  170. const existingKeySegment = { value: 'filterkey1', type: 'key' };
  171. const existingOperatorSegment = { value: '=', type: 'operator' };
  172. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  173. const plusSegment = { value: '', type: 'plus-button' };
  174. ctrl.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment, plusSegment];
  175. ctrl.filterSegmentUpdated(existingKeySegment, 0);
  176. });
  177. it('should not add any new segments', () => {
  178. expect(ctrl.filterSegments.length).toBe(4);
  179. expect(ctrl.filterSegments[0].type).toBe('key');
  180. expect(ctrl.filterSegments[1].type).toBe('operator');
  181. expect(ctrl.filterSegments[2].type).toBe('value');
  182. });
  183. });
  184. describe('and user clicks on value segment and value not equal to fake value', () => {
  185. beforeEach(() => {
  186. const existingKeySegment = { value: 'filterkey1', type: 'key' };
  187. const existingOperatorSegment = { value: '=', type: 'operator' };
  188. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  189. ctrl.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment];
  190. ctrl.filterSegmentUpdated(existingValueSegment, 2);
  191. });
  192. it('should ensure that plus segment exists', () => {
  193. expect(ctrl.filterSegments.length).toBe(4);
  194. expect(ctrl.filterSegments[0].type).toBe('key');
  195. expect(ctrl.filterSegments[1].type).toBe('operator');
  196. expect(ctrl.filterSegments[2].type).toBe('value');
  197. expect(ctrl.filterSegments[3].type).toBe('plus-button');
  198. });
  199. });
  200. describe('and user clicks on value segment and value is equal to fake value', () => {
  201. beforeEach(() => {
  202. const existingKeySegment = { value: 'filterkey1', type: 'key' };
  203. const existingOperatorSegment = { value: '=', type: 'operator' };
  204. const existingValueSegment = { value: ctrl.defaultFilterValue, type: 'value' };
  205. ctrl.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment];
  206. ctrl.filterSegmentUpdated(existingValueSegment, 2);
  207. });
  208. it('should not add plus segment', () => {
  209. expect(ctrl.filterSegments.length).toBe(3);
  210. expect(ctrl.filterSegments[0].type).toBe('key');
  211. expect(ctrl.filterSegments[1].type).toBe('operator');
  212. expect(ctrl.filterSegments[2].type).toBe('value');
  213. });
  214. });
  215. describe('and user removes key segment', () => {
  216. beforeEach(() => {
  217. const existingKeySegment = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
  218. const existingOperatorSegment = { value: '=', type: 'operator' };
  219. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  220. const plusSegment = { value: '', type: 'plus-button' };
  221. ctrl.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment, plusSegment];
  222. ctrl.filterSegmentUpdated(existingKeySegment, 0);
  223. });
  224. it('should remove filter segments', () => {
  225. expect(ctrl.filterSegments.length).toBe(1);
  226. expect(ctrl.filterSegments[0].type).toBe('plus-button');
  227. });
  228. });
  229. describe('and user removes key segment and there is a previous filter', () => {
  230. beforeEach(() => {
  231. const existingKeySegment1 = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
  232. const existingKeySegment2 = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
  233. const existingOperatorSegment = { value: '=', type: 'operator' };
  234. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  235. const conditionSegment = { value: 'AND', type: 'condition' };
  236. const plusSegment = { value: '', type: 'plus-button' };
  237. ctrl.filterSegments = [
  238. existingKeySegment1,
  239. existingOperatorSegment,
  240. existingValueSegment,
  241. conditionSegment,
  242. existingKeySegment2,
  243. Object.assign({}, existingOperatorSegment),
  244. Object.assign({}, existingValueSegment),
  245. plusSegment,
  246. ];
  247. ctrl.filterSegmentUpdated(existingKeySegment2, 4);
  248. });
  249. it('should remove filter segments and the condition segment', () => {
  250. expect(ctrl.filterSegments.length).toBe(4);
  251. expect(ctrl.filterSegments[0].type).toBe('key');
  252. expect(ctrl.filterSegments[1].type).toBe('operator');
  253. expect(ctrl.filterSegments[2].type).toBe('value');
  254. expect(ctrl.filterSegments[3].type).toBe('plus-button');
  255. });
  256. });
  257. describe('and user removes key segment and there is a filter after it', () => {
  258. beforeEach(() => {
  259. const existingKeySegment1 = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
  260. const existingKeySegment2 = { value: ctrl.defaultRemoveFilterValue, type: 'key' };
  261. const existingOperatorSegment = { value: '=', type: 'operator' };
  262. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  263. const conditionSegment = { value: 'AND', type: 'condition' };
  264. const plusSegment = { value: '', type: 'plus-button' };
  265. ctrl.filterSegments = [
  266. existingKeySegment1,
  267. existingOperatorSegment,
  268. existingValueSegment,
  269. conditionSegment,
  270. existingKeySegment2,
  271. Object.assign({}, existingOperatorSegment),
  272. Object.assign({}, existingValueSegment),
  273. plusSegment,
  274. ];
  275. ctrl.filterSegmentUpdated(existingKeySegment1, 0);
  276. });
  277. it('should remove filter segments and the condition segment', () => {
  278. expect(ctrl.filterSegments.length).toBe(4);
  279. expect(ctrl.filterSegments[0].type).toBe('key');
  280. expect(ctrl.filterSegments[1].type).toBe('operator');
  281. expect(ctrl.filterSegments[2].type).toBe('value');
  282. expect(ctrl.filterSegments[3].type).toBe('plus-button');
  283. });
  284. });
  285. describe('and user clicks on plus button', () => {
  286. beforeEach(() => {
  287. const existingKeySegment = { value: 'filterkey1', type: 'key' };
  288. const existingOperatorSegment = { value: '=', type: 'operator' };
  289. const existingValueSegment = { value: 'filtervalue', type: 'value' };
  290. const plusSegment = { value: 'filterkey2', type: 'plus-button' };
  291. ctrl.filterSegments = [existingKeySegment, existingOperatorSegment, existingValueSegment, plusSegment];
  292. ctrl.filterSegmentUpdated(plusSegment, 3);
  293. });
  294. it('should condition segment and new filter segments', () => {
  295. expect(ctrl.filterSegments.length).toBe(7);
  296. expect(ctrl.filterSegments[0].type).toBe('key');
  297. expect(ctrl.filterSegments[1].type).toBe('operator');
  298. expect(ctrl.filterSegments[2].type).toBe('value');
  299. expect(ctrl.filterSegments[3].type).toBe('condition');
  300. expect(ctrl.filterSegments[4].type).toBe('key');
  301. expect(ctrl.filterSegments[5].type).toBe('operator');
  302. expect(ctrl.filterSegments[6].type).toBe('value');
  303. });
  304. });
  305. });
  306. });
  307. });
  308. function createCtrlWithFakes(existingFilters?: string[]) {
  309. StackdriverQueryCtrl.prototype.panelCtrl = {
  310. events: { on: () => {} },
  311. panel: { scopedVars: [], targets: [] },
  312. refresh: () => {},
  313. };
  314. StackdriverQueryCtrl.prototype.target = createTarget(existingFilters);
  315. StackdriverQueryCtrl.prototype.getMetricTypes = () => {
  316. return Promise.resolve();
  317. };
  318. StackdriverQueryCtrl.prototype.getLabels = () => {
  319. return Promise.resolve();
  320. };
  321. const fakeSegmentServer = {
  322. newKey: val => {
  323. return { value: val, type: 'key' };
  324. },
  325. newKeyValue: val => {
  326. return { value: val, type: 'value' };
  327. },
  328. newSegment: obj => {
  329. return { value: obj.value ? obj.value : obj };
  330. },
  331. newOperators: ops => {
  332. return ops.map(o => {
  333. return { type: 'operator', value: o };
  334. });
  335. },
  336. newFake: (value, type, cssClass) => {
  337. return { value, type, cssClass };
  338. },
  339. newOperator: op => {
  340. return { value: op, type: 'operator' };
  341. },
  342. newPlusButton: () => {
  343. return { type: 'plus-button' };
  344. },
  345. newCondition: val => {
  346. return { type: 'condition', value: val };
  347. },
  348. };
  349. return new StackdriverQueryCtrl(null, null, fakeSegmentServer, null);
  350. }
  351. function createTarget(existingFilters?: string[]) {
  352. return {
  353. project: {
  354. id: '',
  355. name: '',
  356. },
  357. metricType: 'ametric',
  358. refId: 'A',
  359. aggregation: {
  360. crossSeriesReducer: '',
  361. secondaryCrossSeriesReducer: '',
  362. alignmentPeriod: '',
  363. perSeriesAligner: '',
  364. groupBys: [],
  365. },
  366. filters: existingFilters || [],
  367. };
  368. }