time_region_manager.test.ts 15 KB

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