useExploreAggregatesTable.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {useMemo} from 'react';
  2. import type {NewQuery} from 'sentry/types/organization';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import {
  7. useExploreDataset,
  8. useExploreGroupBys,
  9. useExploreSortBys,
  10. useExploreVisualizes,
  11. } from 'sentry/views/explore/contexts/pageParamsContext';
  12. import {formatSort} from 'sentry/views/explore/contexts/pageParamsContext/sortBys';
  13. import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  14. interface UseExploreAggregatesTableOptions {
  15. enabled: boolean;
  16. limit: number;
  17. query: string;
  18. }
  19. export interface AggregatesTableResult {
  20. eventView: EventView;
  21. fields: string[];
  22. result: ReturnType<typeof useSpansQuery<any[]>>;
  23. }
  24. export function useExploreAggregatesTable({
  25. enabled,
  26. limit,
  27. query,
  28. }: UseExploreAggregatesTableOptions): AggregatesTableResult {
  29. const {selection} = usePageFilters();
  30. const dataset = useExploreDataset();
  31. const groupBys = useExploreGroupBys();
  32. const sorts = useExploreSortBys();
  33. const visualizes = useExploreVisualizes();
  34. const fields = useMemo(() => {
  35. // When rendering the table, we want the group bys first
  36. // then the aggregates.
  37. const allFields: string[] = [];
  38. for (const groupBy of groupBys) {
  39. if (allFields.includes(groupBy)) {
  40. continue;
  41. }
  42. allFields.push(groupBy);
  43. }
  44. for (const visualize of visualizes) {
  45. for (const yAxis of visualize.yAxes) {
  46. if (allFields.includes(yAxis)) {
  47. continue;
  48. }
  49. allFields.push(yAxis);
  50. }
  51. }
  52. return allFields.filter(Boolean);
  53. }, [groupBys, visualizes]);
  54. const eventView = useMemo(() => {
  55. const search = new MutableSearch(query);
  56. // Filtering out all spans with op like 'ui.interaction*' which aren't
  57. // embedded under transactions. The trace view does not support rendering
  58. // such spans yet.
  59. search.addFilterValues('!transaction.span_id', ['00']);
  60. const discoverQuery: NewQuery = {
  61. id: undefined,
  62. name: 'Explore - Span Aggregates',
  63. fields,
  64. orderby: sorts.map(formatSort),
  65. query: search.formatString(),
  66. version: 2,
  67. dataset,
  68. };
  69. return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
  70. }, [dataset, fields, sorts, query, selection]);
  71. const result = useSpansQuery({
  72. enabled,
  73. eventView,
  74. initialData: [],
  75. limit,
  76. referrer: 'api.explore.spans-aggregates-table',
  77. trackResponseAnalytics: false,
  78. });
  79. return useMemo(() => {
  80. return {eventView, fields, result};
  81. }, [eventView, fields, result]);
  82. }