useProjectRawWebVitalsValuesTimeseriesQuery.tsx 3.8 KB

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