utils.tsx 1.6 KB

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