useMetricsSamples.tsx 2.6 KB

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