time_region_manager.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import 'vendor/flot/jquery.flot';
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import { GrafanaTheme, getColorFromHexRgbOrName } from '@grafana/ui';
  5. type TimeRegionColorDefinition = {
  6. fill: string;
  7. line: string;
  8. };
  9. export const colorModes = {
  10. gray: {
  11. themeDependent: true,
  12. title: 'Gray',
  13. darkColor: { fill: 'rgba(255, 255, 255, 0.09)', line: 'rgba(255, 255, 255, 0.2)' },
  14. lightColor: { fill: 'rgba(0, 0, 0, 0.09)', line: 'rgba(0, 0, 0, 0.2)' },
  15. },
  16. red: {
  17. title: 'Red',
  18. color: { fill: 'rgba(234, 112, 112, 0.12)', line: 'rgba(237, 46, 24, 0.60)' },
  19. },
  20. green: {
  21. title: 'Green',
  22. color: { fill: 'rgba(11, 237, 50, 0.090)', line: 'rgba(6,163,69, 0.60)' },
  23. },
  24. blue: {
  25. title: 'Blue',
  26. color: { fill: 'rgba(11, 125, 238, 0.12)', line: 'rgba(11, 125, 238, 0.60)' },
  27. },
  28. yellow: {
  29. title: 'Yellow',
  30. color: { fill: 'rgba(235, 138, 14, 0.12)', line: 'rgba(247, 149, 32, 0.60)' },
  31. },
  32. custom: { title: 'Custom' },
  33. };
  34. export function getColorModes() {
  35. return _.map(Object.keys(colorModes), key => {
  36. return {
  37. key: key,
  38. value: colorModes[key].title,
  39. };
  40. });
  41. }
  42. function getColor(timeRegion, theme: GrafanaTheme): TimeRegionColorDefinition {
  43. if (Object.keys(colorModes).indexOf(timeRegion.colorMode) === -1) {
  44. timeRegion.colorMode = 'red';
  45. }
  46. if (timeRegion.colorMode === 'custom') {
  47. return {
  48. fill: timeRegion.fill && timeRegion.fillColor ? getColorFromHexRgbOrName(timeRegion.fillColor, theme) : null,
  49. line: timeRegion.line && timeRegion.lineColor ? getColorFromHexRgbOrName(timeRegion.lineColor, theme) : null,
  50. };
  51. }
  52. const colorMode = colorModes[timeRegion.colorMode];
  53. if (colorMode.themeDependent === true) {
  54. return theme === GrafanaTheme.Light ? colorMode.lightColor : colorMode.darkColor;
  55. }
  56. return {
  57. fill: timeRegion.fill ? getColorFromHexRgbOrName(colorMode.color.fill, theme) : null,
  58. line: timeRegion.fill ? getColorFromHexRgbOrName(colorMode.color.line, theme) : null,
  59. };
  60. }
  61. export class TimeRegionManager {
  62. plot: any;
  63. timeRegions: any;
  64. constructor(private panelCtrl, private theme: GrafanaTheme = GrafanaTheme.Dark) {}
  65. draw(plot) {
  66. this.timeRegions = this.panelCtrl.panel.timeRegions;
  67. this.plot = plot;
  68. }
  69. addFlotOptions(options, panel) {
  70. if (!panel.timeRegions || panel.timeRegions.length === 0) {
  71. return;
  72. }
  73. const tRange = { from: moment(this.panelCtrl.range.from).utc(), to: moment(this.panelCtrl.range.to).utc() };
  74. let i, hRange, timeRegion, regions, fromStart, fromEnd, timeRegionColor: TimeRegionColorDefinition;
  75. const timeRegionsCopy = panel.timeRegions.map(a => ({ ...a }));
  76. for (i = 0; i < timeRegionsCopy.length; i++) {
  77. timeRegion = timeRegionsCopy[i];
  78. if (!(timeRegion.fromDayOfWeek || timeRegion.from) && !(timeRegion.toDayOfWeek || timeRegion.to)) {
  79. continue;
  80. }
  81. if (timeRegion.from && !timeRegion.to) {
  82. timeRegion.to = timeRegion.from;
  83. }
  84. if (!timeRegion.from && timeRegion.to) {
  85. timeRegion.from = timeRegion.to;
  86. }
  87. hRange = {
  88. from: this.parseTimeRange(timeRegion.from),
  89. to: this.parseTimeRange(timeRegion.to),
  90. };
  91. if (!timeRegion.fromDayOfWeek && timeRegion.toDayOfWeek) {
  92. timeRegion.fromDayOfWeek = timeRegion.toDayOfWeek;
  93. }
  94. if (!timeRegion.toDayOfWeek && timeRegion.fromDayOfWeek) {
  95. timeRegion.toDayOfWeek = timeRegion.fromDayOfWeek;
  96. }
  97. if (timeRegion.fromDayOfWeek) {
  98. hRange.from.dayOfWeek = Number(timeRegion.fromDayOfWeek);
  99. }
  100. if (timeRegion.toDayOfWeek) {
  101. hRange.to.dayOfWeek = Number(timeRegion.toDayOfWeek);
  102. }
  103. if (hRange.from.dayOfWeek && hRange.from.h === null && hRange.from.m === null) {
  104. hRange.from.h = 0;
  105. hRange.from.m = 0;
  106. hRange.from.s = 0;
  107. }
  108. if (hRange.to.dayOfWeek && hRange.to.h === null && hRange.to.m === null) {
  109. hRange.to.h = 23;
  110. hRange.to.m = 59;
  111. hRange.to.s = 59;
  112. }
  113. if (!hRange.from || !hRange.to) {
  114. continue;
  115. }
  116. regions = [];
  117. if (
  118. hRange.from.h >= tRange.from.hour() &&
  119. hRange.from.h <= tRange.from.hour() &&
  120. hRange.from.m >= tRange.from.minute() &&
  121. hRange.from.m <= tRange.from.minute() &&
  122. hRange.to.h >= tRange.to.hour() &&
  123. hRange.to.h <= tRange.to.hour() &&
  124. hRange.to.m >= tRange.to.minute() &&
  125. hRange.to.m <= tRange.to.minute()
  126. ) {
  127. regions.push({ from: tRange.from.valueOf(), to: tRange.to.startOf('hour').valueOf() });
  128. } else {
  129. fromStart = moment(tRange.from);
  130. fromStart.set('hour', 0);
  131. fromStart.set('minute', 0);
  132. fromStart.set('second', 0);
  133. fromStart.add(hRange.from.h, 'hours');
  134. fromStart.add(hRange.from.m, 'minutes');
  135. fromStart.add(hRange.from.s, 'seconds');
  136. while (fromStart.unix() <= tRange.to.unix()) {
  137. while (hRange.from.dayOfWeek && hRange.from.dayOfWeek !== fromStart.isoWeekday()) {
  138. fromStart.add(24, 'hours');
  139. }
  140. if (fromStart.unix() > tRange.to.unix()) {
  141. break;
  142. }
  143. fromEnd = moment(fromStart);
  144. if (hRange.from.h <= hRange.to.h) {
  145. fromEnd.add(hRange.to.h - hRange.from.h, 'hours');
  146. } else if (hRange.from.h + hRange.to.h < 23) {
  147. fromEnd.add(hRange.to.h, 'hours');
  148. while (fromEnd.hour() !== hRange.to.h) {
  149. fromEnd.add(-1, 'hours');
  150. }
  151. } else {
  152. fromEnd.add(24 - hRange.from.h, 'hours');
  153. while (fromEnd.hour() !== hRange.to.h) {
  154. fromEnd.add(1, 'hours');
  155. }
  156. }
  157. fromEnd.set('minute', hRange.to.m);
  158. fromEnd.set('second', hRange.to.s);
  159. while (hRange.to.dayOfWeek && hRange.to.dayOfWeek !== fromEnd.isoWeekday()) {
  160. fromEnd.add(24, 'hours');
  161. }
  162. const outsideRange =
  163. (fromStart.unix() < tRange.from.unix() && fromEnd.unix() < tRange.from.unix()) ||
  164. (fromStart.unix() > tRange.to.unix() && fromEnd.unix() > tRange.to.unix());
  165. if (!outsideRange) {
  166. regions.push({ from: fromStart.valueOf(), to: fromEnd.valueOf() });
  167. }
  168. fromStart.add(24, 'hours');
  169. }
  170. }
  171. timeRegionColor = getColor(timeRegion, this.theme);
  172. for (let j = 0; j < regions.length; j++) {
  173. const r = regions[j];
  174. if (timeRegion.fill) {
  175. options.grid.markings.push({
  176. xaxis: { from: r.from, to: r.to },
  177. color: timeRegionColor.fill,
  178. });
  179. }
  180. if (timeRegion.line) {
  181. options.grid.markings.push({
  182. xaxis: { from: r.from, to: r.from },
  183. color: timeRegionColor.line,
  184. });
  185. options.grid.markings.push({
  186. xaxis: { from: r.to, to: r.to },
  187. color: timeRegionColor.line,
  188. });
  189. }
  190. }
  191. }
  192. }
  193. parseTimeRange(str) {
  194. const timeRegex = /^([\d]+):?(\d{2})?/;
  195. const result = { h: null, m: null };
  196. const match = timeRegex.exec(str);
  197. if (!match) {
  198. return result;
  199. }
  200. if (match.length > 1) {
  201. result.h = Number(match[1]);
  202. result.m = 0;
  203. if (match.length > 2 && match[2] !== undefined) {
  204. result.m = Number(match[2]);
  205. }
  206. if (result.h > 23) {
  207. result.h = 23;
  208. }
  209. if (result.m > 59) {
  210. result.m = 59;
  211. }
  212. }
  213. return result;
  214. }
  215. }