utils.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import cronstrue from 'cronstrue';
  2. import {t, tn} from 'sentry/locale';
  3. import {shouldUse24Hours} from 'sentry/utils/dates';
  4. import {MonitorConfig, ScheduleType} from 'sentry/views/monitors/types';
  5. export function crontabAsText(crontabInput: string | null): string | null {
  6. if (!crontabInput) {
  7. return null;
  8. }
  9. let parsedSchedule: string;
  10. try {
  11. parsedSchedule = cronstrue.toString(crontabInput, {
  12. verbose: true,
  13. use24HourTimeFormat: shouldUse24Hours(),
  14. });
  15. } catch (_e) {
  16. return null;
  17. }
  18. return parsedSchedule;
  19. }
  20. export function scheduleAsText(config: MonitorConfig) {
  21. // Crontab format uses cronstrue
  22. if (config.schedule_type === ScheduleType.CRONTAB) {
  23. const parsedSchedule = crontabAsText(config.schedule);
  24. return parsedSchedule ?? t('Unknown schedule');
  25. }
  26. // Interval format is simpler
  27. const [value, timeUnit] = config.schedule;
  28. if (timeUnit === 'minute') {
  29. return tn('Every minute', 'Every %s minutes', value);
  30. }
  31. if (timeUnit === 'hour') {
  32. return tn('Every hour', 'Every %s hours', value);
  33. }
  34. if (timeUnit === 'day') {
  35. return tn('Every day', 'Every %s days', value);
  36. }
  37. if (timeUnit === 'week') {
  38. return tn('Every week', 'Every %s weeks', value);
  39. }
  40. if (timeUnit === 'month') {
  41. return tn('Every month', 'Every %s months', value);
  42. }
  43. return t('Unknown schedule');
  44. }