useSlowMedianFastSamplesQuery.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. field: 'spans.browser',
  37. kind: 'field',
  38. },
  39. {
  40. field: 'spans.db',
  41. kind: 'field',
  42. },
  43. {
  44. field: 'spans.http',
  45. kind: 'field',
  46. },
  47. {
  48. field: 'spans.resource',
  49. kind: 'field',
  50. },
  51. {
  52. field: 'spans.ui',
  53. kind: 'field',
  54. },
  55. ];
  56. const eventViewAggregates = eventView.clone().withColumns([
  57. {kind: 'function', function: ['p50', 'transaction.duration', undefined, undefined]},
  58. {kind: 'function', function: ['p95', 'transaction.duration', undefined, undefined]},
  59. ]);
  60. const {isLoading: isLoadingAgg, data: aggregatesData} = useDiscoverQuery({
  61. eventView: eventViewAggregates,
  62. referrer: 'starfish-transaction-summary-sample-events',
  63. location,
  64. orgSlug: organization.slug,
  65. });
  66. const slowestSamplesEventView = eventView
  67. .clone()
  68. .withColumns(commonColumns)
  69. .withSorts([
  70. {
  71. field: 'transaction.duration',
  72. kind: 'desc',
  73. },
  74. ]);
  75. slowestSamplesEventView.additionalConditions = new MutableSearch(
  76. `transaction.duration:>${
  77. aggregatesData?.data?.[0]?.['p95(transaction.duration)'] ?? 0
  78. }`
  79. );
  80. const {isLoading: isLoadingSlowest, data: slowestSamplesData} = useDiscoverQuery({
  81. eventView: slowestSamplesEventView,
  82. referrer: 'starfish-transaction-summary-sample-events',
  83. location,
  84. orgSlug: organization.slug,
  85. limit: LIMIT_PER_POPULATION,
  86. });
  87. const medianSamplesEventView = eventView
  88. .clone()
  89. .withColumns(commonColumns)
  90. .withSorts([
  91. {
  92. field: 'transaction.duration',
  93. kind: 'desc',
  94. },
  95. ]);
  96. medianSamplesEventView.additionalConditions = new MutableSearch(
  97. `transaction.duration:<=${
  98. aggregatesData?.data?.[0]?.['p50(transaction.duration)'] ?? 0
  99. }`
  100. );
  101. const {isLoading: isLoadingMedian, data: medianSamplesData} = useDiscoverQuery({
  102. eventView: medianSamplesEventView,
  103. referrer: 'starfish-transaction-summary-sample-events',
  104. location,
  105. orgSlug: organization.slug,
  106. limit: LIMIT_PER_POPULATION,
  107. });
  108. const fastestSamplesEventView = eventView
  109. .clone()
  110. .withColumns(commonColumns)
  111. .withSorts([
  112. {
  113. field: 'transaction.duration',
  114. kind: 'asc',
  115. },
  116. ]);
  117. fastestSamplesEventView.additionalConditions = new MutableSearch(
  118. `transaction.duration:<=${
  119. aggregatesData?.data?.[0]?.['p50(transaction.duration)'] ?? 0
  120. }`
  121. );
  122. const {isLoading: isLoadingFastest, data: fastestSamplesData} = useDiscoverQuery({
  123. eventView: fastestSamplesEventView,
  124. referrer: 'starfish-transaction-summary-sample-events',
  125. location,
  126. orgSlug: organization.slug,
  127. limit: LIMIT_PER_POPULATION,
  128. });
  129. if (isLoadingAgg || isLoadingSlowest || isLoadingMedian || isLoadingFastest) {
  130. return {isLoading: true, data: []};
  131. }
  132. const combinedData = [
  133. ...(slowestSamplesData?.data ?? []),
  134. ...(medianSamplesData?.data ?? []),
  135. ...(fastestSamplesData?.data ?? []),
  136. ];
  137. return {
  138. isLoading: false,
  139. data: combinedData,
  140. aggregatesData: aggregatesData?.data[0] ?? [],
  141. };
  142. }