useSlowMedianFastSamplesQuery.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {QueryFieldValue} from 'sentry/utils/discover/fields';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. const LIMIT_PER_POPULATION = 2;
  8. /**
  9. * This hook will fetch transaction events from 3 different types of populations and combine them in one set, then return them:
  10. *
  11. * - Slowest Events
  12. * - Median / Baseline Events
  13. * - Fastest Events
  14. *
  15. * It assumes that you are passing an eventView object with a query scoped to a specific transaction
  16. *
  17. * @param eventView An eventView containing query information, such as the transaction and other filters
  18. */
  19. export default function useSlowMedianFastSamplesQuery(eventView: EventView) {
  20. const location = useLocation();
  21. const organization = useOrganization();
  22. const commonColumns: QueryFieldValue[] = [
  23. {
  24. field: 'transaction.duration',
  25. kind: 'field',
  26. },
  27. {
  28. field: 'profile_id',
  29. kind: 'field',
  30. },
  31. {
  32. field: 'timestamp',
  33. kind: 'field',
  34. },
  35. ];
  36. const eventViewAggregates = eventView.clone().withColumns([
  37. {kind: 'function', function: ['p50', 'transaction.duration', undefined, undefined]},
  38. {kind: 'function', function: ['p95', 'transaction.duration', undefined, undefined]},
  39. ]);
  40. const {isLoading: isLoadingAgg, data: aggregatesData} = useDiscoverQuery({
  41. eventView: eventViewAggregates,
  42. referrer: 'starfish-transaction-summary-sample-events',
  43. location,
  44. orgSlug: organization.slug,
  45. });
  46. const slowestSamplesEventView = eventView
  47. .clone()
  48. .withColumns(commonColumns)
  49. .withSorts([
  50. {
  51. field: 'transaction.duration',
  52. kind: 'desc',
  53. },
  54. ]);
  55. slowestSamplesEventView.additionalConditions = new MutableSearch(
  56. `transaction.duration:>${
  57. aggregatesData?.data?.[0]?.['p95(transaction.duration)'] ?? 0
  58. }`
  59. );
  60. const {isLoading: isLoadingSlowest, data: slowestSamplesData} = useDiscoverQuery({
  61. eventView: slowestSamplesEventView,
  62. referrer: 'starfish-transaction-summary-sample-events',
  63. location,
  64. orgSlug: organization.slug,
  65. limit: LIMIT_PER_POPULATION,
  66. });
  67. const medianSamplesEventView = eventView
  68. .clone()
  69. .withColumns(commonColumns)
  70. .withSorts([
  71. {
  72. field: 'transaction.duration',
  73. kind: 'desc',
  74. },
  75. ]);
  76. medianSamplesEventView.additionalConditions = new MutableSearch(
  77. `transaction.duration:<=${
  78. aggregatesData?.data?.[0]?.['p50(transaction.duration)'] ?? 0
  79. }`
  80. );
  81. const {isLoading: isLoadingMedian, data: medianSamplesData} = useDiscoverQuery({
  82. eventView: medianSamplesEventView,
  83. referrer: 'starfish-transaction-summary-sample-events',
  84. location,
  85. orgSlug: organization.slug,
  86. limit: LIMIT_PER_POPULATION,
  87. });
  88. const fastestSamplesEventView = eventView
  89. .clone()
  90. .withColumns(commonColumns)
  91. .withSorts([
  92. {
  93. field: 'transaction.duration',
  94. kind: 'asc',
  95. },
  96. ]);
  97. fastestSamplesEventView.additionalConditions = new MutableSearch(
  98. `transaction.duration:<=${
  99. aggregatesData?.data?.[0]?.['p50(transaction.duration)'] ?? 0
  100. }`
  101. );
  102. const {isLoading: isLoadingFastest, data: fastestSamplesData} = useDiscoverQuery({
  103. eventView: fastestSamplesEventView,
  104. referrer: 'starfish-transaction-summary-sample-events',
  105. location,
  106. orgSlug: organization.slug,
  107. limit: LIMIT_PER_POPULATION,
  108. });
  109. if (isLoadingAgg || isLoadingSlowest || isLoadingMedian || isLoadingFastest) {
  110. return {isLoading: true, data: []};
  111. }
  112. const combinedData = [
  113. ...(slowestSamplesData?.data ?? []),
  114. ...(medianSamplesData?.data ?? []),
  115. ...(fastestSamplesData?.data ?? []),
  116. ];
  117. return {
  118. isLoading: false,
  119. data: combinedData,
  120. aggregatesData: aggregatesData?.data[0] ?? [],
  121. };
  122. }