metricWidgetQueries.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {Component} from 'react';
  2. import isEqual from 'lodash/isEqual';
  3. import omit from 'lodash/omit';
  4. import {Client} from 'sentry/api';
  5. import {isSelectionEqual} from 'sentry/components/organizations/pageFilters/utils';
  6. import {MetricsApiResponse, Organization, PageFilters} from 'sentry/types';
  7. import {Series} from 'sentry/types/echarts';
  8. import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  9. import {TOP_N} from 'sentry/utils/discover/types';
  10. import {mapToMRIFields} from 'sentry/utils/metrics';
  11. import {MetricsConfig} from '../datasetConfig/metrics';
  12. import {DashboardFilters, DEFAULT_TABLE_LIMIT, DisplayType, Widget} from '../types';
  13. import GenericWidgetQueries, {
  14. GenericWidgetQueriesChildrenProps,
  15. GenericWidgetQueriesProps,
  16. } from './genericWidgetQueries';
  17. type Props = {
  18. api: Client;
  19. children: (props: GenericWidgetQueriesChildrenProps) => JSX.Element;
  20. organization: Organization;
  21. selection: PageFilters;
  22. widget: Widget;
  23. cursor?: string;
  24. dashboardFilters?: DashboardFilters;
  25. limit?: number;
  26. onDataFetched?: (results: {
  27. tableResults?: TableDataWithTitle[];
  28. timeseriesResults?: Series[];
  29. }) => void;
  30. };
  31. type State = {
  32. loading: boolean;
  33. errorMessage?: string;
  34. };
  35. class MetricWidgetQueries extends Component<Props, State> {
  36. state: State = {
  37. loading: true,
  38. errorMessage: undefined,
  39. };
  40. config = MetricsConfig;
  41. get limit() {
  42. const {limit} = this.props;
  43. switch (this.props.widget.displayType) {
  44. case DisplayType.TOP_N:
  45. return TOP_N;
  46. case DisplayType.TABLE:
  47. return limit ?? DEFAULT_TABLE_LIMIT;
  48. case DisplayType.BIG_NUMBER:
  49. return 1;
  50. default:
  51. return limit ?? 20; // TODO(dam): Can be changed to undefined once [INGEST-1079] is resolved
  52. }
  53. }
  54. customDidUpdateComparator = (
  55. prevProps: GenericWidgetQueriesProps<MetricsApiResponse, MetricsApiResponse>,
  56. nextProps: GenericWidgetQueriesProps<MetricsApiResponse, MetricsApiResponse>
  57. ) => {
  58. const {loading, limit, widget, cursor, organization, selection, dashboardFilters} =
  59. nextProps;
  60. const ignoredWidgetProps = [
  61. 'queries',
  62. 'title',
  63. 'id',
  64. 'layout',
  65. 'tempId',
  66. 'widgetType',
  67. ];
  68. const ignoredQueryProps = ['name', 'fields', 'aggregates', 'columns'];
  69. return (
  70. limit !== prevProps.limit ||
  71. organization.slug !== prevProps.organization.slug ||
  72. !isEqual(dashboardFilters, prevProps.dashboardFilters) ||
  73. !isSelectionEqual(selection, prevProps.selection) ||
  74. // If the widget changed (ignore unimportant fields, + queries as they are handled lower)
  75. !isEqual(
  76. omit(widget, ignoredWidgetProps),
  77. omit(prevProps.widget, ignoredWidgetProps)
  78. ) ||
  79. // If the queries changed (ignore unimportant name, + fields as they are handled lower)
  80. !isEqual(
  81. widget.queries.map(q => omit(q, ignoredQueryProps)),
  82. prevProps.widget.queries.map(q => omit(q, ignoredQueryProps))
  83. ) ||
  84. // If the fields changed (ignore falsy/empty fields -> they can happen after clicking on Add Overlay)
  85. !isEqual(
  86. widget.queries.flatMap(q => q.fields?.filter(field => !!field)),
  87. prevProps.widget.queries.flatMap(q => q.fields?.filter(field => !!field))
  88. ) ||
  89. !isEqual(
  90. widget.queries.flatMap(q => q.aggregates.filter(aggregate => !!aggregate)),
  91. prevProps.widget.queries.flatMap(q =>
  92. q.aggregates.filter(aggregate => !!aggregate)
  93. )
  94. ) ||
  95. !isEqual(
  96. widget.queries.flatMap(q => q.columns.filter(column => !!column)),
  97. prevProps.widget.queries.flatMap(q => q.columns.filter(column => !!column))
  98. ) ||
  99. loading !== prevProps.loading ||
  100. cursor !== prevProps.cursor
  101. );
  102. };
  103. afterFetchData = (data: MetricsApiResponse) => {
  104. const fields = this.props.widget.queries[0].aggregates;
  105. mapToMRIFields(data, fields);
  106. };
  107. render() {
  108. const {
  109. api,
  110. children,
  111. organization,
  112. selection,
  113. widget,
  114. cursor,
  115. dashboardFilters,
  116. onDataFetched,
  117. } = this.props;
  118. const config = MetricsConfig;
  119. return (
  120. <GenericWidgetQueries<MetricsApiResponse, MetricsApiResponse>
  121. config={config}
  122. api={api}
  123. organization={organization}
  124. selection={selection}
  125. widget={widget}
  126. dashboardFilters={dashboardFilters}
  127. cursor={cursor}
  128. limit={this.limit}
  129. onDataFetched={onDataFetched}
  130. loading={undefined}
  131. customDidUpdateComparator={this.customDidUpdateComparator}
  132. afterFetchTableData={this.afterFetchData}
  133. afterFetchSeriesData={this.afterFetchData}
  134. >
  135. {({errorMessage, ...rest}) =>
  136. children({
  137. errorMessage: this.state.errorMessage ?? errorMessage,
  138. ...rest,
  139. })
  140. }
  141. </GenericWidgetQueries>
  142. );
  143. }
  144. }
  145. export default MetricWidgetQueries;