utils.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, SelectValue} 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. const {query, project, environment, cursor} = location.query;
  10. return [
  11. `/organizations/${organization.slug}/monitors/`,
  12. {query: {cursor, query, project, environment, includeNew: true, per_page: 20}},
  13. ] as const;
  14. }
  15. export function crontabAsText(crontabInput: string | null): string | null {
  16. if (!crontabInput) {
  17. return null;
  18. }
  19. let parsedSchedule: string;
  20. try {
  21. parsedSchedule = cronstrue.toString(crontabInput, {
  22. verbose: false,
  23. use24HourTimeFormat: shouldUse24Hours(),
  24. });
  25. } catch (_e) {
  26. return null;
  27. }
  28. return parsedSchedule;
  29. }
  30. export function scheduleAsText(config: MonitorConfig) {
  31. // Crontab format uses cronstrue
  32. if (config.schedule_type === ScheduleType.CRONTAB) {
  33. const parsedSchedule = crontabAsText(config.schedule);
  34. return parsedSchedule ?? t('Unknown schedule');
  35. }
  36. // Interval format is simpler
  37. const [value, timeUnit] = config.schedule;
  38. if (timeUnit === 'minute') {
  39. return tn('Every minute', 'Every %s minutes', value);
  40. }
  41. if (timeUnit === 'hour') {
  42. return tn('Every hour', 'Every %s hours', value);
  43. }
  44. if (timeUnit === 'day') {
  45. return tn('Every day', 'Every %s days', value);
  46. }
  47. if (timeUnit === 'week') {
  48. return tn('Every week', 'Every %s weeks', value);
  49. }
  50. if (timeUnit === 'month') {
  51. return tn('Every month', 'Every %s months', value);
  52. }
  53. return t('Unknown schedule');
  54. }
  55. export const statusToText: Record<CheckInStatus, string> = {
  56. [CheckInStatus.OK]: t('Okay'),
  57. [CheckInStatus.ERROR]: t('Failed'),
  58. [CheckInStatus.IN_PROGRESS]: t('In Progress'),
  59. [CheckInStatus.MISSED]: t('Missed'),
  60. [CheckInStatus.TIMEOUT]: t('Timed Out'),
  61. };
  62. export function getColorsFromStatus(status: CheckInStatus, theme: Theme) {
  63. const statusToColor: Record<CheckInStatus, {labelColor: string; tickColor: string}> = {
  64. [CheckInStatus.ERROR]: {tickColor: theme.red300, labelColor: theme.red400},
  65. [CheckInStatus.TIMEOUT]: {tickColor: theme.red300, labelColor: theme.red400},
  66. [CheckInStatus.OK]: {tickColor: theme.green300, labelColor: theme.green400},
  67. [CheckInStatus.MISSED]: {tickColor: theme.yellow300, labelColor: theme.yellow400},
  68. [CheckInStatus.IN_PROGRESS]: {tickColor: theme.disabled, labelColor: theme.disabled},
  69. };
  70. return statusToColor[status];
  71. }
  72. export const getScheduleIntervals = (n: number): SelectValue<string>[] => [
  73. {value: 'minute', label: tn('minute', 'minutes', n)},
  74. {value: 'hour', label: tn('hour', 'hours', n)},
  75. {value: 'day', label: tn('day', 'days', n)},
  76. {value: 'week', label: tn('week', 'weeks', n)},
  77. {value: 'month', label: tn('month', 'months', n)},
  78. {value: 'year', label: tn('year', 'years', n)},
  79. ];