mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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>
This commit is contained in:
parent
fee0d44e5c
commit
6d64c603c2
@ -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
|
// BuildPipeline builds a graph of the nodes, and returns the nodes in an
|
||||||
// executable order.
|
// executable order.
|
||||||
func buildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
|
func buildPipeline(req *backend.QueryDataRequest) (DataPipeline, error) {
|
||||||
graph, err := buildDependencyGraph(queries)
|
graph, err := buildDependencyGraph(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -67,8 +67,8 @@ func buildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// buildDependencyGraph returns a dependency graph for a set of queries.
|
// buildDependencyGraph returns a dependency graph for a set of queries.
|
||||||
func buildDependencyGraph(queries []backend.DataQuery) (*simple.DirectedGraph, error) {
|
func buildDependencyGraph(req *backend.QueryDataRequest) (*simple.DirectedGraph, error) {
|
||||||
graph, err := buildGraph(queries)
|
graph, err := buildGraph(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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.
|
// 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()
|
dp := simple.NewDirectedGraph()
|
||||||
|
|
||||||
for _, query := range queries {
|
for _, query := range req.Queries {
|
||||||
rawQueryProp := make(map[string]interface{})
|
rawQueryProp := make(map[string]interface{})
|
||||||
err := json.Unmarshal(query.JSON, &rawQueryProp)
|
err := json.Unmarshal(query.JSON, &rawQueryProp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -139,7 +139,7 @@ func buildGraph(queries []backend.DataQuery) (*simple.DirectedGraph, error) {
|
|||||||
case DatasourceName:
|
case DatasourceName:
|
||||||
node, err = buildCMDNode(dp, rn)
|
node, err = buildCMDNode(dp, rn)
|
||||||
default: // If it's not an expression query, it's a data source query.
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -137,7 +137,7 @@ func (dn *DSNode) NodeType() NodeType {
|
|||||||
return TypeDatasourceNode
|
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)
|
encodedQuery, err := json.Marshal(rn.Query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -148,6 +148,7 @@ func buildDSNode(dp *simple.DirectedGraph, rn *rawNode) (*DSNode, error) {
|
|||||||
id: dp.NewNode().ID(),
|
id: dp.NewNode().ID(),
|
||||||
refID: rn.RefID,
|
refID: rn.RefID,
|
||||||
},
|
},
|
||||||
|
orgID: orgID,
|
||||||
query: json.RawMessage(encodedQuery),
|
query: json.RawMessage(encodedQuery),
|
||||||
queryType: rn.QueryType,
|
queryType: rn.QueryType,
|
||||||
intervalMS: defaultIntervalMS,
|
intervalMS: defaultIntervalMS,
|
||||||
@ -165,16 +166,6 @@ func buildDSNode(dp *simple.DirectedGraph, rn *rawNode) (*DSNode, error) {
|
|||||||
}
|
}
|
||||||
dsNode.datasourceID = int64(floatDsID)
|
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
|
var floatIntervalMS float64
|
||||||
if rawIntervalMS := rn.Query["intervalMs"]; ok {
|
if rawIntervalMS := rn.Query["intervalMs"]; ok {
|
||||||
if floatIntervalMS, ok = rawIntervalMS.(float64); !ok {
|
if floatIntervalMS, ok = rawIntervalMS.(float64); !ok {
|
||||||
|
@ -19,8 +19,8 @@ type Service struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildPipeline builds a pipeline from a request.
|
// BuildPipeline builds a pipeline from a request.
|
||||||
func (s *Service) BuildPipeline(queries []backend.DataQuery) (DataPipeline, error) {
|
func (s *Service) BuildPipeline(req *backend.QueryDataRequest) (DataPipeline, error) {
|
||||||
return buildPipeline(queries)
|
return buildPipeline(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecutePipeline executes an expression pipeline and returns all the results.
|
// 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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
res, err := s.ExecutePipeline(context.Background(), pl)
|
res, err := s.ExecutePipeline(context.Background(), pl)
|
||||||
|
@ -73,7 +73,7 @@ func TransformData(ctx context.Context, req *backend.QueryDataRequest) (*backend
|
|||||||
svc := Service{}
|
svc := Service{}
|
||||||
// Build the pipeline from the request, checking for ordering issues (e.g. loops)
|
// Build the pipeline from the request, checking for ordering issues (e.g. loops)
|
||||||
// and parsing graph nodes from the queries.
|
// and parsing graph nodes from the queries.
|
||||||
pipeline, err := svc.BuildPipeline(req.Queries)
|
pipeline, err := svc.BuildPipeline(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
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.
|
// preSave sets datasource and loads the updated model for each alert query.
|
||||||
func (alertDefinition *AlertDefinition) preSave() error {
|
func (alertDefinition *AlertDefinition) preSave() error {
|
||||||
for i, q := range alertDefinition.Data {
|
for i, q := range alertDefinition.Data {
|
||||||
err := q.PreSave(alertDefinition.OrgId)
|
err := q.PreSave()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid alert query %s: %w", q.RefID, err)
|
return fmt.Errorf("invalid alert query %s: %w", q.RefID, err)
|
||||||
}
|
}
|
||||||
|
@ -237,18 +237,6 @@ func (aq *AlertQuery) getModel() ([]byte, error) {
|
|||||||
return model, nil
|
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 {
|
func (aq *AlertQuery) setQueryType() error {
|
||||||
if aq.modelProps == nil {
|
if aq.modelProps == nil {
|
||||||
err := aq.setModelProps()
|
err := aq.setModelProps()
|
||||||
@ -272,12 +260,7 @@ func (aq *AlertQuery) setQueryType() error {
|
|||||||
|
|
||||||
// PreSave sets query's properties.
|
// PreSave sets query's properties.
|
||||||
// It should be called before being saved.
|
// It should be called before being saved.
|
||||||
func (aq *AlertQuery) PreSave(orgID int64) error {
|
func (aq *AlertQuery) PreSave() error {
|
||||||
err := aq.setOrgID(orgID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to set orgId to query model: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := aq.setDatasource(); err != nil {
|
if err := aq.setDatasource(); err != nil {
|
||||||
return fmt.Errorf("failed to set datasource to query model: %w", err)
|
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{}
|
result := ExecutionResults{}
|
||||||
if !c.IsValid() {
|
if !c.IsValid() {
|
||||||
return nil, fmt.Errorf("invalid conditions")
|
return nil, fmt.Errorf("invalid conditions")
|
||||||
|
// TODO: Things probably
|
||||||
}
|
}
|
||||||
|
|
||||||
queryDataReq := &backend.QueryDataRequest{
|
queryDataReq := &backend.QueryDataRequest{
|
||||||
PluginContext: backend.PluginContext{
|
PluginContext: backend.PluginContext{
|
||||||
// TODO: Things probably
|
OrgID: ctx.SignedInUser.OrgId,
|
||||||
},
|
},
|
||||||
Queries: []backend.DataQuery{},
|
Queries: []backend.DataQuery{},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user