metricsRequest.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 {doMetricsRequest, DoMetricsRequestOptions} from 'sentry/actionCreators/metrics';
  6. import {Client, ResponseMeta} from 'sentry/api';
  7. import {shouldFetchPreviousPeriod} from 'sentry/components/charts/utils';
  8. import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
  9. import {t} from 'sentry/locale';
  10. import {MetricsApiResponse} from 'sentry/types';
  11. import {Series} from 'sentry/types/echarts';
  12. import {getPeriod} from 'sentry/utils/getPeriod';
  13. import {TableData} from '../discover/discoverQuery';
  14. import {deprecatedTransformMetricsResponseToTable} from './deprecatedTransformMetricsResponseToTable';
  15. import {transformMetricsResponseToSeries} from './transformMetricsResponseToSeries';
  16. const propNamesToIgnore = ['api', 'children'];
  17. const omitIgnoredProps = (props: Props) =>
  18. omitBy(props, (_value, key) => propNamesToIgnore.includes(key));
  19. export type MetricsRequestRenderProps = {
  20. error: string | null;
  21. errored: boolean;
  22. isLoading: boolean;
  23. loading: boolean;
  24. pageLinks: string | null;
  25. reloading: boolean;
  26. response: MetricsApiResponse | null;
  27. responsePrevious: MetricsApiResponse | null;
  28. seriesData?: Series[];
  29. seriesDataPrevious?: Series[];
  30. tableData?: TableData;
  31. };
  32. type DefaultProps = {
  33. /**
  34. * @deprecated
  35. * Transform the response data to be something ingestible by GridEditable tables and rename fields for performance
  36. */
  37. includeDeprecatedTabularData: boolean;
  38. /**
  39. * Include data for previous period
  40. */
  41. includePrevious: boolean;
  42. /**
  43. * Transform the response data to be something ingestible by charts
  44. */
  45. includeSeriesData: boolean;
  46. /**
  47. * If true, no request will be made
  48. */
  49. isDisabled?: boolean;
  50. };
  51. type Props = DefaultProps &
  52. Omit<
  53. DoMetricsRequestOptions,
  54. 'includeAllArgs' | 'statsPeriodStart' | 'statsPeriodEnd'
  55. > & {
  56. api: Client;
  57. children?: (renderProps: MetricsRequestRenderProps) => React.ReactNode;
  58. };
  59. type State = {
  60. error: string | null;
  61. errored: boolean;
  62. pageLinks: string | null;
  63. reloading: boolean;
  64. response: MetricsApiResponse | null;
  65. responsePrevious: MetricsApiResponse | null;
  66. };
  67. class MetricsRequest extends React.Component<Props, State> {
  68. static defaultProps: DefaultProps = {
  69. includePrevious: false,
  70. includeSeriesData: false,
  71. includeDeprecatedTabularData: false,
  72. isDisabled: false,
  73. };
  74. state: State = {
  75. reloading: false,
  76. errored: false,
  77. error: null,
  78. response: null,
  79. responsePrevious: null,
  80. pageLinks: null,
  81. };
  82. componentDidMount() {
  83. this.fetchData();
  84. }
  85. componentDidUpdate(prevProps: Props) {
  86. if (isEqual(omitIgnoredProps(prevProps), omitIgnoredProps(this.props))) {
  87. return;
  88. }
  89. this.fetchData();
  90. }
  91. componentWillUnmount() {
  92. this.unmounting = true;
  93. }
  94. private unmounting: boolean = false;
  95. getQueryParams({previousPeriod = false} = {}) {
  96. const {
  97. project,
  98. environment,
  99. field,
  100. query,
  101. groupBy,
  102. orderBy,
  103. limit,
  104. interval,
  105. cursor,
  106. statsPeriod,
  107. start,
  108. end,
  109. orgSlug,
  110. } = this.props;
  111. const commonQuery = {
  112. field,
  113. cursor,
  114. environment,
  115. groupBy,
  116. interval,
  117. query,
  118. limit,
  119. project,
  120. orderBy,
  121. orgSlug,
  122. };
  123. if (!previousPeriod) {
  124. return {
  125. ...commonQuery,
  126. statsPeriod,
  127. start,
  128. end,
  129. };
  130. }
  131. const doubledStatsPeriod = getPeriod(
  132. {period: statsPeriod, start: undefined, end: undefined},
  133. {shouldDoublePeriod: true}
  134. ).statsPeriod;
  135. return {
  136. ...commonQuery,
  137. statsPeriodStart: doubledStatsPeriod,
  138. statsPeriodEnd: statsPeriod ?? DEFAULT_STATS_PERIOD,
  139. };
  140. }
  141. fetchData = async () => {
  142. const {api, isDisabled, start, end, statsPeriod, includePrevious} = this.props;
  143. if (isDisabled) {
  144. return;
  145. }
  146. this.setState(state => ({
  147. reloading: state.response !== null,
  148. errored: false,
  149. error: null,
  150. pageLinks: null,
  151. }));
  152. const promises = [
  153. doMetricsRequest(api, {includeAllArgs: true, ...this.getQueryParams()}),
  154. ];
  155. // TODO(metrics): this could be merged into one request by doubling the statsPeriod and then splitting the response in half
  156. if (shouldFetchPreviousPeriod({start, end, period: statsPeriod, includePrevious})) {
  157. promises.push(doMetricsRequest(api, this.getQueryParams({previousPeriod: true})));
  158. }
  159. try {
  160. const [[response, _, responseMeta], responsePrevious] = (await Promise.all(
  161. promises
  162. )) as [
  163. [MetricsApiResponse, string | undefined, ResponseMeta | undefined],
  164. MetricsApiResponse | undefined
  165. ];
  166. if (this.unmounting) {
  167. return;
  168. }
  169. this.setState({
  170. reloading: false,
  171. response,
  172. responsePrevious: responsePrevious ?? null,
  173. pageLinks: responseMeta?.getResponseHeader('Link') ?? null,
  174. });
  175. } catch (error) {
  176. addErrorMessage(error.responseJSON?.detail ?? t('Error loading metrics data'));
  177. this.setState({
  178. reloading: false,
  179. errored: true,
  180. error: error.responseJSON?.detail ?? null,
  181. pageLinks: null,
  182. });
  183. }
  184. };
  185. render() {
  186. const {reloading, errored, error, response, responsePrevious, pageLinks} = this.state;
  187. const {
  188. children,
  189. isDisabled,
  190. includeDeprecatedTabularData,
  191. includeSeriesData,
  192. includePrevious,
  193. } = this.props;
  194. const loading = response === null && !isDisabled && !error;
  195. return children?.({
  196. loading,
  197. reloading,
  198. isLoading: loading || reloading, // some components downstream are used to loading/reloading or isLoading that combines both (EventsRequest vs DiscoverQuery)
  199. errored,
  200. error,
  201. response,
  202. responsePrevious,
  203. pageLinks,
  204. tableData: includeDeprecatedTabularData
  205. ? deprecatedTransformMetricsResponseToTable({response})
  206. : undefined,
  207. seriesData: includeSeriesData
  208. ? transformMetricsResponseToSeries(response)
  209. : undefined,
  210. seriesDataPrevious:
  211. includeSeriesData && includePrevious
  212. ? transformMetricsResponseToSeries(responsePrevious)
  213. : undefined,
  214. });
  215. }
  216. }
  217. export default MetricsRequest;