utils.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import cronstrue from 'cronstrue';
  2. import {t, tn} from 'sentry/locale';
  3. import type {Organization, SelectValue} from 'sentry/types';
  4. import {shouldUse24Hours} from 'sentry/utils/dates';
  5. import type {ColorOrAlias} from 'sentry/utils/theme';
  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. interface TickStyle {
  93. /**
  94. * The color of the tooltip label
  95. */
  96. labelColor: ColorOrAlias;
  97. /**
  98. * The color of the tick
  99. */
  100. tickColor: ColorOrAlias;
  101. /**
  102. * Use a cross hatch fill for the tick instead of a solid color. The tick
  103. * color will be used as the border color
  104. */
  105. hatchTick?: ColorOrAlias;
  106. }
  107. export const tickStyle: Record<CheckInStatus, TickStyle> = {
  108. [CheckInStatus.ERROR]: {
  109. labelColor: 'red400',
  110. tickColor: 'red300',
  111. },
  112. [CheckInStatus.TIMEOUT]: {
  113. labelColor: 'red400',
  114. tickColor: 'red300',
  115. hatchTick: 'red200',
  116. },
  117. [CheckInStatus.OK]: {
  118. labelColor: 'green400',
  119. tickColor: 'green300',
  120. },
  121. [CheckInStatus.MISSED]: {
  122. labelColor: 'yellow400',
  123. tickColor: 'yellow300',
  124. },
  125. [CheckInStatus.IN_PROGRESS]: {
  126. labelColor: 'disabled',
  127. tickColor: 'disabled',
  128. },
  129. };
  130. export const getScheduleIntervals = (n: number): SelectValue<string>[] => [
  131. {value: 'minute', label: tn('minute', 'minutes', n)},
  132. {value: 'hour', label: tn('hour', 'hours', n)},
  133. {value: 'day', label: tn('day', 'days', n)},
  134. {value: 'week', label: tn('week', 'weeks', n)},
  135. {value: 'month', label: tn('month', 'months', n)},
  136. {value: 'year', label: tn('year', 'years', n)},
  137. ];