base.tsx 8.6 KB

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