| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | package expr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2024-04-17 01:35:41 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							| 
									
										
										
										
											2024-04-17 01:35:41 +08:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/data" | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 12:11:35 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/apimachinery/identity" | 
					
						
							| 
									
										
										
										
											2024-04-17 01:35:41 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/expr/mathexp" | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasources" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type fakePluginContextProvider struct { | 
					
						
							|  |  |  | 	recordings []struct { | 
					
						
							|  |  |  | 		method string | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		params []any | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	result      map[string]*backend.AppInstanceSettings | 
					
						
							|  |  |  | 	errorResult error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var _ pluginContextProvider = &fakePluginContextProvider{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 22:51:18 +08:00
										 |  |  | func (f *fakePluginContextProvider) Get(_ context.Context, pluginID string, user identity.Requester, orgID int64) (backend.PluginContext, error) { | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	f.recordings = append(f.recordings, struct { | 
					
						
							|  |  |  | 		method string | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		params []any | 
					
						
							|  |  |  | 	}{method: "Get", params: []any{pluginID, user, orgID}}) | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	if f.errorResult != nil { | 
					
						
							|  |  |  | 		return backend.PluginContext{}, f.errorResult | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var u *backend.User | 
					
						
							|  |  |  | 	if user != nil { | 
					
						
							|  |  |  | 		u = &backend.User{ | 
					
						
							| 
									
										
										
										
											2023-08-30 22:51:18 +08:00
										 |  |  | 			Login: user.GetLogin(), | 
					
						
							| 
									
										
										
										
											2024-11-26 22:29:31 +08:00
										 |  |  | 			Name:  user.GetName(), | 
					
						
							| 
									
										
										
										
											2023-08-30 22:51:18 +08:00
										 |  |  | 			Email: user.GetEmail(), | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return backend.PluginContext{ | 
					
						
							|  |  |  | 		OrgID:                      orgID, | 
					
						
							|  |  |  | 		PluginID:                   pluginID, | 
					
						
							|  |  |  | 		User:                       u, | 
					
						
							|  |  |  | 		AppInstanceSettings:        f.result[pluginID], | 
					
						
							|  |  |  | 		DataSourceInstanceSettings: nil, | 
					
						
							|  |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 22:51:18 +08:00
										 |  |  | func (f *fakePluginContextProvider) GetWithDataSource(ctx context.Context, pluginID string, user identity.Requester, ds *datasources.DataSource) (backend.PluginContext, error) { | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	f.recordings = append(f.recordings, struct { | 
					
						
							|  |  |  | 		method string | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		params []any | 
					
						
							|  |  |  | 	}{method: "GetWithDataSource", params: []any{pluginID, user, ds}}) | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if f.errorResult != nil { | 
					
						
							|  |  |  | 		return backend.PluginContext{}, f.errorResult | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	orgId := int64(1) | 
					
						
							|  |  |  | 	if user != nil { | 
					
						
							| 
									
										
										
										
											2023-08-30 22:51:18 +08:00
										 |  |  | 		orgId = user.GetOrgID() | 
					
						
							| 
									
										
										
										
											2023-07-14 01:37:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	r, err := f.Get(ctx, pluginID, user, orgId) | 
					
						
							|  |  |  | 	if ds != nil { | 
					
						
							|  |  |  | 		r.DataSourceInstanceSettings = &backend.DataSourceInstanceSettings{ | 
					
						
							|  |  |  | 			ID:   ds.ID, | 
					
						
							|  |  |  | 			UID:  ds.UID, | 
					
						
							|  |  |  | 			Type: ds.Type, | 
					
						
							|  |  |  | 			Name: ds.Name, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return r, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type recordingCallResourceHandler struct { | 
					
						
							|  |  |  | 	recordings []*backend.CallResourceRequest | 
					
						
							|  |  |  | 	response   *backend.CallResourceResponse | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var _ backend.CallResourceHandler = &recordingCallResourceHandler{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *recordingCallResourceHandler) CallResource(_ context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { | 
					
						
							|  |  |  | 	f.recordings = append(f.recordings, req) | 
					
						
							|  |  |  | 	return sender.Send(f.response) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-17 01:35:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func newScalar(value *float64) mathexp.Scalar { | 
					
						
							|  |  |  | 	n := mathexp.NewScalar("", value) | 
					
						
							|  |  |  | 	return n | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newNumber(labels data.Labels, value *float64) mathexp.Number { | 
					
						
							|  |  |  | 	n := mathexp.NewNumber("", labels) | 
					
						
							|  |  |  | 	n.SetValue(value) | 
					
						
							|  |  |  | 	return n | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newSeries(points ...float64) mathexp.Series { | 
					
						
							|  |  |  | 	series := mathexp.NewSeries("", nil, len(points)) | 
					
						
							|  |  |  | 	for idx, point := range points { | 
					
						
							|  |  |  | 		p := point | 
					
						
							|  |  |  | 		series.SetPoint(idx, time.Unix(int64(idx), 0), &p) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return series | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newSeriesPointer(points ...*float64) mathexp.Series { | 
					
						
							|  |  |  | 	series := mathexp.NewSeries("", nil, len(points)) | 
					
						
							|  |  |  | 	for idx, point := range points { | 
					
						
							|  |  |  | 		series.SetPoint(idx, time.Unix(int64(idx), 0), point) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return series | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newSeriesWithLabels(labels data.Labels, values ...*float64) mathexp.Series { | 
					
						
							|  |  |  | 	series := mathexp.NewSeries("", labels, len(values)) | 
					
						
							|  |  |  | 	for idx, value := range values { | 
					
						
							|  |  |  | 		series.SetPoint(idx, time.Unix(int64(idx), 0), value) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return series | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newResults(values ...mathexp.Value) mathexp.Results { | 
					
						
							|  |  |  | 	return mathexp.Results{Values: values} | 
					
						
							|  |  |  | } |