dashboard_model.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. import _ from 'lodash';
  2. import { DashboardModel } from '../dashboard_model';
  3. import { PanelModel } from '../panel_model';
  4. jest.mock('app/core/services/context_srv', () => ({}));
  5. describe('DashboardModel', function() {
  6. describe('when creating new dashboard model defaults only', function() {
  7. let model;
  8. beforeEach(function() {
  9. model = new DashboardModel({}, {});
  10. });
  11. it('should have title', function() {
  12. expect(model.title).toBe('No Title');
  13. });
  14. it('should have meta', function() {
  15. expect(model.meta.canSave).toBe(true);
  16. expect(model.meta.canShare).toBe(true);
  17. });
  18. it('should have default properties', function() {
  19. expect(model.panels.length).toBe(0);
  20. });
  21. });
  22. describe('when getting next panel id', function() {
  23. let model;
  24. beforeEach(function() {
  25. model = new DashboardModel({
  26. panels: [{ id: 5 }],
  27. });
  28. });
  29. it('should return max id + 1', function() {
  30. expect(model.getNextPanelId()).toBe(6);
  31. });
  32. });
  33. describe('getSaveModelClone', function() {
  34. it('should sort keys', () => {
  35. const model = new DashboardModel({});
  36. const saveModel = model.getSaveModelClone();
  37. const keys = _.keys(saveModel);
  38. expect(keys[0]).toBe('annotations');
  39. expect(keys[1]).toBe('autoUpdate');
  40. });
  41. it('should remove add panel panels', () => {
  42. const model = new DashboardModel({});
  43. model.addPanel({
  44. type: 'add-panel',
  45. });
  46. model.addPanel({
  47. type: 'graph',
  48. });
  49. model.addPanel({
  50. type: 'add-panel',
  51. });
  52. const saveModel = model.getSaveModelClone();
  53. const panels = saveModel.panels;
  54. expect(panels.length).toBe(1);
  55. });
  56. });
  57. describe('row and panel manipulation', function() {
  58. let dashboard;
  59. beforeEach(function() {
  60. dashboard = new DashboardModel({});
  61. });
  62. it('adding panel should new up panel model', function() {
  63. dashboard.addPanel({ type: 'test', title: 'test' });
  64. expect(dashboard.panels[0] instanceof PanelModel).toBe(true);
  65. });
  66. it('duplicate panel should try to add to the right if there is space', function() {
  67. const panel = { id: 10, gridPos: { x: 0, y: 0, w: 6, h: 2 } };
  68. dashboard.addPanel(panel);
  69. dashboard.duplicatePanel(dashboard.panels[0]);
  70. expect(dashboard.panels[1].gridPos).toMatchObject({
  71. x: 6,
  72. y: 0,
  73. h: 2,
  74. w: 6,
  75. });
  76. });
  77. it('duplicate panel should remove repeat data', function() {
  78. const panel = {
  79. id: 10,
  80. gridPos: { x: 0, y: 0, w: 6, h: 2 },
  81. repeat: 'asd',
  82. scopedVars: { test: 'asd' },
  83. };
  84. dashboard.addPanel(panel);
  85. dashboard.duplicatePanel(dashboard.panels[0]);
  86. expect(dashboard.panels[1].repeat).toBe(undefined);
  87. expect(dashboard.panels[1].scopedVars).toBe(undefined);
  88. });
  89. });
  90. describe('Given editable false dashboard', function() {
  91. let model;
  92. beforeEach(function() {
  93. model = new DashboardModel({ editable: false });
  94. });
  95. it('Should set meta canEdit and canSave to false', function() {
  96. expect(model.meta.canSave).toBe(false);
  97. expect(model.meta.canEdit).toBe(false);
  98. });
  99. it('getSaveModelClone should remove meta', function() {
  100. const clone = model.getSaveModelClone();
  101. expect(clone.meta).toBe(undefined);
  102. });
  103. });
  104. describe('when loading dashboard with old influxdb query schema', function() {
  105. let model;
  106. let target;
  107. beforeEach(function() {
  108. model = new DashboardModel({
  109. panels: [
  110. {
  111. type: 'graph',
  112. grid: {},
  113. yaxes: [{}, {}],
  114. targets: [
  115. {
  116. alias: '$tag_datacenter $tag_source $col',
  117. column: 'value',
  118. measurement: 'logins.count',
  119. fields: [
  120. {
  121. func: 'mean',
  122. name: 'value',
  123. mathExpr: '*2',
  124. asExpr: 'value',
  125. },
  126. {
  127. name: 'one-minute',
  128. func: 'mean',
  129. mathExpr: '*3',
  130. asExpr: 'one-minute',
  131. },
  132. ],
  133. tags: [],
  134. fill: 'previous',
  135. function: 'mean',
  136. groupBy: [
  137. {
  138. interval: 'auto',
  139. type: 'time',
  140. },
  141. {
  142. key: 'source',
  143. type: 'tag',
  144. },
  145. {
  146. type: 'tag',
  147. key: 'datacenter',
  148. },
  149. ],
  150. },
  151. ],
  152. },
  153. ],
  154. });
  155. target = model.panels[0].targets[0];
  156. });
  157. it('should update query schema', function() {
  158. expect(target.fields).toBe(undefined);
  159. expect(target.select.length).toBe(2);
  160. expect(target.select[0].length).toBe(4);
  161. expect(target.select[0][0].type).toBe('field');
  162. expect(target.select[0][1].type).toBe('mean');
  163. expect(target.select[0][2].type).toBe('math');
  164. expect(target.select[0][3].type).toBe('alias');
  165. });
  166. });
  167. describe('when creating dashboard model with missing list for annoations or templating', function() {
  168. let model;
  169. beforeEach(function() {
  170. model = new DashboardModel({
  171. annotations: {
  172. enable: true,
  173. },
  174. templating: {
  175. enable: true,
  176. },
  177. });
  178. });
  179. it('should add empty list', function() {
  180. expect(model.annotations.list.length).toBe(1);
  181. expect(model.templating.list.length).toBe(0);
  182. });
  183. it('should add builtin annotation query', function() {
  184. expect(model.annotations.list[0].builtIn).toBe(1);
  185. expect(model.templating.list.length).toBe(0);
  186. });
  187. });
  188. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  189. let dashboard;
  190. beforeEach(function() {
  191. dashboard = new DashboardModel({ timezone: 'utc' });
  192. });
  193. it('Should format timestamp with second resolution by default', function() {
  194. expect(dashboard.formatDate(1234567890000)).toBe('2009-02-13 23:31:30');
  195. });
  196. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  197. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss')).toBe('2009-02-13 23:31:30');
  198. });
  199. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  200. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss.SSS')).toBe('2009-02-13 23:31:30.007');
  201. });
  202. });
  203. describe('updateSubmenuVisibility with empty lists', function() {
  204. let model;
  205. beforeEach(function() {
  206. model = new DashboardModel({});
  207. model.updateSubmenuVisibility();
  208. });
  209. it('should not enable submmenu', function() {
  210. expect(model.meta.submenuEnabled).toBe(false);
  211. });
  212. });
  213. describe('updateSubmenuVisibility with annotation', function() {
  214. let model;
  215. beforeEach(function() {
  216. model = new DashboardModel({
  217. annotations: {
  218. list: [{}],
  219. },
  220. });
  221. model.updateSubmenuVisibility();
  222. });
  223. it('should enable submmenu', function() {
  224. expect(model.meta.submenuEnabled).toBe(true);
  225. });
  226. });
  227. describe('updateSubmenuVisibility with template var', function() {
  228. let model;
  229. beforeEach(function() {
  230. model = new DashboardModel({
  231. templating: {
  232. list: [{}],
  233. },
  234. });
  235. model.updateSubmenuVisibility();
  236. });
  237. it('should enable submmenu', function() {
  238. expect(model.meta.submenuEnabled).toBe(true);
  239. });
  240. });
  241. describe('updateSubmenuVisibility with hidden template var', function() {
  242. let model;
  243. beforeEach(function() {
  244. model = new DashboardModel({
  245. templating: {
  246. list: [{ hide: 2 }],
  247. },
  248. });
  249. model.updateSubmenuVisibility();
  250. });
  251. it('should not enable submmenu', function() {
  252. expect(model.meta.submenuEnabled).toBe(false);
  253. });
  254. });
  255. describe('updateSubmenuVisibility with hidden annotation toggle', function() {
  256. let dashboard;
  257. beforeEach(function() {
  258. dashboard = new DashboardModel({
  259. annotations: {
  260. list: [{ hide: true }],
  261. },
  262. });
  263. dashboard.updateSubmenuVisibility();
  264. });
  265. it('should not enable submmenu', function() {
  266. expect(dashboard.meta.submenuEnabled).toBe(false);
  267. });
  268. });
  269. describe('When collapsing row', function() {
  270. let dashboard;
  271. beforeEach(function() {
  272. dashboard = new DashboardModel({
  273. panels: [
  274. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  275. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  276. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  277. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  278. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  279. ],
  280. });
  281. dashboard.toggleRow(dashboard.panels[1]);
  282. });
  283. it('should remove panels and put them inside collapsed row', function() {
  284. expect(dashboard.panels.length).toBe(3);
  285. expect(dashboard.panels[1].panels.length).toBe(2);
  286. });
  287. describe('and when removing row and its panels', function() {
  288. beforeEach(function() {
  289. dashboard.removeRow(dashboard.panels[1], true);
  290. });
  291. it('should remove row and its panels', function() {
  292. expect(dashboard.panels.length).toBe(2);
  293. });
  294. });
  295. describe('and when removing only the row', function() {
  296. beforeEach(function() {
  297. dashboard.removeRow(dashboard.panels[1], false);
  298. });
  299. it('should only remove row', function() {
  300. expect(dashboard.panels.length).toBe(4);
  301. });
  302. });
  303. });
  304. describe('When expanding row', function() {
  305. let dashboard;
  306. beforeEach(function() {
  307. dashboard = new DashboardModel({
  308. panels: [
  309. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  310. {
  311. id: 2,
  312. type: 'row',
  313. gridPos: { x: 0, y: 6, w: 24, h: 1 },
  314. collapsed: true,
  315. panels: [
  316. { id: 3, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  317. { id: 4, type: 'graph', gridPos: { x: 12, y: 7, w: 12, h: 2 } },
  318. ],
  319. },
  320. { id: 5, type: 'row', gridPos: { x: 0, y: 7, w: 1, h: 1 } },
  321. ],
  322. });
  323. dashboard.toggleRow(dashboard.panels[1]);
  324. });
  325. it('should add panels back', function() {
  326. expect(dashboard.panels.length).toBe(5);
  327. });
  328. it('should add them below row in array', function() {
  329. expect(dashboard.panels[2].id).toBe(3);
  330. expect(dashboard.panels[3].id).toBe(4);
  331. });
  332. it('should position them below row', function() {
  333. expect(dashboard.panels[2].gridPos).toMatchObject({
  334. x: 0,
  335. y: 7,
  336. w: 12,
  337. h: 2,
  338. });
  339. });
  340. it('should move panels below down', function() {
  341. expect(dashboard.panels[4].gridPos).toMatchObject({
  342. x: 0,
  343. y: 9,
  344. w: 1,
  345. h: 1,
  346. });
  347. });
  348. describe('and when removing row and its panels', function() {
  349. beforeEach(function() {
  350. dashboard.removeRow(dashboard.panels[1], true);
  351. });
  352. it('should remove row and its panels', function() {
  353. expect(dashboard.panels.length).toBe(2);
  354. });
  355. });
  356. describe('and when removing only the row', function() {
  357. beforeEach(function() {
  358. dashboard.removeRow(dashboard.panels[1], false);
  359. });
  360. it('should only remove row', function() {
  361. expect(dashboard.panels.length).toBe(4);
  362. });
  363. });
  364. });
  365. describe('Given model with time', () => {
  366. let model: DashboardModel;
  367. beforeEach(() => {
  368. model = new DashboardModel({
  369. time: {
  370. from: 'now-6h',
  371. to: 'now',
  372. },
  373. });
  374. expect(model.hasTimeChanged()).toBeFalsy();
  375. model.time = {
  376. from: 'now-3h',
  377. to: 'now-1h',
  378. };
  379. });
  380. it('hasTimeChanged should be true', () => {
  381. expect(model.hasTimeChanged()).toBeTruthy();
  382. });
  383. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  384. const options = { saveTimerange: false };
  385. const saveModel = model.getSaveModelClone(options);
  386. expect(saveModel.time.from).toBe('now-6h');
  387. expect(saveModel.time.to).toBe('now');
  388. });
  389. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  390. const options = { saveTimerange: true };
  391. const saveModel = model.getSaveModelClone(options);
  392. expect(saveModel.time.from).toBe('now-3h');
  393. expect(saveModel.time.to).toBe('now-1h');
  394. });
  395. it('hasTimeChanged should be false when reset original time', () => {
  396. model.resetOriginalTime();
  397. expect(model.hasTimeChanged()).toBeFalsy();
  398. });
  399. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  400. const options = { saveTimerange: false };
  401. const saveModel = model.getSaveModelClone(options);
  402. expect(saveModel.time.from).toBe('now-6h');
  403. expect(saveModel.time.to).toBe('now');
  404. });
  405. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  406. const options = { saveTimerange: true };
  407. const saveModel = model.getSaveModelClone(options);
  408. expect(saveModel.time.from).toBe('now-3h');
  409. expect(saveModel.time.to).toBe('now-1h');
  410. });
  411. });
  412. describe('Given model with template variable of type query', () => {
  413. let model: DashboardModel;
  414. beforeEach(() => {
  415. model = new DashboardModel({
  416. templating: {
  417. list: [
  418. {
  419. name: 'Server',
  420. type: 'query',
  421. current: {
  422. selected: true,
  423. text: 'server_001',
  424. value: 'server_001',
  425. },
  426. },
  427. ],
  428. },
  429. });
  430. expect(model.hasVariableValuesChanged()).toBeFalsy();
  431. });
  432. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  433. model.templating.list.push({
  434. name: 'Server2',
  435. type: 'query',
  436. current: {
  437. selected: true,
  438. text: 'server_002',
  439. value: 'server_002',
  440. },
  441. });
  442. expect(model.hasVariableValuesChanged()).toBeFalsy();
  443. });
  444. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  445. model.templating.list = [];
  446. expect(model.hasVariableValuesChanged()).toBeFalsy();
  447. });
  448. it('hasVariableValuesChanged should be true when changing value of template variable', () => {
  449. model.templating.list[0].current.text = 'server_002';
  450. expect(model.hasVariableValuesChanged()).toBeTruthy();
  451. });
  452. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  453. model.templating.list[0].current.text = 'server_002';
  454. const options = { saveVariables: false };
  455. const saveModel = model.getSaveModelClone(options);
  456. expect(saveModel.templating.list[0].current.text).toBe('server_001');
  457. });
  458. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  459. model.templating.list[0].current.text = 'server_002';
  460. const options = { saveVariables: true };
  461. const saveModel = model.getSaveModelClone(options);
  462. expect(saveModel.templating.list[0].current.text).toBe('server_002');
  463. });
  464. });
  465. describe('Given model with template variable of type adhoc', () => {
  466. let model: DashboardModel;
  467. beforeEach(() => {
  468. model = new DashboardModel({
  469. templating: {
  470. list: [
  471. {
  472. name: 'Filter',
  473. type: 'adhoc',
  474. filters: [
  475. {
  476. key: '@hostname',
  477. operator: '=',
  478. value: 'server 20',
  479. },
  480. ],
  481. },
  482. ],
  483. },
  484. });
  485. expect(model.hasVariableValuesChanged()).toBeFalsy();
  486. });
  487. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  488. model.templating.list.push({
  489. name: 'Filter',
  490. type: 'adhoc',
  491. filters: [
  492. {
  493. key: '@hostname',
  494. operator: '=',
  495. value: 'server 1',
  496. },
  497. ],
  498. });
  499. expect(model.hasVariableValuesChanged()).toBeFalsy();
  500. });
  501. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  502. model.templating.list = [];
  503. expect(model.hasVariableValuesChanged()).toBeFalsy();
  504. });
  505. it('hasVariableValuesChanged should be true when changing value of filter', () => {
  506. model.templating.list[0].filters[0].value = 'server 1';
  507. expect(model.hasVariableValuesChanged()).toBeTruthy();
  508. });
  509. it('hasVariableValuesChanged should be true when adding an additional condition', () => {
  510. model.templating.list[0].filters[0].condition = 'AND';
  511. model.templating.list[0].filters[1] = {
  512. key: '@metric',
  513. operator: '=',
  514. value: 'logins.count',
  515. };
  516. expect(model.hasVariableValuesChanged()).toBeTruthy();
  517. });
  518. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  519. model.templating.list[0].filters[0].value = 'server 1';
  520. const options = { saveVariables: false };
  521. const saveModel = model.getSaveModelClone(options);
  522. expect(saveModel.templating.list[0].filters[0].value).toBe('server 20');
  523. });
  524. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  525. model.templating.list[0].filters[0].value = 'server 1';
  526. const options = { saveVariables: true };
  527. const saveModel = model.getSaveModelClone(options);
  528. expect(saveModel.templating.list[0].filters[0].value).toBe('server 1');
  529. });
  530. });
  531. });