metrics.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import {getDateTimeParams, getMetricsInterval, parseMRI} from 'sentry/utils/metrics';
  7. export type DoReleaseHealthRequestOptions = {
  8. field: string[];
  9. orgSlug: Organization['slug'];
  10. cursor?: string;
  11. end?: DateString;
  12. environment?: Readonly<string[]>;
  13. groupBy?: string[];
  14. includeAllArgs?: boolean;
  15. includeSeries?: number;
  16. includeTotals?: number;
  17. interval?: string;
  18. limit?: number;
  19. orderBy?: string;
  20. project?: Readonly<number[]>;
  21. query?: string;
  22. start?: DateString;
  23. statsPeriod?: string | null;
  24. statsPeriodEnd?: string;
  25. statsPeriodStart?: string;
  26. };
  27. export const doReleaseHealthRequest = (
  28. api: Client,
  29. {
  30. field,
  31. orgSlug,
  32. cursor,
  33. environment,
  34. groupBy,
  35. includeSeries,
  36. includeTotals,
  37. interval,
  38. limit,
  39. orderBy,
  40. project,
  41. query,
  42. includeAllArgs = false,
  43. statsPeriodStart,
  44. statsPeriodEnd,
  45. ...dateTime
  46. }: DoReleaseHealthRequestOptions
  47. ): Promise<MetricsApiResponse | [MetricsApiResponse, string, ResponseMeta]> => {
  48. const {start, end, statsPeriod} = normalizeDateTimeParams(dateTime, {
  49. allowEmptyPeriod: true,
  50. });
  51. const urlQuery = Object.fromEntries(
  52. Object.entries({
  53. field: field.filter(f => !!f),
  54. cursor,
  55. end,
  56. environment,
  57. groupBy: groupBy?.filter(g => !!g),
  58. includeSeries,
  59. includeTotals,
  60. interval: interval || getInterval({start, end, period: statsPeriod}),
  61. query: query || undefined,
  62. per_page: limit,
  63. project,
  64. orderBy,
  65. start,
  66. statsPeriod,
  67. statsPeriodStart,
  68. statsPeriodEnd,
  69. }).filter(([, value]) => defined(value) && value !== '')
  70. );
  71. const pathname = `/organizations/${orgSlug}/metrics/data/`;
  72. return api.requestPromise(pathname, {includeAllArgs, query: urlQuery});
  73. };
  74. export type DoMetricsRequestOptions = {
  75. field: string[];
  76. orgSlug: Organization['slug'];
  77. cursor?: string;
  78. end?: DateString;
  79. environment?: Readonly<string[]>;
  80. groupBy?: string[];
  81. includeAllArgs?: boolean;
  82. includeSeries?: number;
  83. includeTotals?: number;
  84. interval?: string;
  85. limit?: number;
  86. orderBy?: string;
  87. project?: Readonly<number[]>;
  88. query?: string;
  89. start?: DateString;
  90. statsPeriod?: string | null;
  91. statsPeriodEnd?: string;
  92. statsPeriodStart?: string;
  93. };
  94. export const doMetricsRequest = (
  95. api: Client,
  96. {
  97. field,
  98. orgSlug,
  99. environment,
  100. groupBy,
  101. interval,
  102. limit,
  103. project,
  104. query,
  105. includeAllArgs = false,
  106. ...dateTime
  107. }: DoReleaseHealthRequestOptions
  108. ): Promise<MetricsApiResponse | [MetricsApiResponse, string, ResponseMeta]> => {
  109. const {useCase} = parseMRI(field[0]) ?? {useCase: 'custom'};
  110. const {start, end, statsPeriod, utc} = normalizeDateTimeParams(dateTime, {
  111. allowEmptyPeriod: true,
  112. allowAbsoluteDatetime: true,
  113. allowAbsolutePageDatetime: true,
  114. });
  115. // @ts-expect-error
  116. const datetime = getDateTimeParams({start, end, period: statsPeriod, utc});
  117. const metricsInterval = getMetricsInterval(datetime, useCase);
  118. const urlQuery = {
  119. ...datetime,
  120. query,
  121. project,
  122. environment,
  123. field,
  124. useCase,
  125. interval: interval || metricsInterval,
  126. groupBy,
  127. per_page: limit,
  128. useNewMetricsLayer: false,
  129. };
  130. const pathname = `/organizations/${orgSlug}/metrics/data/`;
  131. return api.requestPromise(pathname, {includeAllArgs, query: urlQuery});
  132. };