useSpanSamples.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import moment from 'moment';
  2. import * as qs from 'query-string';
  3. import {useQuery} from 'sentry/utils/queryClient';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import useApi from 'sentry/utils/useApi';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {computeAxisMax} from 'sentry/views/starfish/components/chart';
  10. import {SpanSummaryQueryFilters} from 'sentry/views/starfish/queries/useSpanMetrics';
  11. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  12. import {SpanIndexedField, SpanIndexedFieldTypes} from 'sentry/views/starfish/types';
  13. import {getDateConditions} from 'sentry/views/starfish/utils/getDateConditions';
  14. import {DATE_FORMAT} from 'sentry/views/starfish/utils/useSpansQuery';
  15. const {SPAN_SELF_TIME, SPAN_GROUP} = SpanIndexedField;
  16. type Options = {
  17. groupId: string;
  18. transactionName: string;
  19. query?: string[];
  20. release?: string;
  21. transactionMethod?: string;
  22. };
  23. export type SpanSample = Pick<
  24. SpanIndexedFieldTypes,
  25. | SpanIndexedField.SPAN_SELF_TIME
  26. | SpanIndexedField.TRANSACTION_ID
  27. | SpanIndexedField.PROJECT
  28. | SpanIndexedField.TIMESTAMP
  29. | SpanIndexedField.ID
  30. | SpanIndexedField.PROFILE_ID
  31. >;
  32. export const useSpanSamples = (options: Options) => {
  33. const organization = useOrganization();
  34. const url = `/api/0/organizations/${organization.slug}/spans-samples/`;
  35. const api = useApi();
  36. const pageFilter = usePageFilters();
  37. const {
  38. groupId,
  39. transactionName,
  40. transactionMethod,
  41. release,
  42. query: extraQuery = [],
  43. } = options;
  44. const location = useLocation();
  45. const query = new MutableSearch([
  46. `${SPAN_GROUP}:${groupId}`,
  47. `transaction:"${transactionName}"`,
  48. ...extraQuery,
  49. ]);
  50. const filters: SpanSummaryQueryFilters = {
  51. transactionName,
  52. };
  53. if (transactionMethod) {
  54. query.addFilterValue('transaction.method', transactionMethod);
  55. filters['transaction.method'] = transactionMethod;
  56. }
  57. if (release) {
  58. query.addFilterValue('release', release);
  59. filters.release = release;
  60. }
  61. const dateCondtions = getDateConditions(pageFilter.selection);
  62. const {isLoading: isLoadingSeries, data: spanMetricsSeriesData} = useSpanMetricsSeries(
  63. groupId,
  64. filters,
  65. [`avg(${SPAN_SELF_TIME})`],
  66. 'api.starfish.sidebar-span-metrics'
  67. );
  68. const maxYValue = computeAxisMax([spanMetricsSeriesData?.[`avg(${SPAN_SELF_TIME})`]]);
  69. const enabled = Boolean(
  70. groupId && transactionName && !isLoadingSeries && pageFilter.isReady
  71. );
  72. const queryString = query.formatString();
  73. const result = useQuery<SpanSample[]>({
  74. queryKey: [
  75. 'span-samples',
  76. groupId,
  77. transactionName,
  78. dateCondtions.statsPeriod,
  79. dateCondtions.start,
  80. dateCondtions.end,
  81. queryString,
  82. ],
  83. queryFn: async () => {
  84. const {data} = await api.requestPromise(
  85. `${url}?${qs.stringify({
  86. ...dateCondtions,
  87. ...{utc: location.query.utc},
  88. lowerBound: 0,
  89. firstBound: maxYValue * (1 / 3),
  90. secondBound: maxYValue * (2 / 3),
  91. upperBound: maxYValue,
  92. project: pageFilter.selection.projects,
  93. query: queryString,
  94. })}`
  95. );
  96. return data
  97. ?.map((d: SpanSample) => ({
  98. ...d,
  99. timestamp: moment(d.timestamp).format(DATE_FORMAT),
  100. }))
  101. .sort((a: SpanSample, b: SpanSample) => b[SPAN_SELF_TIME] - a[SPAN_SELF_TIME]);
  102. },
  103. refetchOnWindowFocus: false,
  104. enabled,
  105. initialData: [],
  106. });
  107. return {...result, isEnabled: enabled};
  108. };