time_region_manager.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import 'vendor/flot/jquery.flot';
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import { GrafanaThemeType, 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: GrafanaThemeType): 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 === GrafanaThemeType.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: GrafanaThemeType = GrafanaThemeType.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. fromStart = moment(tRange.from);
  118. fromStart.set('hour', 0);
  119. fromStart.set('minute', 0);
  120. fromStart.set('second', 0);
  121. fromStart.add(hRange.from.h, 'hours');
  122. fromStart.add(hRange.from.m, 'minutes');
  123. fromStart.add(hRange.from.s, 'seconds');
  124. while (fromStart.unix() <= tRange.to.unix()) {
  125. while (hRange.from.dayOfWeek && hRange.from.dayOfWeek !== fromStart.isoWeekday()) {
  126. fromStart.add(24, 'hours');
  127. }
  128. if (fromStart.unix() > tRange.to.unix()) {
  129. break;
  130. }
  131. fromEnd = moment(fromStart);
  132. if (hRange.from.h <= hRange.to.h) {
  133. fromEnd.add(hRange.to.h - hRange.from.h, 'hours');
  134. } else if (hRange.from.h > hRange.to.h) {
  135. while (fromEnd.hour() !== hRange.to.h) {
  136. fromEnd.add(1, 'hours');
  137. }
  138. } else {
  139. fromEnd.add(24 - hRange.from.h, 'hours');
  140. while (fromEnd.hour() !== hRange.to.h) {
  141. fromEnd.add(1, 'hours');
  142. }
  143. }
  144. fromEnd.set('minute', hRange.to.m);
  145. fromEnd.set('second', hRange.to.s);
  146. while (hRange.to.dayOfWeek && hRange.to.dayOfWeek !== fromEnd.isoWeekday()) {
  147. fromEnd.add(24, 'hours');
  148. }
  149. const outsideRange =
  150. (fromStart.unix() < tRange.from.unix() && fromEnd.unix() < tRange.from.unix()) ||
  151. (fromStart.unix() > tRange.to.unix() && fromEnd.unix() > tRange.to.unix());
  152. if (!outsideRange) {
  153. regions.push({ from: fromStart.valueOf(), to: fromEnd.valueOf() });
  154. }
  155. fromStart.add(24, 'hours');
  156. }
  157. timeRegionColor = getColor(timeRegion, this.theme);
  158. for (let j = 0; j < regions.length; j++) {
  159. const r = regions[j];
  160. if (timeRegion.fill) {
  161. options.grid.markings.push({
  162. xaxis: { from: r.from, to: r.to },
  163. color: timeRegionColor.fill,
  164. });
  165. }
  166. if (timeRegion.line) {
  167. options.grid.markings.push({
  168. xaxis: { from: r.from, to: r.from },
  169. color: timeRegionColor.line,
  170. });
  171. options.grid.markings.push({
  172. xaxis: { from: r.to, to: r.to },
  173. color: timeRegionColor.line,
  174. });
  175. }
  176. }
  177. }
  178. }
  179. parseTimeRange(str) {
  180. const timeRegex = /^([\d]+):?(\d{2})?/;
  181. const result = { h: null, m: null };
  182. const match = timeRegex.exec(str);
  183. if (!match) {
  184. return result;
  185. }
  186. if (match.length > 1) {
  187. result.h = Number(match[1]);
  188. result.m = 0;
  189. if (match.length > 2 && match[2] !== undefined) {
  190. result.m = Number(match[2]);
  191. }
  192. if (result.h > 23) {
  193. result.h = 23;
  194. }
  195. if (result.m > 59) {
  196. result.m = 59;
  197. }
  198. }
  199. return result;
  200. }
  201. }