time_region_manager.test.ts 16 KB

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