issues.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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 parsedResult = parseSearch(query);
  128. const filteredTerms = parsedResult?.filter(
  129. p => !(p.type === Token.Filter && DISCOVER_EXCLUSION_FIELDS.includes(p.key.text))
  130. );
  131. transformedTableResult.discoverSearchQuery = joinQuery(filteredTerms, true);
  132. transformedTableResult.projectId = project.id;
  133. const {period, start, end} = pageFilters.datetime || {};
  134. if (start && end) {
  135. transformedTableResult.start = getUtcDateString(start);
  136. transformedTableResult.end = getUtcDateString(end);
  137. }
  138. transformedTableResult.period = period ?? '';
  139. transformedTableResults.push(transformedTableResult);
  140. }
  141. );
  142. return {data: transformedTableResults} as TableData;
  143. }
  144. function getTableRequest(
  145. api: Client,
  146. query: WidgetQuery,
  147. organization: Organization,
  148. pageFilters: PageFilters,
  149. limit?: number,
  150. cursor?: string
  151. ) {
  152. const groupListUrl = `/organizations/${organization.slug}/issues/`;
  153. const params: EndpointParams = {
  154. project: pageFilters.projects ?? [],
  155. environment: pageFilters.environments ?? [],
  156. query: query.conditions,
  157. sort: query.orderby || DEFAULT_SORT,
  158. expand: DEFAULT_EXPAND,
  159. limit: limit ?? DEFAULT_TABLE_LIMIT,
  160. cursor,
  161. };
  162. if (pageFilters.datetime.period) {
  163. params.statsPeriod = pageFilters.datetime.period;
  164. }
  165. if (pageFilters.datetime.end) {
  166. params.end = getUtcDateString(pageFilters.datetime.end);
  167. }
  168. if (pageFilters.datetime.start) {
  169. params.start = getUtcDateString(pageFilters.datetime.start);
  170. }
  171. if (pageFilters.datetime.utc) {
  172. params.utc = pageFilters.datetime.utc;
  173. }
  174. return api.requestPromise(groupListUrl, {
  175. includeAllArgs: true,
  176. method: 'GET',
  177. data: {
  178. ...params,
  179. },
  180. });
  181. }