metricsRequest.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import * as React from 'react';
  2. import isEqual from 'lodash/isEqual';
  3. import omitBy from 'lodash/omitBy';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import {Client} from 'sentry/api';
  6. import {getInterval, shouldFetchPreviousPeriod} from 'sentry/components/charts/utils';
  7. import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
  8. import {t} from 'sentry/locale';
  9. import {DateString, MetricsApiResponse, Organization} from 'sentry/types';
  10. import {getPeriod} from 'sentry/utils/getPeriod';
  11. import {getMetricsDataSource} from './getMetricsDataSource';
  12. const propNamesToIgnore = ['api', 'children'];
  13. const omitIgnoredProps = (props: Props) =>
  14. omitBy(props, (_value, key) => propNamesToIgnore.includes(key));
  15. export type MetricsRequestRenderProps = {
  16. loading: boolean;
  17. reloading: boolean;
  18. errored: boolean;
  19. response: MetricsApiResponse | null;
  20. responsePrevious: MetricsApiResponse | null;
  21. };
  22. type DefaultProps = {
  23. /**
  24. * Include data for previous period
  25. */
  26. includePrevious: boolean;
  27. };
  28. type Props = DefaultProps & {
  29. api: Client;
  30. orgSlug: Organization['slug'];
  31. field: string[];
  32. children?: (renderProps: MetricsRequestRenderProps) => React.ReactNode;
  33. project?: Readonly<number[]>;
  34. environment?: Readonly<string[]>;
  35. statsPeriod?: string;
  36. start?: DateString;
  37. end?: DateString;
  38. query?: string;
  39. groupBy?: string[];
  40. orderBy?: string;
  41. limit?: number;
  42. interval?: string;
  43. isDisabled?: boolean;
  44. };
  45. type State = {
  46. reloading: boolean;
  47. errored: boolean;
  48. response: MetricsApiResponse | null;
  49. responsePrevious: MetricsApiResponse | null;
  50. };
  51. class MetricsRequest extends React.Component<Props, State> {
  52. static defaultProps: DefaultProps = {
  53. includePrevious: false,
  54. };
  55. state: State = {
  56. reloading: false,
  57. errored: false,
  58. response: null,
  59. responsePrevious: null,
  60. };
  61. componentDidMount() {
  62. this.fetchData();
  63. }
  64. componentDidUpdate(prevProps: Props) {
  65. if (isEqual(omitIgnoredProps(prevProps), omitIgnoredProps(this.props))) {
  66. return;
  67. }
  68. this.fetchData();
  69. }
  70. componentWillUnmount() {
  71. this.unmounting = true;
  72. }
  73. private unmounting: boolean = false;
  74. get path() {
  75. const {orgSlug} = this.props;
  76. return `/organizations/${orgSlug}/metrics/data/`;
  77. }
  78. baseQueryParams({previousPeriod = false} = {}) {
  79. const {project, environment, field, query, groupBy, orderBy, limit, interval} =
  80. this.props;
  81. const {start, end, statsPeriod} = getPeriod({
  82. period: this.props.statsPeriod,
  83. start: this.props.start,
  84. end: this.props.end,
  85. });
  86. const commonQuery = {
  87. project,
  88. environment,
  89. field,
  90. query: query || undefined,
  91. groupBy,
  92. orderBy,
  93. limit,
  94. interval: interval ? interval : getInterval({start, end, period: statsPeriod}),
  95. datasource: getMetricsDataSource(),
  96. };
  97. if (!previousPeriod) {
  98. return {
  99. ...commonQuery,
  100. statsPeriod,
  101. start,
  102. end,
  103. };
  104. }
  105. const doubledStatsPeriod = getPeriod(
  106. {period: statsPeriod, start: undefined, end: undefined},
  107. {shouldDoublePeriod: true}
  108. ).statsPeriod;
  109. return {
  110. ...commonQuery,
  111. statsPeriodStart: doubledStatsPeriod,
  112. statsPeriodEnd: statsPeriod ?? DEFAULT_STATS_PERIOD,
  113. };
  114. }
  115. fetchData = async () => {
  116. const {api, isDisabled, start, end, statsPeriod, includePrevious} = this.props;
  117. if (isDisabled) {
  118. return;
  119. }
  120. this.setState(state => ({
  121. reloading: state.response !== null,
  122. errored: false,
  123. }));
  124. const promises = [api.requestPromise(this.path, {query: this.baseQueryParams()})];
  125. if (shouldFetchPreviousPeriod({start, end, period: statsPeriod, includePrevious})) {
  126. promises.push(
  127. api.requestPromise(this.path, {
  128. query: this.baseQueryParams({previousPeriod: true}),
  129. })
  130. );
  131. }
  132. try {
  133. const [response, responsePrevious] = (await Promise.all(promises)) as [
  134. MetricsApiResponse,
  135. MetricsApiResponse | undefined
  136. ];
  137. if (this.unmounting) {
  138. return;
  139. }
  140. this.setState({
  141. reloading: false,
  142. response,
  143. responsePrevious: responsePrevious ?? null,
  144. });
  145. } catch (error) {
  146. addErrorMessage(error.responseJSON?.detail ?? t('Error loading metrics data'));
  147. this.setState({
  148. reloading: false,
  149. errored: true,
  150. });
  151. }
  152. };
  153. render() {
  154. const {reloading, errored, response, responsePrevious} = this.state;
  155. const {children, isDisabled} = this.props;
  156. const loading = response === null && !isDisabled;
  157. return children?.({
  158. loading,
  159. reloading,
  160. errored,
  161. response,
  162. responsePrevious,
  163. });
  164. }
  165. }
  166. export default MetricsRequest;