useIndexedSpan.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {Location} from 'history';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import type {IndexedSpan} from 'sentry/views/starfish/queries/types';
  6. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  7. export const useIndexedSpan = (
  8. groupId: string,
  9. _referrer: string = 'use-indexed-span'
  10. ) => {
  11. const location = useLocation();
  12. const query = getQuery(groupId);
  13. const eventView = getEventView(groupId, location);
  14. // TODO: Add referrer
  15. const {isLoading, data} = useSpansQuery<IndexedSpan[]>({
  16. eventView,
  17. limit: 1,
  18. queryString: query,
  19. initialData: [],
  20. enabled: Boolean(query),
  21. });
  22. return {
  23. isLoading,
  24. data: data[0],
  25. };
  26. };
  27. function getQuery(groupId: string) {
  28. return `
  29. SELECT
  30. span_id as "id",
  31. group_id as "group",
  32. action,
  33. description,
  34. span_operation as "op",
  35. domain,
  36. module
  37. FROM spans_experimental_starfish
  38. WHERE group_id = '${groupId}'
  39. LIMIT 1
  40. `;
  41. }
  42. function getEventView(groupId: string, location: Location) {
  43. const cleanGroupID = groupId.replaceAll('-', '').slice(-16);
  44. return EventView.fromNewQueryWithLocation(
  45. {
  46. name: '',
  47. query: `group:${cleanGroupID}`,
  48. fields: ['group', 'action', 'description', 'domain', 'module', 'op'],
  49. dataset: DiscoverDatasets.SPANS_INDEXED,
  50. projects: [1],
  51. version: 2,
  52. },
  53. location
  54. );
  55. }