tags.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {Query} from 'history';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {Client} from 'sentry/api';
  4. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  5. import {t} from 'sentry/locale';
  6. import AlertStore from 'sentry/stores/alertStore';
  7. import TagStore from 'sentry/stores/tagStore';
  8. import {PageFilters, Tag} from 'sentry/types';
  9. const MAX_TAGS = 1000;
  10. function tagFetchSuccess(tags: Tag[] | undefined) {
  11. // We occasionally get undefined passed in when APIs are having a bad time.
  12. tags = tags || [];
  13. const trimmedTags = tags.slice(0, MAX_TAGS);
  14. if (tags.length > MAX_TAGS) {
  15. AlertStore.addAlert({
  16. message: t('You have too many unique tags and some have been truncated'),
  17. type: 'warning',
  18. });
  19. }
  20. TagStore.loadTagsSuccess(trimmedTags);
  21. }
  22. /**
  23. * Load an organization's tags based on a global selection value.
  24. */
  25. export function loadOrganizationTags(
  26. api: Client,
  27. orgSlug: string,
  28. selection: PageFilters
  29. ): Promise<void> {
  30. TagStore.reset();
  31. const query: Query = selection.datetime
  32. ? {...normalizeDateTimeParams(selection.datetime)}
  33. : {};
  34. query.use_cache = '1';
  35. if (selection.projects) {
  36. query.project = selection.projects.map(String);
  37. }
  38. return api
  39. .requestPromise(`/organizations/${orgSlug}/tags/`, {
  40. method: 'GET',
  41. query,
  42. })
  43. .then(tagFetchSuccess)
  44. .catch(() => {
  45. addErrorMessage(t('Unable to load tags'));
  46. });
  47. }
  48. /**
  49. * Fetch tags for an organization or a subset or projects.
  50. */
  51. export function fetchOrganizationTags(
  52. api: Client,
  53. orgId: string,
  54. projectIds: string[] | null = null
  55. ) {
  56. TagStore.reset();
  57. const url = `/organizations/${orgId}/tags/`;
  58. const query: Query = {use_cache: '1'};
  59. if (projectIds) {
  60. query.project = projectIds;
  61. }
  62. const promise = api.requestPromise(url, {
  63. method: 'GET',
  64. query,
  65. });
  66. promise.then(tagFetchSuccess);
  67. return promise;
  68. }
  69. /**
  70. * Fetch tag values for an organization.
  71. * The `projectIds` argument can be used to subset projects.
  72. */
  73. export function fetchTagValues({
  74. api,
  75. orgSlug,
  76. tagKey,
  77. endpointParams,
  78. includeReplays,
  79. includeSessions,
  80. includeTransactions,
  81. projectIds,
  82. search,
  83. sort,
  84. }: {
  85. api: Client;
  86. orgSlug: string;
  87. tagKey: string;
  88. endpointParams?: Query;
  89. includeReplays?: boolean;
  90. includeSessions?: boolean;
  91. includeTransactions?: boolean;
  92. projectIds?: string[];
  93. search?: string;
  94. sort?: string;
  95. }) {
  96. const url = `/organizations/${orgSlug}/tags/${tagKey}/values/`;
  97. const query: Query = {};
  98. if (search) {
  99. query.query = search;
  100. }
  101. if (projectIds) {
  102. query.project = projectIds;
  103. }
  104. if (endpointParams) {
  105. if (endpointParams.start) {
  106. query.start = endpointParams.start;
  107. }
  108. if (endpointParams.end) {
  109. query.end = endpointParams.end;
  110. }
  111. if (endpointParams.statsPeriod) {
  112. query.statsPeriod = endpointParams.statsPeriod;
  113. }
  114. }
  115. if (includeTransactions) {
  116. query.includeTransactions = '1';
  117. }
  118. if (includeSessions) {
  119. query.includeSessions = '1';
  120. }
  121. if (includeReplays) {
  122. query.includeReplays = '1';
  123. }
  124. if (sort) {
  125. query.sort = sort;
  126. }
  127. return api.requestPromise(url, {
  128. method: 'GET',
  129. query,
  130. });
  131. }