useGoodMehAndBadTransactionsSamplesQuery.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {
  2. PERFORMANCE_SCORE_MEDIANS,
  3. PERFORMANCE_SCORE_P90S,
  4. } from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  5. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionSamplesWebVitalsQuery';
  6. import type {
  7. TransactionSampleRowWithScore,
  8. WebVitals,
  9. } from 'sentry/views/performance/browser/webVitals/utils/types';
  10. type Props = {
  11. enabled: boolean;
  12. transaction: string;
  13. webVital: WebVitals | null;
  14. };
  15. export function useGoodMehAndBadTransactionsSamplesQuery({
  16. transaction,
  17. webVital,
  18. enabled,
  19. }: Props) {
  20. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  21. useTransactionSamplesWebVitalsQuery({
  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. useTransactionSamplesWebVitalsQuery({
  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. useTransactionSamplesWebVitalsQuery({
  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. }