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