useTransactionsCategorizedSamplesQuery.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. type Props = {
  12. browserTypes: BrowserType[];
  13. enabled: boolean;
  14. transaction: string;
  15. webVital: WebVitals | null;
  16. };
  17. export function useTransactionsCategorizedSamplesQuery({
  18. transaction,
  19. webVital,
  20. enabled,
  21. browserTypes,
  22. }: Props) {
  23. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  24. useTransactionSamplesWebVitalsScoresQuery({
  25. limit: 3,
  26. transaction: transaction ?? '',
  27. query: webVital
  28. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_P90S[webVital]}`
  29. : undefined,
  30. enabled,
  31. withProfiles: true,
  32. sortName: 'webVitalSort',
  33. webVital: webVital ?? undefined,
  34. browserTypes,
  35. });
  36. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  37. useTransactionSamplesWebVitalsScoresQuery({
  38. limit: 3,
  39. transaction: transaction ?? '',
  40. query: webVital
  41. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  42. : undefined,
  43. enabled,
  44. withProfiles: true,
  45. sortName: 'webVitalSort',
  46. webVital: webVital ?? undefined,
  47. browserTypes,
  48. });
  49. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  50. useTransactionSamplesWebVitalsScoresQuery({
  51. limit: 3,
  52. transaction: transaction ?? '',
  53. query: webVital
  54. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  55. : undefined,
  56. enabled,
  57. withProfiles: true,
  58. sortName: 'webVitalSort',
  59. webVital: webVital ?? undefined,
  60. browserTypes,
  61. });
  62. const data = [...goodData, ...mehData, ...poorData];
  63. const isTransactionWebVitalsQueryLoading =
  64. isGoodTransactionWebVitalsQueryLoading ||
  65. isMehTransactionWebVitalsQueryLoading ||
  66. isPoorTransactionWebVitalsQueryLoading;
  67. const transactionsTableData: TransactionSampleRowWithScore[] = data.sort(
  68. (a, b) => a[`${webVital}Score`] - b[`${webVital}Score`]
  69. );
  70. return {
  71. data: transactionsTableData,
  72. isLoading: isTransactionWebVitalsQueryLoading,
  73. };
  74. }