useProjectRawWebVitalsQuery.tsx 2.6 KB

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