alertChart.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // eslint-disable-next-line no-restricted-imports
  2. import {withRouter, WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import AsyncComponent from 'sentry/components/asyncComponent';
  5. import {AreaChart} from 'sentry/components/charts/areaChart';
  6. import ChartZoom from 'sentry/components/charts/chartZoom';
  7. import {HeaderTitleLegend, SectionHeading} from 'sentry/components/charts/styles';
  8. import type {DateTimeObject} from 'sentry/components/charts/utils';
  9. import {Panel, PanelBody, PanelFooter} from 'sentry/components/panels';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Organization, Project} from 'sentry/types';
  14. import {IssueAlertRule, ProjectAlertRuleStats} from 'sentry/types/alerts';
  15. import getDynamicText from 'sentry/utils/getDynamicText';
  16. type Props = AsyncComponent['props'] &
  17. DateTimeObject &
  18. WithRouterProps & {
  19. orgId: string;
  20. organization: Organization;
  21. project: Project;
  22. rule: IssueAlertRule;
  23. };
  24. type State = AsyncComponent['state'] & {
  25. ruleFireHistory: ProjectAlertRuleStats[];
  26. };
  27. class AlertChart extends AsyncComponent<Props, State> {
  28. componentDidUpdate(prevProps: Props) {
  29. const {project, organization, start, end, period, utc} = this.props;
  30. if (
  31. prevProps.start !== start ||
  32. prevProps.end !== end ||
  33. prevProps.period !== period ||
  34. prevProps.utc !== utc ||
  35. prevProps.organization.id !== organization.id ||
  36. prevProps.project.id !== project.id
  37. ) {
  38. this.remountComponent();
  39. }
  40. }
  41. getDefaultState(): State {
  42. return {
  43. ...super.getDefaultState(),
  44. ruleFireHistory: [],
  45. };
  46. }
  47. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  48. const {project, organization, period, start, end, utc, rule} = this.props;
  49. return [
  50. [
  51. 'ruleFireHistory',
  52. `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/stats/`,
  53. {
  54. query: {
  55. ...(period && {statsPeriod: period}),
  56. start,
  57. end,
  58. utc,
  59. },
  60. },
  61. ],
  62. ];
  63. }
  64. renderChart() {
  65. const {router, period, start, end, utc} = this.props;
  66. const {ruleFireHistory} = this.state;
  67. const series = {
  68. seriesName: 'Alerts Triggered',
  69. data: ruleFireHistory.map(alert => ({
  70. name: alert.date,
  71. value: alert.count,
  72. })),
  73. emphasis: {
  74. disabled: true,
  75. },
  76. };
  77. return (
  78. <ChartZoom
  79. router={router}
  80. period={period}
  81. start={start}
  82. end={end}
  83. utc={utc}
  84. usePageDate
  85. >
  86. {zoomRenderProps => (
  87. <AreaChart
  88. {...zoomRenderProps}
  89. isGroupedByDate
  90. showTimeInTooltip
  91. grid={{
  92. left: space(0.25),
  93. right: space(2),
  94. top: space(3),
  95. bottom: 0,
  96. }}
  97. yAxis={{
  98. minInterval: 1,
  99. }}
  100. series={[series]}
  101. />
  102. )}
  103. </ChartZoom>
  104. );
  105. }
  106. renderEmpty() {
  107. return (
  108. <Panel>
  109. <PanelBody withPadding>
  110. <Placeholder height="200px" />
  111. </PanelBody>
  112. </Panel>
  113. );
  114. }
  115. render() {
  116. const {ruleFireHistory, loading} = this.state;
  117. const totalAlertsTriggered = ruleFireHistory.reduce(
  118. (acc, curr) => acc + curr.count,
  119. 0
  120. );
  121. return loading ? (
  122. this.renderEmpty()
  123. ) : (
  124. <Panel>
  125. <StyledPanelBody withPadding>
  126. <ChartHeader>
  127. <HeaderTitleLegend>{t('Alerts Triggered')}</HeaderTitleLegend>
  128. </ChartHeader>
  129. {getDynamicText({
  130. value: this.renderChart(),
  131. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  132. })}
  133. </StyledPanelBody>
  134. <ChartFooter>
  135. <FooterHeader>{t('Total Alerts')}</FooterHeader>
  136. <FooterValue>{totalAlertsTriggered.toLocaleString()}</FooterValue>
  137. </ChartFooter>
  138. </Panel>
  139. );
  140. }
  141. }
  142. export default withRouter(AlertChart);
  143. const ChartHeader = styled('div')`
  144. margin-bottom: ${space(3)};
  145. `;
  146. const ChartFooter = styled(PanelFooter)`
  147. display: flex;
  148. align-items: center;
  149. padding: ${space(1)} 20px;
  150. `;
  151. const FooterHeader = styled(SectionHeading)`
  152. display: flex;
  153. align-items: center;
  154. margin: 0;
  155. font-weight: bold;
  156. font-size: ${p => p.theme.fontSizeMedium};
  157. line-height: 1;
  158. `;
  159. const FooterValue = styled('div')`
  160. display: flex;
  161. align-items: center;
  162. margin: 0 ${space(1)};
  163. `;
  164. /* Override padding to make chart appear centered */
  165. const StyledPanelBody = styled(PanelBody)`
  166. padding-right: 6px;
  167. `;