issues.tsx 5.7 KB

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