mute-timings.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { omitBy, isUndefined } from 'lodash';
  2. import { MuteTimeInterval, TimeInterval } from 'app/plugins/datasource/alertmanager/types';
  3. import { MuteTimingFields, MuteTimingIntervalFields } from '../types/mute-timing-form';
  4. export const DAYS_OF_THE_WEEK = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
  5. export const MONTHS = [
  6. 'january',
  7. 'february',
  8. 'march',
  9. 'april',
  10. 'may',
  11. 'june',
  12. 'july',
  13. 'august',
  14. 'september',
  15. 'october',
  16. 'november',
  17. 'december',
  18. ];
  19. export const defaultTimeInterval: MuteTimingIntervalFields = {
  20. times: [{ start_time: '', end_time: '' }],
  21. weekdays: '',
  22. days_of_month: '',
  23. months: '',
  24. years: '',
  25. };
  26. export const validateArrayField = (value: string, validateValue: (input: string) => boolean, invalidText: string) => {
  27. if (value) {
  28. return (
  29. value
  30. .split(',')
  31. .map((x) => x.trim())
  32. .every((entry) => entry.split(':').every(validateValue)) || invalidText
  33. );
  34. } else {
  35. return true;
  36. }
  37. };
  38. const convertStringToArray = (str: string) => {
  39. return str ? str.split(',').map((s) => s.trim()) : undefined;
  40. };
  41. export const createMuteTiming = (fields: MuteTimingFields): MuteTimeInterval => {
  42. const timeIntervals: TimeInterval[] = fields.time_intervals.map(
  43. ({ times, weekdays, days_of_month, months, years }) => {
  44. const interval = {
  45. times: times.filter(({ start_time, end_time }) => !!start_time && !!end_time),
  46. weekdays: convertStringToArray(weekdays)?.map((v) => v.toLowerCase()),
  47. days_of_month: convertStringToArray(days_of_month),
  48. months: convertStringToArray(months),
  49. years: convertStringToArray(years),
  50. };
  51. return omitBy(interval, isUndefined);
  52. }
  53. );
  54. return {
  55. name: fields.name,
  56. time_intervals: timeIntervals,
  57. };
  58. };