123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- import {Query} from 'history';
- import cloneDeep from 'lodash/cloneDeep';
- import pick from 'lodash/pick';
- import trimStart from 'lodash/trimStart';
- import * as qs from 'query-string';
- import WidgetArea from 'sentry-images/dashboard/widget-area.svg';
- import WidgetBar from 'sentry-images/dashboard/widget-bar.svg';
- import WidgetBigNumber from 'sentry-images/dashboard/widget-big-number.svg';
- import WidgetLine from 'sentry-images/dashboard/widget-line-1.svg';
- import WidgetTable from 'sentry-images/dashboard/widget-table.svg';
- import WidgetWorldMap from 'sentry-images/dashboard/widget-world-map.svg';
- import {parseArithmetic} from 'sentry/components/arithmeticInput/parser';
- import {
- Fidelity,
- getDiffInMinutes,
- getInterval,
- SIX_HOURS,
- TWENTY_FOUR_HOURS,
- } from 'sentry/components/charts/utils';
- import {Organization, PageFilters} from 'sentry/types';
- import {defined} from 'sentry/utils';
- import {getUtcDateString, parsePeriodToHours} from 'sentry/utils/dates';
- import EventView from 'sentry/utils/discover/eventView';
- import {
- getAggregateAlias,
- getAggregateArg,
- getColumnsAndAggregates,
- isEquation,
- isMeasurement,
- stripEquationPrefix,
- } from 'sentry/utils/discover/fields';
- import {DisplayModes} from 'sentry/utils/discover/types';
- import {getMeasurements} from 'sentry/utils/measurements/measurements';
- import {
- DashboardDetails,
- DisplayType,
- Widget,
- WidgetQuery,
- WidgetType,
- } from 'sentry/views/dashboardsV2/types';
- export type ValidationError = {
- [key: string]: string | string[] | ValidationError[] | ValidationError;
- };
- export type FlatValidationError = {
- [key: string]: string | FlatValidationError[] | FlatValidationError;
- };
- export function cloneDashboard(dashboard: DashboardDetails): DashboardDetails {
- return cloneDeep(dashboard);
- }
- export function eventViewFromWidget(
- title: string,
- query: WidgetQuery,
- selection: PageFilters,
- widgetDisplayType?: DisplayType
- ): EventView {
- const {start, end, period: statsPeriod} = selection.datetime;
- const {projects, environments} = selection;
- // World Map requires an additional column (geo.country_code) to display in discover when navigating from the widget
- const fields =
- widgetDisplayType === DisplayType.WORLD_MAP &&
- !query.columns.includes('geo.country_code')
- ? ['geo.country_code', ...query.columns, ...query.aggregates]
- : [...query.columns, ...query.aggregates];
- const conditions =
- widgetDisplayType === DisplayType.WORLD_MAP &&
- !query.conditions.includes('has:geo.country_code')
- ? `${query.conditions} has:geo.country_code`.trim()
- : query.conditions;
- const {orderby} = query;
- // Need to convert orderby to aggregate alias because eventView still uses aggregate alias format
- const aggregateAliasOrderBy = orderby
- ? `${orderby.startsWith('-') ? '-' : ''}${getAggregateAlias(trimStart(orderby, '-'))}`
- : orderby;
- return EventView.fromSavedQuery({
- id: undefined,
- name: title,
- version: 2,
- fields,
- query: conditions,
- orderby: aggregateAliasOrderBy,
- projects,
- range: statsPeriod ?? undefined,
- start: start ? getUtcDateString(start) : undefined,
- end: end ? getUtcDateString(end) : undefined,
- environment: environments,
- });
- }
- function coerceStringToArray(value?: string | string[] | null) {
- return typeof value === 'string' ? [value] : value;
- }
- export function constructWidgetFromQuery(query?: Query): Widget | undefined {
- if (query) {
- const queryNames = coerceStringToArray(query.queryNames);
- const queryConditions = coerceStringToArray(query.queryConditions);
- const queryFields = coerceStringToArray(query.queryFields);
- const queries: WidgetQuery[] = [];
- if (
- queryConditions &&
- queryNames &&
- queryFields &&
- typeof query.queryOrderby === 'string'
- ) {
- const {columns, aggregates} = getColumnsAndAggregates(queryFields);
- queryConditions.forEach((condition, index) => {
- queries.push({
- name: queryNames[index],
- conditions: condition,
- fields: queryFields,
- columns,
- aggregates,
- orderby: query.queryOrderby as string,
- });
- });
- }
- if (query.title && query.displayType && query.interval && queries.length > 0) {
- const newWidget: Widget = {
- ...(pick(query, ['title', 'displayType', 'interval']) as {
- displayType: DisplayType;
- interval: string;
- title: string;
- }),
- widgetType: WidgetType.DISCOVER,
- queries,
- };
- return newWidget;
- }
- }
- return undefined;
- }
- export function miniWidget(displayType: DisplayType): string {
- switch (displayType) {
- case DisplayType.BAR:
- return WidgetBar;
- case DisplayType.AREA:
- case DisplayType.TOP_N:
- return WidgetArea;
- case DisplayType.BIG_NUMBER:
- return WidgetBigNumber;
- case DisplayType.TABLE:
- return WidgetTable;
- case DisplayType.WORLD_MAP:
- return WidgetWorldMap;
- case DisplayType.LINE:
- default:
- return WidgetLine;
- }
- }
- export function getWidgetInterval(
- displayType: DisplayType,
- datetimeObj: Partial<PageFilters['datetime']>,
- widgetInterval?: string,
- fidelity?: Fidelity
- ): string {
- // Don't fetch more than 66 bins as we're plotting on a small area.
- const MAX_BIN_COUNT = 66;
- // Bars charts are daily totals to aligned with discover. It also makes them
- // usefully different from line/area charts until we expose the interval control, or remove it.
- let interval = displayType === 'bar' ? '1d' : widgetInterval;
- if (!interval) {
- // Default to 5 minutes
- interval = '5m';
- }
- const desiredPeriod = parsePeriodToHours(interval);
- const selectedRange = getDiffInMinutes(datetimeObj);
- if (fidelity) {
- // Primarily to support lower fidelity for Release Health widgets
- // the sort on releases and hit the metrics API endpoint.
- interval = getInterval(datetimeObj, fidelity);
- if (selectedRange > SIX_HOURS && selectedRange <= TWENTY_FOUR_HOURS) {
- interval = '1h';
- }
- return displayType === 'bar' ? '1d' : interval;
- }
- // selectedRange is in minutes, desiredPeriod is in hours
- // convert desiredPeriod to minutes
- if (selectedRange / (desiredPeriod * 60) > MAX_BIN_COUNT) {
- const highInterval = getInterval(datetimeObj, 'high');
- // Only return high fidelity interval if desired interval is higher fidelity
- if (desiredPeriod < parsePeriodToHours(highInterval)) {
- return highInterval;
- }
- }
- return interval;
- }
- export function getFieldsFromEquations(fields: string[]): string[] {
- // Gather all fields and functions used in equations and prepend them to the provided fields
- const termsSet: Set<string> = new Set();
- fields.filter(isEquation).forEach(field => {
- const parsed = parseArithmetic(stripEquationPrefix(field)).tc;
- parsed.fields.forEach(({term}) => termsSet.add(term as string));
- parsed.functions.forEach(({term}) => termsSet.add(term as string));
- });
- return Array.from(termsSet);
- }
- export function getWidgetDiscoverUrl(
- widget: Widget,
- selection: PageFilters,
- organization: Organization,
- index: number = 0,
- isMetricsData: boolean = false
- ) {
- const eventView = eventViewFromWidget(
- widget.title,
- widget.queries[index],
- selection,
- widget.displayType
- );
- const discoverLocation = eventView.getResultsViewUrlTarget(organization.slug);
- // Pull a max of 3 valid Y-Axis from the widget
- const yAxisOptions = eventView.getYAxisOptions().map(({value}) => value);
- discoverLocation.query.yAxis = [
- ...new Set(
- widget.queries[0].aggregates.filter(aggregate => yAxisOptions.includes(aggregate))
- ),
- ].slice(0, 3);
- // Visualization specific transforms
- switch (widget.displayType) {
- case DisplayType.WORLD_MAP:
- discoverLocation.query.display = DisplayModes.WORLDMAP;
- break;
- case DisplayType.BAR:
- discoverLocation.query.display = DisplayModes.BAR;
- break;
- case DisplayType.TOP_N:
- discoverLocation.query.display = DisplayModes.TOP5;
- // Last field is used as the yAxis
- const aggregates = widget.queries[0].aggregates;
- discoverLocation.query.yAxis = aggregates[aggregates.length - 1];
- if (aggregates.slice(0, -1).includes(aggregates[aggregates.length - 1])) {
- discoverLocation.query.field = aggregates.slice(0, -1);
- }
- break;
- default:
- break;
- }
- // Equation fields need to have their terms explicitly selected as columns in the discover table
- const fields = discoverLocation.query.field;
- const query = widget.queries[0];
- const queryFields = defined(query.fields)
- ? query.fields
- : [...query.columns, ...query.aggregates];
- const equationFields = getFieldsFromEquations(queryFields);
- // Updates fields by adding any individual terms from equation fields as a column
- equationFields.forEach(term => {
- if (Array.isArray(fields) && !fields.includes(term)) {
- fields.unshift(term);
- }
- });
- if (isMetricsData) {
- discoverLocation.query.fromMetric = 'true';
- }
- // Construct and return the discover url
- const discoverPath = `${discoverLocation.pathname}?${qs.stringify({
- ...discoverLocation.query,
- })}`;
- return discoverPath;
- }
- export function getWidgetIssueUrl(
- widget: Widget,
- selection: PageFilters,
- organization: Organization
- ) {
- const {start, end, utc, period} = selection.datetime;
- const datetime =
- start && end
- ? {start: getUtcDateString(start), end: getUtcDateString(end), utc}
- : {statsPeriod: period};
- const issuesLocation = `/organizations/${organization.slug}/issues/?${qs.stringify({
- query: widget.queries?.[0]?.conditions,
- sort: widget.queries?.[0]?.orderby,
- ...datetime,
- project: selection.projects,
- environment: selection.environments,
- })}`;
- return issuesLocation;
- }
- export function getWidgetReleasesUrl(
- _widget: Widget,
- selection: PageFilters,
- organization: Organization
- ) {
- const {start, end, utc, period} = selection.datetime;
- const datetime =
- start && end
- ? {start: getUtcDateString(start), end: getUtcDateString(end), utc}
- : {statsPeriod: period};
- const releasesLocation = `/organizations/${organization.slug}/releases/?${qs.stringify({
- ...datetime,
- project: selection.projects,
- environment: selection.environments,
- })}`;
- return releasesLocation;
- }
- export function flattenErrors(
- data: ValidationError | string,
- update: FlatValidationError
- ): FlatValidationError {
- if (typeof data === 'string') {
- update.error = data;
- } else {
- Object.keys(data).forEach((key: string) => {
- const value = data[key];
- if (typeof value === 'string') {
- update[key] = value;
- return;
- }
- // Recurse into nested objects.
- if (Array.isArray(value) && typeof value[0] === 'string') {
- update[key] = value[0];
- return;
- }
- if (Array.isArray(value) && typeof value[0] === 'object') {
- (value as ValidationError[]).map(item => flattenErrors(item, update));
- } else {
- flattenErrors(value as ValidationError, update);
- }
- });
- }
- return update;
- }
- export function getDashboardsMEPQueryParams(isMEPEnabled: boolean) {
- return isMEPEnabled
- ? {
- dataset: 'metricsEnhanced',
- }
- : {};
- }
- export function getNumEquations(possibleEquations: string[]) {
- return possibleEquations.filter(isEquation).length;
- }
- function isCustomMeasurement(field: string) {
- const definedMeasurements = Object.keys(getMeasurements());
- return isMeasurement(field) && !definedMeasurements.includes(field);
- }
- export function isCustomMeasurementWidget(widget: Widget) {
- return (
- widget.widgetType === WidgetType.DISCOVER &&
- widget.queries.some(({aggregates, columns, fields}) => {
- const aggregateArgs = aggregates.reduce((acc: string[], aggregate) => {
- // Should be ok to use getAggregateArg. getAggregateArg only returns the first arg
- // but there aren't any custom measurement aggregates that use custom measurements
- // outside of the first arg.
- const aggregateArg = getAggregateArg(aggregate);
- if (aggregateArg) {
- acc.push(aggregateArg);
- }
- return acc;
- }, []);
- return [...aggregateArgs, ...columns, ...(fields ?? [])].some(field =>
- isCustomMeasurement(field)
- );
- })
- );
- }
- export function getCustomMeasurementQueryParams() {
- return {
- dataset: 'metrics',
- };
- }
|