issues.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {Client} from 'sentry/api';
  2. import {joinQuery, parseSearch, Token} from 'sentry/components/searchSyntax/parser';
  3. import {t} from 'sentry/locale';
  4. import GroupStore from 'sentry/stores/groupStore';
  5. import {Group, Organization, PageFilters} from 'sentry/types';
  6. import {getIssueFieldRenderer} from 'sentry/utils/dashboards/issueFieldRenderers';
  7. import {getUtcDateString} from 'sentry/utils/dates';
  8. import {TableData, TableDataRow} from 'sentry/utils/discover/discoverQuery';
  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 hasBetterPrioritySort = organization.features.includes(
  69. 'issue-list-better-priority-sort'
  70. );
  71. const sortOptions = [...ISSUE_WIDGET_SORT_OPTIONS];
  72. if (hasBetterPrioritySort) {
  73. sortOptions.push(IssueSortOptions.BETTER_PRIORITY);
  74. }
  75. return sortOptions.map(sortOption => ({
  76. label: getSortLabel(sortOption),
  77. value: sortOption,
  78. }));
  79. }
  80. export function transformIssuesResponseToTable(
  81. data: Group[],
  82. widgetQuery: WidgetQuery,
  83. _organization: Organization,
  84. pageFilters: PageFilters
  85. ): TableData {
  86. GroupStore.add(data);
  87. const transformedTableResults: TableDataRow[] = [];
  88. data.forEach(
  89. ({
  90. id,
  91. shortId,
  92. title,
  93. lifetime,
  94. filtered,
  95. count,
  96. userCount,
  97. project,
  98. annotations,
  99. ...resultProps
  100. }) => {
  101. const transformedResultProps: Omit<TableDataRow, 'id'> = {};
  102. Object.keys(resultProps)
  103. .filter(key => ['number', 'string'].includes(typeof resultProps[key]))
  104. .forEach(key => {
  105. transformedResultProps[key] = resultProps[key];
  106. });
  107. const transformedTableResult: TableDataRow = {
  108. ...transformedResultProps,
  109. events: count,
  110. users: userCount,
  111. id,
  112. 'issue.id': id,
  113. issue: shortId,
  114. title,
  115. project: project.slug,
  116. links: annotations?.join(', '),
  117. };
  118. // Get lifetime stats
  119. if (lifetime) {
  120. transformedTableResult.lifetimeEvents = lifetime?.count;
  121. transformedTableResult.lifetimeUsers = lifetime?.userCount;
  122. }
  123. // Get filtered stats
  124. if (filtered) {
  125. transformedTableResult.filteredEvents = filtered?.count;
  126. transformedTableResult.filteredUsers = filtered?.userCount;
  127. }
  128. // Discover Url properties
  129. const query = widgetQuery.conditions;
  130. const parsedResult = parseSearch(query);
  131. const filteredTerms = parsedResult?.filter(
  132. p => !(p.type === Token.FILTER && DISCOVER_EXCLUSION_FIELDS.includes(p.key.text))
  133. );
  134. transformedTableResult.discoverSearchQuery = joinQuery(filteredTerms, true);
  135. transformedTableResult.projectId = project.id;
  136. const {period, start, end} = pageFilters.datetime || {};
  137. if (start && end) {
  138. transformedTableResult.start = getUtcDateString(start);
  139. transformedTableResult.end = getUtcDateString(end);
  140. }
  141. transformedTableResult.period = period ?? '';
  142. transformedTableResults.push(transformedTableResult);
  143. }
  144. );
  145. return {data: transformedTableResults} as TableData;
  146. }
  147. function getTableRequest(
  148. api: Client,
  149. query: WidgetQuery,
  150. organization: Organization,
  151. pageFilters: PageFilters,
  152. limit?: number,
  153. cursor?: string
  154. ) {
  155. const groupListUrl = `/organizations/${organization.slug}/issues/`;
  156. const params: EndpointParams = {
  157. project: pageFilters.projects ?? [],
  158. environment: pageFilters.environments ?? [],
  159. query: query.conditions,
  160. sort: query.orderby || DEFAULT_SORT,
  161. expand: DEFAULT_EXPAND,
  162. limit: limit ?? DEFAULT_TABLE_LIMIT,
  163. cursor,
  164. };
  165. if (pageFilters.datetime.period) {
  166. params.statsPeriod = pageFilters.datetime.period;
  167. }
  168. if (pageFilters.datetime.end) {
  169. params.end = getUtcDateString(pageFilters.datetime.end);
  170. }
  171. if (pageFilters.datetime.start) {
  172. params.start = getUtcDateString(pageFilters.datetime.start);
  173. }
  174. if (pageFilters.datetime.utc) {
  175. params.utc = pageFilters.datetime.utc;
  176. }
  177. return api.requestPromise(groupListUrl, {
  178. includeAllArgs: true,
  179. method: 'GET',
  180. data: {
  181. ...params,
  182. },
  183. });
  184. }