time_region_manager.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import 'vendor/flot/jquery.flot';
  2. import _ from 'lodash';
  3. import { GrafanaThemeType, getColorFromHexRgbOrName } from '@grafana/ui';
  4. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  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 = {
  74. from: dateTime(this.panelCtrl.range.from).utc(),
  75. to: dateTime(this.panelCtrl.range.to).utc(),
  76. };
  77. let i, hRange, timeRegion, regions, fromStart, fromEnd, timeRegionColor: TimeRegionColorDefinition;
  78. const timeRegionsCopy = panel.timeRegions.map(a => ({ ...a }));
  79. for (i = 0; i < timeRegionsCopy.length; i++) {
  80. timeRegion = timeRegionsCopy[i];
  81. if (!(timeRegion.fromDayOfWeek || timeRegion.from) && !(timeRegion.toDayOfWeek || timeRegion.to)) {
  82. continue;
  83. }
  84. if (timeRegion.from && !timeRegion.to) {
  85. timeRegion.to = timeRegion.from;
  86. }
  87. if (!timeRegion.from && timeRegion.to) {
  88. timeRegion.from = timeRegion.to;
  89. }
  90. hRange = {
  91. from: this.parseTimeRange(timeRegion.from),
  92. to: this.parseTimeRange(timeRegion.to),
  93. };
  94. if (!timeRegion.fromDayOfWeek && timeRegion.toDayOfWeek) {
  95. timeRegion.fromDayOfWeek = timeRegion.toDayOfWeek;
  96. }
  97. if (!timeRegion.toDayOfWeek && timeRegion.fromDayOfWeek) {
  98. timeRegion.toDayOfWeek = timeRegion.fromDayOfWeek;
  99. }
  100. if (timeRegion.fromDayOfWeek) {
  101. hRange.from.dayOfWeek = Number(timeRegion.fromDayOfWeek);
  102. }
  103. if (timeRegion.toDayOfWeek) {
  104. hRange.to.dayOfWeek = Number(timeRegion.toDayOfWeek);
  105. }
  106. if (hRange.from.dayOfWeek && hRange.from.h === null && hRange.from.m === null) {
  107. hRange.from.h = 0;
  108. hRange.from.m = 0;
  109. hRange.from.s = 0;
  110. }
  111. if (hRange.to.dayOfWeek && hRange.to.h === null && hRange.to.m === null) {
  112. hRange.to.h = 23;
  113. hRange.to.m = 59;
  114. hRange.to.s = 59;
  115. }
  116. if (!hRange.from || !hRange.to) {
  117. continue;
  118. }
  119. regions = [];
  120. fromStart = dateTime(tRange.from);
  121. fromStart.set('hour', 0);
  122. fromStart.set('minute', 0);
  123. fromStart.set('second', 0);
  124. fromStart.add(hRange.from.h, 'hours');
  125. fromStart.add(hRange.from.m, 'minutes');
  126. fromStart.add(hRange.from.s, 'seconds');
  127. while (fromStart.unix() <= tRange.to.unix()) {
  128. while (hRange.from.dayOfWeek && hRange.from.dayOfWeek !== fromStart.isoWeekday()) {
  129. fromStart.add(24, 'hours');
  130. }
  131. if (fromStart.unix() > tRange.to.unix()) {
  132. break;
  133. }
  134. fromEnd = dateTime(fromStart);
  135. if (hRange.from.h <= hRange.to.h) {
  136. fromEnd.add(hRange.to.h - hRange.from.h, 'hours');
  137. } else if (hRange.from.h > hRange.to.h) {
  138. while (fromEnd.hour() !== hRange.to.h) {
  139. fromEnd.add(1, 'hours');
  140. }
  141. } else {
  142. fromEnd.add(24 - hRange.from.h, 'hours');
  143. while (fromEnd.hour() !== hRange.to.h) {
  144. fromEnd.add(1, 'hours');
  145. }
  146. }
  147. fromEnd.set('minute', hRange.to.m);
  148. fromEnd.set('second', hRange.to.s);
  149. while (hRange.to.dayOfWeek && hRange.to.dayOfWeek !== fromEnd.isoWeekday()) {
  150. fromEnd.add(24, 'hours');
  151. }
  152. const outsideRange =
  153. (fromStart.unix() < tRange.from.unix() && fromEnd.unix() < tRange.from.unix()) ||
  154. (fromStart.unix() > tRange.to.unix() && fromEnd.unix() > tRange.to.unix());
  155. if (!outsideRange) {
  156. regions.push({ from: fromStart.valueOf(), to: fromEnd.valueOf() });
  157. }
  158. fromStart.add(24, 'hours');
  159. }
  160. timeRegionColor = getColor(timeRegion, this.theme);
  161. for (let j = 0; j < regions.length; j++) {
  162. const r = regions[j];
  163. if (timeRegion.fill) {
  164. options.grid.markings.push({
  165. xaxis: { from: r.from, to: r.to },
  166. color: timeRegionColor.fill,
  167. });
  168. }
  169. if (timeRegion.line) {
  170. options.grid.markings.push({
  171. xaxis: { from: r.from, to: r.from },
  172. color: timeRegionColor.line,
  173. });
  174. options.grid.markings.push({
  175. xaxis: { from: r.to, to: r.to },
  176. color: timeRegionColor.line,
  177. });
  178. }
  179. }
  180. }
  181. }
  182. parseTimeRange(str) {
  183. const timeRegex = /^([\d]+):?(\d{2})?/;
  184. const result = { h: null, m: null };
  185. const match = timeRegex.exec(str);
  186. if (!match) {
  187. return result;
  188. }
  189. if (match.length > 1) {
  190. result.h = Number(match[1]);
  191. result.m = 0;
  192. if (match.length > 2 && match[2] !== undefined) {
  193. result.m = Number(match[2]);
  194. }
  195. if (result.h > 23) {
  196. result.h = 23;
  197. }
  198. if (result.m > 59) {
  199. result.m = 59;
  200. }
  201. }
  202. return result;
  203. }
  204. }