repeat.jest.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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', function() {
  158. let dashboard, dashboardJSON;
  159. beforeEach(function() {
  160. dashboardJSON = {
  161. panels: [
  162. {
  163. id: 1,
  164. type: 'row',
  165. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  166. repeat: 'apps',
  167. },
  168. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  169. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  170. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } },
  171. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  172. ],
  173. templating: {
  174. list: [
  175. {
  176. name: 'apps',
  177. current: {
  178. text: 'se1, se2',
  179. value: ['se1', 'se2'],
  180. },
  181. options: [
  182. { text: 'se1', value: 'se1', selected: true },
  183. { text: 'se2', value: 'se2', selected: true },
  184. { text: 'se3', value: 'se3', selected: false },
  185. ],
  186. },
  187. ],
  188. },
  189. };
  190. dashboard = new DashboardModel(dashboardJSON);
  191. dashboard.processRepeats();
  192. });
  193. it('should not repeat only row', function() {
  194. const panel_types = _.map(dashboard.panels, 'type');
  195. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph', 'row', 'graph']);
  196. });
  197. it('should set scopedVars for each panel', function() {
  198. dashboardJSON.templating.list[0].options[2].selected = true;
  199. dashboard = new DashboardModel(dashboardJSON);
  200. dashboard.processRepeats();
  201. expect(dashboard.panels[1].scopedVars).toMatchObject({
  202. apps: { text: 'se1', value: 'se1' },
  203. });
  204. expect(dashboard.panels[4].scopedVars).toMatchObject({
  205. apps: { text: 'se2', value: 'se2' },
  206. });
  207. const scopedVars = _.compact(
  208. _.map(dashboard.panels, panel => {
  209. return panel.scopedVars ? panel.scopedVars.apps.value : null;
  210. })
  211. );
  212. expect(scopedVars).toEqual(['se1', 'se1', 'se1', 'se2', 'se2', 'se2', 'se3', 'se3', 'se3']);
  213. });
  214. it('should repeat only configured row', function() {
  215. expect(dashboard.panels[6].id).toBe(4);
  216. expect(dashboard.panels[7].id).toBe(5);
  217. });
  218. it('should repeat only row if it is collapsed', function() {
  219. dashboardJSON.panels = [
  220. {
  221. id: 1,
  222. type: 'row',
  223. collapsed: true,
  224. repeat: 'apps',
  225. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  226. panels: [
  227. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  228. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  229. ],
  230. },
  231. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  232. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  233. ];
  234. dashboard = new DashboardModel(dashboardJSON);
  235. dashboard.processRepeats();
  236. const panel_types = _.map(dashboard.panels, 'type');
  237. expect(panel_types).toEqual(['row', 'row', 'row', 'graph']);
  238. expect(dashboard.panels[0].panels).toHaveLength(2);
  239. expect(dashboard.panels[1].panels).toHaveLength(2);
  240. });
  241. it('should properly repeat multiple rows', function() {
  242. dashboardJSON.panels = [
  243. {
  244. id: 1,
  245. type: 'row',
  246. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  247. repeat: 'apps',
  248. }, // repeat
  249. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  250. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  251. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } }, // don't touch
  252. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  253. {
  254. id: 6,
  255. type: 'row',
  256. gridPos: { x: 0, y: 4, h: 1, w: 24 },
  257. repeat: 'hosts',
  258. }, // repeat
  259. { id: 7, type: 'graph', gridPos: { x: 0, y: 5, h: 1, w: 6 } },
  260. { id: 8, type: 'graph', gridPos: { x: 6, y: 5, h: 1, w: 6 } },
  261. ];
  262. dashboardJSON.templating.list.push({
  263. name: 'hosts',
  264. current: {
  265. text: 'backend01, backend02',
  266. value: ['backend01', 'backend02'],
  267. },
  268. options: [
  269. { text: 'backend01', value: 'backend01', selected: true },
  270. { text: 'backend02', value: 'backend02', selected: true },
  271. { text: 'backend03', value: 'backend03', selected: false },
  272. ],
  273. });
  274. dashboard = new DashboardModel(dashboardJSON);
  275. dashboard.processRepeats();
  276. const panel_types = _.map(dashboard.panels, 'type');
  277. expect(panel_types).toEqual([
  278. 'row',
  279. 'graph',
  280. 'graph',
  281. 'row',
  282. 'graph',
  283. 'graph',
  284. 'row',
  285. 'graph',
  286. 'row',
  287. 'graph',
  288. 'graph',
  289. 'row',
  290. 'graph',
  291. 'graph',
  292. ]);
  293. expect(dashboard.panels[0].scopedVars['apps'].value).toBe('se1');
  294. expect(dashboard.panels[1].scopedVars['apps'].value).toBe('se1');
  295. expect(dashboard.panels[3].scopedVars['apps'].value).toBe('se2');
  296. expect(dashboard.panels[4].scopedVars['apps'].value).toBe('se2');
  297. expect(dashboard.panels[8].scopedVars['hosts'].value).toBe('backend01');
  298. expect(dashboard.panels[9].scopedVars['hosts'].value).toBe('backend01');
  299. expect(dashboard.panels[11].scopedVars['hosts'].value).toBe('backend02');
  300. expect(dashboard.panels[12].scopedVars['hosts'].value).toBe('backend02');
  301. });
  302. it('should assign unique ids for repeated panels', function() {
  303. dashboardJSON.panels = [
  304. {
  305. id: 1,
  306. type: 'row',
  307. collapsed: true,
  308. repeat: 'apps',
  309. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  310. panels: [
  311. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  312. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  313. ],
  314. },
  315. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  316. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  317. ];
  318. dashboard = new DashboardModel(dashboardJSON);
  319. dashboard.processRepeats();
  320. const panel_ids = _.flattenDeep(
  321. _.map(dashboard.panels, panel => {
  322. let ids = [];
  323. if (panel.panels && panel.panels.length) {
  324. ids = _.map(panel.panels, 'id');
  325. }
  326. ids.push(panel.id);
  327. return ids;
  328. })
  329. );
  330. expect(panel_ids.length).toEqual(_.uniq(panel_ids).length);
  331. });
  332. });
  333. describe('given dashboard with row and panel repeat', () => {
  334. let dashboard, dashboardJSON;
  335. beforeEach(() => {
  336. dashboardJSON = {
  337. panels: [
  338. {
  339. id: 1,
  340. type: 'row',
  341. repeat: 'region',
  342. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  343. },
  344. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  345. ],
  346. templating: {
  347. list: [
  348. {
  349. name: 'region',
  350. current: {
  351. text: 'reg1, reg2',
  352. value: ['reg1', 'reg2'],
  353. },
  354. options: [
  355. { text: 'reg1', value: 'reg1', selected: true },
  356. { text: 'reg2', value: 'reg2', selected: true },
  357. { text: 'reg3', value: 'reg3', selected: false },
  358. ],
  359. },
  360. {
  361. name: 'app',
  362. current: {
  363. text: 'se1, se2',
  364. value: ['se1', 'se2'],
  365. },
  366. options: [
  367. { text: 'se1', value: 'se1', selected: true },
  368. { text: 'se2', value: 'se2', selected: true },
  369. { text: 'se3', value: 'se3', selected: false },
  370. ],
  371. },
  372. ],
  373. },
  374. };
  375. dashboard = new DashboardModel(dashboardJSON);
  376. dashboard.processRepeats();
  377. });
  378. it('should repeat row and panels for each row', () => {
  379. const panel_types = _.map(dashboard.panels, 'type');
  380. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  381. });
  382. it('should clean up old repeated panels', () => {
  383. dashboardJSON.panels = [
  384. {
  385. id: 1,
  386. type: 'row',
  387. repeat: 'region',
  388. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  389. },
  390. { id: 2, type: 'graph', repeat: 'app', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  391. { id: 3, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 7, y: 1, h: 1, w: 6 } },
  392. {
  393. id: 11,
  394. type: 'row',
  395. repeatPanelId: 1,
  396. repeatIteration: 101,
  397. gridPos: { x: 0, y: 2, h: 1, w: 24 },
  398. },
  399. { id: 12, type: 'graph', repeatPanelId: 2, repeatIteration: 101, gridPos: { x: 0, y: 3, h: 1, w: 6 } },
  400. ];
  401. dashboard = new DashboardModel(dashboardJSON);
  402. dashboard.processRepeats();
  403. const panel_types = _.map(dashboard.panels, 'type');
  404. expect(panel_types).toEqual(['row', 'graph', 'graph', 'row', 'graph', 'graph']);
  405. });
  406. it('should set scopedVars for each row', () => {
  407. dashboard = new DashboardModel(dashboardJSON);
  408. dashboard.processRepeats();
  409. expect(dashboard.panels[0].scopedVars).toMatchObject({
  410. region: { text: 'reg1', value: 'reg1' },
  411. });
  412. expect(dashboard.panels[3].scopedVars).toMatchObject({
  413. region: { text: 'reg2', value: 'reg2' },
  414. });
  415. });
  416. it('should set panel-repeat variable for each panel', () => {
  417. dashboard = new DashboardModel(dashboardJSON);
  418. dashboard.processRepeats();
  419. expect(dashboard.panels[1].scopedVars).toMatchObject({
  420. app: { text: 'se1', value: 'se1' },
  421. });
  422. expect(dashboard.panels[2].scopedVars).toMatchObject({
  423. app: { text: 'se2', value: 'se2' },
  424. });
  425. expect(dashboard.panels[4].scopedVars).toMatchObject({
  426. app: { text: 'se1', value: 'se1' },
  427. });
  428. expect(dashboard.panels[5].scopedVars).toMatchObject({
  429. app: { text: 'se2', value: 'se2' },
  430. });
  431. });
  432. it('should set row-repeat variable for each panel', () => {
  433. dashboard = new DashboardModel(dashboardJSON);
  434. dashboard.processRepeats();
  435. expect(dashboard.panels[1].scopedVars).toMatchObject({
  436. region: { text: 'reg1', value: 'reg1' },
  437. });
  438. expect(dashboard.panels[2].scopedVars).toMatchObject({
  439. region: { text: 'reg1', value: 'reg1' },
  440. });
  441. expect(dashboard.panels[4].scopedVars).toMatchObject({
  442. region: { text: 'reg2', value: 'reg2' },
  443. });
  444. expect(dashboard.panels[5].scopedVars).toMatchObject({
  445. region: { text: 'reg2', value: 'reg2' },
  446. });
  447. });
  448. });