Commit 6d64c603 by Kyle Brandt Committed by GitHub

Expr: fix failure to execute due to OrgID (#29653)

* Expr: fix failure to execute due to OrgID
Get orgID from the plugin context, which makes more sense anyways.
makes expressions work again after https://github.com/grafana/grafana/pull/29449 changes.

* Do not save organisation on its alert query model

Co-authored-by: Sofia Papagiannaki <sofia@grafana.com>
parent fee0d44e
......@@ -52,8 +52,8 @@ func (dp *DataPipeline) execute(c context.Context) (mathexp.Vars, error) {
// BuildPipeline builds a graph of the nodes, and returns the nodes in an
// executable order.
func buildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
graph, err := buildDependencyGraph(queries)
func buildPipeline(req *backend.QueryDataRequest) (DataPipeline, error) {
graph, err := buildDependencyGraph(req)
if err != nil {
return nil, err
}
......@@ -67,8 +67,8 @@ func buildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
}
// buildDependencyGraph returns a dependency graph for a set of queries.
func buildDependencyGraph(queries []backend.DataQuery) (*simple.DirectedGraph, error) {
graph, err := buildGraph(queries)
func buildDependencyGraph(req *backend.QueryDataRequest) (*simple.DirectedGraph, error) {
graph, err := buildGraph(req)
if err != nil {
return nil, err
}
......@@ -113,10 +113,10 @@ func buildNodeRegistry(g *simple.DirectedGraph) map[string]Node {
}
// buildGraph creates a new graph populated with nodes for every query.
func buildGraph(queries []backend.DataQuery) (*simple.DirectedGraph, error) {
func buildGraph(req *backend.QueryDataRequest) (*simple.DirectedGraph, error) {
dp := simple.NewDirectedGraph()
for _, query := range queries {
for _, query := range req.Queries {
rawQueryProp := make(map[string]interface{})
err := json.Unmarshal(query.JSON, &rawQueryProp)
if err != nil {
......@@ -139,7 +139,7 @@ func buildGraph(queries []backend.DataQuery) (*simple.DirectedGraph, error) {
case DatasourceName:
node, err = buildCMDNode(dp, rn)
default: // If it's not an expression query, it's a data source query.
node, err = buildDSNode(dp, rn)
node, err = buildDSNode(dp, rn, req.PluginContext.OrgID)
}
if err != nil {
return nil, err
......
......@@ -137,7 +137,7 @@ func (dn *DSNode) NodeType() NodeType {
return TypeDatasourceNode
}
func buildDSNode(dp *simple.DirectedGraph, rn *rawNode) (*DSNode, error) {
func buildDSNode(dp *simple.DirectedGraph, rn *rawNode, orgID int64) (*DSNode, error) {
encodedQuery, err := json.Marshal(rn.Query)
if err != nil {
return nil, err
......@@ -148,6 +148,7 @@ func buildDSNode(dp *simple.DirectedGraph, rn *rawNode) (*DSNode, error) {
id: dp.NewNode().ID(),
refID: rn.RefID,
},
orgID: orgID,
query: json.RawMessage(encodedQuery),
queryType: rn.QueryType,
intervalMS: defaultIntervalMS,
......@@ -165,16 +166,6 @@ func buildDSNode(dp *simple.DirectedGraph, rn *rawNode) (*DSNode, error) {
}
dsNode.datasourceID = int64(floatDsID)
rawOrgID, ok := rn.Query["orgId"]
if !ok {
return nil, fmt.Errorf("no orgId in expression data source request command for refId %v", rn.RefID)
}
floatOrgID, ok := rawOrgID.(float64)
if !ok {
return nil, fmt.Errorf("expected orgId to be a float64, got type %T for refId %v", rawOrgID, rn.RefID)
}
dsNode.orgID = int64(floatOrgID)
var floatIntervalMS float64
if rawIntervalMS := rn.Query["intervalMs"]; ok {
if floatIntervalMS, ok = rawIntervalMS.(float64); !ok {
......
......@@ -19,8 +19,8 @@ type Service struct {
}
// BuildPipeline builds a pipeline from a request.
func (s *Service) BuildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
return buildPipeline(queries)
func (s *Service) BuildPipeline(req *backend.QueryDataRequest) (DataPipeline, error) {
return buildPipeline(req)
}
// ExecutePipeline executes an expression pipeline and returns all the results.
......
......@@ -36,7 +36,9 @@ func TestService(t *testing.T) {
},
}
pl, err := s.BuildPipeline(queries)
req := &backend.QueryDataRequest{Queries: queries}
pl, err := s.BuildPipeline(req)
require.NoError(t, err)
res, err := s.ExecutePipeline(context.Background(), pl)
......
......@@ -73,7 +73,7 @@ func TransformData(ctx context.Context, req *backend.QueryDataRequest) (*backend
svc := Service{}
// Build the pipeline from the request, checking for ordering issues (e.g. loops)
// and parsing graph nodes from the queries.
pipeline, err := svc.BuildPipeline(req.Queries)
pipeline, err := svc.BuildPipeline(req)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
......
......@@ -5,7 +5,7 @@ import "fmt"
// preSave sets datasource and loads the updated model for each alert query.
func (alertDefinition *AlertDefinition) preSave() error {
for i, q := range alertDefinition.Data {
err := q.PreSave(alertDefinition.OrgId)
err := q.PreSave()
if err != nil {
return fmt.Errorf("invalid alert query %s: %w", q.RefID, err)
}
......
......@@ -237,18 +237,6 @@ func (aq *AlertQuery) getModel() ([]byte, error) {
return model, nil
}
func (aq *AlertQuery) setOrgID(orgID int64) error {
if aq.modelProps == nil {
err := aq.setModelProps()
if err != nil {
return err
}
}
aq.modelProps["orgId"] = orgID
return nil
}
func (aq *AlertQuery) setQueryType() error {
if aq.modelProps == nil {
err := aq.setModelProps()
......@@ -272,12 +260,7 @@ func (aq *AlertQuery) setQueryType() error {
// PreSave sets query's properties.
// It should be called before being saved.
func (aq *AlertQuery) PreSave(orgID int64) error {
err := aq.setOrgID(orgID)
if err != nil {
return fmt.Errorf("failed to set orgId to query model: %w", err)
}
func (aq *AlertQuery) PreSave() error {
if err := aq.setDatasource(); err != nil {
return fmt.Errorf("failed to set datasource to query model: %w", err)
}
......
......@@ -96,11 +96,12 @@ func (c *Condition) Execute(ctx AlertExecCtx, fromStr, toStr string) (*Execution
result := ExecutionResults{}
if !c.IsValid() {
return nil, fmt.Errorf("invalid conditions")
// TODO: Things probably
}
queryDataReq := &backend.QueryDataRequest{
PluginContext: backend.PluginContext{
// TODO: Things probably
OrgID: ctx.SignedInUser.OrgId,
},
Queries: []backend.DataQuery{},
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment