utils.tsx 3.5 KB

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