dashboard_migration.jest.ts 13 KB

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