tags.tsx 4.1 KB

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