useMetricsSamples.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  2. import type {DateString, PageFilters} from 'sentry/types/core';
  3. import type {MetricAggregation, MRI} from 'sentry/types/metrics';
  4. import {useApiQuery} from 'sentry/utils/queryClient';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. /**
  8. * This type is incomplete as there are other fields available.
  9. */
  10. type FieldTypes = {
  11. id: string;
  12. 'profile.id': string | null;
  13. project: string;
  14. 'project.id': number;
  15. 'span.description': string;
  16. 'span.duration': number;
  17. 'span.op': string;
  18. 'span.self_time': number;
  19. timestamp: DateString;
  20. trace: string;
  21. transaction: string;
  22. // There are some spans where the transaction id can be null
  23. // because they're not associated to any transactions such
  24. // as the INP spans.
  25. 'transaction.id': string | null;
  26. };
  27. export type Summary = {
  28. count: number;
  29. max: number;
  30. min: number;
  31. sum: number;
  32. };
  33. type ResultFieldTypes = FieldTypes & {
  34. summary: Summary;
  35. };
  36. export type Field = keyof FieldTypes;
  37. export type ResultField = keyof ResultFieldTypes;
  38. interface UseMetricSamplesOptions<F extends Field> {
  39. fields: F[];
  40. referrer: string;
  41. aggregation?: MetricAggregation;
  42. datetime?: PageFilters['datetime'];
  43. enabled?: boolean;
  44. limit?: number;
  45. max?: number;
  46. min?: number;
  47. mri?: MRI;
  48. query?: string;
  49. sort?: string;
  50. }
  51. export interface MetricsSamplesResults<F extends Field> {
  52. data: Pick<ResultFieldTypes, F | 'summary'>[];
  53. meta: any; // not going to type this yet
  54. }
  55. export function useMetricsSamples<F extends Field>({
  56. datetime,
  57. enabled,
  58. fields,
  59. limit,
  60. max,
  61. min,
  62. mri,
  63. aggregation,
  64. referrer,
  65. query,
  66. sort,
  67. }: UseMetricSamplesOptions<F>) {
  68. const organization = useOrganization();
  69. const {selection} = usePageFilters();
  70. const path = `/organizations/${organization.slug}/metrics/samples/`;
  71. const endpointOptions = {
  72. query: {
  73. project: selection.projects,
  74. environment: selection.environments,
  75. ...(datetime ?? normalizeDateTimeParams(selection.datetime)),
  76. field: fields,
  77. max,
  78. min,
  79. mri,
  80. operation: aggregation,
  81. query,
  82. referrer,
  83. per_page: limit,
  84. sort,
  85. },
  86. };
  87. return useApiQuery<MetricsSamplesResults<F>>([path, endpointOptions], {
  88. staleTime: 0,
  89. refetchOnWindowFocus: false,
  90. retry: false,
  91. enabled,
  92. });
  93. }
  94. export function getSummaryValueForAggregation(
  95. summary: Summary,
  96. aggregation?: MetricAggregation
  97. ) {
  98. switch (aggregation) {
  99. case 'count':
  100. return summary.count;
  101. case 'min':
  102. return summary.min;
  103. case 'max':
  104. return summary.max;
  105. case 'sum':
  106. return summary.sum;
  107. case 'avg':
  108. default:
  109. return summary.sum / summary.count;
  110. }
  111. }