utils.tsx 3.4 KB

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