metricsRequest.tsx 4.6 KB

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