metricWidgetQueries.tsx 4.7 KB

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