utils.tsx 3.5 KB

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