sessions.tsx 1.7 KB

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