sessions.tsx 2.0 KB

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