useTransactionsCategorizedSamplesQuery.tsx 2.4 KB

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