base.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import trimStart from 'lodash/trimStart';
  2. import type {Client, ResponseMeta} from 'sentry/api';
  3. import type {SearchBarProps} from 'sentry/components/events/searchBar';
  4. import type {PageFilters, SelectValue} from 'sentry/types/core';
  5. import type {Series} from 'sentry/types/echarts';
  6. import type {TagCollection} from 'sentry/types/group';
  7. import type {Organization} from 'sentry/types/organization';
  8. import type {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements';
  9. import type {TableData} from 'sentry/utils/discover/discoverQuery';
  10. import type {MetaType} from 'sentry/utils/discover/eventView';
  11. import type {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  12. import type {AggregationOutputType, QueryFieldValue} from 'sentry/utils/discover/fields';
  13. import {isEquation} from 'sentry/utils/discover/fields';
  14. import type {DiscoverDatasets} from 'sentry/utils/discover/types';
  15. import type {MEPState} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  16. import type {OnDemandControlContext} from 'sentry/utils/performance/contexts/onDemandControl';
  17. import type {FieldValueOption} from 'sentry/views/discover/table/queryField';
  18. import type {FieldValue} from 'sentry/views/discover/table/types';
  19. import type {DisplayType, Widget, WidgetQuery} from '../types';
  20. import {WidgetType} from '../types';
  21. import {getNumEquations} from '../utils';
  22. import {ErrorsConfig} from './errors';
  23. import {ErrorsAndTransactionsConfig} from './errorsAndTransactions';
  24. import {IssuesConfig} from './issues';
  25. import {ReleasesConfig} from './releases';
  26. import {TransactionsConfig} from './transactions';
  27. export type WidgetBuilderSearchBarProps = {
  28. getFilterWarning: SearchBarProps['getFilterWarning'];
  29. onClose: SearchBarProps['onClose'];
  30. onSearch: SearchBarProps['onSearch'];
  31. organization: Organization;
  32. pageFilters: PageFilters;
  33. widgetQuery: WidgetQuery;
  34. dataset?: DiscoverDatasets;
  35. };
  36. export interface DatasetConfig<SeriesResponse, TableResponse> {
  37. /**
  38. * Dataset specific search bar for the 'Filter' step in the
  39. * widget builder.
  40. */
  41. SearchBar: (props: WidgetBuilderSearchBarProps) => JSX.Element;
  42. /**
  43. * Default query to display when dataset is selected in the
  44. * Widget Builder.
  45. */
  46. defaultWidgetQuery: WidgetQuery;
  47. enableEquations: boolean;
  48. /**
  49. * Field options to display in the Column selectors for
  50. * Table display type.
  51. */
  52. getTableFieldOptions: (
  53. organization: Organization,
  54. tags?: TagCollection,
  55. customMeasurements?: CustomMeasurementCollection,
  56. api?: Client
  57. ) => Record<string, SelectValue<FieldValue>>;
  58. /**
  59. * List of supported display types for dataset.
  60. */
  61. supportedDisplayTypes: DisplayType[];
  62. /**
  63. * Transforms table API results into format that is used by
  64. * table and big number components.
  65. */
  66. transformTable: (
  67. data: TableResponse,
  68. widgetQuery: WidgetQuery,
  69. organization: Organization,
  70. pageFilters: PageFilters
  71. ) => TableData;
  72. /**
  73. * Configure enabling/disabling sort/direction options with an
  74. * optional message for why it is disabled.
  75. */
  76. disableSortOptions?: (widgetQuery: WidgetQuery) => {
  77. disableSort: boolean;
  78. disableSortDirection: boolean;
  79. disableSortReason?: string;
  80. };
  81. /**
  82. * Filter the options available to the parameters list
  83. * of an aggregate function in QueryField component on the
  84. * Widget Builder.
  85. */
  86. filterAggregateParams?: (
  87. option: FieldValueOption,
  88. fieldValue?: QueryFieldValue
  89. ) => boolean;
  90. /**
  91. * Refine the options available in the sort options for timeseries
  92. * displays on the 'Sort by' step of the Widget Builder.
  93. */
  94. filterSeriesSortOptions?: (
  95. columns: Set<string>
  96. ) => (option: FieldValueOption) => boolean;
  97. /**
  98. * Filter the primary options available in a table widget
  99. * columns on the Widget Builder.
  100. */
  101. filterTableOptions?: (option: FieldValueOption) => boolean;
  102. /**
  103. * Filter the options available to the parameters list
  104. * of an aggregate function in QueryField component on the
  105. * Widget Builder.
  106. */
  107. filterYAxisAggregateParams?: (
  108. fieldValue: QueryFieldValue,
  109. displayType: DisplayType
  110. ) => (option: FieldValueOption) => boolean;
  111. filterYAxisOptions?: (
  112. displayType: DisplayType
  113. ) => (option: FieldValueOption) => boolean;
  114. /**
  115. * Used to select custom renderers for field types.
  116. */
  117. getCustomFieldRenderer?: (
  118. field: string,
  119. meta: MetaType,
  120. organization?: Organization
  121. ) => ReturnType<typeof getFieldRenderer> | null;
  122. /**
  123. * Generate field header used for mapping column
  124. * names to more desirable values in tables.
  125. */
  126. getFieldHeaderMap?: (widgetQuery?: WidgetQuery) => Record<string, string>;
  127. /**
  128. * Field options to display in the Group by selector.
  129. */
  130. getGroupByFieldOptions?: (
  131. organization: Organization,
  132. tags?: TagCollection,
  133. customMeasurements?: CustomMeasurementCollection,
  134. api?: Client,
  135. queries?: WidgetQuery[]
  136. ) => Record<string, SelectValue<FieldValue>>;
  137. /**
  138. * Generate the request promises for fetching
  139. * series data.
  140. */
  141. getSeriesRequest?: (
  142. api: Client,
  143. widget: Widget,
  144. queryIndex: number,
  145. organization: Organization,
  146. pageFilters: PageFilters,
  147. onDemandControlContext?: OnDemandControlContext,
  148. referrer?: string,
  149. mepSetting?: MEPState | null
  150. ) => Promise<[SeriesResponse, string | undefined, ResponseMeta | undefined]>;
  151. /**
  152. * Get the result type of the series. ie duration, size, percentage, etc
  153. */
  154. getSeriesResultType?: (
  155. data: SeriesResponse,
  156. widgetQuery: WidgetQuery
  157. ) => Record<string, AggregationOutputType>;
  158. /**
  159. * Generate the request promises for fetching
  160. * tabular data.
  161. */
  162. getTableRequest?: (
  163. api: Client,
  164. widget: Widget,
  165. query: WidgetQuery,
  166. organization: Organization,
  167. pageFilters: PageFilters,
  168. onDemandControlContext?: OnDemandControlContext,
  169. limit?: number,
  170. cursor?: string,
  171. referrer?: string,
  172. mepSetting?: MEPState | null
  173. ) => Promise<[TableResponse, string | undefined, ResponseMeta | undefined]>;
  174. /**
  175. * Generate the list of sort options for table
  176. * displays on the 'Sort by' step of the Widget Builder.
  177. */
  178. getTableSortOptions?: (
  179. organization: Organization,
  180. widgetQuery: WidgetQuery
  181. ) => SelectValue<string>[];
  182. /**
  183. * Generate the list of sort options for timeseries
  184. * displays on the 'Sort by' step of the Widget Builder.
  185. */
  186. getTimeseriesSortOptions?: (
  187. organization: Organization,
  188. widgetQuery: WidgetQuery,
  189. tags?: TagCollection
  190. ) => Record<string, SelectValue<FieldValue>>;
  191. /**
  192. * Apply dataset specific overrides to the logic that handles
  193. * column updates for tables in the Widget Builder.
  194. */
  195. handleColumnFieldChangeOverride?: (widgetQuery: WidgetQuery) => WidgetQuery;
  196. /**
  197. * Called on column or y-axis change in the Widget Builder
  198. * to reset the orderby of the widget query.
  199. */
  200. handleOrderByReset?: (widgetQuery: WidgetQuery, newFields: string[]) => WidgetQuery;
  201. /**
  202. * Transforms timeseries API results into series data that is
  203. * ingestable by echarts for timeseries visualizations.
  204. */
  205. transformSeries?: (
  206. data: SeriesResponse,
  207. widgetQuery: WidgetQuery,
  208. organization: Organization
  209. ) => Series[];
  210. }
  211. export function getDatasetConfig<T extends WidgetType | undefined>(
  212. widgetType: T
  213. ): T extends WidgetType.ISSUE
  214. ? typeof IssuesConfig
  215. : T extends WidgetType.RELEASE
  216. ? typeof ReleasesConfig
  217. : T extends WidgetType.ERRORS
  218. ? typeof ErrorsConfig
  219. : T extends WidgetType.TRANSACTIONS
  220. ? typeof TransactionsConfig
  221. : typeof ErrorsAndTransactionsConfig;
  222. export function getDatasetConfig(
  223. widgetType?: WidgetType
  224. ):
  225. | typeof IssuesConfig
  226. | typeof ReleasesConfig
  227. | typeof ErrorsAndTransactionsConfig
  228. | typeof ErrorsConfig
  229. | typeof TransactionsConfig {
  230. switch (widgetType) {
  231. case WidgetType.ISSUE:
  232. return IssuesConfig;
  233. case WidgetType.RELEASE:
  234. return ReleasesConfig;
  235. case WidgetType.ERRORS:
  236. return ErrorsConfig;
  237. case WidgetType.TRANSACTIONS:
  238. return TransactionsConfig;
  239. case WidgetType.DISCOVER:
  240. default:
  241. return ErrorsAndTransactionsConfig;
  242. }
  243. }
  244. /**
  245. * A generic orderby reset helper function that updates the query's
  246. * orderby based on new selected fields.
  247. */
  248. export function handleOrderByReset(
  249. widgetQuery: WidgetQuery,
  250. newFields: string[]
  251. ): WidgetQuery {
  252. const rawOrderby = trimStart(widgetQuery.orderby, '-');
  253. const isDescending = widgetQuery.orderby.startsWith('-');
  254. const orderbyPrefix = isDescending ? '-' : '';
  255. if (!newFields.includes(rawOrderby) && widgetQuery.orderby !== '') {
  256. const isFromAggregates = widgetQuery.aggregates.includes(rawOrderby);
  257. const isCustomEquation = isEquation(rawOrderby);
  258. const isUsedInGrouping = widgetQuery.columns.includes(rawOrderby);
  259. const keepCurrentOrderby = isFromAggregates || isCustomEquation || isUsedInGrouping;
  260. const firstAggregateAlias = isEquation(widgetQuery.aggregates[0] ?? '')
  261. ? `equation[${getNumEquations(widgetQuery.aggregates) - 1}]`
  262. : newFields[0];
  263. widgetQuery.orderby =
  264. (keepCurrentOrderby && widgetQuery.orderby) ||
  265. `${orderbyPrefix}${firstAggregateAlias}`;
  266. }
  267. return widgetQuery;
  268. }