mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Migrate to Wire for dependency injection (#32289)
Fixes #30144 Co-authored-by: dsotirakis <sotirakis.dim@gmail.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com> Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com> Co-authored-by: Will Browne <wbrowne@users.noreply.github.com> Co-authored-by: Leon Sorokin <leeoniya@gmail.com> Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com> Co-authored-by: spinillos <selenepinillos@gmail.com> Co-authored-by: Karl Persson <kalle.persson@grafana.com> Co-authored-by: Leonard Gram <leo@xlson.com>
This commit is contained in:
@@ -18,7 +18,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
func (p *testDataPlugin) handleCsvContentScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleCsvContentScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -43,7 +43,7 @@ func (p *testDataPlugin) handleCsvContentScenario(ctx context.Context, req *back
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleCsvFileScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleCsvFileScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -72,14 +72,14 @@ func (p *testDataPlugin) handleCsvFileScenario(ctx context.Context, req *backend
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) loadCsvFile(fileName string) (*data.Frame, error) {
|
||||
func (p *TestDataPlugin) loadCsvFile(fileName string) (*data.Frame, error) {
|
||||
validFileName := regexp.MustCompile(`([\w_]+)\.csv`)
|
||||
|
||||
if !validFileName.MatchString(fileName) {
|
||||
return nil, fmt.Errorf("invalid csv file name: %q", fileName)
|
||||
}
|
||||
|
||||
filePath := filepath.Join(p.Cfg.StaticRootPath, "testdata", fileName)
|
||||
filePath := filepath.Join(p.cfg.StaticRootPath, "testdata", fileName)
|
||||
|
||||
// Can ignore gosec G304 here, because we check the file pattern above
|
||||
// nolint:gosec
|
||||
@@ -97,7 +97,7 @@ func (p *testDataPlugin) loadCsvFile(fileName string) (*data.Frame, error) {
|
||||
return p.loadCsvContent(fileReader, fileName)
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) loadCsvContent(ioReader io.Reader, name string) (*data.Frame, error) {
|
||||
func (p *TestDataPlugin) loadCsvContent(ioReader io.Reader, name string) (*data.Frame, error) {
|
||||
reader := csv.NewReader(ioReader)
|
||||
|
||||
// Read the header records
|
||||
|
||||
@@ -17,8 +17,8 @@ func TestCSVFileScenario(t *testing.T) {
|
||||
cfg.DataPath = t.TempDir()
|
||||
cfg.StaticRootPath = "../../../public"
|
||||
|
||||
p := &testDataPlugin{
|
||||
Cfg: cfg,
|
||||
p := &TestDataPlugin{
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
t.Run("loadCsvFile", func(t *testing.T) {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
func (p *testDataPlugin) handleFlightPathScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleFlightPathScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
|
||||
func TestFlightPathScenario(t *testing.T) {
|
||||
cfg := setting.NewCfg()
|
||||
p := &testDataPlugin{
|
||||
Cfg: cfg,
|
||||
p := &TestDataPlugin{
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
t.Run("simple flight", func(t *testing.T) {
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
||||
)
|
||||
|
||||
func (p *testDataPlugin) registerRoutes(mux *http.ServeMux) {
|
||||
func (p *TestDataPlugin) registerRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/", p.testGetHandler)
|
||||
mux.HandleFunc("/scenarios", p.getScenariosHandler)
|
||||
mux.HandleFunc("/stream", p.testStreamHandler)
|
||||
@@ -24,7 +24,7 @@ func (p *testDataPlugin) registerRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/boom", p.testPanicHandler)
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) testGetHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *TestDataPlugin) testGetHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
p.logger.Debug("Received resource call", "url", req.URL.String(), "method", req.Method)
|
||||
|
||||
if req.Method != http.MethodGet {
|
||||
@@ -38,7 +38,7 @@ func (p *testDataPlugin) testGetHandler(rw http.ResponseWriter, req *http.Reques
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) getScenariosHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *TestDataPlugin) getScenariosHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
result := make([]interface{}, 0)
|
||||
|
||||
scenarioIds := make([]string, 0)
|
||||
@@ -69,7 +69,7 @@ func (p *testDataPlugin) getScenariosHandler(rw http.ResponseWriter, req *http.R
|
||||
}
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) testStreamHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *TestDataPlugin) testStreamHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
p.logger.Debug("Received resource call", "url", req.URL.String(), "method", req.Method)
|
||||
|
||||
if req.Method != http.MethodGet {
|
||||
@@ -152,6 +152,6 @@ func createJSONHandler(logger log.Logger) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) testPanicHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *TestDataPlugin) testPanicHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
panic("BOOM")
|
||||
}
|
||||
|
||||
@@ -54,12 +54,12 @@ type Scenario struct {
|
||||
handler backend.QueryDataHandlerFunc
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) registerScenario(scenario *Scenario) {
|
||||
func (p *TestDataPlugin) registerScenario(scenario *Scenario) {
|
||||
p.scenarios[scenario.ID] = scenario
|
||||
p.queryMux.HandleFunc(scenario.ID, scenario.handler)
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) registerScenarios() {
|
||||
func (p *TestDataPlugin) registerScenarios() {
|
||||
p.registerScenario(&Scenario{
|
||||
ID: string(exponentialHeatmapBucketDataQuery),
|
||||
Name: "Exponential heatmap bucket data",
|
||||
@@ -213,7 +213,7 @@ Timestamps will line up evenly on timeStepSeconds (For example, 60 seconds means
|
||||
}
|
||||
|
||||
// handleFallbackScenario handles the scenario where queryType is not set and fallbacks to scenarioId.
|
||||
func (p *testDataPlugin) handleFallbackScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleFallbackScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
scenarioQueries := map[string][]backend.DataQuery{}
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -256,7 +256,7 @@ func (p *testDataPlugin) handleFallbackScenario(ctx context.Context, req *backen
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleRandomWalkScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleRandomWalkScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -276,7 +276,7 @@ func (p *testDataPlugin) handleRandomWalkScenario(ctx context.Context, req *back
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleDatapointsOutsideRangeScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleDatapointsOutsideRangeScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -300,7 +300,7 @@ func (p *testDataPlugin) handleDatapointsOutsideRangeScenario(ctx context.Contex
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleCSVMetricValuesScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleCSVMetricValuesScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -344,7 +344,7 @@ func (p *testDataPlugin) handleCSVMetricValuesScenario(ctx context.Context, req
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleRandomWalkWithErrorScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleRandomWalkWithErrorScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -362,7 +362,7 @@ func (p *testDataPlugin) handleRandomWalkWithErrorScenario(ctx context.Context,
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleRandomWalkSlowScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleRandomWalkSlowScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -383,7 +383,7 @@ func (p *testDataPlugin) handleRandomWalkSlowScenario(ctx context.Context, req *
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleRandomWalkTableScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleRandomWalkTableScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -400,7 +400,7 @@ func (p *testDataPlugin) handleRandomWalkTableScenario(ctx context.Context, req
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handlePredictableCSVWaveScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handlePredictableCSVWaveScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -421,7 +421,7 @@ func (p *testDataPlugin) handlePredictableCSVWaveScenario(ctx context.Context, r
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handlePredictablePulseScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handlePredictablePulseScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -442,15 +442,15 @@ func (p *testDataPlugin) handlePredictablePulseScenario(ctx context.Context, req
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleServerError500Scenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleServerError500Scenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
panic("Test Data Panic!")
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleClientSideScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleClientSideScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
return backend.NewQueryDataResponse(), nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleArrowScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleArrowScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -474,7 +474,7 @@ func (p *testDataPlugin) handleArrowScenario(ctx context.Context, req *backend.Q
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleExponentialHeatmapBucketDataScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleExponentialHeatmapBucketDataScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -489,7 +489,7 @@ func (p *testDataPlugin) handleExponentialHeatmapBucketDataScenario(ctx context.
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleLinearHeatmapBucketDataScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleLinearHeatmapBucketDataScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -504,7 +504,7 @@ func (p *testDataPlugin) handleLinearHeatmapBucketDataScenario(ctx context.Conte
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleTableStaticScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleTableStaticScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
@@ -533,7 +533,7 @@ func (p *testDataPlugin) handleTableStaticScenario(ctx context.Context, req *bac
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleLogsScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleLogsScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestTestdataScenarios(t *testing.T) {
|
||||
p := &testDataPlugin{}
|
||||
p := &TestDataPlugin{}
|
||||
|
||||
t.Run("random walk ", func(t *testing.T) {
|
||||
t.Run("Should start at the requested value", func(t *testing.T) {
|
||||
|
||||
@@ -9,37 +9,41 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegisterService(&testDataPlugin{})
|
||||
}
|
||||
|
||||
type testDataPlugin struct {
|
||||
BackendPluginManager backendplugin.Manager `inject:""`
|
||||
Cfg *setting.Cfg `inject:""`
|
||||
logger log.Logger
|
||||
scenarios map[string]*Scenario
|
||||
queryMux *datasource.QueryTypeMux
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) Init() error {
|
||||
p.logger = log.New("tsdb.testdata")
|
||||
p.scenarios = map[string]*Scenario{}
|
||||
p.queryMux = datasource.NewQueryTypeMux()
|
||||
p.registerScenarios()
|
||||
func ProvideService(cfg *setting.Cfg, manager backendplugin.Manager) (*TestDataPlugin, error) {
|
||||
resourceMux := http.NewServeMux()
|
||||
p.registerRoutes(resourceMux)
|
||||
p := new(resourceMux)
|
||||
factory := coreplugin.New(backend.ServeOpts{
|
||||
QueryDataHandler: p.queryMux,
|
||||
CallResourceHandler: httpadapter.New(resourceMux),
|
||||
StreamHandler: newTestStreamHandler(p.logger),
|
||||
})
|
||||
err := p.BackendPluginManager.Register("testdata", factory)
|
||||
err := manager.Register("testdata", factory)
|
||||
if err != nil {
|
||||
p.logger.Error("Failed to register plugin", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func new(resourceMux *http.ServeMux) *TestDataPlugin {
|
||||
p := &TestDataPlugin{
|
||||
logger: log.New("tsdb.testdata"),
|
||||
scenarios: map[string]*Scenario{},
|
||||
queryMux: datasource.NewQueryTypeMux(),
|
||||
}
|
||||
|
||||
p.registerScenarios()
|
||||
p.registerRoutes(resourceMux)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
type TestDataPlugin struct {
|
||||
cfg *setting.Cfg
|
||||
logger log.Logger
|
||||
scenarios map[string]*Scenario
|
||||
queryMux *datasource.QueryTypeMux
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ type usaQuery struct {
|
||||
period time.Duration
|
||||
}
|
||||
|
||||
func (p *testDataPlugin) handleUSAScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
func (p *TestDataPlugin) handleUSAScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
|
||||
@@ -17,8 +17,8 @@ import (
|
||||
|
||||
func TestUSAScenario(t *testing.T) {
|
||||
cfg := setting.NewCfg()
|
||||
p := &testDataPlugin{
|
||||
Cfg: cfg,
|
||||
p := &TestDataPlugin{
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
t.Run("usa query modes", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user