dashboard_migration.jest.ts 13 KB

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