useProjectRawWebVitalsValuesTimeseriesQuery.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {getInterval} from 'sentry/components/charts/utils';
  2. import type {PageFilters} from 'sentry/types/core';
  3. import type {SeriesDataUnit} from 'sentry/types/echarts';
  4. import type {MetaType} from 'sentry/utils/discover/eventView';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import type {DiscoverQueryProps} from 'sentry/utils/discover/genericDiscoverQuery';
  7. import {useGenericDiscoverQuery} from 'sentry/utils/discover/genericDiscoverQuery';
  8. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  9. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  10. import {useLocation} from 'sentry/utils/useLocation';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import usePageFilters from 'sentry/utils/usePageFilters';
  13. import {DEFAULT_QUERY_FILTER} from 'sentry/views/insights/browser/webVitals/settings';
  14. import type {BrowserType} from 'sentry/views/insights/browser/webVitals/utils/queryParameterDecoders/browserType';
  15. import {SpanIndexedField} from 'sentry/views/insights/types';
  16. type Props = {
  17. browserTypes?: BrowserType[];
  18. datetime?: PageFilters['datetime'];
  19. transaction?: string | null;
  20. };
  21. export const useProjectRawWebVitalsValuesTimeseriesQuery = ({
  22. transaction,
  23. datetime,
  24. browserTypes,
  25. }: Props) => {
  26. const pageFilters = usePageFilters();
  27. const location = useLocation();
  28. const organization = useOrganization();
  29. const search = new MutableSearch([]);
  30. if (transaction) {
  31. search.addFilterValue('transaction', transaction);
  32. }
  33. if (browserTypes) {
  34. search.addDisjunctionFilterValues(SpanIndexedField.BROWSER_NAME, browserTypes);
  35. }
  36. const projectTimeSeriesEventView = EventView.fromNewQueryWithPageFilters(
  37. {
  38. yAxis: [
  39. 'p75(measurements.lcp)',
  40. 'p75(measurements.fcp)',
  41. 'p75(measurements.cls)',
  42. 'p75(measurements.ttfb)',
  43. 'p75(measurements.inp)',
  44. 'count()',
  45. 'count_scores(measurements.score.inp)',
  46. ],
  47. name: 'Web Vitals',
  48. query: [DEFAULT_QUERY_FILTER, search.formatString()].join(' ').trim(),
  49. version: 2,
  50. fields: [],
  51. interval: getInterval(pageFilters.selection.datetime, 'low'),
  52. dataset: DiscoverDatasets.METRICS,
  53. },
  54. {
  55. ...pageFilters.selection,
  56. datetime: datetime ?? pageFilters.selection.datetime,
  57. }
  58. );
  59. const result = useGenericDiscoverQuery<
  60. {
  61. data: any[];
  62. meta: MetaType;
  63. },
  64. DiscoverQueryProps
  65. >({
  66. route: 'events-stats',
  67. eventView: projectTimeSeriesEventView,
  68. location,
  69. orgSlug: organization.slug,
  70. getRequestPayload: () => ({
  71. ...projectTimeSeriesEventView.getEventsAPIPayload(location),
  72. yAxis: projectTimeSeriesEventView.yAxis,
  73. topEvents: projectTimeSeriesEventView.topEvents,
  74. excludeOther: 0,
  75. partial: 1,
  76. orderby: undefined,
  77. interval: projectTimeSeriesEventView.interval,
  78. }),
  79. options: {
  80. refetchOnWindowFocus: false,
  81. },
  82. referrer: 'api.performance.browser.web-vitals.timeseries',
  83. });
  84. const data: {
  85. cls: SeriesDataUnit[];
  86. count: SeriesDataUnit[];
  87. countInp: SeriesDataUnit[];
  88. fcp: SeriesDataUnit[];
  89. inp: SeriesDataUnit[];
  90. lcp: SeriesDataUnit[];
  91. ttfb: SeriesDataUnit[];
  92. } = {
  93. lcp: [],
  94. fcp: [],
  95. cls: [],
  96. ttfb: [],
  97. inp: [],
  98. count: [],
  99. countInp: [],
  100. };
  101. result?.data?.['p75(measurements.lcp)']?.data.forEach((interval, index) => {
  102. const map: {key: string; series: SeriesDataUnit[]}[] = [
  103. {key: 'p75(measurements.cls)', series: data.cls},
  104. {key: 'p75(measurements.lcp)', series: data.lcp},
  105. {key: 'p75(measurements.fcp)', series: data.fcp},
  106. {key: 'p75(measurements.ttfb)', series: data.ttfb},
  107. {key: 'p75(measurements.inp)', series: data.inp},
  108. {key: 'count()', series: data.count},
  109. {key: 'count_scores(measurements.score.inp)', series: data.countInp},
  110. ];
  111. map.forEach(({key, series}) => {
  112. if (result?.data?.[key].data[index][1][0].count !== null) {
  113. series.push({
  114. value: result?.data?.[key].data[index][1][0].count,
  115. name: interval[0] * 1000,
  116. });
  117. }
  118. });
  119. });
  120. return {data, isLoading: result.isLoading};
  121. };