issues.tsx 6.1 KB

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