utils.tsx 3.6 KB

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