dashboard_migration.jest.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import _ from 'lodash';
  2. import { DashboardModel } from '../dashboard_model';
  3. import { PanelModel } from '../panel_model';
  4. import {GRID_CELL_HEIGHT, GRID_CELL_VMARGIN} from 'app/core/constants';
  5. import { expect } from 'test/lib/common';
  6. jest.mock('app/core/services/context_srv', () => ({}));
  7. describe('DashboardModel', function() {
  8. describe('when creating dashboard with old schema', function() {
  9. var model;
  10. var graph;
  11. var singlestat;
  12. var table;
  13. beforeEach(function() {
  14. model = new DashboardModel({
  15. services: { filter: { time: { from: 'now-1d', to: 'now' }, list: [{}] } },
  16. pulldowns: [
  17. { type: 'filtering', enable: true },
  18. { type: 'annotations', enable: true, annotations: [{ name: 'old' }] },
  19. ],
  20. panels: [
  21. {
  22. type: 'graph',
  23. legend: true,
  24. aliasYAxis: { test: 2 },
  25. y_formats: ['kbyte', 'ms'],
  26. grid: {
  27. min: 1,
  28. max: 10,
  29. rightMin: 5,
  30. rightMax: 15,
  31. leftLogBase: 1,
  32. rightLogBase: 2,
  33. threshold1: 200,
  34. threshold2: 400,
  35. threshold1Color: 'yellow',
  36. threshold2Color: 'red',
  37. },
  38. leftYAxisLabel: 'left label',
  39. targets: [{ refId: 'A' }, {}],
  40. },
  41. {
  42. type: 'singlestat',
  43. legend: true,
  44. thresholds: '10,20,30',
  45. aliasYAxis: { test: 2 },
  46. grid: { min: 1, max: 10 },
  47. targets: [{ refId: 'A' }, {}],
  48. },
  49. {
  50. type: 'table',
  51. legend: true,
  52. styles: [{ thresholds: ['10', '20', '30'] }, { thresholds: ['100', '200', '300'] }],
  53. targets: [{ refId: 'A' }, {}],
  54. },
  55. ],
  56. });
  57. graph = model.panels[0];
  58. singlestat = model.panels[1];
  59. table = model.panels[2];
  60. });
  61. it('should have title', function() {
  62. expect(model.title).toBe('No Title');
  63. });
  64. it('should have panel id', function() {
  65. expect(graph.id).toBe(1);
  66. });
  67. it('should move time and filtering list', function() {
  68. expect(model.time.from).toBe('now-1d');
  69. expect(model.templating.list[0].allFormat).toBe('glob');
  70. });
  71. it('graphite panel should change name too graph', function() {
  72. expect(graph.type).toBe('graph');
  73. });
  74. it('single stat panel should have two thresholds', function() {
  75. expect(singlestat.thresholds).toBe('20,30');
  76. });
  77. it('queries without refId should get it', function() {
  78. expect(graph.targets[1].refId).toBe('B');
  79. });
  80. it('update legend setting', function() {
  81. expect(graph.legend.show).toBe(true);
  82. });
  83. it('move aliasYAxis to series override', function() {
  84. expect(graph.seriesOverrides[0].alias).toBe('test');
  85. expect(graph.seriesOverrides[0].yaxis).toBe(2);
  86. });
  87. it('should move pulldowns to new schema', function() {
  88. expect(model.annotations.list[1].name).toBe('old');
  89. });
  90. it('table panel should only have two thresholds values', function() {
  91. expect(table.styles[0].thresholds[0]).toBe('20');
  92. expect(table.styles[0].thresholds[1]).toBe('30');
  93. expect(table.styles[1].thresholds[0]).toBe('200');
  94. expect(table.styles[1].thresholds[1]).toBe('300');
  95. });
  96. it('graph grid to yaxes options', function() {
  97. expect(graph.yaxes[0].min).toBe(1);
  98. expect(graph.yaxes[0].max).toBe(10);
  99. expect(graph.yaxes[0].format).toBe('kbyte');
  100. expect(graph.yaxes[0].label).toBe('left label');
  101. expect(graph.yaxes[0].logBase).toBe(1);
  102. expect(graph.yaxes[1].min).toBe(5);
  103. expect(graph.yaxes[1].max).toBe(15);
  104. expect(graph.yaxes[1].format).toBe('ms');
  105. expect(graph.yaxes[1].logBase).toBe(2);
  106. expect(graph.grid.rightMax).toBe(undefined);
  107. expect(graph.grid.rightLogBase).toBe(undefined);
  108. expect(graph.y_formats).toBe(undefined);
  109. });
  110. it('dashboard schema version should be set to latest', function() {
  111. expect(model.schemaVersion).toBe(16);
  112. });
  113. it('graph thresholds should be migrated', function() {
  114. expect(graph.thresholds.length).toBe(2);
  115. expect(graph.thresholds[0].op).toBe('gt');
  116. expect(graph.thresholds[0].value).toBe(200);
  117. expect(graph.thresholds[0].fillColor).toBe('yellow');
  118. expect(graph.thresholds[1].value).toBe(400);
  119. expect(graph.thresholds[1].fillColor).toBe('red');
  120. });
  121. });
  122. describe('when migrating to the grid layout', function() {
  123. let model;
  124. beforeEach(function() {
  125. model = {
  126. rows: []
  127. };
  128. });
  129. it('should create proper grid', function() {
  130. model.rows = [
  131. createRow({collapse: false, height: 8}, [[6], [6]])
  132. ];
  133. let dashboard = new DashboardModel(model);
  134. let panelGridPos = getGridPositions(dashboard);
  135. let expectedGrid = [
  136. {x: 0, y: 0, w: 12, h: 8}, {x: 12, y: 0, w: 12, h: 8}
  137. ];
  138. expect(panelGridPos).toEqual(expectedGrid);
  139. });
  140. it('should add special "row" panel if row is collapsed', function() {
  141. model.rows = [
  142. createRow({collapse: true, height: 8}, [[6], [6]]),
  143. createRow({height: 8}, [[12]])
  144. ];
  145. let dashboard = new DashboardModel(model);
  146. let panelGridPos = getGridPositions(dashboard);
  147. let expectedGrid = [
  148. {x: 0, y: 0, w: 24, h: 8}, // row
  149. {x: 0, y: 1, w: 24, h: 8}, // row
  150. {x: 0, y: 2, w: 24, h: 8}
  151. ];
  152. expect(panelGridPos).toEqual(expectedGrid);
  153. });
  154. it('should add special "row" panel if row has visible title', function() {
  155. model.rows = [
  156. createRow({showTitle: true, title: "Row", height: 8}, [[6], [6]]),
  157. createRow({height: 8}, [[12]])
  158. ];
  159. let dashboard = new DashboardModel(model);
  160. let panelGridPos = getGridPositions(dashboard);
  161. let expectedGrid = [
  162. {x: 0, y: 0, w: 24, h: 8}, // row
  163. {x: 0, y: 1, w: 12, h: 8}, {x: 12, y: 1, w: 12, h: 8},
  164. {x: 0, y: 9, w: 24, h: 8}, // row
  165. {x: 0, y: 10, w: 24, h: 8},
  166. ];
  167. expect(panelGridPos).toEqual(expectedGrid);
  168. });
  169. it('should not add "row" panel if row has not visible title or not collapsed', function() {
  170. model.rows = [
  171. createRow({collapse: true, height: 8}, [[12]]),
  172. createRow({height: 8}, [[12]]),
  173. createRow({height: 8}, [[12], [6], [6]]),
  174. createRow({collapse: true, height: 8}, [[12]])
  175. ];
  176. let dashboard = new DashboardModel(model);
  177. let panelGridPos = getGridPositions(dashboard);
  178. let expectedGrid = [
  179. {x: 0, y: 0, w: 24, h: 8}, // row
  180. {x: 0, y: 1, w: 24, h: 8}, // row
  181. {x: 0, y: 2, w: 24, h: 8},
  182. {x: 0, y: 10, w: 24, h: 8}, // row
  183. {x: 0, y: 11, w: 24, h: 8},
  184. {x: 0, y: 19, w: 12, h: 8}, {x: 12, y: 19, w: 12, h: 8},
  185. {x: 0, y: 27, w: 24, h: 8}, // row
  186. ];
  187. expect(panelGridPos).toEqual(expectedGrid);
  188. });
  189. it('should add all rows if even one collapsed or titled row is present', function() {
  190. model.rows = [
  191. createRow({collapse: true, height: 8}, [[6], [6]]),
  192. createRow({height: 8}, [[12]])
  193. ];
  194. let dashboard = new DashboardModel(model);
  195. let panelGridPos = getGridPositions(dashboard);
  196. let expectedGrid = [
  197. {x: 0, y: 0, w: 24, h: 8}, // row
  198. {x: 0, y: 1, w: 24, h: 8}, // row
  199. {x: 0, y: 2, w: 24, h: 8}
  200. ];
  201. expect(panelGridPos).toEqual(expectedGrid);
  202. });
  203. it('should properly place panels with fixed height', function() {
  204. model.rows = [
  205. createRow({height: 6}, [[6], [6, 3], [6, 3]]),
  206. createRow({height: 6}, [[4], [4], [4, 3], [4, 3]])
  207. ];
  208. let dashboard = new DashboardModel(model);
  209. let panelGridPos = getGridPositions(dashboard);
  210. let expectedGrid = [
  211. {x: 0, y: 0, w: 12, h: 6}, {x: 12, y: 0, w: 12, h: 3}, {x: 12, y: 3, w: 12, h: 3},
  212. {x: 0, y: 6, w: 8, h: 6}, {x: 8, y: 6, w: 8, h: 6}, {x: 16, y: 6, w: 8, h: 3}, {x: 16, y: 9, w: 8, h: 3}
  213. ];
  214. expect(panelGridPos).toEqual(expectedGrid);
  215. });
  216. it('should place panel to the right side of panel having bigger height', function() {
  217. model.rows = [
  218. createRow({height: 6}, [[4], [2, 3], [4, 6], [2, 3], [2, 3]])
  219. ];
  220. let dashboard = new DashboardModel(model);
  221. let panelGridPos = getGridPositions(dashboard);
  222. let expectedGrid = [
  223. {x: 0, y: 0, w: 8, h: 6}, {x: 8, y: 0, w: 4, h: 3},
  224. {x: 12, y: 0, w: 8, h: 6}, {x: 20, y: 0, w: 4, h: 3}, {x: 20, y: 3, w: 4, h: 3}
  225. ];
  226. expect(panelGridPos).toEqual(expectedGrid);
  227. });
  228. it('should fill current row if it possible', function() {
  229. model.rows = [
  230. createRow({height: 9}, [[4], [2, 3], [4, 6], [2, 3], [2, 3], [8, 3]])
  231. ];
  232. let dashboard = new DashboardModel(model);
  233. let panelGridPos = getGridPositions(dashboard);
  234. let expectedGrid = [
  235. {x: 0, y: 0, w: 8, h: 9}, {x: 8, y: 0, w: 4, h: 3}, {x: 12, y: 0, w: 8, h: 6},
  236. {x: 20, y: 0, w: 4, h: 3}, {x: 20, y: 3, w: 4, h: 3}, {x: 8, y: 6, w: 16, h: 3}
  237. ];
  238. expect(panelGridPos).toEqual(expectedGrid);
  239. });
  240. it('should fill current row if it possible (2)', function() {
  241. model.rows = [
  242. createRow({height: 8}, [[4], [2, 3], [4, 6], [2, 3], [2, 3], [8, 3]])
  243. ];
  244. let dashboard = new DashboardModel(model);
  245. let panelGridPos = getGridPositions(dashboard);
  246. let expectedGrid = [
  247. {x: 0, y: 0, w: 8, h: 8}, {x: 8, y: 0, w: 4, h: 3}, {x: 12, y: 0, w: 8, h: 6},
  248. {x: 20, y: 0, w: 4, h: 3}, {x: 20, y: 3, w: 4, h: 3}, {x: 8, y: 6, w: 16, h: 3}
  249. ];
  250. expect(panelGridPos).toEqual(expectedGrid);
  251. });
  252. it('should fill current row if panel height more than row height', function() {
  253. model.rows = [
  254. createRow({height: 6}, [[4], [2, 3], [4, 8], [2, 3], [2, 3]])
  255. ];
  256. let dashboard = new DashboardModel(model);
  257. let panelGridPos = getGridPositions(dashboard);
  258. let expectedGrid = [
  259. {x: 0, y: 0, w: 8, h: 6}, {x: 8, y: 0, w: 4, h: 3},
  260. {x: 12, y: 0, w: 8, h: 8}, {x: 20, y: 0, w: 4, h: 3}, {x: 20, y: 3, w: 4, h: 3}
  261. ];
  262. expect(panelGridPos).toEqual(expectedGrid);
  263. });
  264. it('should wrap panels to multiple rows', function() {
  265. model.rows = [
  266. createRow({height: 6}, [[6], [6], [12], [6], [3], [3]])
  267. ];
  268. let dashboard = new DashboardModel(model);
  269. let panelGridPos = getGridPositions(dashboard);
  270. let expectedGrid = [
  271. {x: 0, y: 0, w: 12, h: 6}, {x: 12, y: 0, w: 12, h: 6},
  272. {x: 0, y: 6, w: 24, h: 6},
  273. {x: 0, y: 12, w: 12, h: 6}, {x: 12, y: 12, w: 6, h: 6}, {x: 18, y: 12, w: 6, h: 6},
  274. ];
  275. expect(panelGridPos).toEqual(expectedGrid);
  276. });
  277. it('should add repeated row if repeat set', function() {
  278. model.rows = [
  279. createRow({showTitle: true, title: "Row", height: 8, repeat: "server"}, [[6]]),
  280. createRow({height: 8}, [[12]])
  281. ];
  282. let dashboard = new DashboardModel(model);
  283. let panelGridPos = getGridPositions(dashboard);
  284. let expectedGrid = [
  285. {x: 0, y: 0, w: 24, h: 8},
  286. {x: 0, y: 1, w: 12, h: 8},
  287. {x: 0, y: 9, w: 24, h: 8},
  288. {x: 0, y: 10, w: 24, h: 8}
  289. ];
  290. expect(panelGridPos).toEqual(expectedGrid);
  291. expect(dashboard.panels[0].repeat).toBe("server");
  292. expect(dashboard.panels[1].repeat).toBeUndefined();
  293. expect(dashboard.panels[2].repeat).toBeUndefined();
  294. expect(dashboard.panels[3].repeat).toBeUndefined();
  295. });
  296. it('should ignore repeated row', function() {
  297. model.rows = [
  298. createRow({showTitle: true, title: "Row1", height: 8, repeat: "server"}, [[6]]),
  299. createRow({showTitle: true, title: "Row2", height: 8, repeatIteration: 12313, repeatRowId: 1}, [[6]]),
  300. ];
  301. let dashboard = new DashboardModel(model);
  302. expect(dashboard.panels[0].repeat).toBe("server");
  303. expect(dashboard.panels.length).toBe(2);
  304. });
  305. });
  306. });
  307. function createRow(options, panelDescriptions: any[]) {
  308. const PANEL_HEIGHT_STEP = GRID_CELL_HEIGHT + GRID_CELL_VMARGIN;
  309. let {collapse, height, showTitle, title, repeat, repeatIteration} = options;
  310. height = height * PANEL_HEIGHT_STEP;
  311. let panels = [];
  312. _.each(panelDescriptions, panelDesc => {
  313. let panel = {span: panelDesc[0]};
  314. if (panelDesc.length > 1) {
  315. panel['height'] = panelDesc[1] * PANEL_HEIGHT_STEP;
  316. }
  317. panels.push(panel);
  318. });
  319. let row = {collapse, height, showTitle, title, panels, repeat, repeatIteration};
  320. return row;
  321. }
  322. function getGridPositions(dashboard: DashboardModel) {
  323. return _.map(dashboard.panels, (panel: PanelModel) => {
  324. return panel.gridPos;
  325. });
  326. }