useProjectRawWebVitalsQuery.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import type {BrowserType} from 'sentry/views/insights/browser/webVitals/utils/queryParameterDecoders/browserType';
  11. import {SpanIndexedField} from 'sentry/views/insights/types';
  12. type Props = {
  13. browserTypes?: BrowserType[];
  14. dataset?: DiscoverDatasets;
  15. tag?: Tag;
  16. transaction?: string;
  17. };
  18. export const useProjectRawWebVitalsQuery = ({
  19. transaction,
  20. tag,
  21. dataset,
  22. browserTypes,
  23. }: Props = {}) => {
  24. const organization = useOrganization();
  25. const pageFilters = usePageFilters();
  26. const location = useLocation();
  27. const search = new MutableSearch([]);
  28. if (transaction) {
  29. search.addFilterValue('transaction', transaction);
  30. }
  31. if (tag) {
  32. search.addFilterValue(tag.key, tag.name);
  33. }
  34. if (browserTypes) {
  35. search.addDisjunctionFilterValues(SpanIndexedField.BROWSER_NAME, browserTypes);
  36. }
  37. const projectEventView = EventView.fromNewQueryWithPageFilters(
  38. {
  39. fields: [
  40. 'p75(measurements.lcp)',
  41. 'p75(measurements.fcp)',
  42. 'p75(measurements.cls)',
  43. 'p75(measurements.ttfb)',
  44. 'p75(measurements.inp)',
  45. 'p75(transaction.duration)',
  46. 'count_web_vitals(measurements.lcp, any)',
  47. 'count_web_vitals(measurements.fcp, any)',
  48. 'count_web_vitals(measurements.cls, any)',
  49. 'count_web_vitals(measurements.ttfb, any)',
  50. 'count_web_vitals(measurements.inp, any)',
  51. 'count()',
  52. ],
  53. name: 'Web Vitals',
  54. query: [DEFAULT_QUERY_FILTER, search.formatString()].join(' ').trim(),
  55. version: 2,
  56. dataset: dataset ?? DiscoverDatasets.METRICS,
  57. },
  58. pageFilters.selection
  59. );
  60. const result = useDiscoverQuery({
  61. eventView: projectEventView,
  62. limit: 50,
  63. location,
  64. orgSlug: organization.slug,
  65. cursor: '',
  66. options: {
  67. refetchOnWindowFocus: false,
  68. },
  69. skipAbort: true,
  70. referrer: 'api.performance.browser.web-vitals.project',
  71. });
  72. return result;
  73. };