2021-09-10 09:44:47 -05:00
|
|
|
package grafanads
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/experimental"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2021-11-01 04:53:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-02-15 12:26:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/searchV2"
|
2021-09-10 09:44:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/testdatasource"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DatasourceName is the string constant used as the datasource name in requests
|
|
|
|
// to identify it as a Grafana DS command.
|
|
|
|
const DatasourceName = "-- Grafana --"
|
|
|
|
|
|
|
|
// DatasourceID is the fake datasource id used in requests to identify it as a
|
|
|
|
// Grafana DS command.
|
|
|
|
const DatasourceID = -1
|
|
|
|
|
|
|
|
// DatasourceUID is the fake datasource uid used in requests to identify it as a
|
|
|
|
// Grafana DS command.
|
|
|
|
const DatasourceUID = "grafana"
|
|
|
|
|
|
|
|
// Make sure Service implements required interfaces.
|
|
|
|
// This is important to do since otherwise we will only get a
|
|
|
|
// not implemented error response from plugin at runtime.
|
|
|
|
var (
|
|
|
|
_ backend.QueryDataHandler = (*Service)(nil)
|
|
|
|
_ backend.CheckHealthHandler = (*Service)(nil)
|
|
|
|
logger = log.New("tsdb.grafana")
|
|
|
|
)
|
|
|
|
|
2022-02-15 12:26:03 -06:00
|
|
|
func ProvideService(cfg *setting.Cfg, search searchV2.SearchService) *Service {
|
|
|
|
return newService(cfg, search)
|
2021-09-10 09:44:47 -05:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:26:03 -06:00
|
|
|
func newService(cfg *setting.Cfg, search searchV2.SearchService) *Service {
|
2021-09-10 09:44:47 -05:00
|
|
|
s := &Service{
|
2021-12-14 08:22:40 -06:00
|
|
|
staticRootPath: cfg.StaticRootPath,
|
2021-09-10 09:44:47 -05:00
|
|
|
roots: []string{
|
|
|
|
"testdata",
|
|
|
|
"img/icons",
|
|
|
|
"img/bg",
|
|
|
|
"gazetteer",
|
2022-02-02 00:08:07 -06:00
|
|
|
"maps",
|
2021-09-10 09:44:47 -05:00
|
|
|
"upload", // does not exist yet
|
|
|
|
},
|
2022-02-15 12:26:03 -06:00
|
|
|
search: search,
|
2021-09-10 09:44:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service exists regardless of user settings
|
|
|
|
type Service struct {
|
|
|
|
// path to the public folder
|
|
|
|
staticRootPath string
|
|
|
|
roots []string
|
2022-02-15 12:26:03 -06:00
|
|
|
search searchV2.SearchService
|
2021-09-10 09:44:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func DataSourceModel(orgId int64) *models.DataSource {
|
|
|
|
return &models.DataSource{
|
|
|
|
Id: DatasourceID,
|
|
|
|
Uid: DatasourceUID,
|
|
|
|
Name: DatasourceName,
|
|
|
|
Type: "grafana",
|
|
|
|
OrgId: orgId,
|
|
|
|
JsonData: simplejson.New(),
|
2021-10-07 09:33:50 -05:00
|
|
|
SecureJsonData: make(map[string][]byte),
|
2021-09-10 09:44:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:26:03 -06:00
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
2021-09-10 09:44:47 -05:00
|
|
|
response := backend.NewQueryDataResponse()
|
|
|
|
|
|
|
|
for _, q := range req.Queries {
|
|
|
|
switch q.QueryType {
|
|
|
|
case queryTypeRandomWalk:
|
|
|
|
response.Responses[q.RefID] = s.doRandomWalk(q)
|
|
|
|
case queryTypeList:
|
|
|
|
response.Responses[q.RefID] = s.doListQuery(q)
|
|
|
|
case queryTypeRead:
|
|
|
|
response.Responses[q.RefID] = s.doReadQuery(q)
|
2022-02-15 12:26:03 -06:00
|
|
|
case queryTypeSearch:
|
|
|
|
response.Responses[q.RefID] = s.doSearchQuery(ctx, req, q)
|
2021-09-10 09:44:47 -05:00
|
|
|
default:
|
|
|
|
response.Responses[q.RefID] = backend.DataResponse{
|
|
|
|
Error: fmt.Errorf("unknown query type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) CheckHealth(_ context.Context, _ *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
|
|
|
return &backend.CheckHealthResult{
|
|
|
|
Status: backend.HealthStatusOk,
|
|
|
|
Message: "OK",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) publicPath(path string) (string, error) {
|
|
|
|
if strings.Contains(path, "..") {
|
|
|
|
return "", fmt.Errorf("invalid string")
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := false
|
|
|
|
for _, root := range s.roots {
|
|
|
|
if strings.HasPrefix(path, root) {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("bad root path")
|
|
|
|
}
|
|
|
|
return filepath.Join(s.staticRootPath, path), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) doListQuery(query backend.DataQuery) backend.DataResponse {
|
|
|
|
q := &listQueryModel{}
|
|
|
|
response := backend.DataResponse{}
|
|
|
|
err := json.Unmarshal(query.JSON, &q)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Path == "" {
|
|
|
|
count := len(s.roots)
|
|
|
|
names := data.NewFieldFromFieldType(data.FieldTypeString, count)
|
|
|
|
mtype := data.NewFieldFromFieldType(data.FieldTypeString, count)
|
|
|
|
names.Name = "name"
|
|
|
|
mtype.Name = "mediaType"
|
|
|
|
for i, f := range s.roots {
|
|
|
|
names.Set(i, f)
|
|
|
|
mtype.Set(i, "directory")
|
|
|
|
}
|
|
|
|
frame := data.NewFrame("", names, mtype)
|
|
|
|
frame.SetMeta(&data.FrameMeta{
|
|
|
|
Type: data.FrameTypeDirectoryListing,
|
|
|
|
})
|
|
|
|
response.Frames = data.Frames{frame}
|
|
|
|
} else {
|
|
|
|
path, err := s.publicPath(q.Path)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
frame, err := experimental.GetDirectoryFrame(path, false)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
response.Frames = data.Frames{frame}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) doReadQuery(query backend.DataQuery) backend.DataResponse {
|
|
|
|
q := &listQueryModel{}
|
|
|
|
response := backend.DataResponse{}
|
|
|
|
err := json.Unmarshal(query.JSON, &q)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(q.Path) != ".csv" {
|
|
|
|
response.Error = fmt.Errorf("unsupported file type")
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := s.publicPath(q.Path)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can ignore gosec G304 here, because we check the file pattern above
|
|
|
|
// nolint:gosec
|
|
|
|
fileReader, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
response.Error = fmt.Errorf("failed to read file")
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := fileReader.Close(); err != nil {
|
|
|
|
logger.Warn("Failed to close file", "err", err, "path", path)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
frame, err := testdatasource.LoadCsvContent(fileReader, filepath.Base(path))
|
|
|
|
if err != nil {
|
|
|
|
response.Error = err
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
response.Frames = data.Frames{frame}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) doRandomWalk(query backend.DataQuery) backend.DataResponse {
|
|
|
|
response := backend.DataResponse{}
|
|
|
|
|
|
|
|
model := simplejson.New()
|
|
|
|
response.Frames = data.Frames{testdatasource.RandomWalk(query, model, 0)}
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
2022-02-15 12:26:03 -06:00
|
|
|
|
|
|
|
func (s *Service) doSearchQuery(ctx context.Context, req *backend.QueryDataRequest, query backend.DataQuery) backend.DataResponse {
|
|
|
|
q := searchV2.DashboardQuery{}
|
|
|
|
err := json.Unmarshal(query.JSON, &q)
|
|
|
|
if err != nil {
|
|
|
|
return backend.DataResponse{
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *s.search.DoDashboardQuery(ctx, req.PluginContext.User, req.PluginContext.OrgID, q)
|
|
|
|
}
|