Skip to content

Commit

Permalink
Expressions: Fixes the issue showing expressions editor (#62510)
Browse files Browse the repository at this point in the history
* Use suggested value for uid

* update the snapshot

* use __expr__

* replace all -100 with __expr__

* update snapshot

* more changes

* revert redundant change

* Use expr.DatasourceUID where it's possible

* generate files
  • Loading branch information
itsmylife authored Jan 31, 2023
1 parent fdb1a47 commit 91221bc
Show file tree
Hide file tree
Showing 54 changed files with 258 additions and 241 deletions.
4 changes: 2 additions & 2 deletions conf/provisioning/alerting/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apiVersion: 1
# # evaluation - should be obtained via the API
# data:
# - refId: A
# datasourceUid: "-100"
# datasourceUid: "__expr__"
# model:
# conditions:
# - evaluator:
Expand All @@ -40,7 +40,7 @@ apiVersion: 1
# type: query
# datasource:
# type: __expr__
# uid: "-100"
# uid: "__expr__"
# expression: 1==0
# intervalMs: 1000
# maxDataPoints: 43200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ groups:
# evaluation - should be obtained trough the API
data:
- refId: A
datasourceUid: '-100'
datasourceUid: '__expr__'
model:
conditions:
- evaluator:
Expand All @@ -86,7 +86,7 @@ groups:
type: query
datasource:
type: __expr__
uid: '-100'
uid: '__expr__'
expression: 1==0
intervalMs: 1000
maxDataPoints: 43200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ resource "grafana_rule_group" "my_rule_group" {
// The query was configured to obtain data from the last 60 seconds. Let's alert on the average value of that series using a Reduce stage.
data {
datasource_uid = "-100"
datasource_uid = "__expr__"
// You can also create a rule in the UI, then GET that rule to obtain the JSON.
// This can be helpful when using more complex reduce expressions.
model = <<EOT
Expand All @@ -298,7 +298,7 @@ EOT
// Now, let's use a math expression as our threshold.
// We want to alert when the value of stage "B" above exceeds 70.
data {
datasource_uid = "-100"
datasource_uid = "__expr__"
ref_id = "C"
relative_time_range {
from = 0
Expand Down
48 changes: 24 additions & 24 deletions docs/sources/developers/http_api/alerting_provisioning.md

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions packages/grafana-runtime/src/utils/DataSourceWithBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { lastValueFrom, merge, Observable, of } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';

import {
DataSourceApi,
DataFrame,
dataFrameToJSON,
DataQuery,
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
DataQuery,
DataSourceJsonData,
ScopedVars,
DataSourceRef,
getDataSourceRef,
makeClassES5Compatible,
DataFrame,
parseLiveChannelAddress,
getDataSourceRef,
DataSourceRef,
dataFrameToJSON,
ScopedVars,
} from '@grafana/data';

import { config } from '../config';
import {
BackendSrvRequest,
FetchResponse,
getBackendSrv,
getDataSourceSrv,
getGrafanaLiveSrv,
StreamingFrameOptions,
StreamingFrameAction,
BackendSrvRequest,
FetchResponse,
StreamingFrameOptions,
} from '../services';

import { BackendDataSourceResponse, toDataQueryResponse } from './queryResponse';
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/dashboards/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/grafana/grafana/pkg/expr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -737,7 +738,7 @@ func insertTestRule(t *testing.T, sqlStore db.DB, foderOrgID int64, folderUID st
Data: []alertQuery{
{
RefID: "A",
DatasourceUID: "-100",
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(`{
"type": "math",
"expression": "2 + 3 > 1"
Expand Down
9 changes: 5 additions & 4 deletions pkg/services/ngalert/api/api_prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/expr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -571,7 +572,7 @@ func withClassicConditionSingleQuery() func(r *ngmodels.AlertRule) {
RefID: "B",
QueryType: "",
RelativeTimeRange: ngmodels.RelativeTimeRange{From: ngmodels.Duration(0), To: ngmodels.Duration(0)},
DatasourceUID: "-100",
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(fmt.Sprintf(classicConditionsModel, "A", "B")),
},
}
Expand Down Expand Up @@ -600,21 +601,21 @@ func withExpressionsMultiQuery() func(r *ngmodels.AlertRule) {
RefID: "C",
QueryType: "",
RelativeTimeRange: ngmodels.RelativeTimeRange{From: ngmodels.Duration(0), To: ngmodels.Duration(0)},
DatasourceUID: "-100",
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(fmt.Sprintf(reduceLastExpressionModel, "A", "C")),
},
{
RefID: "D",
QueryType: "",
RelativeTimeRange: ngmodels.RelativeTimeRange{From: ngmodels.Duration(0), To: ngmodels.Duration(0)},
DatasourceUID: "-100",
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(fmt.Sprintf(reduceLastExpressionModel, "B", "D")),
},
{
RefID: "E",
QueryType: "",
RelativeTimeRange: ngmodels.RelativeTimeRange{From: ngmodels.Duration(0), To: ngmodels.Duration(0)},
DatasourceUID: "-100",
DatasourceUID: expr.DatasourceUID,
Model: json.RawMessage(fmt.Sprintf(mathExpressionModel, "A", "B", "E")),
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/ngalert/api/api_prometheus_test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const classicConditionsModel = `
}],
"datasource": {
"type": "__expr__",
"uid": "-100"
"uid": "__expr__"
},
"hide": false,
"intervalMs": 1000,
Expand Down
18 changes: 11 additions & 7 deletions pkg/services/ngalert/api/api_provisioning_test.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pkg/services/ngalert/api/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ func (api *API) authorize(method, path string) web.Handler {
// authorizeDatasourceAccessForRule checks that user has access to all data sources declared by the rule
func authorizeDatasourceAccessForRule(rule *ngmodels.AlertRule, evaluator func(evaluator ac.Evaluator) bool) bool {
for _, query := range rule.Data {
if query.QueryType == expr.DatasourceType || query.DatasourceUID == expr.OldDatasourceUID {
if query.QueryType == expr.DatasourceType || query.DatasourceUID == expr.DatasourceUID || query.
DatasourceUID == expr.
OldDatasourceUID {
continue
}
if !evaluator(ac.EvalPermission(datasources.ActionQuery, datasources.ScopeProvider.GetResourceScopeUID(query.DatasourceUID))) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/ngalert/api/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func TestCheckDatasourcePermissionsForRule(t *testing.T) {
expressionByType := models.GenerateAlertQuery()
expressionByType.QueryType = expr.DatasourceType
expressionByUID := models.GenerateAlertQuery()
expressionByUID.DatasourceUID = expr.OldDatasourceUID
expressionByUID.DatasourceUID = expr.DatasourceUID

var data []models.AlertQuery
var scopes []string
Expand Down
8 changes: 4 additions & 4 deletions pkg/services/ngalert/api/test-data/post-rulegroup-101.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "query",
"hide": false,
Expand All @@ -50,7 +50,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$reduced > 10",
"hide": false,
Expand Down Expand Up @@ -95,7 +95,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$A",
"intervalMs": 2000,
Expand All @@ -112,4 +112,4 @@
}
}
]
}
}
8 changes: 4 additions & 4 deletions pkg/services/ngalert/api/test-data/post-rulegroup-42.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "query",
"hide": false,
Expand All @@ -57,7 +57,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$reduced > 10",
"hide": false,
Expand Down Expand Up @@ -102,7 +102,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$A",
"intervalMs": 2000,
Expand All @@ -119,4 +119,4 @@
}
}
]
}
}
14 changes: 7 additions & 7 deletions pkg/services/ngalert/api/test-data/ruler-grafana-recipient.http
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "query",
"hide": false,
Expand All @@ -116,7 +116,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$reduced > 10",
"hide": false,
Expand Down Expand Up @@ -184,7 +184,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "query",
"hide": false,
Expand All @@ -202,7 +202,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$reduced > 42",
"hide": false,
Expand Down Expand Up @@ -283,7 +283,7 @@ Content-Type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "query",
"hide": false,
Expand All @@ -301,7 +301,7 @@ Content-Type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$reduced > 42",
"hide": false,
Expand All @@ -317,4 +317,4 @@ Content-Type: application/json
}
}
]
}
}
4 changes: 2 additions & 2 deletions pkg/services/ngalert/api/test-data/test-rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"type": "math",
"expression": "2 + 2 > 1"
Expand All @@ -18,4 +18,4 @@
],
"now": "2021-03-11T14:50:42.218+02:00"
}
}
}
10 changes: 5 additions & 5 deletions pkg/services/ngalert/api/test-data/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"type":"math",
"expression":"1 < 2"
Expand Down Expand Up @@ -56,7 +56,7 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"expression": "$A",
"intervalMs": 2000,
Expand Down Expand Up @@ -113,20 +113,20 @@ content-type: application/json
"from": 18000,
"to": 10800
},
"datasourceUid": "-100",
"datasourceUid": "__expr__",
"model": {
"type":"math",
"expression":"1 < 2"
}
}
]
}
}}
}

### grafana recipient - lotex payload
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{grafanaRecipient}}
content-type: application/json

{
"expr": "rate({cluster=\"us-central1\", job=\"loki-prod/loki-canary\"}[1m]) > 0"
}
}
Loading

0 comments on commit 91221bc

Please sign in to comment.