useProjectRawWebVitalsQuery.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type {Tag} from 'sentry/types';
  2. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {DEFAULT_QUERY_FILTER} from 'sentry/views/insights/browser/webVitals/settings';
  10. type Props = {
  11. dataset?: DiscoverDatasets;
  12. tag?: Tag;
  13. transaction?: string;
  14. };
  15. export const useProjectRawWebVitalsQuery = ({transaction, tag, dataset}: Props = {}) => {
  16. const organization = useOrganization();
  17. const pageFilters = usePageFilters();
  18. const location = useLocation();
  19. const search = new MutableSearch([]);
  20. if (transaction) {
  21. search.addFilterValue('transaction', transaction);
  22. }
  23. if (tag) {
  24. search.addFilterValue(tag.key, tag.name);
  25. }
  26. const projectEventView = EventView.fromNewQueryWithPageFilters(
  27. {
  28. fields: [
  29. 'p75(measurements.lcp)',
  30. 'p75(measurements.fcp)',
  31. 'p75(measurements.cls)',
  32. 'p75(measurements.ttfb)',
  33. 'p75(measurements.inp)',
  34. 'p75(transaction.duration)',
  35. 'count_web_vitals(measurements.lcp, any)',
  36. 'count_web_vitals(measurements.fcp, any)',
  37. 'count_web_vitals(measurements.cls, any)',
  38. 'count_web_vitals(measurements.ttfb, any)',
  39. 'count_web_vitals(measurements.inp, any)',
  40. 'count()',
  41. ],
  42. name: 'Web Vitals',
  43. query: [DEFAULT_QUERY_FILTER, search.formatString()].join(' ').trim(),
  44. version: 2,
  45. dataset: dataset ?? DiscoverDatasets.METRICS,
  46. },
  47. pageFilters.selection
  48. );
  49. const result = useDiscoverQuery({
  50. eventView: projectEventView,
  51. limit: 50,
  52. location,
  53. orgSlug: organization.slug,
  54. cursor: '',
  55. options: {
  56. refetchOnWindowFocus: false,
  57. },
  58. skipAbort: true,
  59. referrer: 'api.performance.browser.web-vitals.project',
  60. });
  61. return result;
  62. };