previewChart.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import styled from '@emotion/styled';
  2. import {AreaChart, AreaChartSeries} from 'sentry/components/charts/areaChart';
  3. import {HeaderTitleLegend, SectionHeading} from 'sentry/components/charts/styles';
  4. import {Panel, PanelBody, PanelFooter} from 'sentry/components/panels';
  5. import Placeholder from 'sentry/components/placeholder';
  6. import {t} from 'sentry/locale';
  7. import space from 'sentry/styles/space';
  8. import {ProjectAlertRuleStats} from 'sentry/types/alerts';
  9. import getDynamicText from 'sentry/utils/getDynamicText';
  10. type Props = {
  11. ruleFireHistory: ProjectAlertRuleStats[];
  12. };
  13. const PreviewChart = ({ruleFireHistory}: Props) => {
  14. const renderChart = fireHistory => {
  15. const series: AreaChartSeries = {
  16. seriesName: 'Alerts Triggered',
  17. data: fireHistory.map(alert => ({
  18. name: alert.date,
  19. value: alert.count,
  20. })),
  21. emphasis: {
  22. disabled: true,
  23. },
  24. };
  25. return (
  26. <AreaChart
  27. isGroupedByDate
  28. showTimeInTooltip
  29. grid={{
  30. left: space(0.25),
  31. right: space(2),
  32. top: space(3),
  33. bottom: 0,
  34. }}
  35. yAxis={{
  36. minInterval: 1,
  37. }}
  38. series={[series]}
  39. />
  40. );
  41. };
  42. const totalAlertsTriggered = ruleFireHistory.reduce((acc, curr) => acc + curr.count, 0);
  43. return (
  44. <Panel>
  45. <StyledPanelBody withPadding>
  46. <ChartHeader>
  47. <HeaderTitleLegend>{t('Alerts Triggered')}</HeaderTitleLegend>
  48. </ChartHeader>
  49. {getDynamicText({
  50. value: renderChart(ruleFireHistory),
  51. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  52. })}
  53. </StyledPanelBody>
  54. <ChartFooter>
  55. <FooterHeader>{t('Total Alerts')}</FooterHeader>
  56. <FooterValue>{totalAlertsTriggered.toLocaleString()}</FooterValue>
  57. </ChartFooter>
  58. </Panel>
  59. );
  60. };
  61. export default PreviewChart;
  62. const ChartHeader = styled('div')`
  63. margin-bottom: ${space(3)};
  64. `;
  65. const ChartFooter = styled(PanelFooter)`
  66. display: flex;
  67. align-items: center;
  68. padding: ${space(1)} 20px;
  69. `;
  70. const FooterHeader = styled(SectionHeading)`
  71. display: flex;
  72. align-items: center;
  73. margin: 0;
  74. font-weight: bold;
  75. font-size: ${p => p.theme.fontSizeMedium};
  76. line-height: 1;
  77. `;
  78. const FooterValue = styled('div')`
  79. display: flex;
  80. align-items: center;
  81. margin: 0 ${space(1)};
  82. `;
  83. /* Override padding to make chart appear centered */
  84. const StyledPanelBody = styled(PanelBody)`
  85. padding-right: ${space(0.75)};
  86. `;