alertChart.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 RouteError from 'sentry/views/routeError';
  18. interface IssueAlertDetailsProps extends DateTimeObject {
  19. project: Project;
  20. rule: IssueAlertRule;
  21. }
  22. export function IssueAlertDetailsChart({
  23. project,
  24. period,
  25. start,
  26. end,
  27. utc,
  28. rule,
  29. }: IssueAlertDetailsProps) {
  30. const organization = useOrganization();
  31. const {
  32. data: ruleFireHistory,
  33. isPending,
  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: isPending ? (
  63. <Placeholder height="200px" />
  64. ) : (
  65. <ChartZoom period={period} start={start} end={end} utc={utc} usePageDate>
  66. {zoomRenderProps => (
  67. <AreaChart
  68. {...zoomRenderProps}
  69. isGroupedByDate
  70. showTimeInTooltip
  71. grid={{
  72. left: space(0.25),
  73. right: space(2),
  74. top: space(3),
  75. bottom: 0,
  76. }}
  77. yAxis={{
  78. minInterval: 1,
  79. }}
  80. series={[
  81. {
  82. seriesName: 'Alerts Triggered',
  83. data:
  84. ruleFireHistory?.map(alert => ({
  85. name: alert.date,
  86. value: alert.count,
  87. })) ?? [],
  88. emphasis: {
  89. disabled: true,
  90. },
  91. },
  92. ]}
  93. />
  94. )}
  95. </ChartZoom>
  96. ),
  97. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  98. })}
  99. </StyledPanelBody>
  100. <ChartFooter>
  101. <FooterHeader>{t('Total Alerts')}</FooterHeader>
  102. <FooterValue>
  103. {isPending ? (
  104. <Placeholder height="16px" width="50px" />
  105. ) : (
  106. totalAlertsTriggered.toLocaleString()
  107. )}
  108. </FooterValue>
  109. </ChartFooter>
  110. </Panel>
  111. );
  112. }
  113. const ChartHeader = styled('div')`
  114. margin-bottom: ${space(3)};
  115. `;
  116. const ChartFooter = styled(PanelFooter)`
  117. display: flex;
  118. align-items: center;
  119. padding: ${space(1)} 20px;
  120. `;
  121. const FooterHeader = styled(SectionHeading)`
  122. display: flex;
  123. align-items: center;
  124. margin: 0;
  125. font-weight: ${p => p.theme.fontWeightBold};
  126. font-size: ${p => p.theme.fontSizeMedium};
  127. line-height: 1;
  128. `;
  129. const FooterValue = styled('div')`
  130. display: flex;
  131. align-items: center;
  132. margin: 0 ${space(1)};
  133. `;
  134. /* Override padding to make chart appear centered */
  135. const StyledPanelBody = styled(PanelBody)`
  136. padding-right: 6px;
  137. `;