monitorStats.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import React, {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 LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelBody from 'sentry/components/panels/panelBody';
  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. orgId: string;
  24. };
  25. function MonitorStats({monitor, monitorEnvs, orgId}: 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/${orgId}/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. if (isLoading) {
  51. return <LoadingIndicator />;
  52. }
  53. let emptyStats = true;
  54. const success: BarChartSeries = {
  55. seriesName: t('Successful'),
  56. data: [],
  57. };
  58. const failed: BarChartSeries = {
  59. seriesName: t('Failed'),
  60. data: [],
  61. };
  62. const missed: BarChartSeries = {
  63. seriesName: t('Missed'),
  64. data: [],
  65. };
  66. const timeout: BarChartSeries = {
  67. seriesName: t('Timeout'),
  68. data: [],
  69. };
  70. const duration: AreaChartSeries = {
  71. seriesName: t('Average Duration'),
  72. data: [],
  73. };
  74. stats?.forEach(p => {
  75. if (p.ok || p.error || p.missed || p.timeout) {
  76. emptyStats = false;
  77. }
  78. const timestamp = p.ts * 1000;
  79. success.data.push({name: timestamp, value: p.ok});
  80. failed.data.push({name: timestamp, value: p.error});
  81. timeout.data.push({name: timestamp, value: p.timeout});
  82. missed.data.push({name: timestamp, value: p.missed});
  83. duration.data.push({name: timestamp, value: Math.trunc(p.duration)});
  84. });
  85. const colors = [theme.green200, theme.red200, theme.red200, theme.yellow200];
  86. const height = 150;
  87. const getYAxisOptions = (aggregateType: AggregationOutputType) => ({
  88. max: getYAxisMaxFn(height),
  89. splitLine: {
  90. show: false,
  91. },
  92. axisLabel: {
  93. formatter: (value: number) => axisLabelFormatter(value, aggregateType, true),
  94. showMaxLabel: false,
  95. },
  96. });
  97. if (emptyStats) {
  98. return (
  99. <Panel>
  100. <PanelBody withPadding>
  101. <EmptyMessage
  102. title={t('No check-ins have been recorded for this time period.')}
  103. />
  104. </PanelBody>
  105. </Panel>
  106. );
  107. }
  108. return (
  109. <React.Fragment>
  110. <Panel>
  111. <PanelBody withPadding>
  112. <StyledHeaderTitle>{t('Check-Ins')}</StyledHeaderTitle>
  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. </PanelBody>
  134. </Panel>
  135. <Panel>
  136. <PanelBody withPadding>
  137. <StyledHeaderTitle>{t('Average Duration')}</StyledHeaderTitle>
  138. <AreaChart
  139. isGroupedByDate
  140. showTimeInTooltip
  141. useShortDate
  142. series={[duration]}
  143. height={height}
  144. colors={[theme.charts.colors[0]]}
  145. yAxis={getYAxisOptions('duration')}
  146. grid={{
  147. top: 6,
  148. bottom: 0,
  149. left: 0,
  150. right: 0,
  151. }}
  152. tooltip={{
  153. valueFormatter: value => tooltipFormatter(value, 'duration'),
  154. }}
  155. />
  156. </PanelBody>
  157. </Panel>
  158. </React.Fragment>
  159. );
  160. }
  161. const StyledHeaderTitle = styled(HeaderTitle)`
  162. margin-bottom: ${space(1)};
  163. `;
  164. export default MonitorStats;