useMultiQueryTable.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {useMemo} from 'react';
  2. import type {NewQuery} from 'sentry/types/organization';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import type {Sort} from 'sentry/utils/discover/fields';
  5. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {formatSort} from 'sentry/views/explore/contexts/pageParamsContext/sortBys';
  9. import type {AggregatesTableResult} from 'sentry/views/explore/hooks/useExploreAggregatesTable';
  10. import type {SpansTableResult} from 'sentry/views/explore/hooks/useExploreSpansTable';
  11. import {getFieldsForConstructedQuery} from 'sentry/views/explore/multiQueryMode/locationUtils';
  12. import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  13. type Props = {
  14. enabled: boolean;
  15. groupBys: string[];
  16. query: string;
  17. sortBys: Sort[];
  18. yAxes: string[];
  19. };
  20. export function useMultiQueryTableAggregateMode({
  21. groupBys,
  22. query,
  23. yAxes,
  24. sortBys,
  25. enabled,
  26. }: Props): AggregatesTableResult {
  27. const {selection} = usePageFilters();
  28. const fields = useMemo(() => {
  29. const allFields: Set<string> = new Set();
  30. for (const groupBy of groupBys) {
  31. allFields.add(groupBy);
  32. }
  33. for (const yAxis of yAxes) {
  34. allFields.add(yAxis);
  35. }
  36. return Array.from(allFields).filter(Boolean);
  37. }, [groupBys, yAxes]);
  38. const eventView = useMemo(() => {
  39. const search = new MutableSearch(query);
  40. // Filtering out all spans with op like 'ui.interaction*' which aren't
  41. // embedded under transactions. The trace view does not support rendering
  42. // such spans yet.
  43. search.addFilterValues('!transaction.span_id', ['00']);
  44. const discoverQuery: NewQuery = {
  45. id: undefined,
  46. name: 'Multi Query Mode - Span Aggregates',
  47. fields,
  48. orderby: sortBys.map(formatSort),
  49. query: search.formatString(),
  50. version: 2,
  51. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  52. };
  53. return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
  54. }, [query, fields, sortBys, selection]);
  55. const result = useSpansQuery({
  56. enabled,
  57. eventView,
  58. initialData: [],
  59. limit: 10,
  60. referrer: 'api.explore.multi-query-spans-table',
  61. trackResponseAnalytics: false,
  62. });
  63. return {eventView, fields, result};
  64. }
  65. export function useMultiQueryTableSampleMode({
  66. query,
  67. yAxes,
  68. sortBys,
  69. enabled,
  70. }: Props): SpansTableResult {
  71. const {selection} = usePageFilters();
  72. const fields = useMemo(() => {
  73. const allFields: string[] = [];
  74. allFields.push(...getFieldsForConstructedQuery(yAxes));
  75. allFields.push(...['transaction.span_id', 'trace', 'project', 'timestamp']);
  76. return allFields;
  77. }, [yAxes]);
  78. const eventView = useMemo(() => {
  79. const search = new MutableSearch(query);
  80. // Filtering out all spans with op like 'ui.interaction*' which aren't
  81. // embedded under transactions. The trace view does not support rendering
  82. // such spans yet.
  83. search.addFilterValues('!transaction.span_id', ['00']);
  84. const discoverQuery: NewQuery = {
  85. id: undefined,
  86. name: 'Multi Query Mode - Samples',
  87. fields,
  88. orderby: sortBys.map(formatSort),
  89. query: search.formatString(),
  90. version: 2,
  91. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  92. };
  93. return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
  94. }, [query, fields, sortBys, selection]);
  95. const result = useSpansQuery({
  96. enabled,
  97. eventView,
  98. initialData: [],
  99. limit: 10,
  100. referrer: 'api.explore.multi-query-spans-table',
  101. trackResponseAnalytics: false,
  102. });
  103. return {eventView, result};
  104. }