monitorStats.tsx 5.0 KB

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