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 {generateIssueWidgetFieldOptions} from '../widgetBuilder/issueWidget/utils';
  18. import {DatasetConfig} from './base';
  19. const DEFAULT_WIDGET_QUERY: WidgetQuery = {
  20. name: '',
  21. fields: ['issue', 'assignee', 'title'] as string[],
  22. columns: ['issue', 'assignee', 'title'],
  23. fieldAliases: [],
  24. aggregates: [],
  25. conditions: '',
  26. orderby: IssueSortOptions.DATE,
  27. };
  28. const DEFAULT_SORT = IssueSortOptions.DATE;
  29. const DEFAULT_EXPAND = ['owners'];
  30. type EndpointParams = Partial<PageFilters['datetime']> & {
  31. environment: string[];
  32. project: number[];
  33. collapse?: string[];
  34. cursor?: string;
  35. expand?: string[];
  36. groupStatsPeriod?: string | null;
  37. limit?: number;
  38. page?: number | string;
  39. query?: string;
  40. sort?: string;
  41. statsPeriod?: string | null;
  42. };
  43. export const IssuesConfig: DatasetConfig<never, Group[]> = {
  44. defaultWidgetQuery: DEFAULT_WIDGET_QUERY,
  45. enableEquations: false,
  46. disableSortOptions,
  47. getTableRequest,
  48. getCustomFieldRenderer: getIssueFieldRenderer,
  49. SearchBar: IssuesSearchBar,
  50. getTableSortOptions,
  51. getTableFieldOptions: (_organization: Organization) =>
  52. generateIssueWidgetFieldOptions(),
  53. fieldHeaderMap: ISSUE_FIELD_TO_HEADER_MAP,
  54. supportedDisplayTypes: [DisplayType.TABLE],
  55. transformTable: transformIssuesResponseToTable,
  56. };
  57. function disableSortOptions(_widgetQuery: WidgetQuery) {
  58. return {
  59. disableSort: false,
  60. disableSortDirection: true,
  61. disableSortReason: t('Issues dataset does not yet support descending order'),
  62. };
  63. }
  64. function getTableSortOptions(_organization: Organization, _widgetQuery: WidgetQuery) {
  65. const sortOptions = [
  66. IssueSortOptions.DATE,
  67. IssueSortOptions.NEW,
  68. IssueSortOptions.PRIORITY,
  69. IssueSortOptions.FREQ,
  70. IssueSortOptions.USER,
  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. }