genericWidgetQueries.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import {Component} from 'react';
  2. import cloneDeep from 'lodash/cloneDeep';
  3. import isEqual from 'lodash/isEqual';
  4. import omit from 'lodash/omit';
  5. import {Client, ResponseMeta} from 'sentry/api';
  6. import {isSelectionEqual} from 'sentry/components/organizations/pageFilters/utils';
  7. import {t} from 'sentry/locale';
  8. import {Organization, PageFilters} from 'sentry/types';
  9. import {Series} from 'sentry/types/echarts';
  10. import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  11. import {AggregationOutputType} from 'sentry/utils/discover/fields';
  12. import {MEPState} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  13. import {dashboardFiltersToString} from 'sentry/views/dashboards/utils';
  14. import {DatasetConfig} from '../datasetConfig/base';
  15. import {
  16. DashboardFilters,
  17. DEFAULT_TABLE_LIMIT,
  18. DisplayType,
  19. Widget,
  20. WidgetQuery,
  21. } from '../types';
  22. function getReferrer(displayType: DisplayType) {
  23. let referrer: string = '';
  24. if (displayType === DisplayType.TABLE) {
  25. referrer = 'api.dashboards.tablewidget';
  26. } else if (displayType === DisplayType.BIG_NUMBER) {
  27. referrer = 'api.dashboards.bignumberwidget';
  28. } else {
  29. referrer = `api.dashboards.widget.${displayType}-chart`;
  30. }
  31. return referrer;
  32. }
  33. export type OnDataFetchedProps = {
  34. pageLinks?: string;
  35. tableResults?: TableDataWithTitle[];
  36. timeseriesResults?: Series[];
  37. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  38. totalIssuesCount?: string;
  39. };
  40. export type GenericWidgetQueriesChildrenProps = {
  41. loading: boolean;
  42. errorMessage?: string;
  43. pageLinks?: string;
  44. tableResults?: TableDataWithTitle[];
  45. timeseriesResults?: Series[];
  46. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  47. totalCount?: string;
  48. };
  49. export type GenericWidgetQueriesProps<SeriesResponse, TableResponse> = {
  50. api: Client;
  51. children: (props: GenericWidgetQueriesChildrenProps) => JSX.Element;
  52. config: DatasetConfig<SeriesResponse, TableResponse>;
  53. organization: Organization;
  54. selection: PageFilters;
  55. widget: Widget;
  56. afterFetchSeriesData?: (result: SeriesResponse) => void;
  57. afterFetchTableData?: (
  58. result: TableResponse,
  59. response?: ResponseMeta
  60. ) => void | {totalIssuesCount?: string};
  61. cursor?: string;
  62. customDidUpdateComparator?: (
  63. prevProps: GenericWidgetQueriesProps<SeriesResponse, TableResponse>,
  64. nextProps: GenericWidgetQueriesProps<SeriesResponse, TableResponse>
  65. ) => boolean;
  66. dashboardFilters?: DashboardFilters;
  67. limit?: number;
  68. loading?: boolean;
  69. mepSetting?: MEPState | null;
  70. onDataFetched?: ({
  71. tableResults,
  72. timeseriesResults,
  73. totalIssuesCount,
  74. pageLinks,
  75. timeseriesResultsTypes,
  76. }: OnDataFetchedProps) => void;
  77. };
  78. type State<SeriesResponse> = {
  79. loading: boolean;
  80. errorMessage?: GenericWidgetQueriesChildrenProps['errorMessage'];
  81. pageLinks?: GenericWidgetQueriesChildrenProps['pageLinks'];
  82. queryFetchID?: symbol;
  83. rawResults?: SeriesResponse[];
  84. tableResults?: GenericWidgetQueriesChildrenProps['tableResults'];
  85. timeseriesResults?: GenericWidgetQueriesChildrenProps['timeseriesResults'];
  86. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  87. };
  88. class GenericWidgetQueries<SeriesResponse, TableResponse> extends Component<
  89. GenericWidgetQueriesProps<SeriesResponse, TableResponse>,
  90. State<SeriesResponse>
  91. > {
  92. state: State<SeriesResponse> = {
  93. loading: true,
  94. queryFetchID: undefined,
  95. errorMessage: undefined,
  96. timeseriesResults: undefined,
  97. rawResults: undefined,
  98. tableResults: undefined,
  99. pageLinks: undefined,
  100. timeseriesResultsTypes: undefined,
  101. };
  102. componentDidMount() {
  103. this._isMounted = true;
  104. if (!this.props.loading) {
  105. this.fetchData();
  106. }
  107. }
  108. componentDidUpdate(
  109. prevProps: GenericWidgetQueriesProps<SeriesResponse, TableResponse>
  110. ) {
  111. const {selection, widget, cursor, organization, config, customDidUpdateComparator} =
  112. this.props;
  113. // We do not fetch data whenever the query name changes.
  114. // Also don't count empty fields when checking for field changes
  115. const [prevWidgetQueryNames, prevWidgetQueries] = prevProps.widget.queries
  116. .map((query: WidgetQuery) => {
  117. query.aggregates = query.aggregates.filter(field => !!field);
  118. query.columns = query.columns.filter(field => !!field);
  119. return query;
  120. })
  121. .reduce(
  122. ([names, queries]: [string[], Omit<WidgetQuery, 'name'>[]], {name, ...rest}) => {
  123. names.push(name);
  124. rest.fields = rest.fields?.filter(field => !!field) ?? [];
  125. // Ignore aliases because changing alias does not need a query
  126. rest = omit(rest, 'fieldAliases');
  127. queries.push(rest);
  128. return [names, queries];
  129. },
  130. [[], []]
  131. );
  132. const [widgetQueryNames, widgetQueries] = widget.queries
  133. .map((query: WidgetQuery) => {
  134. query.aggregates = query.aggregates.filter(
  135. field => !!field && field !== 'equation|'
  136. );
  137. query.columns = query.columns.filter(field => !!field && field !== 'equation|');
  138. return query;
  139. })
  140. .reduce(
  141. ([names, queries]: [string[], Omit<WidgetQuery, 'name'>[]], {name, ...rest}) => {
  142. names.push(name);
  143. rest.fields = rest.fields?.filter(field => !!field) ?? [];
  144. // Ignore aliases because changing alias does not need a query
  145. rest = omit(rest, 'fieldAliases');
  146. queries.push(rest);
  147. return [names, queries];
  148. },
  149. [[], []]
  150. );
  151. if (
  152. customDidUpdateComparator
  153. ? customDidUpdateComparator(prevProps, this.props)
  154. : widget.limit !== prevProps.widget.limit ||
  155. !isEqual(widget.displayType, prevProps.widget.displayType) ||
  156. !isEqual(widget.interval, prevProps.widget.interval) ||
  157. !isEqual(new Set(widgetQueries), new Set(prevWidgetQueries)) ||
  158. !isEqual(this.props.dashboardFilters, prevProps.dashboardFilters) ||
  159. !isSelectionEqual(selection, prevProps.selection) ||
  160. cursor !== prevProps.cursor
  161. ) {
  162. this.fetchData();
  163. return;
  164. }
  165. if (
  166. !this.state.loading &&
  167. !isEqual(prevWidgetQueryNames, widgetQueryNames) &&
  168. this.state.rawResults?.length === widget.queries.length
  169. ) {
  170. // If the query names has changed, then update timeseries labels
  171. // eslint-disable-next-line react/no-did-update-set-state
  172. this.setState(prevState => {
  173. const timeseriesResults = widget.queries.reduce((acc: Series[], query, index) => {
  174. return acc.concat(
  175. config.transformSeries!(prevState.rawResults![index], query, organization)
  176. );
  177. }, []);
  178. return {...prevState, timeseriesResults};
  179. });
  180. }
  181. }
  182. componentWillUnmount() {
  183. this._isMounted = false;
  184. }
  185. private _isMounted: boolean = false;
  186. applyDashboardFilters(widget: Widget): Widget {
  187. const {dashboardFilters} = this.props;
  188. const dashboardFilterConditions = dashboardFiltersToString(dashboardFilters);
  189. widget.queries.forEach(query => {
  190. query.conditions =
  191. query.conditions +
  192. (dashboardFilterConditions === '' ? '' : ` ${dashboardFilterConditions}`);
  193. });
  194. return widget;
  195. }
  196. async fetchTableData(queryFetchID: symbol) {
  197. const {
  198. widget: originalWidget,
  199. limit,
  200. config,
  201. api,
  202. organization,
  203. selection,
  204. cursor,
  205. afterFetchTableData,
  206. onDataFetched,
  207. mepSetting,
  208. } = this.props;
  209. const widget = this.applyDashboardFilters(cloneDeep(originalWidget));
  210. const responses = await Promise.all(
  211. widget.queries.map(query => {
  212. const requestLimit: number | undefined = limit ?? DEFAULT_TABLE_LIMIT;
  213. const requestCreator = config.getTableRequest;
  214. if (!requestCreator) {
  215. throw new Error(
  216. t('This display type is not supported by the selected dataset.')
  217. );
  218. }
  219. return requestCreator(
  220. api,
  221. query,
  222. organization,
  223. selection,
  224. requestLimit,
  225. cursor,
  226. getReferrer(widget.displayType),
  227. mepSetting
  228. );
  229. })
  230. );
  231. let transformedTableResults: TableDataWithTitle[] = [];
  232. let responsePageLinks: string | undefined;
  233. let afterTableFetchData: OnDataFetchedProps | undefined;
  234. responses.forEach(([data, _textstatus, resp], i) => {
  235. afterTableFetchData = afterFetchTableData?.(data, resp) ?? {};
  236. // Cast so we can add the title.
  237. const transformedData = config.transformTable(
  238. data,
  239. widget.queries[0],
  240. organization,
  241. selection
  242. ) as TableDataWithTitle;
  243. transformedData.title = widget.queries[i]?.name ?? '';
  244. // Overwrite the local var to work around state being stale in tests.
  245. transformedTableResults = [...transformedTableResults, transformedData];
  246. // There is some inconsistency with the capitalization of "link" in response headers
  247. responsePageLinks =
  248. (resp?.getResponseHeader('Link') || resp?.getResponseHeader('link')) ?? undefined;
  249. });
  250. if (this._isMounted && this.state.queryFetchID === queryFetchID) {
  251. onDataFetched?.({
  252. tableResults: transformedTableResults,
  253. pageLinks: responsePageLinks,
  254. ...afterTableFetchData,
  255. });
  256. this.setState({
  257. tableResults: transformedTableResults,
  258. pageLinks: responsePageLinks,
  259. });
  260. }
  261. }
  262. async fetchSeriesData(queryFetchID: symbol) {
  263. const {
  264. widget: originalWidget,
  265. config,
  266. api,
  267. organization,
  268. selection,
  269. afterFetchSeriesData,
  270. onDataFetched,
  271. mepSetting,
  272. } = this.props;
  273. const widget = this.applyDashboardFilters(cloneDeep(originalWidget));
  274. const responses = await Promise.all(
  275. widget.queries.map((_query, index) => {
  276. return config.getSeriesRequest!(
  277. api,
  278. widget,
  279. index,
  280. organization,
  281. selection,
  282. getReferrer(widget.displayType),
  283. mepSetting
  284. );
  285. })
  286. );
  287. const rawResultsClone = cloneDeep(this.state.rawResults) ?? [];
  288. const transformedTimeseriesResults: Series[] = [];
  289. responses.forEach(([data], requestIndex) => {
  290. afterFetchSeriesData?.(data);
  291. rawResultsClone[requestIndex] = data;
  292. const transformedResult = config.transformSeries!(
  293. data,
  294. widget.queries[requestIndex],
  295. organization
  296. );
  297. // When charting timeseriesData on echarts, color association to a timeseries result
  298. // is order sensitive, ie series at index i on the timeseries array will use color at
  299. // index i on the color array. This means that on multi series results, we need to make
  300. // sure that the order of series in our results do not change between fetches to avoid
  301. // coloring inconsistencies between renders.
  302. transformedResult.forEach((result, resultIndex) => {
  303. transformedTimeseriesResults[
  304. requestIndex * transformedResult.length + resultIndex
  305. ] = result;
  306. });
  307. });
  308. // Get series result type
  309. // Only used by custom measurements in errorsAndTransactions at the moment
  310. const timeseriesResultsTypes = config.getSeriesResultType?.(
  311. responses[0][0],
  312. widget.queries[0]
  313. );
  314. if (this._isMounted && this.state.queryFetchID === queryFetchID) {
  315. onDataFetched?.({
  316. timeseriesResults: transformedTimeseriesResults,
  317. timeseriesResultsTypes,
  318. });
  319. this.setState({
  320. timeseriesResults: transformedTimeseriesResults,
  321. rawResults: rawResultsClone,
  322. timeseriesResultsTypes,
  323. });
  324. }
  325. }
  326. async fetchData() {
  327. const {widget} = this.props;
  328. const queryFetchID = Symbol('queryFetchID');
  329. this.setState({
  330. loading: true,
  331. tableResults: undefined,
  332. timeseriesResults: undefined,
  333. errorMessage: undefined,
  334. queryFetchID,
  335. });
  336. try {
  337. if ([DisplayType.TABLE, DisplayType.BIG_NUMBER].includes(widget.displayType)) {
  338. await this.fetchTableData(queryFetchID);
  339. } else {
  340. await this.fetchSeriesData(queryFetchID);
  341. }
  342. } catch (err) {
  343. if (this._isMounted) {
  344. this.setState({
  345. errorMessage:
  346. err?.responseJSON?.detail || err?.message || t('An unknown error occurred.'),
  347. });
  348. }
  349. } finally {
  350. if (this._isMounted) {
  351. this.setState({loading: false});
  352. }
  353. }
  354. }
  355. render() {
  356. const {children} = this.props;
  357. const {
  358. loading,
  359. tableResults,
  360. timeseriesResults,
  361. errorMessage,
  362. pageLinks,
  363. timeseriesResultsTypes,
  364. } = this.state;
  365. return children({
  366. loading,
  367. tableResults,
  368. timeseriesResults,
  369. errorMessage,
  370. pageLinks,
  371. timeseriesResultsTypes,
  372. });
  373. }
  374. }
  375. export default GenericWidgetQueries;