projectBaseEventsChart.tsx 3.7 KB

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