variable_srv.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import '../all';
  2. import { VariableSrv } from '../variable_srv';
  3. import { DashboardModel } from '../../dashboard/state/DashboardModel';
  4. // @ts-ignore
  5. import $q from 'q';
  6. import { dateTime } from '@grafana/data';
  7. import { CustomVariable } from '../custom_variable';
  8. describe('VariableSrv', function(this: any) {
  9. const ctx = {
  10. datasourceSrv: {},
  11. timeSrv: {
  12. timeRange: () => {
  13. return { from: '2018-01-29', to: '2019-01-29' };
  14. },
  15. },
  16. $rootScope: {
  17. $on: () => {},
  18. },
  19. $injector: {
  20. instantiate: (ctr: any, obj: { model: any }) => new ctr(obj.model),
  21. },
  22. templateSrv: {
  23. setGrafanaVariable: jest.fn(),
  24. init: (vars: any) => {
  25. this.variables = vars;
  26. },
  27. updateIndex: () => {},
  28. replace: (str: any) =>
  29. str.replace(this.regex, (match: string) => {
  30. return match;
  31. }),
  32. },
  33. $location: {
  34. search: () => {},
  35. },
  36. } as any;
  37. function describeUpdateVariable(desc: string, fn: Function) {
  38. describe(desc, () => {
  39. const scenario: any = {};
  40. scenario.setup = (setupFn: Function) => {
  41. scenario.setupFn = setupFn;
  42. };
  43. beforeEach(async () => {
  44. scenario.setupFn();
  45. const ds: any = {};
  46. ds.metricFindQuery = () => Promise.resolve(scenario.queryResult);
  47. ctx.variableSrv = new VariableSrv($q, ctx.$location, ctx.$injector, ctx.templateSrv, ctx.timeSrv);
  48. ctx.variableSrv.timeSrv = ctx.timeSrv;
  49. ctx.datasourceSrv = {
  50. get: () => Promise.resolve(ds),
  51. getMetricSources: () => scenario.metricSources,
  52. };
  53. ctx.$injector.instantiate = (ctr: any, model: any) => {
  54. return getVarMockConstructor(ctr, model, ctx);
  55. };
  56. ctx.variableSrv.init(
  57. new DashboardModel({
  58. templating: { list: [] },
  59. updateSubmenuVisibility: () => {},
  60. })
  61. );
  62. scenario.variable = ctx.variableSrv.createVariableFromModel(scenario.variableModel);
  63. ctx.variableSrv.addVariable(scenario.variable);
  64. await ctx.variableSrv.updateOptions(scenario.variable);
  65. });
  66. fn(scenario);
  67. });
  68. }
  69. describeUpdateVariable('interval variable without auto', (scenario: any) => {
  70. scenario.setup(() => {
  71. scenario.variableModel = {
  72. type: 'interval',
  73. query: '1s,2h,5h,1d',
  74. name: 'test',
  75. };
  76. });
  77. it('should update options array', () => {
  78. expect(scenario.variable.options.length).toBe(4);
  79. expect(scenario.variable.options[0].text).toBe('1s');
  80. expect(scenario.variable.options[0].value).toBe('1s');
  81. });
  82. });
  83. //
  84. // Interval variable update
  85. //
  86. describeUpdateVariable('interval variable with auto', (scenario: any) => {
  87. scenario.setup(() => {
  88. scenario.variableModel = {
  89. type: 'interval',
  90. query: '1s,2h,5h,1d',
  91. name: 'test',
  92. auto: true,
  93. auto_count: 10,
  94. };
  95. const range = {
  96. from: dateTime(new Date())
  97. .subtract(7, 'days')
  98. .toDate(),
  99. to: new Date(),
  100. };
  101. ctx.timeSrv.timeRange = () => range;
  102. // ctx.templateSrv.setGrafanaVariable = jest.fn();
  103. });
  104. it('should update options array', () => {
  105. expect(scenario.variable.options.length).toBe(5);
  106. expect(scenario.variable.options[0].text).toBe('auto');
  107. expect(scenario.variable.options[0].value).toBe('$__auto_interval_test');
  108. });
  109. it('should set $__auto_interval_test', () => {
  110. const call = ctx.templateSrv.setGrafanaVariable.mock.calls[0];
  111. expect(call[0]).toBe('$__auto_interval_test');
  112. expect(call[1]).toBe('12h');
  113. });
  114. // updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState()
  115. // So use lastCall instead of a specific call number
  116. it('should set $__auto_interval', () => {
  117. const call = ctx.templateSrv.setGrafanaVariable.mock.calls.pop();
  118. expect(call[0]).toBe('$__auto_interval');
  119. expect(call[1]).toBe('12h');
  120. });
  121. });
  122. //
  123. // Query variable update
  124. //
  125. describeUpdateVariable('query variable with empty current object and refresh', (scenario: any) => {
  126. scenario.setup(() => {
  127. scenario.variableModel = {
  128. type: 'query',
  129. query: '',
  130. name: 'test',
  131. current: {},
  132. };
  133. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  134. });
  135. it('should set current value to first option', () => {
  136. expect(scenario.variable.options.length).toBe(2);
  137. expect(scenario.variable.current.value).toBe('backend1');
  138. });
  139. });
  140. describeUpdateVariable(
  141. 'query variable with multi select and new options does not contain some selected values',
  142. (scenario: any) => {
  143. scenario.setup(() => {
  144. scenario.variableModel = {
  145. type: 'query',
  146. query: '',
  147. name: 'test',
  148. current: {
  149. value: ['val1', 'val2', 'val3'],
  150. text: 'val1 + val2 + val3',
  151. },
  152. };
  153. scenario.queryResult = [{ text: 'val2' }, { text: 'val3' }];
  154. });
  155. it('should update current value', () => {
  156. expect(scenario.variable.current.value).toEqual(['val2', 'val3']);
  157. expect(scenario.variable.current.text).toEqual('val2 + val3');
  158. });
  159. }
  160. );
  161. describeUpdateVariable(
  162. 'query variable with multi select and new options does not contain any selected values',
  163. (scenario: any) => {
  164. scenario.setup(() => {
  165. scenario.variableModel = {
  166. type: 'query',
  167. query: '',
  168. name: 'test',
  169. current: {
  170. value: ['val1', 'val2', 'val3'],
  171. text: 'val1 + val2 + val3',
  172. },
  173. };
  174. scenario.queryResult = [{ text: 'val5' }, { text: 'val6' }];
  175. });
  176. it('should update current value with first one', () => {
  177. expect(scenario.variable.current.value).toEqual('val5');
  178. expect(scenario.variable.current.text).toEqual('val5');
  179. });
  180. }
  181. );
  182. describeUpdateVariable('query variable with multi select and $__all selected', (scenario: any) => {
  183. scenario.setup(() => {
  184. scenario.variableModel = {
  185. type: 'query',
  186. query: '',
  187. name: 'test',
  188. includeAll: true,
  189. current: {
  190. value: ['$__all'],
  191. text: 'All',
  192. },
  193. };
  194. scenario.queryResult = [{ text: 'val5' }, { text: 'val6' }];
  195. });
  196. it('should keep current All value', () => {
  197. expect(scenario.variable.current.value).toEqual(['$__all']);
  198. expect(scenario.variable.current.text).toEqual('All');
  199. });
  200. });
  201. describeUpdateVariable('query variable with numeric results', (scenario: any) => {
  202. scenario.setup(() => {
  203. scenario.variableModel = {
  204. type: 'query',
  205. query: '',
  206. name: 'test',
  207. current: {},
  208. };
  209. scenario.queryResult = [{ text: 12, value: 12 }];
  210. });
  211. it('should set current value to first option', () => {
  212. expect(scenario.variable.current.value).toBe('12');
  213. expect(scenario.variable.options[0].value).toBe('12');
  214. expect(scenario.variable.options[0].text).toBe('12');
  215. });
  216. });
  217. describeUpdateVariable('basic query variable', (scenario: any) => {
  218. scenario.setup(() => {
  219. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  220. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  221. });
  222. it('should update options array', () => {
  223. expect(scenario.variable.options.length).toBe(2);
  224. expect(scenario.variable.options[0].text).toBe('backend1');
  225. expect(scenario.variable.options[0].value).toBe('backend1');
  226. expect(scenario.variable.options[1].value).toBe('backend2');
  227. });
  228. it('should select first option as value', () => {
  229. expect(scenario.variable.current.value).toBe('backend1');
  230. });
  231. });
  232. describeUpdateVariable('and existing value still exists in options', (scenario: any) => {
  233. scenario.setup(() => {
  234. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  235. scenario.variableModel.current = { value: 'backend2', text: 'backend2' };
  236. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }];
  237. });
  238. it('should keep variable value', () => {
  239. expect(scenario.variable.current.text).toBe('backend2');
  240. });
  241. });
  242. describeUpdateVariable('and regex pattern exists', (scenario: any) => {
  243. scenario.setup(() => {
  244. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  245. scenario.variableModel.regex = '/apps.*(backend_[0-9]+)/';
  246. scenario.queryResult = [
  247. { text: 'apps.backend.backend_01.counters.req' },
  248. { text: 'apps.backend.backend_02.counters.req' },
  249. ];
  250. });
  251. it('should extract and use match group', () => {
  252. expect(scenario.variable.options[0].value).toBe('backend_01');
  253. });
  254. });
  255. describeUpdateVariable('and regex pattern exists and no match', (scenario: any) => {
  256. scenario.setup(() => {
  257. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  258. scenario.variableModel.regex = '/apps.*(backendasd[0-9]+)/';
  259. scenario.queryResult = [
  260. { text: 'apps.backend.backend_01.counters.req' },
  261. { text: 'apps.backend.backend_02.counters.req' },
  262. ];
  263. });
  264. it('should not add non matching items, None option should be added instead', () => {
  265. expect(scenario.variable.options.length).toBe(1);
  266. expect(scenario.variable.options[0].isNone).toBe(true);
  267. });
  268. });
  269. describeUpdateVariable('regex pattern without slashes', (scenario: any) => {
  270. scenario.setup(() => {
  271. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  272. scenario.variableModel.regex = 'backend_01';
  273. scenario.queryResult = [
  274. { text: 'apps.backend.backend_01.counters.req' },
  275. { text: 'apps.backend.backend_02.counters.req' },
  276. ];
  277. });
  278. it('should return matches options', () => {
  279. expect(scenario.variable.options.length).toBe(1);
  280. });
  281. });
  282. describeUpdateVariable('regex pattern remove duplicates', (scenario: any) => {
  283. scenario.setup(() => {
  284. scenario.variableModel = { type: 'query', query: 'apps.*', name: 'test' };
  285. scenario.variableModel.regex = '/backend_01/';
  286. scenario.queryResult = [
  287. { text: 'apps.backend.backend_01.counters.req' },
  288. { text: 'apps.backend.backend_01.counters.req' },
  289. ];
  290. });
  291. it('should return matches options', () => {
  292. expect(scenario.variable.options.length).toBe(1);
  293. });
  294. });
  295. describeUpdateVariable('with include All', (scenario: any) => {
  296. scenario.setup(() => {
  297. scenario.variableModel = {
  298. type: 'query',
  299. query: 'apps.*',
  300. name: 'test',
  301. includeAll: true,
  302. };
  303. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }, { text: 'backend3' }];
  304. });
  305. it('should add All option', () => {
  306. expect(scenario.variable.options[0].text).toBe('All');
  307. expect(scenario.variable.options[0].value).toBe('$__all');
  308. });
  309. });
  310. describeUpdateVariable('with include all and custom value', (scenario: any) => {
  311. scenario.setup(() => {
  312. scenario.variableModel = {
  313. type: 'query',
  314. query: 'apps.*',
  315. name: 'test',
  316. includeAll: true,
  317. allValue: '*',
  318. };
  319. scenario.queryResult = [{ text: 'backend1' }, { text: 'backend2' }, { text: 'backend3' }];
  320. });
  321. it('should add All option with custom value', () => {
  322. expect(scenario.variable.options[0].value).toBe('$__all');
  323. });
  324. });
  325. describeUpdateVariable('without sort', (scenario: any) => {
  326. scenario.setup(() => {
  327. scenario.variableModel = {
  328. type: 'query',
  329. query: 'apps.*',
  330. name: 'test',
  331. sort: 0,
  332. };
  333. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  334. });
  335. it('should return options without sort', () => {
  336. expect(scenario.variable.options[0].text).toBe('bbb2');
  337. expect(scenario.variable.options[1].text).toBe('aaa10');
  338. expect(scenario.variable.options[2].text).toBe('ccc3');
  339. });
  340. });
  341. describeUpdateVariable('with alphabetical sort (asc)', (scenario: any) => {
  342. scenario.setup(() => {
  343. scenario.variableModel = {
  344. type: 'query',
  345. query: 'apps.*',
  346. name: 'test',
  347. sort: 1,
  348. };
  349. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  350. });
  351. it('should return options with alphabetical sort', () => {
  352. expect(scenario.variable.options[0].text).toBe('aaa10');
  353. expect(scenario.variable.options[1].text).toBe('bbb2');
  354. expect(scenario.variable.options[2].text).toBe('ccc3');
  355. });
  356. });
  357. describeUpdateVariable('with alphabetical sort (desc)', (scenario: any) => {
  358. scenario.setup(() => {
  359. scenario.variableModel = {
  360. type: 'query',
  361. query: 'apps.*',
  362. name: 'test',
  363. sort: 2,
  364. };
  365. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  366. });
  367. it('should return options with alphabetical sort', () => {
  368. expect(scenario.variable.options[0].text).toBe('ccc3');
  369. expect(scenario.variable.options[1].text).toBe('bbb2');
  370. expect(scenario.variable.options[2].text).toBe('aaa10');
  371. });
  372. });
  373. describeUpdateVariable('with numerical sort (asc)', (scenario: any) => {
  374. scenario.setup(() => {
  375. scenario.variableModel = {
  376. type: 'query',
  377. query: 'apps.*',
  378. name: 'test',
  379. sort: 3,
  380. };
  381. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  382. });
  383. it('should return options with numerical sort', () => {
  384. expect(scenario.variable.options[0].text).toBe('bbb2');
  385. expect(scenario.variable.options[1].text).toBe('ccc3');
  386. expect(scenario.variable.options[2].text).toBe('aaa10');
  387. });
  388. });
  389. describeUpdateVariable('with numerical sort (desc)', (scenario: any) => {
  390. scenario.setup(() => {
  391. scenario.variableModel = {
  392. type: 'query',
  393. query: 'apps.*',
  394. name: 'test',
  395. sort: 4,
  396. };
  397. scenario.queryResult = [{ text: 'bbb2' }, { text: 'aaa10' }, { text: 'ccc3' }];
  398. });
  399. it('should return options with numerical sort', () => {
  400. expect(scenario.variable.options[0].text).toBe('aaa10');
  401. expect(scenario.variable.options[1].text).toBe('ccc3');
  402. expect(scenario.variable.options[2].text).toBe('bbb2');
  403. });
  404. });
  405. //
  406. // datasource variable update
  407. //
  408. describeUpdateVariable('datasource variable with regex filter', (scenario: any) => {
  409. scenario.setup(() => {
  410. scenario.variableModel = {
  411. type: 'datasource',
  412. query: 'graphite',
  413. name: 'test',
  414. current: { value: 'backend4_pee', text: 'backend4_pee' },
  415. regex: '/pee$/',
  416. };
  417. scenario.metricSources = [
  418. { name: 'backend1', meta: { id: 'influx' } },
  419. { name: 'backend2_pee', meta: { id: 'graphite' } },
  420. { name: 'backend3', meta: { id: 'graphite' } },
  421. { name: 'backend4_pee', meta: { id: 'graphite' } },
  422. ];
  423. });
  424. it('should set only contain graphite ds and filtered using regex', () => {
  425. expect(scenario.variable.options.length).toBe(2);
  426. expect(scenario.variable.options[0].value).toBe('backend2_pee');
  427. expect(scenario.variable.options[1].value).toBe('backend4_pee');
  428. });
  429. it('should keep current value if available', () => {
  430. expect(scenario.variable.current.value).toBe('backend4_pee');
  431. });
  432. });
  433. //
  434. // Custom variable update
  435. //
  436. describeUpdateVariable('update custom variable', (scenario: any) => {
  437. scenario.setup(() => {
  438. scenario.variableModel = {
  439. type: 'custom',
  440. query: 'hej, hop, asd, escaped\\,var',
  441. name: 'test',
  442. };
  443. });
  444. it('should update options array', () => {
  445. expect(scenario.variable.options.length).toBe(4);
  446. expect(scenario.variable.options[0].text).toBe('hej');
  447. expect(scenario.variable.options[1].value).toBe('hop');
  448. expect(scenario.variable.options[2].value).toBe('asd');
  449. expect(scenario.variable.options[3].value).toBe('escaped,var');
  450. });
  451. });
  452. describe('multiple interval variables with auto', () => {
  453. let variable1: any, variable2: any;
  454. beforeEach(() => {
  455. const range = {
  456. from: dateTime(new Date())
  457. .subtract(7, 'days')
  458. .toDate(),
  459. to: new Date(),
  460. };
  461. ctx.timeSrv.timeRange = () => range;
  462. ctx.templateSrv.setGrafanaVariable = jest.fn();
  463. const variableModel1 = {
  464. type: 'interval',
  465. query: '1s,2h,5h,1d',
  466. name: 'variable1',
  467. auto: true,
  468. auto_count: 10,
  469. };
  470. variable1 = ctx.variableSrv.createVariableFromModel(variableModel1);
  471. ctx.variableSrv.addVariable(variable1);
  472. const variableModel2 = {
  473. type: 'interval',
  474. query: '1s,2h,5h',
  475. name: 'variable2',
  476. auto: true,
  477. auto_count: 1000,
  478. };
  479. variable2 = ctx.variableSrv.createVariableFromModel(variableModel2);
  480. ctx.variableSrv.addVariable(variable2);
  481. ctx.variableSrv.updateOptions(variable1);
  482. ctx.variableSrv.updateOptions(variable2);
  483. // ctx.$rootScope.$digest();
  484. });
  485. it('should update options array', () => {
  486. expect(variable1.options.length).toBe(5);
  487. expect(variable1.options[0].text).toBe('auto');
  488. expect(variable1.options[0].value).toBe('$__auto_interval_variable1');
  489. expect(variable2.options.length).toBe(4);
  490. expect(variable2.options[0].text).toBe('auto');
  491. expect(variable2.options[0].value).toBe('$__auto_interval_variable2');
  492. });
  493. it('should correctly set $__auto_interval_variableX', () => {
  494. let variable1Set,
  495. variable2Set,
  496. legacySet,
  497. unknownSet = false;
  498. // updateAutoValue() gets called repeatedly: once directly once via VariableSrv.validateVariableSelectionState()
  499. // So check that all calls are valid rather than expect a specific number and/or ordering of calls
  500. for (let i = 0; i < ctx.templateSrv.setGrafanaVariable.mock.calls.length; i++) {
  501. const call = ctx.templateSrv.setGrafanaVariable.mock.calls[i];
  502. switch (call[0]) {
  503. case '$__auto_interval_variable1':
  504. expect(call[1]).toBe('12h');
  505. variable1Set = true;
  506. break;
  507. case '$__auto_interval_variable2':
  508. expect(call[1]).toBe('10m');
  509. variable2Set = true;
  510. break;
  511. case '$__auto_interval':
  512. expect(call[1]).toEqual(expect.stringMatching(/^(12h|10m)$/));
  513. legacySet = true;
  514. break;
  515. default:
  516. unknownSet = true;
  517. break;
  518. }
  519. }
  520. expect(variable1Set).toEqual(true);
  521. expect(variable2Set).toEqual(true);
  522. expect(legacySet).toEqual(true);
  523. expect(unknownSet).toEqual(false);
  524. });
  525. });
  526. describe('setOptionFromUrl', () => {
  527. it('sets single value as string if not multi choice', async () => {
  528. const [setValueMock, setFromUrl] = setupSetFromUrlTest(ctx);
  529. await setFromUrl('one');
  530. expect(setValueMock).toHaveBeenCalledWith({ text: 'one', value: 'one' });
  531. });
  532. it('sets single value as array if multi choice', async () => {
  533. const [setValueMock, setFromUrl] = setupSetFromUrlTest(ctx, { multi: true });
  534. await setFromUrl('one');
  535. expect(setValueMock).toHaveBeenCalledWith({ text: ['one'], value: ['one'] });
  536. });
  537. it('sets both text and value as array if multiple values in url', async () => {
  538. const [setValueMock, setFromUrl] = setupSetFromUrlTest(ctx, { multi: true });
  539. await setFromUrl(['one', 'two']);
  540. expect(setValueMock).toHaveBeenCalledWith({ text: ['one', 'two'], value: ['one', 'two'] });
  541. });
  542. it('sets text and value even if it does not match any option', async () => {
  543. const [setValueMock, setFromUrl] = setupSetFromUrlTest(ctx);
  544. await setFromUrl('none');
  545. expect(setValueMock).toHaveBeenCalledWith({ text: 'none', value: 'none' });
  546. });
  547. it('sets text and value even if it does not match any option and it is array', async () => {
  548. const [setValueMock, setFromUrl] = setupSetFromUrlTest(ctx);
  549. await setFromUrl(['none', 'none2']);
  550. expect(setValueMock).toHaveBeenCalledWith({ text: ['none', 'none2'], value: ['none', 'none2'] });
  551. });
  552. });
  553. });
  554. function setupSetFromUrlTest(ctx: any, model = {}) {
  555. const variableSrv = new VariableSrv($q, ctx.$location, ctx.$injector, ctx.templateSrv, ctx.timeSrv);
  556. const finalModel = {
  557. type: 'custom',
  558. options: ['one', 'two', 'three'].map(v => ({ text: v, value: v })),
  559. name: 'test',
  560. ...model,
  561. };
  562. const variable = new CustomVariable(finalModel, variableSrv);
  563. // We are mocking the setValue here instead of just checking the final variable.current value because there is lots
  564. // of stuff going when the setValue is called that is hard to mock out.
  565. variable.setValue = jest.fn();
  566. return [variable.setValue, (val: any) => variableSrv.setOptionFromUrl(variable, val)];
  567. }
  568. function getVarMockConstructor(variable: any, model: any, ctx: any) {
  569. switch (model.model.type) {
  570. case 'datasource':
  571. return new variable(model.model, ctx.datasourceSrv, ctx.variableSrv, ctx.templateSrv);
  572. case 'query':
  573. return new variable(model.model, ctx.datasourceSrv, ctx.templateSrv, ctx.variableSrv);
  574. case 'interval':
  575. return new variable(model.model, ctx.timeSrv, ctx.templateSrv, ctx.variableSrv);
  576. case 'custom':
  577. return new variable(model.model, ctx.variableSrv);
  578. default:
  579. return new variable(model.model);
  580. }
  581. }