time_region_manager.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { TimeRegionManager, colorModes } from '../time_region_manager';
  2. import { dateTime } from '@grafana/data';
  3. describe('TimeRegionManager', () => {
  4. function plotOptionsScenario(desc: string, func: any) {
  5. describe(desc, () => {
  6. const ctx: any = {
  7. panel: {
  8. timeRegions: [],
  9. },
  10. options: {
  11. grid: { markings: [] },
  12. },
  13. panelCtrl: {
  14. range: {},
  15. dashboard: {
  16. isTimezoneUtc: () => false,
  17. },
  18. },
  19. };
  20. ctx.setup = (regions: any, from: any, to: any) => {
  21. ctx.panel.timeRegions = regions;
  22. ctx.panelCtrl.range.from = from;
  23. ctx.panelCtrl.range.to = to;
  24. const manager = new TimeRegionManager(ctx.panelCtrl);
  25. manager.addFlotOptions(ctx.options, ctx.panel);
  26. };
  27. ctx.printScenario = () => {
  28. console.log(
  29. `Time range: from=${ctx.panelCtrl.range.from.format()}, to=${ctx.panelCtrl.range.to.format()}`,
  30. ctx.panelCtrl.range.from._isUTC
  31. );
  32. ctx.options.grid.markings.forEach((m: any, i: number) => {
  33. console.log(
  34. `Marking (${i}): from=${dateTime(m.xaxis.from).format()}, to=${dateTime(m.xaxis.to).format()}, color=${
  35. m.color
  36. }`
  37. );
  38. });
  39. };
  40. func(ctx);
  41. });
  42. }
  43. describe('When colors missing in config', () => {
  44. plotOptionsScenario('should not throw an error when fillColor is undefined', (ctx: any) => {
  45. const regions = [
  46. { fromDayOfWeek: 1, toDayOfWeek: 1, fill: true, line: true, lineColor: '#ffffff', colorMode: 'custom' },
  47. ];
  48. const from = dateTime('2018-01-01T00:00:00+01:00');
  49. const to = dateTime('2018-01-01T23:59:00+01:00');
  50. expect(() => ctx.setup(regions, from, to)).not.toThrow();
  51. });
  52. plotOptionsScenario('should not throw an error when lineColor is undefined', (ctx: any) => {
  53. const regions = [
  54. { fromDayOfWeek: 1, toDayOfWeek: 1, fill: true, fillColor: '#ffffff', line: true, colorMode: 'custom' },
  55. ];
  56. const from = dateTime('2018-01-01T00:00:00+01:00');
  57. const to = dateTime('2018-01-01T23:59:00+01:00');
  58. expect(() => ctx.setup(regions, from, to)).not.toThrow();
  59. });
  60. });
  61. describe('When creating plot markings using local time', () => {
  62. plotOptionsScenario('for day of week region', (ctx: any) => {
  63. const regions = [{ fromDayOfWeek: 1, toDayOfWeek: 1, fill: true, line: true, colorMode: 'red' }];
  64. const from = dateTime('2018-01-01T00:00:00+01:00');
  65. const to = dateTime('2018-01-01T23:59:00+01:00');
  66. ctx.setup(regions, from, to);
  67. it('should add 3 markings', () => {
  68. expect(ctx.options.grid.markings.length).toBe(3);
  69. });
  70. it('should add fill', () => {
  71. const markings = ctx.options.grid.markings;
  72. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-01-01T01:00:00+01:00').format());
  73. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-01-02T00:59:59+01:00').format());
  74. expect(markings[0].color).toBe(colorModes.red.color.fill);
  75. });
  76. it('should add line before', () => {
  77. const markings = ctx.options.grid.markings;
  78. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-01-01T01:00:00+01:00').format());
  79. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-01-01T01:00:00+01:00').format());
  80. expect(markings[1].color).toBe(colorModes.red.color.line);
  81. });
  82. it('should add line after', () => {
  83. const markings = ctx.options.grid.markings;
  84. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-01-02T00:59:59+01:00').format());
  85. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-01-02T00:59:59+01:00').format());
  86. expect(markings[2].color).toBe(colorModes.red.color.line);
  87. });
  88. });
  89. plotOptionsScenario('for time from region', (ctx: any) => {
  90. const regions = [{ from: '05:00', fill: true, colorMode: 'red' }];
  91. const from = dateTime('2018-01-01T00:00+01:00');
  92. const to = dateTime('2018-01-03T23:59+01:00');
  93. ctx.setup(regions, from, to);
  94. it('should add 3 markings', () => {
  95. expect(ctx.options.grid.markings.length).toBe(3);
  96. });
  97. it('should add one fill at 05:00 each day', () => {
  98. const markings = ctx.options.grid.markings;
  99. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-01-01T06:00:00+01:00').format());
  100. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-01-01T06:00:00+01:00').format());
  101. expect(markings[0].color).toBe(colorModes.red.color.fill);
  102. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-01-02T06:00:00+01:00').format());
  103. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-01-02T06:00:00+01:00').format());
  104. expect(markings[1].color).toBe(colorModes.red.color.fill);
  105. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-01-03T06:00:00+01:00').format());
  106. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-01-03T06:00:00+01:00').format());
  107. expect(markings[2].color).toBe(colorModes.red.color.fill);
  108. });
  109. });
  110. plotOptionsScenario('for time to region', (ctx: any) => {
  111. const regions = [{ to: '05:00', fill: true, colorMode: 'red' }];
  112. const from = dateTime('2018-02-01T00:00+01:00');
  113. const to = dateTime('2018-02-03T23:59+01:00');
  114. ctx.setup(regions, from, to);
  115. it('should add 3 markings', () => {
  116. expect(ctx.options.grid.markings.length).toBe(3);
  117. });
  118. it('should add one fill at 05:00 each day', () => {
  119. const markings = ctx.options.grid.markings;
  120. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-02-01T06:00:00+01:00').format());
  121. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-02-01T06:00:00+01:00').format());
  122. expect(markings[0].color).toBe(colorModes.red.color.fill);
  123. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-02-02T06:00:00+01:00').format());
  124. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-02-02T06:00:00+01:00').format());
  125. expect(markings[1].color).toBe(colorModes.red.color.fill);
  126. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-02-03T06:00:00+01:00').format());
  127. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-02-03T06:00:00+01:00').format());
  128. expect(markings[2].color).toBe(colorModes.red.color.fill);
  129. });
  130. });
  131. plotOptionsScenario('for time from/to region', (ctx: any) => {
  132. const regions = [{ from: '00:00', to: '05:00', fill: true, colorMode: 'red' }];
  133. const from = dateTime('2018-12-01T00:00+01:00');
  134. const to = dateTime('2018-12-03T23:59+01:00');
  135. ctx.setup(regions, from, to);
  136. it('should add 3 markings', () => {
  137. expect(ctx.options.grid.markings.length).toBe(3);
  138. });
  139. it('should add one fill between 00:00 and 05:00 each day', () => {
  140. const markings = ctx.options.grid.markings;
  141. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-12-01T01:00:00+01:00').format());
  142. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-12-01T06:00:00+01:00').format());
  143. expect(markings[0].color).toBe(colorModes.red.color.fill);
  144. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-12-02T01:00:00+01:00').format());
  145. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-12-02T06:00:00+01:00').format());
  146. expect(markings[1].color).toBe(colorModes.red.color.fill);
  147. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-12-03T01:00:00+01:00').format());
  148. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-12-03T06:00:00+01:00').format());
  149. expect(markings[2].color).toBe(colorModes.red.color.fill);
  150. });
  151. });
  152. plotOptionsScenario('for time from/to region crossing midnight', (ctx: any) => {
  153. const regions = [{ from: '22:00', to: '00:30', fill: true, colorMode: 'red' }];
  154. const from = dateTime('2018-12-01T12:00+01:00');
  155. const to = dateTime('2018-12-04T08:00+01:00');
  156. ctx.setup(regions, from, to);
  157. it('should add 3 markings', () => {
  158. expect(ctx.options.grid.markings.length).toBe(3);
  159. });
  160. it('should add one fill between 22:00 and 00:30 each day', () => {
  161. const markings = ctx.options.grid.markings;
  162. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-12-01T23:00:00+01:00').format());
  163. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-12-02T01:30:00+01:00').format());
  164. expect(markings[0].color).toBe(colorModes.red.color.fill);
  165. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-12-02T23:00:00+01:00').format());
  166. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-12-03T01:30:00+01:00').format());
  167. expect(markings[1].color).toBe(colorModes.red.color.fill);
  168. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-12-03T23:00:00+01:00').format());
  169. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-12-04T01:30:00+01:00').format());
  170. expect(markings[2].color).toBe(colorModes.red.color.fill);
  171. });
  172. });
  173. plotOptionsScenario('for day of week from/to region', (ctx: any) => {
  174. const regions = [{ fromDayOfWeek: 7, toDayOfWeek: 7, fill: true, colorMode: 'red' }];
  175. const from = dateTime('2018-01-01T18:45:05+01:00');
  176. const to = dateTime('2018-01-22T08:27:00+01:00');
  177. ctx.setup(regions, from, to);
  178. it('should add 3 markings', () => {
  179. expect(ctx.options.grid.markings.length).toBe(3);
  180. });
  181. it('should add one fill at each sunday', () => {
  182. const markings = ctx.options.grid.markings;
  183. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-01-07T01:00:00+01:00').format());
  184. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-01-08T00:59:59+01:00').format());
  185. expect(markings[0].color).toBe(colorModes.red.color.fill);
  186. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-01-14T01:00:00+01:00').format());
  187. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-01-15T00:59:59+01:00').format());
  188. expect(markings[1].color).toBe(colorModes.red.color.fill);
  189. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-01-21T01:00:00+01:00').format());
  190. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-01-22T00:59:59+01:00').format());
  191. expect(markings[2].color).toBe(colorModes.red.color.fill);
  192. });
  193. });
  194. plotOptionsScenario('for day of week from region', (ctx: any) => {
  195. const regions = [{ fromDayOfWeek: 7, fill: true, colorMode: 'red' }];
  196. const from = dateTime('2018-01-01T18:45:05+01:00');
  197. const to = dateTime('2018-01-22T08:27:00+01:00');
  198. ctx.setup(regions, from, to);
  199. it('should add 3 markings', () => {
  200. expect(ctx.options.grid.markings.length).toBe(3);
  201. });
  202. it('should add one fill at each sunday', () => {
  203. const markings = ctx.options.grid.markings;
  204. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-01-07T01:00:00+01:00').format());
  205. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-01-08T00:59:59+01:00').format());
  206. expect(markings[0].color).toBe(colorModes.red.color.fill);
  207. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-01-14T01:00:00+01:00').format());
  208. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-01-15T00:59:59+01:00').format());
  209. expect(markings[1].color).toBe(colorModes.red.color.fill);
  210. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-01-21T01:00:00+01:00').format());
  211. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-01-22T00:59:59+01:00').format());
  212. expect(markings[2].color).toBe(colorModes.red.color.fill);
  213. });
  214. });
  215. plotOptionsScenario('for day of week to region', (ctx: any) => {
  216. const regions = [{ toDayOfWeek: 7, fill: true, colorMode: 'red' }];
  217. const from = dateTime('2018-01-01T18:45:05+01:00');
  218. const to = dateTime('2018-01-22T08:27:00+01:00');
  219. ctx.setup(regions, from, to);
  220. it('should add 3 markings', () => {
  221. expect(ctx.options.grid.markings.length).toBe(3);
  222. });
  223. it('should add one fill at each sunday', () => {
  224. const markings = ctx.options.grid.markings;
  225. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-01-07T01:00:00+01:00').format());
  226. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-01-08T00:59:59+01:00').format());
  227. expect(markings[0].color).toBe(colorModes.red.color.fill);
  228. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-01-14T01:00:00+01:00').format());
  229. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-01-15T00:59:59+01:00').format());
  230. expect(markings[1].color).toBe(colorModes.red.color.fill);
  231. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-01-21T01:00:00+01:00').format());
  232. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-01-22T00:59:59+01:00').format());
  233. expect(markings[2].color).toBe(colorModes.red.color.fill);
  234. });
  235. });
  236. plotOptionsScenario('for day of week from/to time region', (ctx: any) => {
  237. const regions = [{ fromDayOfWeek: 7, from: '23:00', toDayOfWeek: 1, to: '01:40', fill: true, colorMode: 'red' }];
  238. const from = dateTime('2018-12-07T12:51:19+01:00');
  239. const to = dateTime('2018-12-10T13:51:29+01:00');
  240. ctx.setup(regions, from, to);
  241. it('should add 1 marking', () => {
  242. expect(ctx.options.grid.markings.length).toBe(1);
  243. });
  244. it('should add one fill between sunday 23:00 and monday 01:40', () => {
  245. const markings = ctx.options.grid.markings;
  246. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-12-10T00:00:00+01:00').format());
  247. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-12-10T02:40:00+01:00').format());
  248. });
  249. });
  250. plotOptionsScenario('for day of week from/to time region', (ctx: any) => {
  251. const regions = [{ fromDayOfWeek: 6, from: '03:00', toDayOfWeek: 7, to: '02:00', fill: true, colorMode: 'red' }];
  252. const from = dateTime('2018-12-07T12:51:19+01:00');
  253. const to = dateTime('2018-12-10T13:51:29+01:00');
  254. ctx.setup(regions, from, to);
  255. it('should add 1 marking', () => {
  256. expect(ctx.options.grid.markings.length).toBe(1);
  257. });
  258. it('should add one fill between saturday 03:00 and sunday 02:00', () => {
  259. const markings = ctx.options.grid.markings;
  260. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-12-08T04:00:00+01:00').format());
  261. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-12-09T03:00:00+01:00').format());
  262. });
  263. });
  264. plotOptionsScenario('for day of week from/to time region with daylight saving time', (ctx: any) => {
  265. const regions = [{ fromDayOfWeek: 7, from: '20:00', toDayOfWeek: 7, to: '23:00', fill: true, colorMode: 'red' }];
  266. const from = dateTime('2018-03-17T06:00:00+01:00');
  267. const to = dateTime('2018-04-03T06:00:00+02:00');
  268. ctx.setup(regions, from, to);
  269. it('should add 3 markings', () => {
  270. expect(ctx.options.grid.markings.length).toBe(3);
  271. });
  272. it('should add one fill at each sunday between 20:00 and 23:00', () => {
  273. const markings = ctx.options.grid.markings;
  274. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-03-18T21:00:00+01:00').format());
  275. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-03-19T00:00:00+01:00').format());
  276. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-03-25T22:00:00+02:00').format());
  277. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-03-26T01:00:00+02:00').format());
  278. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-04-01T22:00:00+02:00').format());
  279. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-04-02T01:00:00+02:00').format());
  280. });
  281. });
  282. plotOptionsScenario('for each day of week with winter time', (ctx: any) => {
  283. const regions = [{ fromDayOfWeek: 7, toDayOfWeek: 7, fill: true, colorMode: 'red' }];
  284. const from = dateTime('2018-10-20T14:50:11+02:00');
  285. const to = dateTime('2018-11-07T12:56:23+01:00');
  286. ctx.setup(regions, from, to);
  287. it('should add 3 markings', () => {
  288. expect(ctx.options.grid.markings.length).toBe(3);
  289. });
  290. it('should add one fill at each sunday', () => {
  291. const markings = ctx.options.grid.markings;
  292. expect(dateTime(markings[0].xaxis.from).format()).toBe(dateTime('2018-10-21T02:00:00+02:00').format());
  293. expect(dateTime(markings[0].xaxis.to).format()).toBe(dateTime('2018-10-22T01:59:59+02:00').format());
  294. expect(dateTime(markings[1].xaxis.from).format()).toBe(dateTime('2018-10-28T02:00:00+02:00').format());
  295. expect(dateTime(markings[1].xaxis.to).format()).toBe(dateTime('2018-10-29T00:59:59+01:00').format());
  296. expect(dateTime(markings[2].xaxis.from).format()).toBe(dateTime('2018-11-04T01:00:00+01:00').format());
  297. expect(dateTime(markings[2].xaxis.to).format()).toBe(dateTime('2018-11-05T00:59:59+01:00').format());
  298. });
  299. });
  300. });
  301. });