projectBaseEventsChart.tsx 3.5 KB

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