useProjectWebVitalsTimeseriesQuery.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {getInterval} from 'sentry/components/charts/utils';
  2. import {SeriesDataUnit} from 'sentry/types/echarts';
  3. import EventView, {MetaType} from 'sentry/utils/discover/eventView';
  4. import {
  5. DiscoverQueryProps,
  6. useGenericDiscoverQuery,
  7. } from 'sentry/utils/discover/genericDiscoverQuery';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {calculatePerformanceScore} from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
  12. export const useProjectWebVitalsTimeseriesQuery = () => {
  13. const pageFilters = usePageFilters();
  14. const location = useLocation();
  15. const organization = useOrganization();
  16. const projectTimeSeriesEventView = EventView.fromNewQueryWithPageFilters(
  17. {
  18. yAxis: [
  19. 'p75(measurements.lcp)',
  20. 'p75(measurements.fcp)',
  21. 'p75(measurements.cls)',
  22. 'p75(measurements.ttfb)',
  23. 'p75(measurements.fid)',
  24. ],
  25. name: 'Web Vitals',
  26. query:
  27. 'transaction.op:pageload (transaction:/performance* or transaction:/discover* or transaction:/dashboards*)',
  28. version: 2,
  29. fields: [],
  30. interval: getInterval(pageFilters.selection.datetime, 'low'),
  31. },
  32. pageFilters.selection
  33. );
  34. const result = useGenericDiscoverQuery<
  35. {
  36. data: any[];
  37. meta: MetaType;
  38. },
  39. DiscoverQueryProps
  40. >({
  41. route: 'events-stats',
  42. eventView: projectTimeSeriesEventView,
  43. location,
  44. orgSlug: organization.slug,
  45. getRequestPayload: () => ({
  46. ...projectTimeSeriesEventView.getEventsAPIPayload(location),
  47. yAxis: projectTimeSeriesEventView.yAxis,
  48. topEvents: projectTimeSeriesEventView.topEvents,
  49. excludeOther: 0,
  50. partial: 1,
  51. orderby: undefined,
  52. interval: projectTimeSeriesEventView.interval,
  53. }),
  54. options: {
  55. enabled: pageFilters.isReady,
  56. refetchOnWindowFocus: false,
  57. },
  58. });
  59. const data: {
  60. cls: SeriesDataUnit[];
  61. fcp: SeriesDataUnit[];
  62. fid: SeriesDataUnit[];
  63. lcp: SeriesDataUnit[];
  64. total: SeriesDataUnit[];
  65. ttfb: SeriesDataUnit[];
  66. } = {
  67. lcp: [],
  68. fcp: [],
  69. cls: [],
  70. ttfb: [],
  71. fid: [],
  72. total: [],
  73. };
  74. result?.data?.['p75(measurements.lcp)'].data.forEach((interval, index) => {
  75. const {totalScore, lcpScore, fcpScore, fidScore, clsScore, ttfbScore} =
  76. calculatePerformanceScore({
  77. lcp: result?.data?.['p75(measurements.lcp)'].data[index][1][0].count,
  78. fcp: result?.data?.['p75(measurements.fcp)'].data[index][1][0].count,
  79. cls: result?.data?.['p75(measurements.cls)'].data[index][1][0].count,
  80. ttfb: result?.data?.['p75(measurements.ttfb)'].data[index][1][0].count,
  81. fid: result?.data?.['p75(measurements.fid)'].data[index][1][0].count,
  82. });
  83. data.total.push({
  84. value: totalScore,
  85. name: interval[0] * 1000,
  86. });
  87. data.cls.push({
  88. value: clsScore,
  89. name: interval[0] * 1000,
  90. });
  91. data.lcp.push({
  92. value: lcpScore,
  93. name: interval[0] * 1000,
  94. });
  95. data.fcp.push({
  96. value: fcpScore,
  97. name: interval[0] * 1000,
  98. });
  99. data.ttfb.push({
  100. value: ttfbScore,
  101. name: interval[0] * 1000,
  102. });
  103. data.fid.push({
  104. value: fidScore,
  105. name: interval[0] * 1000,
  106. });
  107. });
  108. return {data, isLoading: result.isLoading};
  109. };