sessions.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import type {Client} from 'sentry/api';
  2. import {getInterval} from 'sentry/components/charts/utils';
  3. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  4. import type {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. includeSeries?: boolean;
  15. includeTotals?: boolean;
  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 doSessionsRequest = (
  27. api: Client,
  28. {
  29. field,
  30. orgSlug,
  31. cursor,
  32. environment,
  33. groupBy,
  34. interval,
  35. project,
  36. orderBy,
  37. query,
  38. includeAllArgs = false,
  39. includeSeries,
  40. includeTotals,
  41. statsPeriodStart,
  42. statsPeriodEnd,
  43. limit,
  44. ...dateTime
  45. }: DoSessionsRequestOptions
  46. ): Promise<SessionApiResponse> => {
  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. interval: interval || getInterval({start, end, period: statsPeriod}),
  58. orderBy,
  59. per_page: limit,
  60. query: query || undefined,
  61. project,
  62. start,
  63. statsPeriod,
  64. statsPeriodStart,
  65. statsPeriodEnd,
  66. includeSeries: includeSeries === false ? '0' : '1',
  67. includeTotals: includeTotals === false ? '0' : '1',
  68. }).filter(([, value]) => defined(value) && value !== '')
  69. );
  70. return api.requestPromise(`/organizations/${orgSlug}/sessions/`, {
  71. includeAllArgs,
  72. query: urlQuery,
  73. });
  74. };