useTransactionsCategorizedSamplesQuery.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {useTransactionSamplesWebVitalsScoresQuery} from 'sentry/views/insights/browser/webVitals/queries/storedScoreQueries/useTransactionSamplesWebVitalsScoresQuery';
  2. import type {
  3. TransactionSampleRowWithScore,
  4. WebVitals,
  5. } from 'sentry/views/insights/browser/webVitals/types';
  6. import type {BrowserType} from 'sentry/views/insights/browser/webVitals/utils/queryParameterDecoders/browserType';
  7. import {
  8. PERFORMANCE_SCORE_MEDIANS,
  9. PERFORMANCE_SCORE_P90S,
  10. } from 'sentry/views/insights/browser/webVitals/utils/scoreThresholds';
  11. import type {SubregionCode} from 'sentry/views/insights/types';
  12. type Props = {
  13. browserTypes: BrowserType[];
  14. enabled: boolean;
  15. subregions: SubregionCode[];
  16. transaction: string;
  17. webVital: WebVitals | null;
  18. };
  19. export function useTransactionsCategorizedSamplesQuery({
  20. transaction,
  21. webVital,
  22. enabled,
  23. browserTypes,
  24. subregions,
  25. }: Props) {
  26. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  27. useTransactionSamplesWebVitalsScoresQuery({
  28. limit: 3,
  29. transaction: transaction ?? '',
  30. query: webVital
  31. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_P90S[webVital]}`
  32. : undefined,
  33. enabled,
  34. withProfiles: true,
  35. sortName: 'webVitalSort',
  36. webVital: webVital ?? undefined,
  37. browserTypes,
  38. subregions,
  39. });
  40. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  41. useTransactionSamplesWebVitalsScoresQuery({
  42. limit: 3,
  43. transaction: transaction ?? '',
  44. query: webVital
  45. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  46. : undefined,
  47. enabled,
  48. withProfiles: true,
  49. sortName: 'webVitalSort',
  50. webVital: webVital ?? undefined,
  51. browserTypes,
  52. subregions,
  53. });
  54. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  55. useTransactionSamplesWebVitalsScoresQuery({
  56. limit: 3,
  57. transaction: transaction ?? '',
  58. query: webVital
  59. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  60. : undefined,
  61. enabled,
  62. withProfiles: true,
  63. sortName: 'webVitalSort',
  64. webVital: webVital ?? undefined,
  65. browserTypes,
  66. subregions,
  67. });
  68. const data = [...goodData, ...mehData, ...poorData];
  69. const isTransactionWebVitalsQueryLoading =
  70. isGoodTransactionWebVitalsQueryLoading ||
  71. isMehTransactionWebVitalsQueryLoading ||
  72. isPoorTransactionWebVitalsQueryLoading;
  73. const transactionsTableData: TransactionSampleRowWithScore[] = data.sort(
  74. (a, b) => a[`${webVital}Score`] - b[`${webVital}Score`]
  75. );
  76. return {
  77. data: transactionsTableData,
  78. isLoading: isTransactionWebVitalsQueryLoading,
  79. };
  80. }