alertChart.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import styled from '@emotion/styled';
  2. import {AreaChart} from 'sentry/components/charts/areaChart';
  3. import ChartZoom from 'sentry/components/charts/chartZoom';
  4. import {HeaderTitleLegend, SectionHeading} from 'sentry/components/charts/styles';
  5. import type {DateTimeObject} from 'sentry/components/charts/utils';
  6. import Panel from 'sentry/components/panels/panel';
  7. import PanelBody from 'sentry/components/panels/panelBody';
  8. import PanelFooter from 'sentry/components/panels/panelFooter';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {IssueAlertRule, ProjectAlertRuleStats} from 'sentry/types/alerts';
  13. import type {Project} from 'sentry/types/project';
  14. import getDynamicText from 'sentry/utils/getDynamicText';
  15. import {useApiQuery} from 'sentry/utils/queryClient';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import useRouter from 'sentry/utils/useRouter';
  18. import RouteError from 'sentry/views/routeError';
  19. interface IssueAlertDetailsProps extends DateTimeObject {
  20. project: Project;
  21. rule: IssueAlertRule;
  22. }
  23. export function IssueAlertDetailsChart({
  24. project,
  25. period,
  26. start,
  27. end,
  28. utc,
  29. rule,
  30. }: IssueAlertDetailsProps) {
  31. const organization = useOrganization();
  32. const router = useRouter();
  33. const {
  34. data: ruleFireHistory,
  35. isLoading,
  36. isError,
  37. error,
  38. } = useApiQuery<ProjectAlertRuleStats[]>(
  39. [
  40. `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/stats/`,
  41. {
  42. query: {
  43. ...(period && {statsPeriod: period}),
  44. start,
  45. end,
  46. utc,
  47. },
  48. },
  49. ],
  50. {staleTime: 30000}
  51. );
  52. const totalAlertsTriggered =
  53. ruleFireHistory?.reduce((acc, curr) => acc + curr.count, 0) ?? 0;
  54. if (isError) {
  55. return <RouteError error={error} />;
  56. }
  57. return (
  58. <Panel>
  59. <StyledPanelBody withPadding>
  60. <ChartHeader>
  61. <HeaderTitleLegend>{t('Alerts Triggered')}</HeaderTitleLegend>
  62. </ChartHeader>
  63. {getDynamicText({
  64. value: isLoading ? (
  65. <Placeholder height="200px" />
  66. ) : (
  67. <ChartZoom
  68. router={router}
  69. period={period}
  70. start={start}
  71. end={end}
  72. utc={utc}
  73. usePageDate
  74. >
  75. {zoomRenderProps => (
  76. <AreaChart
  77. {...zoomRenderProps}
  78. isGroupedByDate
  79. showTimeInTooltip
  80. grid={{
  81. left: space(0.25),
  82. right: space(2),
  83. top: space(3),
  84. bottom: 0,
  85. }}
  86. yAxis={{
  87. minInterval: 1,
  88. }}
  89. series={[
  90. {
  91. seriesName: 'Alerts Triggered',
  92. data:
  93. ruleFireHistory?.map(alert => ({
  94. name: alert.date,
  95. value: alert.count,
  96. })) ?? [],
  97. emphasis: {
  98. disabled: true,
  99. },
  100. },
  101. ]}
  102. />
  103. )}
  104. </ChartZoom>
  105. ),
  106. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  107. })}
  108. </StyledPanelBody>
  109. <ChartFooter>
  110. <FooterHeader>{t('Total Alerts')}</FooterHeader>
  111. <FooterValue>
  112. {isLoading ? (
  113. <Placeholder height="16px" width="50px" />
  114. ) : (
  115. totalAlertsTriggered.toLocaleString()
  116. )}
  117. </FooterValue>
  118. </ChartFooter>
  119. </Panel>
  120. );
  121. }
  122. const ChartHeader = styled('div')`
  123. margin-bottom: ${space(3)};
  124. `;
  125. const ChartFooter = styled(PanelFooter)`
  126. display: flex;
  127. align-items: center;
  128. padding: ${space(1)} 20px;
  129. `;
  130. const FooterHeader = styled(SectionHeading)`
  131. display: flex;
  132. align-items: center;
  133. margin: 0;
  134. font-weight: bold;
  135. font-size: ${p => p.theme.fontSizeMedium};
  136. line-height: 1;
  137. `;
  138. const FooterValue = styled('div')`
  139. display: flex;
  140. align-items: center;
  141. margin: 0 ${space(1)};
  142. `;
  143. /* Override padding to make chart appear centered */
  144. const StyledPanelBody = styled(PanelBody)`
  145. padding-right: 6px;
  146. `;