utils.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {Theme} from '@emotion/react';
  2. import cronstrue from 'cronstrue';
  3. import {Location} from 'history';
  4. import {t, tn} from 'sentry/locale';
  5. import {Organization} from 'sentry/types';
  6. import {shouldUse24Hours} from 'sentry/utils/dates';
  7. import {CheckInStatus, MonitorConfig, ScheduleType} from 'sentry/views/monitors/types';
  8. export function makeMonitorListQueryKey(organization: Organization, location: Location) {
  9. return [
  10. `/organizations/${organization.slug}/monitors/`,
  11. {query: {...location.query, includeNew: true, per_page: 20}},
  12. ] as const;
  13. }
  14. export function crontabAsText(crontabInput: string | null): string | null {
  15. if (!crontabInput) {
  16. return null;
  17. }
  18. let parsedSchedule: string;
  19. try {
  20. parsedSchedule = cronstrue.toString(crontabInput, {
  21. verbose: false,
  22. use24HourTimeFormat: shouldUse24Hours(),
  23. });
  24. } catch (_e) {
  25. return null;
  26. }
  27. return parsedSchedule;
  28. }
  29. export function scheduleAsText(config: MonitorConfig) {
  30. // Crontab format uses cronstrue
  31. if (config.schedule_type === ScheduleType.CRONTAB) {
  32. const parsedSchedule = crontabAsText(config.schedule);
  33. return parsedSchedule ?? t('Unknown schedule');
  34. }
  35. // Interval format is simpler
  36. const [value, timeUnit] = config.schedule;
  37. if (timeUnit === 'minute') {
  38. return tn('Every minute', 'Every %s minutes', value);
  39. }
  40. if (timeUnit === 'hour') {
  41. return tn('Every hour', 'Every %s hours', value);
  42. }
  43. if (timeUnit === 'day') {
  44. return tn('Every day', 'Every %s days', value);
  45. }
  46. if (timeUnit === 'week') {
  47. return tn('Every week', 'Every %s weeks', value);
  48. }
  49. if (timeUnit === 'month') {
  50. return tn('Every month', 'Every %s months', value);
  51. }
  52. return t('Unknown schedule');
  53. }
  54. export const statusToText: Record<CheckInStatus, string> = {
  55. [CheckInStatus.OK]: t('Okay'),
  56. [CheckInStatus.ERROR]: t('Failed'),
  57. [CheckInStatus.IN_PROGRESS]: t('In Progress'),
  58. [CheckInStatus.MISSED]: t('Missed'),
  59. [CheckInStatus.TIMEOUT]: t('Timed Out'),
  60. };
  61. export function getColorsFromStatus(status: CheckInStatus, theme: Theme) {
  62. const statusToColor: Record<CheckInStatus, {labelColor: string; tickColor: string}> = {
  63. [CheckInStatus.ERROR]: {tickColor: theme.red200, labelColor: theme.red300},
  64. [CheckInStatus.TIMEOUT]: {tickColor: theme.red200, labelColor: theme.red300},
  65. [CheckInStatus.OK]: {tickColor: theme.green200, labelColor: theme.green300},
  66. [CheckInStatus.MISSED]: {tickColor: theme.yellow200, labelColor: theme.yellow300},
  67. [CheckInStatus.IN_PROGRESS]: {tickColor: theme.disabled, labelColor: theme.disabled},
  68. };
  69. return statusToColor[status];
  70. }