base.tsx 9.4 KB

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