Skip to content

Commit

Permalink
Dashboards: Filter out expressions when going to Explore (#64654)
Browse files Browse the repository at this point in the history
* Filter out expressions when going from dashboard to explore

* Move expression UID constant to types
  • Loading branch information
gelicia authored Mar 13, 2023
1 parent caf9810 commit c363a81
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 24 deletions.
8 changes: 7 additions & 1 deletion public/app/core/utils/explore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ describe('getExploreUrl', () => {
const args = {
panel: {
getSavedId: () => 1,
targets: [{ refId: 'A', expr: 'query1', legendFormat: 'legendFormat1' }],
targets: [
{ refId: 'A', expr: 'query1', legendFormat: 'legendFormat1' },
{ refId: 'B', expr: 'query2', datasource: { type: '__expr__', uid: '__expr__' } },
],
},
datasourceSrv: {
get() {
Expand All @@ -215,6 +218,9 @@ describe('getExploreUrl', () => {
it('should omit legendFormat in explore url', () => {
expect(getExploreUrl(args).then((data) => expect(data).not.toMatch(/legendFormat1/g)));
});
it('should omit expression target in explore url', () => {
expect(getExploreUrl(args).then((data) => expect(data).not.toMatch(/__expr__/g)));
});
});

describe('updateHistory()', () => {
Expand Down
7 changes: 6 additions & 1 deletion public/app/core/utils/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { RefreshPicker } from '@grafana/ui';
import store from 'app/core/store';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { PanelModel } from 'app/features/dashboard/state';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
import { ExploreId, QueryOptions, QueryTransaction } from 'app/types/explore';

import { config } from '../config';
Expand Down Expand Up @@ -67,8 +68,12 @@ export async function getExploreUrl(args: GetExploreUrlArguments): Promise<strin

/** In Explore, we don't have legend formatter and we don't want to keep
* legend formatting as we can't change it
*
* We also don't have expressions, so filter those out
*/
let exploreTargets: DataQuery[] = panel.targets.map((t) => omit(t, 'legendFormat'));
let exploreTargets: DataQuery[] = panel.targets
.map((t) => omit(t, 'legendFormat'))
.filter((t) => t.datasource?.uid !== ExpressionDatasourceUID);
let url: string | undefined;
// if the mixed datasource is not enabled for explore, choose only one datasource
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getDefaultRelativeTimeRange, RelativeTimeRange } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime/src/services/__mocks__/dataSourceSrv';
import {
dataSource as expressionDatasource,
ExpressionDatasourceUID,
} from 'app/features/expressions/ExpressionDatasource';
import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types';
import { dataSource as expressionDatasource } from 'app/features/expressions/ExpressionDatasource';
import { ExpressionQuery, ExpressionQueryType, ExpressionDatasourceUID } from 'app/features/expressions/types';
import { defaultCondition } from 'app/features/expressions/utils/expressionTypes';
import { AlertQuery } from 'app/types/unified-alerting-dto';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { createAction, createReducer } from '@reduxjs/toolkit';
import { DataQuery, getDefaultRelativeTimeRange, RelativeTimeRange } from '@grafana/data';
import { getNextRefIdChar } from 'app/core/utils/query';
import { findDataSourceFromExpressionRecursive } from 'app/features/alerting/utils/dataSourceFromExpression';
import {
dataSource as expressionDatasource,
ExpressionDatasourceUID,
} from 'app/features/expressions/ExpressionDatasource';
import { dataSource as expressionDatasource } from 'app/features/expressions/ExpressionDatasource';
import { isExpressionQuery } from 'app/features/expressions/guards';
import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types';
import { ExpressionQuery, ExpressionQueryType, ExpressionDatasourceUID } from 'app/features/expressions/types';
import { defaultCondition } from 'app/features/expressions/utils/expressionTypes';
import { AlertQuery } from 'app/types/unified-alerting-dto';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useMemo } from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime';
import { useStyles2 } from '@grafana/ui';
import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
import { CombinedRule, RulesSource } from 'app/types/unified-alerting';

import { isCloudRulesSource } from '../../utils/datasource';
Expand Down
2 changes: 1 addition & 1 deletion public/app/features/alerting/unified/utils/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataSourceJsonData, PluginMeta } from '@grafana/data';
import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
import { CombinedRule } from 'app/types/unified-alerting';
import { GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto';

Expand Down
3 changes: 1 addition & 2 deletions public/app/features/alerting/unified/utils/rule-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { getDataSourceSrv } from '@grafana/runtime';
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
import { getNextRefIdChar } from 'app/core/utils/query';
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types';
import { ExpressionQuery, ExpressionQueryType, ExpressionDatasourceUID } from 'app/features/expressions/types';
import { PromQuery } from 'app/plugins/datasource/prometheus/types';
import { RuleWithLocation } from 'app/types/unified-alerting';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
import { AlertQuery } from 'app/types/unified-alerting-dto';

export const hasCyclicalReferences = (queries: AlertQuery[]) => {
Expand Down
7 changes: 1 addition & 6 deletions public/app/features/expressions/ExpressionDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DataSourceWithBackend, getDataSourceSrv, getTemplateSrv } from '@grafan
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';

import { ExpressionQueryEditor } from './ExpressionQueryEditor';
import { ExpressionQuery, ExpressionQueryType } from './types';
import { ExpressionDatasourceUID, ExpressionQuery, ExpressionQueryType } from './types';

/**
* This is a singleton instance that just pretends to be a DataSource
Expand Down Expand Up @@ -60,11 +60,6 @@ export class ExpressionDatasourceApi extends DataSourceWithBackend<ExpressionQue
}
}

/**
* MATCHES a constant in DataSourceWithBackend
*/
export const ExpressionDatasourceUID = '__expr__';

export const instanceSettings: DataSourceInstanceSettings = {
id: -100,
uid: ExpressionDatasourceUID,
Expand Down
5 changes: 5 additions & 0 deletions public/app/features/expressions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { DataQuery, ReducerID, SelectableValue } from '@grafana/data';

import { EvalFunction } from '../alerting/state/alertDef';

/**
* MATCHES a constant in DataSourceWithBackend
*/
export const ExpressionDatasourceUID = '__expr__';

export enum ExpressionQueryType {
math = 'math',
reduce = 'reduce',
Expand Down
2 changes: 1 addition & 1 deletion public/app/features/plugins/datasource_srv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import appEvents from 'app/core/app_events';
import config from 'app/core/config';
import {
dataSource as expressionDatasource,
ExpressionDatasourceUID,
instanceSettings as expressionInstanceSettings,
} from 'app/features/expressions/ExpressionDatasource';
import { ExpressionDatasourceUID } from 'app/features/expressions/types';

import { importDataSourcePlugin } from './plugin_loader';

Expand Down

0 comments on commit c363a81

Please sign in to comment.