useSlowMedianFastSamplesQuery.tsx 4.3 KB

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