utils.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import {useEffect, useState} from 'react';
  2. import cloneDeep from 'lodash/cloneDeep';
  3. import isEqual from 'lodash/isEqual';
  4. import trimStart from 'lodash/trimStart';
  5. import {t} from 'sentry/locale';
  6. import {
  7. Organization,
  8. OrganizationSummary,
  9. SelectValue,
  10. TagCollection,
  11. } from 'sentry/types';
  12. import {
  13. aggregateFunctionOutputType,
  14. aggregateOutputType,
  15. getEquationAliasIndex,
  16. isEquation,
  17. isEquationAlias,
  18. isLegalYAxisType,
  19. SPAN_OP_BREAKDOWN_FIELDS,
  20. stripDerivedMetricsPrefix,
  21. stripEquationPrefix,
  22. } from 'sentry/utils/discover/fields';
  23. import {MeasurementCollection} from 'sentry/utils/measurements/measurements';
  24. import useApi from 'sentry/utils/useApi';
  25. import useCustomMeasurements from 'sentry/utils/useCustomMeasurements';
  26. import {getDatasetConfig} from 'sentry/views/dashboards/datasetConfig/base';
  27. import {
  28. DisplayType,
  29. Widget,
  30. WidgetQuery,
  31. WidgetType,
  32. } from 'sentry/views/dashboards/types';
  33. import {FieldValueOption} from 'sentry/views/discover/table/queryField';
  34. import {FieldValueKind} from 'sentry/views/discover/table/types';
  35. import {generateFieldOptions} from 'sentry/views/discover/utils';
  36. import {IssueSortOptions} from 'sentry/views/issueList/utils';
  37. import {FlatValidationError, getNumEquations, ValidationError} from '../utils';
  38. import {DISABLED_SORT, TAG_SORT_DENY_LIST} from './releaseWidget/fields';
  39. // Used in the widget builder to limit the number of lines plotted in the chart
  40. export const DEFAULT_RESULTS_LIMIT = 5;
  41. const RESULTS_LIMIT = 10;
  42. // Both dashboards and widgets use the 'new' keyword when creating
  43. export const NEW_DASHBOARD_ID = 'new';
  44. export enum DataSet {
  45. EVENTS = 'events',
  46. ISSUES = 'issues',
  47. RELEASES = 'releases',
  48. METRICS = 'metrics',
  49. }
  50. export enum SortDirection {
  51. HIGH_TO_LOW = 'high_to_low',
  52. LOW_TO_HIGH = 'low_to_high',
  53. }
  54. export const sortDirections = {
  55. [SortDirection.HIGH_TO_LOW]: t('High to low'),
  56. [SortDirection.LOW_TO_HIGH]: t('Low to high'),
  57. };
  58. export const displayTypes = {
  59. [DisplayType.AREA]: t('Area Chart'),
  60. [DisplayType.BAR]: t('Bar Chart'),
  61. [DisplayType.LINE]: t('Line Chart'),
  62. [DisplayType.TABLE]: t('Table'),
  63. [DisplayType.BIG_NUMBER]: t('Big Number'),
  64. };
  65. export function mapErrors(
  66. data: ValidationError,
  67. update: FlatValidationError
  68. ): FlatValidationError {
  69. Object.keys(data).forEach((key: string) => {
  70. const value = data[key];
  71. if (typeof value === 'string') {
  72. update[key] = value;
  73. return;
  74. }
  75. // Recurse into nested objects.
  76. if (Array.isArray(value) && typeof value[0] === 'string') {
  77. update[key] = value[0];
  78. return;
  79. }
  80. if (Array.isArray(value) && typeof value[0] === 'object') {
  81. update[key] = (value as ValidationError[]).map(item => mapErrors(item, {}));
  82. } else {
  83. update[key] = mapErrors(value as ValidationError, {});
  84. }
  85. });
  86. return update;
  87. }
  88. export const generateOrderOptions = ({
  89. aggregates,
  90. columns,
  91. widgetType,
  92. }: {
  93. aggregates: string[];
  94. columns: string[];
  95. widgetType: WidgetType;
  96. }): SelectValue<string>[] => {
  97. const isRelease = widgetType === WidgetType.RELEASE;
  98. const options: SelectValue<string>[] = [];
  99. let equations = 0;
  100. (isRelease
  101. ? [...aggregates.map(stripDerivedMetricsPrefix), ...columns]
  102. : [...aggregates, ...columns]
  103. )
  104. .filter(field => !!field)
  105. .filter(field => !DISABLED_SORT.includes(field))
  106. .filter(field => (isRelease ? !TAG_SORT_DENY_LIST.includes(field) : true))
  107. .forEach(field => {
  108. let alias;
  109. const label = stripEquationPrefix(field);
  110. // Equations are referenced via a standard alias following this pattern
  111. if (isEquation(field)) {
  112. alias = `equation[${equations}]`;
  113. equations += 1;
  114. }
  115. options.push({label, value: alias ?? field});
  116. });
  117. return options;
  118. };
  119. export function normalizeQueries({
  120. displayType,
  121. queries,
  122. widgetType,
  123. }: {
  124. displayType: DisplayType;
  125. queries: Widget['queries'];
  126. widgetType?: Widget['widgetType'];
  127. }): Widget['queries'] {
  128. const isTimeseriesChart = getIsTimeseriesChart(displayType);
  129. const isTabularChart = [DisplayType.TABLE, DisplayType.TOP_N].includes(displayType);
  130. queries = cloneDeep(queries);
  131. if ([DisplayType.TABLE, DisplayType.BIG_NUMBER].includes(displayType)) {
  132. // Some display types may only support at most 1 query.
  133. queries = queries.slice(0, 1);
  134. } else if (isTimeseriesChart) {
  135. // Timeseries charts supports at most 3 queries.
  136. queries = queries.slice(0, 3);
  137. }
  138. queries = queries.map(query => {
  139. const {fields = [], columns} = query;
  140. if (isTabularChart) {
  141. // If the groupBy field has values, port everything over to the columnEditCollect field.
  142. query.fields = [...new Set([...fields, ...columns])];
  143. } else {
  144. // If columnEditCollect has field values , port everything over to the groupBy field.
  145. query.fields = fields.filter(field => !columns.includes(field));
  146. }
  147. if (
  148. getIsTimeseriesChart(displayType) &&
  149. !query.columns.filter(column => !!column).length
  150. ) {
  151. // The orderby is only applicable for timeseries charts when there's a
  152. // grouping selected, if all fields are empty then we also reset the orderby
  153. query.orderby = '';
  154. return query;
  155. }
  156. const queryOrderBy =
  157. widgetType === WidgetType.RELEASE
  158. ? stripDerivedMetricsPrefix(queries[0].orderby)
  159. : queries[0].orderby;
  160. const rawOrderBy = trimStart(queryOrderBy, '-');
  161. const resetOrderBy =
  162. // Raw Equation from Top N only applies to timeseries
  163. (isTabularChart && isEquation(rawOrderBy)) ||
  164. // Not contained as tag, field, or function
  165. (!isEquation(rawOrderBy) &&
  166. !isEquationAlias(rawOrderBy) &&
  167. ![...query.columns, ...query.aggregates].includes(rawOrderBy)) ||
  168. // Equation alias and not contained
  169. (isEquationAlias(rawOrderBy) &&
  170. getEquationAliasIndex(rawOrderBy) >
  171. getNumEquations([...query.columns, ...query.aggregates]) - 1);
  172. const orderBy =
  173. (!resetOrderBy && trimStart(queryOrderBy, '-')) ||
  174. (widgetType === WidgetType.ISSUE
  175. ? queryOrderBy ?? IssueSortOptions.DATE
  176. : generateOrderOptions({
  177. widgetType: widgetType ?? WidgetType.DISCOVER,
  178. columns: queries[0].columns,
  179. aggregates: queries[0].aggregates,
  180. })[0]?.value);
  181. if (!orderBy) {
  182. query.orderby = '';
  183. return query;
  184. }
  185. // A widget should be descending if:
  186. // - There is no orderby, so we're defaulting to desc
  187. // - Not an issues widget since issues doesn't support descending and
  188. // the original ordering was descending
  189. const isDescending =
  190. !query.orderby || (widgetType !== WidgetType.ISSUE && queryOrderBy.startsWith('-'));
  191. query.orderby = isDescending ? `-${String(orderBy)}` : String(orderBy);
  192. return query;
  193. });
  194. if (isTabularChart) {
  195. return queries;
  196. }
  197. // Filter out non-aggregate fields
  198. queries = queries.map(query => {
  199. let aggregates = query.aggregates;
  200. if (isTimeseriesChart) {
  201. // Filter out fields that will not generate numeric output types
  202. aggregates = aggregates.filter(aggregate =>
  203. isLegalYAxisType(aggregateOutputType(aggregate))
  204. );
  205. }
  206. if (isTimeseriesChart && aggregates.length && aggregates.length > 3) {
  207. // Timeseries charts supports at most 3 fields.
  208. aggregates = aggregates.slice(0, 3);
  209. }
  210. return {
  211. ...query,
  212. fields: aggregates.length ? aggregates : ['count()'],
  213. columns: query.columns ? query.columns : [],
  214. aggregates: aggregates.length ? aggregates : ['count()'],
  215. };
  216. });
  217. if (isTimeseriesChart) {
  218. // For timeseries widget, all queries must share identical set of fields.
  219. const referenceAggregates = [...queries[0].aggregates];
  220. queryLoop: for (const query of queries) {
  221. if (referenceAggregates.length >= 3) {
  222. break;
  223. }
  224. if (isEqual(referenceAggregates, query.aggregates)) {
  225. continue;
  226. }
  227. for (const aggregate of query.aggregates) {
  228. if (referenceAggregates.length >= 3) {
  229. break queryLoop;
  230. }
  231. if (!referenceAggregates.includes(aggregate)) {
  232. referenceAggregates.push(aggregate);
  233. }
  234. }
  235. }
  236. queries = queries.map(query => {
  237. return {
  238. ...query,
  239. columns: query.columns ? query.columns : [],
  240. aggregates: referenceAggregates,
  241. fields: referenceAggregates,
  242. };
  243. });
  244. }
  245. if (DisplayType.BIG_NUMBER === displayType) {
  246. // For world map chart, cap fields of the queries to only one field.
  247. queries = queries.map(query => {
  248. return {
  249. ...query,
  250. fields: query.aggregates.slice(0, 1),
  251. aggregates: query.aggregates.slice(0, 1),
  252. orderby: '',
  253. columns: [],
  254. };
  255. });
  256. }
  257. return queries;
  258. }
  259. export function getParsedDefaultWidgetQuery(query = ''): WidgetQuery | undefined {
  260. // "any" was needed here because it doesn't pass in getsentry
  261. const urlSeachParams = new URLSearchParams(query) as any;
  262. const parsedQuery = Object.fromEntries(urlSeachParams.entries());
  263. if (!Object.keys(parsedQuery).length) {
  264. return undefined;
  265. }
  266. const columns = parsedQuery.columns ? getFields(parsedQuery.columns) : [];
  267. const aggregates = parsedQuery.aggregates ? getFields(parsedQuery.aggregates) : [];
  268. const fields = [...columns, ...aggregates];
  269. return {
  270. ...parsedQuery,
  271. fields,
  272. columns,
  273. aggregates,
  274. } as WidgetQuery;
  275. }
  276. export function getFields(fieldsString: string): string[] {
  277. // Use a negative lookahead to avoid splitting on commas inside equation fields
  278. return fieldsString.split(/,(?![^(]*\))/g);
  279. }
  280. export function getAmendedFieldOptions({
  281. measurements,
  282. organization,
  283. tags,
  284. }: {
  285. measurements: MeasurementCollection;
  286. organization: OrganizationSummary;
  287. tags: TagCollection;
  288. }) {
  289. return generateFieldOptions({
  290. organization,
  291. tagKeys: Object.values(tags).map(({key}) => key),
  292. measurementKeys: Object.values(measurements).map(({key}) => key),
  293. spanOperationBreakdownKeys: SPAN_OP_BREAKDOWN_FIELDS,
  294. });
  295. }
  296. // Extract metric names from aggregation functions present in the widget queries
  297. export function getMetricFields(queries: WidgetQuery[]) {
  298. return queries.reduce<string[]>((acc, query) => {
  299. for (const field of [...query.aggregates, ...query.columns]) {
  300. const fieldParameter = /\(([^)]*)\)/.exec(field)?.[1];
  301. if (fieldParameter && !acc.includes(fieldParameter)) {
  302. acc.push(fieldParameter);
  303. }
  304. }
  305. return acc;
  306. }, []);
  307. }
  308. // Used to limit the number of results of the "filter your results" fields dropdown
  309. export const MAX_SEARCH_ITEMS = 5;
  310. // Used to set the max height of the smartSearchBar menu
  311. export const MAX_MENU_HEIGHT = 250;
  312. // Any function/field choice for Big Number widgets is legal since the
  313. // data source is from an endpoint that is not timeseries-based.
  314. // The function/field choice for World Map widget will need to be numeric-like.
  315. // Column builder for Table widget is already handled above.
  316. export function doNotValidateYAxis(displayType: DisplayType) {
  317. return displayType === DisplayType.BIG_NUMBER;
  318. }
  319. export function filterPrimaryOptions({
  320. option,
  321. widgetType,
  322. displayType,
  323. }: {
  324. displayType: DisplayType;
  325. option: FieldValueOption;
  326. widgetType?: WidgetType;
  327. }) {
  328. if (widgetType === WidgetType.RELEASE) {
  329. if (displayType === DisplayType.TABLE) {
  330. return [
  331. FieldValueKind.FUNCTION,
  332. FieldValueKind.FIELD,
  333. FieldValueKind.NUMERIC_METRICS,
  334. ].includes(option.value.kind);
  335. }
  336. if (displayType === DisplayType.TOP_N) {
  337. return option.value.kind === FieldValueKind.TAG;
  338. }
  339. }
  340. // Only validate function names for timeseries widgets and
  341. // world map widgets.
  342. if (!doNotValidateYAxis(displayType) && option.value.kind === FieldValueKind.FUNCTION) {
  343. const primaryOutput = aggregateFunctionOutputType(option.value.meta.name, undefined);
  344. if (primaryOutput) {
  345. // If a function returns a specific type, then validate it.
  346. return isLegalYAxisType(primaryOutput);
  347. }
  348. }
  349. return [FieldValueKind.FUNCTION, FieldValueKind.NUMERIC_METRICS].includes(
  350. option.value.kind
  351. );
  352. }
  353. export function getResultsLimit(numQueries: number, numYAxes: number) {
  354. if (numQueries === 0 || numYAxes === 0) {
  355. return DEFAULT_RESULTS_LIMIT;
  356. }
  357. return Math.floor(RESULTS_LIMIT / (numQueries * numYAxes));
  358. }
  359. export function getIsTimeseriesChart(displayType: DisplayType) {
  360. return [DisplayType.LINE, DisplayType.AREA, DisplayType.BAR].includes(displayType);
  361. }
  362. export const useTableFieldOptions = (
  363. organization: Organization,
  364. tags: TagCollection,
  365. widgetType?: WidgetType
  366. ) => {
  367. const {customMeasurements} = useCustomMeasurements();
  368. const api = useApi();
  369. const {getTableFieldOptions} = getDatasetConfig(widgetType);
  370. const [fieldOptions, setFieldOptions] = useState(
  371. getTableFieldOptions(organization, tags, customMeasurements, api)
  372. );
  373. useEffect(() => {
  374. const fetchTableFieldOptions = async () => {
  375. const options = await getTableFieldOptions(
  376. organization,
  377. tags,
  378. customMeasurements,
  379. api
  380. );
  381. setFieldOptions(options);
  382. };
  383. fetchTableFieldOptions();
  384. }, [api, organization, tags, customMeasurements, getTableFieldOptions]);
  385. return fieldOptions;
  386. };
  387. export const useGroupByOptions = (organization, tags, widgetType, queries) => {
  388. const [groupByOptions, setGroupByOptions] = useState<Record<string, any>>({});
  389. const {getGroupByFieldOptions} = getDatasetConfig(widgetType);
  390. const api = useApi();
  391. const {customMeasurements} = useCustomMeasurements();
  392. useEffect(() => {
  393. const fetchGroupByFieldOptions = async () => {
  394. if (getGroupByFieldOptions) {
  395. const options = await getGroupByFieldOptions(
  396. organization,
  397. tags,
  398. customMeasurements,
  399. api,
  400. queries
  401. );
  402. setGroupByOptions(options);
  403. } else {
  404. setGroupByOptions({});
  405. }
  406. };
  407. fetchGroupByFieldOptions();
  408. }, [api, organization, tags, customMeasurements, getGroupByFieldOptions, queries]);
  409. return groupByOptions;
  410. };