projectBaseEventsChart.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {Component} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {fetchTotalCount} from 'sentry/actionCreators/events';
  4. import EventsChart, {EventsChartProps} from 'sentry/components/charts/eventsChart';
  5. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  6. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  7. import {isSelectionEqual} from 'sentry/components/organizations/pageFilters/utils';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import {t} from 'sentry/locale';
  10. import {PageFilters} from 'sentry/types';
  11. import {axisLabelFormatter} from 'sentry/utils/discover/charts';
  12. import getDynamicText from 'sentry/utils/getDynamicText';
  13. import withPageFilters from 'sentry/utils/withPageFilters';
  14. type Props = Omit<
  15. EventsChartProps,
  16. keyof Omit<PageFilters, 'datetime'> | keyof PageFilters['datetime']
  17. > & {
  18. onTotalValuesChange: (value: number | null) => void;
  19. selection: PageFilters;
  20. title: string;
  21. yAxis: string;
  22. help?: string;
  23. };
  24. class ProjectBaseEventsChart extends Component<Props> {
  25. componentDidMount() {
  26. this.fetchTotalCount();
  27. }
  28. componentDidUpdate(prevProps: Props) {
  29. if (!isSelectionEqual(this.props.selection, prevProps.selection)) {
  30. this.fetchTotalCount();
  31. }
  32. }
  33. async fetchTotalCount() {
  34. const {api, organization, selection, onTotalValuesChange, query} = this.props;
  35. const {projects, environments, datetime} = selection;
  36. try {
  37. const totals = await fetchTotalCount(api, organization.slug, {
  38. field: [],
  39. query,
  40. environment: environments,
  41. project: projects.map(proj => String(proj)),
  42. ...normalizeDateTimeParams(datetime),
  43. });
  44. onTotalValuesChange(totals);
  45. } catch (err) {
  46. onTotalValuesChange(null);
  47. Sentry.captureException(err);
  48. }
  49. }
  50. render() {
  51. const {
  52. router,
  53. organization,
  54. selection,
  55. api,
  56. yAxis,
  57. query,
  58. field,
  59. title,
  60. help,
  61. ...eventsChartProps
  62. } = this.props;
  63. const {projects, environments, datetime} = selection;
  64. const {start, end, period, utc} = datetime;
  65. return getDynamicText({
  66. value: (
  67. <EventsChart
  68. {...eventsChartProps}
  69. router={router}
  70. organization={organization}
  71. showLegend
  72. yAxis={yAxis}
  73. query={query}
  74. api={api}
  75. projects={projects}
  76. environments={environments}
  77. start={start}
  78. end={end}
  79. period={period}
  80. utc={utc}
  81. field={field}
  82. currentSeriesName={t('This Period')}
  83. previousSeriesName={t('Previous Period')}
  84. disableableSeries={[t('This Period'), t('Previous Period')]}
  85. chartHeader={
  86. <HeaderTitleLegend>
  87. {title}
  88. {help && <QuestionTooltip size="sm" position="top" title={help} />}
  89. </HeaderTitleLegend>
  90. }
  91. legendOptions={{right: 10, top: 0}}
  92. chartOptions={{
  93. grid: {left: '10px', right: '10px', top: '40px', bottom: '0px'},
  94. yAxis: {
  95. axisLabel: {
  96. formatter: (value: number) => axisLabelFormatter(value, yAxis),
  97. },
  98. scale: true,
  99. },
  100. }}
  101. />
  102. ),
  103. fixed: `${title} Chart`,
  104. });
  105. }
  106. }
  107. export default withPageFilters(ProjectBaseEventsChart);