utils.tsx 3.6 KB

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