repeat.jest.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. import _ from 'lodash';
  2. import { DashboardModel } from '../dashboard_model';
  3. import { expect } from 'test/lib/common';
  4. jest.mock('app/core/services/context_srv', () => ({}));
  5. describe('given dashboard with panel repeat in horizontal direction', function() {
  6. var dashboard;
  7. beforeEach(function() {
  8. dashboard = new DashboardModel({
  9. panels: [
  10. {
  11. id: 2,
  12. repeat: 'apps',
  13. repeatDirection: 'h',
  14. gridPos: { x: 0, y: 0, h: 2, w: 24 },
  15. },
  16. ],
  17. templating: {
  18. list: [
  19. {
  20. name: 'apps',
  21. current: {
  22. text: 'se1, se2, se3',
  23. value: ['se1', 'se2', 'se3'],
  24. },
  25. options: [
  26. { text: 'se1', value: 'se1', selected: true },
  27. { text: 'se2', value: 'se2', selected: true },
  28. { text: 'se3', value: 'se3', selected: true },
  29. { text: 'se4', value: 'se4', selected: false },
  30. ],
  31. },
  32. ],
  33. },
  34. });
  35. dashboard.processRepeats();
  36. });
  37. it('should repeat panel 3 times', function() {
  38. expect(dashboard.panels.length).toBe(3);
  39. });
  40. it('should mark panel repeated', function() {
  41. expect(dashboard.panels[0].repeat).toBe('apps');
  42. expect(dashboard.panels[1].repeatPanelId).toBe(2);
  43. });
  44. it('should set scopedVars on panels', function() {
  45. expect(dashboard.panels[0].scopedVars.apps.value).toBe('se1');
  46. expect(dashboard.panels[1].scopedVars.apps.value).toBe('se2');
  47. expect(dashboard.panels[2].scopedVars.apps.value).toBe('se3');
  48. });
  49. it('should place on first row and adjust width so all fit', function() {
  50. expect(dashboard.panels[0].gridPos).toMatchObject({
  51. x: 0,
  52. y: 0,
  53. h: 2,
  54. w: 8,
  55. });
  56. expect(dashboard.panels[1].gridPos).toMatchObject({
  57. x: 8,
  58. y: 0,
  59. h: 2,
  60. w: 8,
  61. });
  62. expect(dashboard.panels[2].gridPos).toMatchObject({
  63. x: 16,
  64. y: 0,
  65. h: 2,
  66. w: 8,
  67. });
  68. });
  69. describe('After a second iteration', function() {
  70. beforeEach(function() {
  71. dashboard.panels[0].fill = 10;
  72. dashboard.processRepeats();
  73. });
  74. it('reused panel should copy properties from source', function() {
  75. expect(dashboard.panels[1].fill).toBe(10);
  76. });
  77. it('should have same panel count', function() {
  78. expect(dashboard.panels.length).toBe(3);
  79. });
  80. });
  81. describe('After a second iteration with different variable', function() {
  82. beforeEach(function() {
  83. dashboard.templating.list.push({
  84. name: 'server',
  85. current: { text: 'se1, se2, se3', value: ['se1'] },
  86. options: [{ text: 'se1', value: 'se1', selected: true }],
  87. });
  88. dashboard.panels[0].repeat = 'server';
  89. dashboard.processRepeats();
  90. });
  91. it('should remove scopedVars value for last variable', function() {
  92. expect(dashboard.panels[0].scopedVars.apps).toBe(undefined);
  93. });
  94. it('should have new variable value in scopedVars', function() {
  95. expect(dashboard.panels[0].scopedVars.server.value).toBe('se1');
  96. });
  97. });
  98. describe('After a second iteration and selected values reduced', function() {
  99. beforeEach(function() {
  100. dashboard.templating.list[0].options[1].selected = false;
  101. dashboard.processRepeats();
  102. });
  103. it('should clean up repeated panel', function() {
  104. expect(dashboard.panels.length).toBe(2);
  105. });
  106. });
  107. describe('After a second iteration and panel repeat is turned off', function() {
  108. beforeEach(function() {
  109. dashboard.panels[0].repeat = null;
  110. dashboard.processRepeats();
  111. });
  112. it('should clean up repeated panel', function() {
  113. expect(dashboard.panels.length).toBe(1);
  114. });
  115. it('should remove scoped vars from reused panel', function() {
  116. expect(dashboard.panels[0].scopedVars).toBe(undefined);
  117. });
  118. });
  119. });
  120. describe('given dashboard with panel repeat in vertical direction', function() {
  121. var dashboard;
  122. beforeEach(function() {
  123. dashboard = new DashboardModel({
  124. panels: [
  125. { id: 1, type: 'row', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  126. { id: 2, repeat: 'apps', repeatDirection: 'v', gridPos: { x: 5, y: 1, h: 2, w: 8 } },
  127. { id: 3, type: 'row', gridPos: { x: 0, y: 3, h: 1, w: 24 } },
  128. ],
  129. templating: {
  130. list: [
  131. {
  132. name: 'apps',
  133. current: {
  134. text: 'se1, se2, se3',
  135. value: ['se1', 'se2', 'se3'],
  136. },
  137. options: [
  138. { text: 'se1', value: 'se1', selected: true },
  139. { text: 'se2', value: 'se2', selected: true },
  140. { text: 'se3', value: 'se3', selected: true },
  141. { text: 'se4', value: 'se4', selected: false },
  142. ],
  143. },
  144. ],
  145. },
  146. });
  147. dashboard.processRepeats();
  148. });
  149. it('should place on items on top of each other and keep witdh', function() {
  150. expect(dashboard.panels[0].gridPos).toMatchObject({ x: 0, y: 0, h: 1, w: 24 }); // first row
  151. expect(dashboard.panels[1].gridPos).toMatchObject({ x: 5, y: 1, h: 2, w: 8 });
  152. expect(dashboard.panels[2].gridPos).toMatchObject({ x: 5, y: 3, h: 2, w: 8 });
  153. expect(dashboard.panels[3].gridPos).toMatchObject({ x: 5, y: 5, h: 2, w: 8 });
  154. expect(dashboard.panels[4].gridPos).toMatchObject({ x: 0, y: 7, h: 1, w: 24 }); // last row
  155. });
  156. });
  157. describe('given dashboard with row repeat and panel repeat in horizontal direction', () => {
  158. let dashboard, dashboardJSON;
  159. beforeEach(() => {
  160. dashboardJSON = {
  161. panels: [
  162. { id: 1, type: 'row', repeat: 'region', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  163. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 2, w: 6 } },
  164. ],
  165. templating: {
  166. list: [
  167. {
  168. name: 'region',
  169. current: {
  170. text: 'reg1, reg2',
  171. value: ['reg1', 'reg2'],
  172. },
  173. options: [{ text: 'reg1', value: 'reg1', selected: true }, { text: 'reg2', value: 'reg2', selected: true }],
  174. },
  175. {
  176. name: 'app',
  177. current: {
  178. text: 'se1, se2, se3, se4, se5, se6',
  179. value: ['se1', 'se2', 'se3', 'se4', 'se5', 'se6'],
  180. },
  181. options: [
  182. { text: 'se1', value: 'se1', selected: true },
  183. { text: 'se2', value: 'se2', selected: true },
  184. { text: 'se3', value: 'se3', selected: true },
  185. { text: 'se4', value: 'se4', selected: true },
  186. { text: 'se5', value: 'se5', selected: true },
  187. { text: 'se6', value: 'se6', selected: true },
  188. ],
  189. },
  190. ],
  191. },
  192. };
  193. dashboard = new DashboardModel(dashboardJSON);
  194. dashboard.processRepeats(false);
  195. });
  196. it('should panels in self row', () => {
  197. const panel_types = _.map(dashboard.panels, 'type');
  198. expect(panel_types).toEqual([
  199. 'row',
  200. 'graph',
  201. 'graph',
  202. 'graph',
  203. 'graph',
  204. 'graph',
  205. 'graph',
  206. 'row',
  207. 'graph',
  208. 'graph',
  209. 'graph',
  210. 'graph',
  211. 'graph',
  212. 'graph',
  213. ]);
  214. });
  215. it('should be placed in their places', function() {
  216. expect(dashboard.panels[0].gridPos).toMatchObject({ x: 0, y: 0, h: 1, w: 24 }); // 1st row
  217. expect(dashboard.panels[1].gridPos).toMatchObject({ x: 0, y: 1, h: 2, w: 6 });
  218. expect(dashboard.panels[2].gridPos).toMatchObject({ x: 6, y: 1, h: 2, w: 6 });
  219. expect(dashboard.panels[3].gridPos).toMatchObject({ x: 12, y: 1, h: 2, w: 6 });
  220. expect(dashboard.panels[4].gridPos).toMatchObject({ x: 18, y: 1, h: 2, w: 6 });
  221. expect(dashboard.panels[5].gridPos).toMatchObject({ x: 0, y: 3, h: 2, w: 6 }); // next row
  222. expect(dashboard.panels[6].gridPos).toMatchObject({ x: 6, y: 3, h: 2, w: 6 });
  223. expect(dashboard.panels[7].gridPos).toMatchObject({ x: 0, y: 5, h: 1, w: 24 });
  224. expect(dashboard.panels[8].gridPos).toMatchObject({ x: 0, y: 6, h: 2, w: 6 }); // 2nd row
  225. expect(dashboard.panels[9].gridPos).toMatchObject({ x: 6, y: 6, h: 2, w: 6 });
  226. expect(dashboard.panels[10].gridPos).toMatchObject({ x: 12, y: 6, h: 2, w: 6 });
  227. expect(dashboard.panels[11].gridPos).toMatchObject({ x: 18, y: 6, h: 2, w: 6 }); // next row
  228. expect(dashboard.panels[12].gridPos).toMatchObject({ x: 0, y: 8, h: 2, w: 6 });
  229. expect(dashboard.panels[13].gridPos).toMatchObject({ x: 6, y: 8, h: 2, w: 6 });
  230. });
  231. });
  232. describe('given dashboard with row repeat', function() {
  233. let dashboard, dashboardJSON;
  234. beforeEach(function() {
  235. dashboardJSON = {
  236. panels: [
  237. {
  238. id: 1,
  239. type: 'row',
  240. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  241. repeat: 'apps',
  242. },
  243. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  244. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  245. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } },
  246. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  247. ],
  248. templating: {
  249. list: [
  250. {
  251. name: 'apps',
  252. current: {
  253. text: 'se1, se2',
  254. value: ['se1', 'se2'],
  255. },
  256. options: [
  257. { text: 'se1', value: 'se1', selected: true },
  258. { text: 'se2', value: 'se2', selected: true },
  259. { text: 'se3', value: 'se3', selected: false },
  260. ],
  261. },
  262. ],
  263. },
  264. };
  265. dashboard = new DashboardModel(dashboardJSON);
  266. dashboard.processRepeats();
  267. });
  268. it('should not repeat only row', function() {
  269. const panel_types = _.map(dashboard.panels, 'type');
  270. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph', 'row', 'graph']);
  271. });
  272. it('should set scopedVars for each panel', function() {
  273. dashboardJSON.templating.list[0].options[2].selected = true;
  274. dashboard = new DashboardModel(dashboardJSON);
  275. dashboard.processRepeats();
  276. expect(dashboard.panels[1].scopedVars).toMatchObject({
  277. apps: { text: 'se1', value: 'se1' },
  278. });
  279. expect(dashboard.panels[4].scopedVars).toMatchObject({
  280. apps: { text: 'se2', value: 'se2' },
  281. });
  282. const scopedVars = _.compact(
  283. _.map(dashboard.panels, panel => {
  284. return panel.scopedVars ? panel.scopedVars.apps.value : null;
  285. })
  286. );
  287. expect(scopedVars).toEqual(['se1', 'se1', 'se1', 'se2', 'se2', 'se2', 'se3', 'se3', 'se3']);
  288. });
  289. it('should repeat only configured row', function() {
  290. expect(dashboard.panels[6].id).toBe(4);
  291. expect(dashboard.panels[7].id).toBe(5);
  292. });
  293. it('should repeat only row if it is collapsed', function() {
  294. dashboardJSON.panels = [
  295. {
  296. id: 1,
  297. type: 'row',
  298. collapsed: true,
  299. repeat: 'apps',
  300. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  301. panels: [
  302. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  303. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  304. ],
  305. },
  306. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  307. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  308. ];
  309. dashboard = new DashboardModel(dashboardJSON);
  310. dashboard.processRepeats();
  311. const panel_types = _.map(dashboard.panels, 'type');
  312. expect(panel_types).toEqual(['row', 'row', 'row', 'graph']);
  313. expect(dashboard.panels[0].panels).toHaveLength(2);
  314. expect(dashboard.panels[1].panels).toHaveLength(2);
  315. });
  316. it('should properly repeat multiple rows', function() {
  317. dashboardJSON.panels = [
  318. {
  319. id: 1,
  320. type: 'row',
  321. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  322. repeat: 'apps',
  323. }, // repeat
  324. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  325. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  326. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } }, // don't touch
  327. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  328. {
  329. id: 6,
  330. type: 'row',
  331. gridPos: { x: 0, y: 4, h: 1, w: 24 },
  332. repeat: 'hosts',
  333. }, // repeat
  334. { id: 7, type: 'graph', gridPos: { x: 0, y: 5, h: 1, w: 6 } },
  335. { id: 8, type: 'graph', gridPos: { x: 6, y: 5, h: 1, w: 6 } },
  336. ];
  337. dashboardJSON.templating.list.push({
  338. name: 'hosts',
  339. current: {
  340. text: 'backend01, backend02',
  341. value: ['backend01', 'backend02'],
  342. },
  343. options: [
  344. { text: 'backend01', value: 'backend01', selected: true },
  345. { text: 'backend02', value: 'backend02', selected: true },
  346. { text: 'backend03', value: 'backend03', selected: false },
  347. ],
  348. });
  349. dashboard = new DashboardModel(dashboardJSON);
  350. dashboard.processRepeats();
  351. const panel_types = _.map(dashboard.panels, 'type');
  352. expect(panel_types).toEqual([
  353. 'row',
  354. 'graph',
  355. 'graph',
  356. 'row',
  357. 'graph',
  358. 'graph',
  359. 'row',
  360. 'graph',
  361. 'row',
  362. 'graph',
  363. 'graph',
  364. 'row',
  365. 'graph',
  366. 'graph',
  367. ]);
  368. expect(dashboard.panels[0].scopedVars['apps'].value).toBe('se1');
  369. expect(dashboard.panels[1].scopedVars['apps'].value).toBe('se1');
  370. expect(dashboard.panels[3].scopedVars['apps'].value).toBe('se2');
  371. expect(dashboard.panels[4].scopedVars['apps'].value).toBe('se2');
  372. expect(dashboard.panels[8].scopedVars['hosts'].value).toBe('backend01');
  373. expect(dashboard.panels[9].scopedVars['hosts'].value).toBe('backend01');
  374. expect(dashboard.panels[11].scopedVars['hosts'].value).toBe('backend02');
  375. expect(dashboard.panels[12].scopedVars['hosts'].value).toBe('backend02');
  376. });
  377. it('should assign unique ids for repeated panels', function() {
  378. dashboardJSON.panels = [
  379. {
  380. id: 1,
  381. type: 'row',
  382. collapsed: true,
  383. repeat: 'apps',
  384. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  385. panels: [
  386. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  387. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  388. ],
  389. },
  390. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  391. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  392. ];
  393. dashboard = new DashboardModel(dashboardJSON);
  394. dashboard.processRepeats();
  395. const panel_ids = _.flattenDeep(
  396. _.map(dashboard.panels, panel => {
  397. let ids = [];
  398. if (panel.panels && panel.panels.length) {
  399. ids = _.map(panel.panels, 'id');
  400. }
  401. ids.push(panel.id);
  402. return ids;
  403. })
  404. );
  405. expect(panel_ids.length).toEqual(_.uniq(panel_ids).length);
  406. });
  407. });
  408. describe('given dashboard with row and panel repeat', () => {
  409. let dashboard, dashboardJSON;
  410. beforeEach(() => {
  411. dashboardJSON = {
  412. panels: [
  413. {
  414. id: 1,
  415. type: 'row',
  416. repeat: 'region',
  417. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  418. },
  419. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  420. ],
  421. templating: {
  422. list: [
  423. {
  424. name: 'region',
  425. current: {
  426. text: 'reg1, reg2',
  427. value: ['reg1', 'reg2'],
  428. },
  429. options: [
  430. { text: 'reg1', value: 'reg1', selected: true },
  431. { text: 'reg2', value: 'reg2', selected: true },
  432. { text: 'reg3', value: 'reg3', selected: false },
  433. ],
  434. },
  435. {
  436. name: 'app',
  437. current: {
  438. text: 'se1, se2',
  439. value: ['se1', 'se2'],
  440. },
  441. options: [
  442. { text: 'se1', value: 'se1', selected: true },
  443. { text: 'se2', value: 'se2', selected: true },
  444. { text: 'se3', value: 'se3', selected: false },
  445. ],
  446. },
  447. ],
  448. },
  449. };
  450. dashboard = new DashboardModel(dashboardJSON);
  451. dashboard.processRepeats();
  452. });
  453. it('should repeat row and panels for each row', () => {
  454. const panel_types = _.map(dashboard.panels, 'type');
  455. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  456. });
  457. it('should clean up old repeated panels', () => {
  458. dashboardJSON.panels = [
  459. {
  460. id: 1,
  461. type: 'row',
  462. repeat: 'region',
  463. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  464. },
  465. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  466. { id: 3, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 7, y: 1, h: 1, w: 6 } },
  467. {
  468. id: 11,
  469. type: 'row',
  470. repeatPanelId: 1,
  471. repeatIteration: 101,
  472. gridPos: { x: 0, y: 2, h: 1, w: 24 },
  473. },
  474. { id: 12, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 0, y: 3, h: 1, w: 6 } },
  475. ];
  476. dashboard = new DashboardModel(dashboardJSON);
  477. dashboard.processRepeats();
  478. const panel_types = _.map(dashboard.panels, 'type');
  479. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  480. });
  481. it('should set scopedVars for each row', () => {
  482. dashboard = new DashboardModel(dashboardJSON);
  483. dashboard.processRepeats();
  484. expect(dashboard.panels[0].scopedVars).toMatchObject({
  485. region: { text: 'reg1', value: 'reg1' },
  486. });
  487. expect(dashboard.panels[3].scopedVars).toMatchObject({
  488. region: { text: 'reg2', value: 'reg2' },
  489. });
  490. });
  491. it('should set panel-repeat variable for each panel', () => {
  492. dashboard = new DashboardModel(dashboardJSON);
  493. dashboard.processRepeats();
  494. expect(dashboard.panels[1].scopedVars).toMatchObject({
  495. app: { text: 'se1', value: 'se1' },
  496. });
  497. expect(dashboard.panels[2].scopedVars).toMatchObject({
  498. app: { text: 'se2', value: 'se2' },
  499. });
  500. expect(dashboard.panels[4].scopedVars).toMatchObject({
  501. app: { text: 'se1', value: 'se1' },
  502. });
  503. expect(dashboard.panels[5].scopedVars).toMatchObject({
  504. app: { text: 'se2', value: 'se2' },
  505. });
  506. });
  507. it('should set row-repeat variable for each panel', () => {
  508. dashboard = new DashboardModel(dashboardJSON);
  509. dashboard.processRepeats();
  510. expect(dashboard.panels[1].scopedVars).toMatchObject({
  511. region: { text: 'reg1', value: 'reg1' },
  512. });
  513. expect(dashboard.panels[2].scopedVars).toMatchObject({
  514. region: { text: 'reg1', value: 'reg1' },
  515. });
  516. expect(dashboard.panels[4].scopedVars).toMatchObject({
  517. region: { text: 'reg2', value: 'reg2' },
  518. });
  519. expect(dashboard.panels[5].scopedVars).toMatchObject({
  520. region: { text: 'reg2', value: 'reg2' },
  521. });
  522. });
  523. });