monitorStats.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {Fragment, useRef} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import type {AreaChartSeries} from 'sentry/components/charts/areaChart';
  5. import {AreaChart} from 'sentry/components/charts/areaChart';
  6. import type {BarChartSeries} from 'sentry/components/charts/barChart';
  7. import {BarChart} from 'sentry/components/charts/barChart';
  8. import {getYAxisMaxFn} from 'sentry/components/charts/miniBarChart';
  9. import {HeaderTitle} from 'sentry/components/charts/styles';
  10. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import Placeholder from 'sentry/components/placeholder';
  14. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
  18. import type {AggregationOutputType} from 'sentry/utils/discover/fields';
  19. import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
  20. import {useApiQuery} from 'sentry/utils/queryClient';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import usePageFilters from 'sentry/utils/usePageFilters';
  23. import type {Monitor, MonitorEnvironment, MonitorStat} from '../types';
  24. type Props = {
  25. monitor: Monitor;
  26. monitorEnvs: MonitorEnvironment[];
  27. };
  28. export function MonitorStats({monitor, monitorEnvs}: Props) {
  29. const theme = useTheme();
  30. const organization = useOrganization();
  31. const {selection} = usePageFilters();
  32. const {start, end, period} = selection.datetime;
  33. const nowRef = useRef(new Date());
  34. let since: number, until: number;
  35. if (start && end) {
  36. until = new Date(end).getTime() / 1000;
  37. since = new Date(start).getTime() / 1000;
  38. } else {
  39. until = Math.floor(nowRef.current.getTime() / 1000);
  40. const intervalSeconds = intervalToMilliseconds(period ?? '30d') / 1000;
  41. since = until - intervalSeconds;
  42. }
  43. const queryKey = [
  44. `/projects/${organization.slug}/${monitor.project.slug}/monitors/${monitor.slug}/stats/`,
  45. {
  46. query: {
  47. since: since.toString(),
  48. until: until.toString(),
  49. resolution: '1d',
  50. environment: monitorEnvs.map(e => e.name),
  51. },
  52. },
  53. ] as const;
  54. const {data: stats, isPending} = useApiQuery<MonitorStat[]>(queryKey, {staleTime: 0});
  55. let emptyStats = true;
  56. const success: BarChartSeries = {
  57. seriesName: t('Successful'),
  58. data: [],
  59. };
  60. const failed: BarChartSeries = {
  61. seriesName: t('Failed'),
  62. data: [],
  63. };
  64. const missed: BarChartSeries = {
  65. seriesName: t('Missed'),
  66. data: [],
  67. };
  68. const timeout: BarChartSeries = {
  69. seriesName: t('Timeout'),
  70. data: [],
  71. };
  72. const duration: AreaChartSeries = {
  73. seriesName: t('Average Duration'),
  74. data: [],
  75. };
  76. stats?.forEach(p => {
  77. if (p.ok || p.error || p.missed || p.timeout) {
  78. emptyStats = false;
  79. }
  80. const timestamp = p.ts * 1000;
  81. success.data.push({name: timestamp, value: p.ok});
  82. failed.data.push({name: timestamp, value: p.error});
  83. timeout.data.push({name: timestamp, value: p.timeout});
  84. missed.data.push({name: timestamp, value: p.missed});
  85. duration.data.push({name: timestamp, value: Math.trunc(p.duration)});
  86. });
  87. const colors = [theme.green200, theme.red200, theme.red200, theme.yellow200];
  88. const height = 150;
  89. const getYAxisOptions = (aggregateType: AggregationOutputType) => ({
  90. max: getYAxisMaxFn(height),
  91. splitLine: {
  92. show: false,
  93. },
  94. axisLabel: {
  95. formatter: (value: number) => axisLabelFormatter(value, aggregateType, true),
  96. showMaxLabel: false,
  97. },
  98. });
  99. if (!isPending && emptyStats) {
  100. return (
  101. <Panel>
  102. <EmptyStateWarning withIcon={false}>
  103. {t('No check-ins have been recorded for this time period.')}
  104. </EmptyStateWarning>
  105. </Panel>
  106. );
  107. }
  108. return (
  109. <Fragment>
  110. <Panel>
  111. <PanelBody withPadding>
  112. <StyledHeaderTitle>{t('Status')}</StyledHeaderTitle>
  113. {isPending ? (
  114. <Placeholder height={`${height}px`} />
  115. ) : (
  116. <BarChart
  117. isGroupedByDate
  118. showTimeInTooltip
  119. useShortDate
  120. series={[success, failed, timeout, missed]}
  121. stacked
  122. height={height}
  123. colors={colors}
  124. tooltip={{
  125. trigger: 'axis',
  126. }}
  127. yAxis={getYAxisOptions('number')}
  128. grid={{
  129. top: 6,
  130. bottom: 0,
  131. left: 0,
  132. right: 0,
  133. }}
  134. animation={false}
  135. />
  136. )}
  137. </PanelBody>
  138. </Panel>
  139. <Panel>
  140. <PanelBody withPadding>
  141. <StyledHeaderTitle>{t('Average Duration')}</StyledHeaderTitle>
  142. {isPending ? (
  143. <Placeholder height={`${height}px`} />
  144. ) : (
  145. <AreaChart
  146. isGroupedByDate
  147. showTimeInTooltip
  148. useShortDate
  149. series={[duration]}
  150. height={height}
  151. colors={[CHART_PALETTE[CHART_PALETTE.length - 1]![0]]}
  152. yAxis={getYAxisOptions('duration')}
  153. grid={{
  154. top: 6,
  155. bottom: 0,
  156. left: 0,
  157. right: 0,
  158. }}
  159. tooltip={{
  160. valueFormatter: value => tooltipFormatter(value, 'duration'),
  161. }}
  162. />
  163. )}
  164. </PanelBody>
  165. </Panel>
  166. </Fragment>
  167. );
  168. }
  169. const StyledHeaderTitle = styled(HeaderTitle)`
  170. margin-bottom: ${space(1)};
  171. `;
  172. export default MonitorStats;