metrics.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {Client, ResponseMeta} from 'sentry/api';
  2. import {getInterval} from 'sentry/components/charts/utils';
  3. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  4. import {DateString, MetricsApiResponse, Organization} from 'sentry/types';
  5. import {defined} from 'sentry/utils';
  6. export type DoMetricsRequestOptions = {
  7. field: string[];
  8. orgSlug: Organization['slug'];
  9. cursor?: string;
  10. end?: DateString;
  11. environment?: Readonly<string[]>;
  12. groupBy?: string[];
  13. includeAllArgs?: boolean;
  14. includeSeries?: number;
  15. includeTotals?: number;
  16. interval?: string;
  17. limit?: number;
  18. orderBy?: string;
  19. project?: Readonly<number[]>;
  20. query?: string;
  21. start?: DateString;
  22. statsPeriod?: string | null;
  23. statsPeriodEnd?: string;
  24. statsPeriodStart?: string;
  25. };
  26. export const doMetricsRequest = (
  27. api: Client,
  28. {
  29. field,
  30. orgSlug,
  31. cursor,
  32. environment,
  33. groupBy,
  34. includeSeries,
  35. includeTotals,
  36. interval,
  37. limit,
  38. orderBy,
  39. project,
  40. query,
  41. includeAllArgs = false,
  42. statsPeriodStart,
  43. statsPeriodEnd,
  44. ...dateTime
  45. }: DoMetricsRequestOptions
  46. ): Promise<MetricsApiResponse | [MetricsApiResponse, string, ResponseMeta]> => {
  47. const {start, end, statsPeriod} = normalizeDateTimeParams(dateTime, {
  48. allowEmptyPeriod: true,
  49. });
  50. const urlQuery = Object.fromEntries(
  51. Object.entries({
  52. field: field.filter(f => !!f),
  53. cursor,
  54. end,
  55. environment,
  56. groupBy: groupBy?.filter(g => !!g),
  57. includeSeries,
  58. includeTotals,
  59. interval: interval || getInterval({start, end, period: statsPeriod}),
  60. query: query || undefined,
  61. per_page: limit,
  62. project,
  63. orderBy,
  64. start,
  65. statsPeriod,
  66. statsPeriodStart,
  67. statsPeriodEnd,
  68. }).filter(([, value]) => defined(value) && value !== '')
  69. );
  70. const pathname = `/organizations/${orgSlug}/metrics/data/`;
  71. return api.requestPromise(pathname, {includeAllArgs, query: urlQuery});
  72. };