projectBaseEventsChart.tsx 3.7 KB

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