issues.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {Client} from 'sentry/api';
  2. import {t} from 'sentry/locale';
  3. import GroupStore from 'sentry/stores/groupStore';
  4. import {Group, Organization, PageFilters} from 'sentry/types';
  5. import {getIssueFieldRenderer} from 'sentry/utils/dashboards/issueFieldRenderers';
  6. import {getUtcDateString} from 'sentry/utils/dates';
  7. import {TableData, TableDataRow} from 'sentry/utils/discover/discoverQuery';
  8. import {queryToObj} from 'sentry/utils/stream';
  9. import {
  10. DISCOVER_EXCLUSION_FIELDS,
  11. getSortLabel,
  12. IssueSortOptions,
  13. } from 'sentry/views/issueList/utils';
  14. import {DEFAULT_TABLE_LIMIT, DisplayType, WidgetQuery} from '../types';
  15. import {IssuesSearchBar} from '../widgetBuilder/buildSteps/filterResultsStep/issuesSearchBar';
  16. import {ISSUE_FIELD_TO_HEADER_MAP} from '../widgetBuilder/issueWidget/fields';
  17. import {
  18. generateIssueWidgetFieldOptions,
  19. ISSUE_WIDGET_SORT_OPTIONS,
  20. } from '../widgetBuilder/issueWidget/utils';
  21. import {DatasetConfig} from './base';
  22. const DEFAULT_WIDGET_QUERY: WidgetQuery = {
  23. name: '',
  24. fields: ['issue', 'assignee', 'title'] as string[],
  25. columns: ['issue', 'assignee', 'title'],
  26. fieldAliases: [],
  27. aggregates: [],
  28. conditions: '',
  29. orderby: IssueSortOptions.DATE,
  30. };
  31. const DEFAULT_SORT = IssueSortOptions.DATE;
  32. const DEFAULT_EXPAND = ['owners'];
  33. type EndpointParams = Partial<PageFilters['datetime']> & {
  34. environment: string[];
  35. project: number[];
  36. collapse?: string[];
  37. cursor?: string;
  38. expand?: string[];
  39. groupStatsPeriod?: string | null;
  40. limit?: number;
  41. page?: number | string;
  42. query?: string;
  43. sort?: string;
  44. statsPeriod?: string | null;
  45. };
  46. export const IssuesConfig: DatasetConfig<never, Group[]> = {
  47. defaultWidgetQuery: DEFAULT_WIDGET_QUERY,
  48. enableEquations: false,
  49. disableSortOptions,
  50. getTableRequest,
  51. getCustomFieldRenderer: getIssueFieldRenderer,
  52. SearchBar: IssuesSearchBar,
  53. getTableSortOptions,
  54. getTableFieldOptions: (_organization: Organization) =>
  55. generateIssueWidgetFieldOptions(),
  56. fieldHeaderMap: ISSUE_FIELD_TO_HEADER_MAP,
  57. supportedDisplayTypes: [DisplayType.TABLE],
  58. transformTable: transformIssuesResponseToTable,
  59. };
  60. function disableSortOptions(_widgetQuery: WidgetQuery) {
  61. return {
  62. disableSort: false,
  63. disableSortDirection: true,
  64. disableSortReason: t('Issues dataset does not yet support descending order'),
  65. };
  66. }
  67. function getTableSortOptions(organization: Organization, _widgetQuery: WidgetQuery) {
  68. const sortOptions = [...ISSUE_WIDGET_SORT_OPTIONS];
  69. if (organization.features.includes('issue-list-trend-sort')) {
  70. sortOptions.push(IssueSortOptions.TREND);
  71. }
  72. return sortOptions.map(sortOption => ({
  73. label: getSortLabel(sortOption),
  74. value: sortOption,
  75. }));
  76. }
  77. export function transformIssuesResponseToTable(
  78. data: Group[],
  79. widgetQuery: WidgetQuery,
  80. _organization: Organization,
  81. pageFilters: PageFilters
  82. ): TableData {
  83. GroupStore.add(data);
  84. const transformedTableResults: TableDataRow[] = [];
  85. data.forEach(
  86. ({
  87. id,
  88. shortId,
  89. title,
  90. lifetime,
  91. filtered,
  92. count,
  93. userCount,
  94. project,
  95. annotations,
  96. ...resultProps
  97. }) => {
  98. const transformedResultProps: Omit<TableDataRow, 'id'> = {};
  99. Object.keys(resultProps)
  100. .filter(key => ['number', 'string'].includes(typeof resultProps[key]))
  101. .forEach(key => {
  102. transformedResultProps[key] = resultProps[key];
  103. });
  104. const transformedTableResult: TableDataRow = {
  105. ...transformedResultProps,
  106. events: count,
  107. users: userCount,
  108. id,
  109. 'issue.id': id,
  110. issue: shortId,
  111. title,
  112. project: project.slug,
  113. links: annotations?.join(', '),
  114. };
  115. // Get lifetime stats
  116. if (lifetime) {
  117. transformedTableResult.lifetimeEvents = lifetime?.count;
  118. transformedTableResult.lifetimeUsers = lifetime?.userCount;
  119. }
  120. // Get filtered stats
  121. if (filtered) {
  122. transformedTableResult.filteredEvents = filtered?.count;
  123. transformedTableResult.filteredUsers = filtered?.userCount;
  124. }
  125. // Discover Url properties
  126. const query = widgetQuery.conditions;
  127. const queryTerms: string[] = [];
  128. if (typeof query === 'string') {
  129. const queryObj = queryToObj(query);
  130. for (const queryTag in queryObj) {
  131. if (!DISCOVER_EXCLUSION_FIELDS.includes(queryTag)) {
  132. const queryVal = queryObj[queryTag].includes(' ')
  133. ? `"${queryObj[queryTag]}"`
  134. : queryObj[queryTag];
  135. queryTerms.push(`${queryTag}:${queryVal}`);
  136. }
  137. }
  138. if (queryObj.__text) {
  139. queryTerms.push(queryObj.__text);
  140. }
  141. }
  142. transformedTableResult.discoverSearchQuery =
  143. (queryTerms.length ? ' ' : '') + queryTerms.join(' ');
  144. transformedTableResult.projectId = project.id;
  145. const {period, start, end} = pageFilters.datetime || {};
  146. if (start && end) {
  147. transformedTableResult.start = getUtcDateString(start);
  148. transformedTableResult.end = getUtcDateString(end);
  149. }
  150. transformedTableResult.period = period ?? '';
  151. transformedTableResults.push(transformedTableResult);
  152. }
  153. );
  154. return {data: transformedTableResults} as TableData;
  155. }
  156. function getTableRequest(
  157. api: Client,
  158. query: WidgetQuery,
  159. organization: Organization,
  160. pageFilters: PageFilters,
  161. limit?: number,
  162. cursor?: string
  163. ) {
  164. const groupListUrl = `/organizations/${organization.slug}/issues/`;
  165. const params: EndpointParams = {
  166. project: pageFilters.projects ?? [],
  167. environment: pageFilters.environments ?? [],
  168. query: query.conditions,
  169. sort: query.orderby || DEFAULT_SORT,
  170. expand: DEFAULT_EXPAND,
  171. limit: limit ?? DEFAULT_TABLE_LIMIT,
  172. cursor,
  173. };
  174. if (pageFilters.datetime.period) {
  175. params.statsPeriod = pageFilters.datetime.period;
  176. }
  177. if (pageFilters.datetime.end) {
  178. params.end = getUtcDateString(pageFilters.datetime.end);
  179. }
  180. if (pageFilters.datetime.start) {
  181. params.start = getUtcDateString(pageFilters.datetime.start);
  182. }
  183. if (pageFilters.datetime.utc) {
  184. params.utc = pageFilters.datetime.utc;
  185. }
  186. return api.requestPromise(groupListUrl, {
  187. includeAllArgs: true,
  188. method: 'GET',
  189. data: {
  190. ...params,
  191. },
  192. });
  193. }