alertChart.tsx 4.1 KB

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