projectBaseEventsChart.tsx 3.6 KB

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