DashboardModel.repeat.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. import { compact, flattenDeep, map, uniq } from 'lodash';
  2. import { expect } from 'test/lib/common';
  3. import { DashboardPanelsChangedEvent } from 'app/types/events';
  4. import { getDashboardModel } from '../../../../test/helpers/getDashboardModel';
  5. import { DashboardModel } from '../state/DashboardModel';
  6. import { PanelModel } from './PanelModel';
  7. jest.mock('app/core/services/context_srv', () => ({}));
  8. describe('given dashboard with panel repeat', () => {
  9. let dashboard: DashboardModel;
  10. beforeEach(() => {
  11. const dashboardJSON = {
  12. panels: [
  13. { id: 1, type: 'row', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  14. { id: 2, repeat: 'apps', repeatDirection: 'h', gridPos: { x: 0, y: 1, h: 2, w: 8 } },
  15. ],
  16. templating: {
  17. list: [
  18. {
  19. name: 'apps',
  20. type: 'custom',
  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 = getDashboardModel(dashboardJSON);
  36. dashboard.processRepeats();
  37. });
  38. it('should repeat panels when row is expanding', () => {
  39. expect(dashboard.panels.length).toBe(4);
  40. // toggle row
  41. dashboard.toggleRow(dashboard.panels[0]);
  42. expect(dashboard.panels.length).toBe(1);
  43. // change variable
  44. dashboard.templating.list[0].options[2].selected = false;
  45. dashboard.templating.list[0].current = {
  46. text: 'se1, se2',
  47. value: ['se1', 'se2'],
  48. };
  49. // toggle row back
  50. dashboard.toggleRow(dashboard.panels[0]);
  51. expect(dashboard.panels.length).toBe(3);
  52. });
  53. });
  54. describe('given dashboard with panel repeat in horizontal direction', () => {
  55. let dashboard: any;
  56. beforeEach(() => {
  57. const dashboardJSON = {
  58. panels: [
  59. {
  60. id: 2,
  61. repeat: 'apps',
  62. repeatDirection: 'h',
  63. gridPos: { x: 0, y: 0, h: 2, w: 24 },
  64. },
  65. ],
  66. templating: {
  67. list: [
  68. {
  69. name: 'apps',
  70. type: 'custom',
  71. current: {
  72. text: 'se1, se2, se3',
  73. value: ['se1', 'se2', 'se3'],
  74. },
  75. options: [
  76. { text: 'se1', value: 'se1', selected: true },
  77. { text: 'se2', value: 'se2', selected: true },
  78. { text: 'se3', value: 'se3', selected: true },
  79. { text: 'se4', value: 'se4', selected: false },
  80. ],
  81. },
  82. ],
  83. },
  84. };
  85. dashboard = getDashboardModel(dashboardJSON);
  86. dashboard.processRepeats();
  87. });
  88. it('should repeat panel 3 times', () => {
  89. expect(dashboard.panels.length).toBe(3);
  90. });
  91. it('should mark panel repeated', () => {
  92. expect(dashboard.panels[0].repeat).toBe('apps');
  93. expect(dashboard.panels[1].repeatPanelId).toBe(2);
  94. });
  95. it('should set scopedVars on panels', () => {
  96. expect(dashboard.panels[0].scopedVars.apps.value).toBe('se1');
  97. expect(dashboard.panels[1].scopedVars.apps.value).toBe('se2');
  98. expect(dashboard.panels[2].scopedVars.apps.value).toBe('se3');
  99. });
  100. it('should place on first row and adjust width so all fit', () => {
  101. expect(dashboard.panels[0].gridPos).toMatchObject({
  102. x: 0,
  103. y: 0,
  104. h: 2,
  105. w: 8,
  106. });
  107. expect(dashboard.panels[1].gridPos).toMatchObject({
  108. x: 8,
  109. y: 0,
  110. h: 2,
  111. w: 8,
  112. });
  113. expect(dashboard.panels[2].gridPos).toMatchObject({
  114. x: 16,
  115. y: 0,
  116. h: 2,
  117. w: 8,
  118. });
  119. });
  120. describe('After a second iteration', () => {
  121. beforeEach(() => {
  122. dashboard.panels[0].fill = 10;
  123. dashboard.processRepeats();
  124. });
  125. it('reused panel should copy properties from source', () => {
  126. expect(dashboard.panels[1].fill).toBe(10);
  127. });
  128. it('should have same panel count', () => {
  129. expect(dashboard.panels.length).toBe(3);
  130. });
  131. });
  132. describe('After a second iteration with different variable', () => {
  133. beforeEach(() => {
  134. dashboard.templating.list.push({
  135. name: 'server',
  136. current: { text: 'se1, se2, se3', value: ['se1'] },
  137. options: [{ text: 'se1', value: 'se1', selected: true }],
  138. });
  139. dashboard.panels[0].repeat = 'server';
  140. dashboard.processRepeats();
  141. });
  142. it('should remove scopedVars value for last variable', () => {
  143. expect(dashboard.panels[0].scopedVars.apps).toBe(undefined);
  144. });
  145. it('should have new variable value in scopedVars', () => {
  146. expect(dashboard.panels[0].scopedVars.server.value).toBe('se1');
  147. });
  148. });
  149. describe('After a second iteration and selected values reduced', () => {
  150. beforeEach(() => {
  151. dashboard.templating.list[0].options[1].selected = false;
  152. dashboard.processRepeats();
  153. });
  154. it('should clean up repeated panel', () => {
  155. expect(dashboard.panels.length).toBe(2);
  156. });
  157. });
  158. describe('After a second iteration and panel repeat is turned off', () => {
  159. beforeEach(() => {
  160. dashboard.panels[0].repeat = null;
  161. dashboard.processRepeats();
  162. });
  163. it('should clean up repeated panel', () => {
  164. expect(dashboard.panels.length).toBe(1);
  165. });
  166. it('should remove scoped vars from reused panel', () => {
  167. expect(dashboard.panels[0].scopedVars).toBe(undefined);
  168. });
  169. });
  170. });
  171. describe('given dashboard with panel repeat in vertical direction', () => {
  172. let dashboard: any;
  173. beforeEach(() => {
  174. const dashboardJSON = {
  175. panels: [
  176. { id: 1, type: 'row', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  177. { id: 2, repeat: 'apps', repeatDirection: 'v', gridPos: { x: 5, y: 1, h: 2, w: 8 } },
  178. { id: 3, type: 'row', gridPos: { x: 0, y: 3, h: 1, w: 24 } },
  179. ],
  180. templating: {
  181. list: [
  182. {
  183. name: 'apps',
  184. type: 'custom',
  185. current: {
  186. text: 'se1, se2, se3',
  187. value: ['se1', 'se2', 'se3'],
  188. },
  189. options: [
  190. { text: 'se1', value: 'se1', selected: true },
  191. { text: 'se2', value: 'se2', selected: true },
  192. { text: 'se3', value: 'se3', selected: true },
  193. { text: 'se4', value: 'se4', selected: false },
  194. ],
  195. },
  196. ],
  197. },
  198. };
  199. dashboard = getDashboardModel(dashboardJSON);
  200. dashboard.processRepeats();
  201. });
  202. it('should place on items on top of each other and keep witdh', () => {
  203. expect(dashboard.panels[0].gridPos).toMatchObject({ x: 0, y: 0, h: 1, w: 24 }); // first row
  204. expect(dashboard.panels[1].gridPos).toMatchObject({ x: 5, y: 1, h: 2, w: 8 });
  205. expect(dashboard.panels[2].gridPos).toMatchObject({ x: 5, y: 3, h: 2, w: 8 });
  206. expect(dashboard.panels[3].gridPos).toMatchObject({ x: 5, y: 5, h: 2, w: 8 });
  207. expect(dashboard.panels[4].gridPos).toMatchObject({ x: 0, y: 7, h: 1, w: 24 }); // last row
  208. });
  209. });
  210. describe('given dashboard with row repeat and panel repeat in horizontal direction', () => {
  211. let dashboard: any, dashboardJSON: any;
  212. beforeEach(() => {
  213. dashboardJSON = {
  214. panels: [
  215. { id: 1, type: 'row', repeat: 'region', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  216. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 2, w: 6 } },
  217. ],
  218. templating: {
  219. list: [
  220. {
  221. name: 'region',
  222. type: 'custom',
  223. current: {
  224. text: 'reg1, reg2',
  225. value: ['reg1', 'reg2'],
  226. },
  227. options: [
  228. { text: 'reg1', value: 'reg1', selected: true },
  229. { text: 'reg2', value: 'reg2', selected: true },
  230. ],
  231. },
  232. {
  233. name: 'app',
  234. type: 'custom',
  235. current: {
  236. text: 'se1, se2, se3, se4, se5, se6',
  237. value: ['se1', 'se2', 'se3', 'se4', 'se5', 'se6'],
  238. },
  239. options: [
  240. { text: 'se1', value: 'se1', selected: true },
  241. { text: 'se2', value: 'se2', selected: true },
  242. { text: 'se3', value: 'se3', selected: true },
  243. { text: 'se4', value: 'se4', selected: true },
  244. { text: 'se5', value: 'se5', selected: true },
  245. { text: 'se6', value: 'se6', selected: true },
  246. ],
  247. },
  248. ],
  249. },
  250. };
  251. dashboard = getDashboardModel(dashboardJSON);
  252. dashboard.processRepeats(false);
  253. });
  254. it('should panels in self row', () => {
  255. const panelTypes = map(dashboard.panels, 'type');
  256. expect(panelTypes).toEqual([
  257. 'row',
  258. 'graph',
  259. 'graph',
  260. 'graph',
  261. 'graph',
  262. 'graph',
  263. 'graph',
  264. 'row',
  265. 'graph',
  266. 'graph',
  267. 'graph',
  268. 'graph',
  269. 'graph',
  270. 'graph',
  271. ]);
  272. });
  273. it('should be placed in their places', () => {
  274. expect(dashboard.panels[0].gridPos).toMatchObject({ x: 0, y: 0, h: 1, w: 24 }); // 1st row
  275. expect(dashboard.panels[1].gridPos).toMatchObject({ x: 0, y: 1, h: 2, w: 6 });
  276. expect(dashboard.panels[2].gridPos).toMatchObject({ x: 6, y: 1, h: 2, w: 6 });
  277. expect(dashboard.panels[3].gridPos).toMatchObject({ x: 12, y: 1, h: 2, w: 6 });
  278. expect(dashboard.panels[4].gridPos).toMatchObject({ x: 18, y: 1, h: 2, w: 6 });
  279. expect(dashboard.panels[5].gridPos).toMatchObject({ x: 0, y: 3, h: 2, w: 6 }); // next row
  280. expect(dashboard.panels[6].gridPos).toMatchObject({ x: 6, y: 3, h: 2, w: 6 });
  281. expect(dashboard.panels[7].gridPos).toMatchObject({ x: 0, y: 5, h: 1, w: 24 });
  282. expect(dashboard.panels[8].gridPos).toMatchObject({ x: 0, y: 6, h: 2, w: 6 }); // 2nd row
  283. expect(dashboard.panels[9].gridPos).toMatchObject({ x: 6, y: 6, h: 2, w: 6 });
  284. expect(dashboard.panels[10].gridPos).toMatchObject({ x: 12, y: 6, h: 2, w: 6 });
  285. expect(dashboard.panels[11].gridPos).toMatchObject({ x: 18, y: 6, h: 2, w: 6 }); // next row
  286. expect(dashboard.panels[12].gridPos).toMatchObject({ x: 0, y: 8, h: 2, w: 6 });
  287. expect(dashboard.panels[13].gridPos).toMatchObject({ x: 6, y: 8, h: 2, w: 6 });
  288. });
  289. });
  290. describe('given dashboard with row repeat', () => {
  291. let dashboard: any, dashboardJSON: any;
  292. beforeEach(() => {
  293. dashboardJSON = {
  294. panels: [
  295. {
  296. id: 1,
  297. type: 'row',
  298. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  299. repeat: 'apps',
  300. },
  301. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  302. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  303. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } },
  304. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  305. ],
  306. templating: {
  307. list: [
  308. {
  309. name: 'apps',
  310. type: 'custom',
  311. current: {
  312. text: 'se1, se2',
  313. value: ['se1', 'se2'],
  314. },
  315. options: [
  316. { text: 'se1', value: 'se1', selected: true },
  317. { text: 'se2', value: 'se2', selected: true },
  318. { text: 'se3', value: 'se3', selected: false },
  319. ],
  320. },
  321. ],
  322. },
  323. };
  324. dashboard = getDashboardModel(dashboardJSON);
  325. dashboard.processRepeats();
  326. });
  327. it('should not repeat only row', () => {
  328. const panelTypes = map(dashboard.panels, 'type');
  329. expect(panelTypes).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph', 'row', 'graph']);
  330. });
  331. it('should set scopedVars for each panel', () => {
  332. dashboardJSON.templating.list[0].options[2].selected = true;
  333. dashboard = getDashboardModel(dashboardJSON);
  334. dashboard.processRepeats();
  335. expect(dashboard.panels[1].scopedVars).toMatchObject({
  336. apps: { text: 'se1', value: 'se1' },
  337. });
  338. expect(dashboard.panels[4].scopedVars).toMatchObject({
  339. apps: { text: 'se2', value: 'se2' },
  340. });
  341. const scopedVars = compact(
  342. map(dashboard.panels, (panel) => {
  343. return panel.scopedVars ? panel.scopedVars.apps.value : null;
  344. })
  345. );
  346. expect(scopedVars).toEqual(['se1', 'se1', 'se1', 'se2', 'se2', 'se2', 'se3', 'se3', 'se3']);
  347. });
  348. it('should repeat only configured row', () => {
  349. expect(dashboard.panels[6].id).toBe(4);
  350. expect(dashboard.panels[7].id).toBe(5);
  351. });
  352. it('should repeat only row if it is collapsed', () => {
  353. dashboardJSON.panels = [
  354. {
  355. id: 1,
  356. type: 'row',
  357. collapsed: true,
  358. repeat: 'apps',
  359. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  360. panels: [
  361. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  362. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  363. ],
  364. },
  365. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  366. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  367. ];
  368. dashboard = getDashboardModel(dashboardJSON);
  369. dashboard.processRepeats();
  370. const panelTypes = map(dashboard.panels, 'type');
  371. expect(panelTypes).toEqual(['row', 'row', 'row', 'graph']);
  372. expect(dashboard.panels[0].panels).toHaveLength(2);
  373. expect(dashboard.panels[1].panels).toHaveLength(2);
  374. });
  375. it('should properly repeat multiple rows', () => {
  376. dashboardJSON.panels = [
  377. {
  378. id: 1,
  379. type: 'row',
  380. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  381. repeat: 'apps',
  382. }, // repeat
  383. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  384. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  385. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } }, // don't touch
  386. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  387. {
  388. id: 6,
  389. type: 'row',
  390. gridPos: { x: 0, y: 4, h: 1, w: 24 },
  391. repeat: 'hosts',
  392. }, // repeat
  393. { id: 7, type: 'graph', gridPos: { x: 0, y: 5, h: 1, w: 6 } },
  394. { id: 8, type: 'graph', gridPos: { x: 6, y: 5, h: 1, w: 6 } },
  395. ];
  396. dashboardJSON.templating.list.push({
  397. name: 'hosts',
  398. type: 'custom',
  399. current: {
  400. text: 'backend01, backend02',
  401. value: ['backend01', 'backend02'],
  402. },
  403. options: [
  404. { text: 'backend01', value: 'backend01', selected: true },
  405. { text: 'backend02', value: 'backend02', selected: true },
  406. { text: 'backend03', value: 'backend03', selected: false },
  407. ],
  408. });
  409. dashboard = getDashboardModel(dashboardJSON);
  410. dashboard.processRepeats();
  411. const panelTypes = map(dashboard.panels, 'type');
  412. expect(panelTypes).toEqual([
  413. 'row',
  414. 'graph',
  415. 'graph',
  416. 'row',
  417. 'graph',
  418. 'graph',
  419. 'row',
  420. 'graph',
  421. 'row',
  422. 'graph',
  423. 'graph',
  424. 'row',
  425. 'graph',
  426. 'graph',
  427. ]);
  428. expect(dashboard.panels[0].scopedVars['apps'].value).toBe('se1');
  429. expect(dashboard.panels[1].scopedVars['apps'].value).toBe('se1');
  430. expect(dashboard.panels[3].scopedVars['apps'].value).toBe('se2');
  431. expect(dashboard.panels[4].scopedVars['apps'].value).toBe('se2');
  432. expect(dashboard.panels[8].scopedVars['hosts'].value).toBe('backend01');
  433. expect(dashboard.panels[9].scopedVars['hosts'].value).toBe('backend01');
  434. expect(dashboard.panels[11].scopedVars['hosts'].value).toBe('backend02');
  435. expect(dashboard.panels[12].scopedVars['hosts'].value).toBe('backend02');
  436. });
  437. it('should assign unique ids for repeated panels', () => {
  438. dashboardJSON.panels = [
  439. {
  440. id: 1,
  441. type: 'row',
  442. collapsed: true,
  443. repeat: 'apps',
  444. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  445. panels: [
  446. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  447. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  448. ],
  449. },
  450. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  451. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  452. ];
  453. dashboard = getDashboardModel(dashboardJSON);
  454. dashboard.processRepeats();
  455. const panelIds = flattenDeep(
  456. map(dashboard.panels, (panel) => {
  457. let ids = [];
  458. if (panel.panels && panel.panels.length) {
  459. ids = map(panel.panels, 'id');
  460. }
  461. ids.push(panel.id);
  462. return ids;
  463. })
  464. );
  465. expect(panelIds.length).toEqual(uniq(panelIds).length);
  466. });
  467. it('should place new panels in proper order', () => {
  468. dashboardJSON.panels = [
  469. { id: 1, type: 'row', gridPos: { x: 0, y: 0, h: 1, w: 24 }, repeat: 'apps' },
  470. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 3, w: 12 } },
  471. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 4, w: 12 } },
  472. { id: 4, type: 'graph', gridPos: { x: 0, y: 5, h: 2, w: 12 } },
  473. ];
  474. dashboard = getDashboardModel(dashboardJSON);
  475. dashboard.processRepeats();
  476. const panelTypes = map(dashboard.panels, 'type');
  477. expect(panelTypes).toEqual(['row', 'graph', 'graph', 'graph', 'row', 'graph', 'graph', 'graph']);
  478. const panelYPositions = map(dashboard.panels, (p) => p.gridPos.y);
  479. expect(panelYPositions).toEqual([0, 1, 1, 5, 7, 8, 8, 12]);
  480. });
  481. });
  482. describe('given dashboard with row and panel repeat', () => {
  483. let dashboard: DashboardModel, dashboardJSON: any;
  484. beforeEach(() => {
  485. dashboardJSON = {
  486. panels: [
  487. {
  488. id: 1,
  489. type: 'row',
  490. repeat: 'region',
  491. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  492. },
  493. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  494. ],
  495. templating: {
  496. list: [
  497. {
  498. name: 'region',
  499. type: 'custom',
  500. current: {
  501. text: 'reg1, reg2',
  502. value: ['reg1', 'reg2'],
  503. },
  504. options: [
  505. { text: 'reg1', value: 'reg1', selected: true },
  506. { text: 'reg2', value: 'reg2', selected: true },
  507. { text: 'reg3', value: 'reg3', selected: false },
  508. ],
  509. },
  510. {
  511. name: 'app',
  512. type: 'custom',
  513. current: {
  514. text: 'se1, se2',
  515. value: ['se1', 'se2'],
  516. },
  517. options: [
  518. { text: 'se1', value: 'se1', selected: true },
  519. { text: 'se2', value: 'se2', selected: true },
  520. { text: 'se3', value: 'se3', selected: false },
  521. ],
  522. },
  523. ],
  524. },
  525. };
  526. dashboard = getDashboardModel(dashboardJSON);
  527. dashboard.processRepeats();
  528. });
  529. it('should repeat row and panels for each row', () => {
  530. const panelTypes = map(dashboard.panels, 'type');
  531. expect(panelTypes).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  532. });
  533. it('Row repeat should create new panel keys every repeat cycle', () => {
  534. // This is the first repeated panel inside the second repeated row
  535. // Since we create a new panel model every time (and new panel events bus) we need to create a new key here to trigger a re-mount & re-subscribe
  536. const key1 = dashboard.panels[3].key;
  537. dashboard.processRepeats();
  538. expect(key1).not.toEqual(dashboard.panels[3].key);
  539. });
  540. it('should clean up old repeated panels', () => {
  541. dashboardJSON.panels = [
  542. {
  543. id: 1,
  544. type: 'row',
  545. repeat: 'region',
  546. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  547. },
  548. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  549. { id: 3, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 7, y: 1, h: 1, w: 6 } },
  550. {
  551. id: 11,
  552. type: 'row',
  553. repeatPanelId: 1,
  554. repeatIteration: 101,
  555. gridPos: { x: 0, y: 2, h: 1, w: 24 },
  556. },
  557. { id: 12, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 0, y: 3, h: 1, w: 6 } },
  558. ];
  559. let panelChangedEvents: DashboardPanelsChangedEvent[] = [];
  560. dashboard = getDashboardModel(dashboardJSON);
  561. dashboard.events.subscribe(DashboardPanelsChangedEvent, (evt) => panelChangedEvents.push(evt));
  562. dashboard.processRepeats();
  563. const panelTypes = map(dashboard.panels, 'type');
  564. expect(panelTypes).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  565. // Make sure only a single DashboardPanelsChangedEvent event is emitted when processing repeats
  566. expect(panelChangedEvents.length).toBe(1);
  567. });
  568. it('should set scopedVars for each row', () => {
  569. dashboard = getDashboardModel(dashboardJSON);
  570. dashboard.processRepeats();
  571. expect(dashboard.panels[0].scopedVars).toMatchObject({
  572. region: { text: 'reg1', value: 'reg1' },
  573. });
  574. expect(dashboard.panels[3].scopedVars).toMatchObject({
  575. region: { text: 'reg2', value: 'reg2' },
  576. });
  577. });
  578. it('should set panel-repeat variable for each panel', () => {
  579. dashboard = getDashboardModel(dashboardJSON);
  580. dashboard.processRepeats();
  581. expect(dashboard.panels[1].scopedVars).toMatchObject({
  582. app: { text: 'se1', value: 'se1' },
  583. });
  584. expect(dashboard.panels[2].scopedVars).toMatchObject({
  585. app: { text: 'se2', value: 'se2' },
  586. });
  587. expect(dashboard.panels[4].scopedVars).toMatchObject({
  588. app: { text: 'se1', value: 'se1' },
  589. });
  590. expect(dashboard.panels[5].scopedVars).toMatchObject({
  591. app: { text: 'se2', value: 'se2' },
  592. });
  593. });
  594. it('should set row-repeat variable for each panel', () => {
  595. dashboard = getDashboardModel(dashboardJSON);
  596. dashboard.processRepeats();
  597. expect(dashboard.panels[1].scopedVars).toMatchObject({
  598. region: { text: 'reg1', value: 'reg1' },
  599. });
  600. expect(dashboard.panels[2].scopedVars).toMatchObject({
  601. region: { text: 'reg1', value: 'reg1' },
  602. });
  603. expect(dashboard.panels[4].scopedVars).toMatchObject({
  604. region: { text: 'reg2', value: 'reg2' },
  605. });
  606. expect(dashboard.panels[5].scopedVars).toMatchObject({
  607. region: { text: 'reg2', value: 'reg2' },
  608. });
  609. });
  610. it('should repeat panels when row is expanding', () => {
  611. dashboard = getDashboardModel(dashboardJSON);
  612. dashboard.processRepeats();
  613. expect(dashboard.panels.length).toBe(6);
  614. // toggle row
  615. dashboard.toggleRow(dashboard.panels[0]);
  616. dashboard.toggleRow(dashboard.panels[1]);
  617. expect(dashboard.panels.length).toBe(2);
  618. // change variable
  619. dashboard.templating.list[1].current.value = ['se1', 'se2', 'se3'];
  620. // toggle row back
  621. dashboard.toggleRow(dashboard.panels[1]);
  622. expect(dashboard.panels.length).toBe(4);
  623. });
  624. });
  625. // fix for https://github.com/grafana/grafana/issues/38805
  626. describe('given dashboard with row and repeats on same row', () => {
  627. it('should set correct gridPos when row is expanding', () => {
  628. const ROW1 = 1;
  629. const GAUGE1 = 2;
  630. const REPEAT1 = 3;
  631. const GAUGE2 = 4;
  632. const REPEAT2 = 5;
  633. const GAUGE3 = 6;
  634. const dashboardJSON = {
  635. panels: [
  636. {
  637. collapsed: true,
  638. datasource: null,
  639. gridPos: { h: 1, w: 24, x: 0, y: 0 },
  640. id: ROW1,
  641. panels: [
  642. { gridPos: { h: 5, w: 4, x: 0, y: 1 }, id: GAUGE1, type: 'gauge' },
  643. {
  644. gridPos: { h: 5, w: 4, x: 4, y: 1 },
  645. id: REPEAT1,
  646. repeat: 'abc',
  647. repeatDirection: 'v',
  648. type: 'gauge',
  649. },
  650. { gridPos: { h: 5, w: 4, x: 8, y: 1 }, id: GAUGE2, type: 'gauge' },
  651. {
  652. gridPos: { h: 5, w: 4, x: 12, y: 1 },
  653. id: REPEAT2,
  654. repeat: 'abc',
  655. repeatDirection: 'v',
  656. type: 'gauge',
  657. },
  658. { gridPos: { h: 5, w: 4, x: 16, y: 1 }, id: GAUGE3, type: 'gauge' },
  659. ],
  660. title: 'Row title',
  661. type: 'row',
  662. },
  663. ],
  664. templating: {
  665. list: [
  666. {
  667. allValue: null,
  668. current: { selected: true, text: ['All'], value: ['$__all'] },
  669. includeAll: true,
  670. name: 'abc',
  671. options: [
  672. { selected: true, text: 'All', value: '$__all' },
  673. { selected: false, text: 'a', value: 'a' },
  674. { selected: false, text: 'b', value: 'b' },
  675. { selected: false, text: 'c', value: 'c' },
  676. { selected: false, text: 'd', value: 'd' },
  677. { selected: false, text: 'e', value: 'e' },
  678. { selected: false, text: 'f', value: 'f' },
  679. { selected: false, text: 'g', value: 'g' },
  680. ],
  681. type: 'custom',
  682. },
  683. ],
  684. },
  685. };
  686. const dashboard = getDashboardModel(dashboardJSON);
  687. // toggle row
  688. dashboard.toggleRow(dashboard.panels[0]);
  689. // correct number of panels
  690. expect(dashboard.panels.length).toBe(18);
  691. // check row
  692. const rowPanel = dashboard.panels.find((p) => p.id === ROW1);
  693. expect(rowPanel?.gridPos).toEqual({ x: 0, y: 0, w: 24, h: 1 });
  694. // check the gridPos of all the top level panels that are next to each other
  695. const firstGauge = dashboard.panels.find((p) => p.id === GAUGE1);
  696. const secondGauge = dashboard.panels.find((p) => p.id === GAUGE2);
  697. const thirdGauge = dashboard.panels.find((p) => p.id === GAUGE3);
  698. const firstVerticalRepeatingGauge = dashboard.panels.find((p) => p.id === REPEAT1);
  699. const secondVerticalRepeatingGauge = dashboard.panels.find((p) => p.id === REPEAT2);
  700. expect(firstGauge?.gridPos).toEqual({ x: 0, y: 1, w: 4, h: 5 });
  701. expect(secondGauge?.gridPos).toEqual({ x: 8, y: 1, w: 4, h: 5 });
  702. expect(thirdGauge?.gridPos).toEqual({ x: 16, y: 1, w: 4, h: 5 });
  703. expect(firstVerticalRepeatingGauge?.gridPos).toEqual({ x: 4, y: 1, w: 4, h: 5 });
  704. expect(secondVerticalRepeatingGauge?.gridPos).toEqual({ x: 12, y: 1, w: 4, h: 5 });
  705. // check the gridPos of all first vertical repeats children
  706. const { x, h, w } = firstVerticalRepeatingGauge!.gridPos;
  707. expect(dashboard.panels[6].gridPos).toEqual({ x, y: 6, w, h });
  708. expect(dashboard.panels[8].gridPos).toEqual({ x, y: 11, w, h });
  709. expect(dashboard.panels[10].gridPos).toEqual({ x, y: 16, w, h });
  710. expect(dashboard.panels[12].gridPos).toEqual({ x, y: 21, w, h });
  711. expect(dashboard.panels[14].gridPos).toEqual({ x, y: 26, w, h });
  712. expect(dashboard.panels[16].gridPos).toEqual({ x, y: 31, w, h });
  713. // check the gridPos of all second vertical repeats children
  714. const { x: x2, h: h2, w: w2 } = secondVerticalRepeatingGauge!.gridPos;
  715. expect(dashboard.panels[7].gridPos).toEqual({ x: x2, y: 6, w: w2, h: h2 });
  716. expect(dashboard.panels[9].gridPos).toEqual({ x: x2, y: 11, w: w2, h: h2 });
  717. expect(dashboard.panels[11].gridPos).toEqual({ x: x2, y: 16, w: w2, h: h2 });
  718. expect(dashboard.panels[13].gridPos).toEqual({ x: x2, y: 21, w: w2, h: h2 });
  719. expect(dashboard.panels[15].gridPos).toEqual({ x: x2, y: 26, w: w2, h: h2 });
  720. expect(dashboard.panels[17].gridPos).toEqual({ x: x2, y: 31, w: w2, h: h2 });
  721. });
  722. });
  723. describe('given panel is in view mode', () => {
  724. let dashboard: any;
  725. beforeEach(() => {
  726. const dashboardJSON = {
  727. panels: [
  728. {
  729. id: 1,
  730. repeat: 'apps',
  731. repeatDirection: 'h',
  732. gridPos: { x: 0, y: 0, h: 2, w: 24 },
  733. },
  734. ],
  735. templating: {
  736. list: [
  737. {
  738. name: 'apps',
  739. type: 'custom',
  740. current: {
  741. text: 'se1, se2, se3',
  742. value: ['se1', 'se2', 'se3'],
  743. },
  744. options: [
  745. { text: 'se1', value: 'se1', selected: true },
  746. { text: 'se2', value: 'se2', selected: true },
  747. { text: 'se3', value: 'se3', selected: true },
  748. { text: 'se4', value: 'se4', selected: false },
  749. ],
  750. },
  751. ],
  752. },
  753. };
  754. dashboard = getDashboardModel(dashboardJSON);
  755. dashboard.initViewPanel(
  756. new PanelModel({
  757. id: 2,
  758. repeat: undefined,
  759. repeatDirection: 'h',
  760. panels: [
  761. {
  762. id: 2,
  763. repeat: 'apps',
  764. repeatDirection: 'h',
  765. gridPos: { x: 0, y: 0, h: 2, w: 24 },
  766. },
  767. ],
  768. repeatPanelId: 2,
  769. })
  770. );
  771. dashboard.processRepeats();
  772. });
  773. it('should set correct repeated panel to be in view', () => {
  774. expect(dashboard.panels[1].isViewing).toBeTruthy();
  775. });
  776. });