monitorStats.tsx 5.3 KB

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