2022-01-28 17:28:33 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2022-03-03 22:05:47 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-01-28 17:28:33 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboardimport/api"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboardimport/utils"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/librarypanels"
|
2022-03-11 01:38:04 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
2022-01-28 17:28:33 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-16 21:15:44 +08:00
|
|
|
func ProvideService(routeRegister routing.RouteRegister,
|
2022-06-07 08:52:44 +08:00
|
|
|
quotaService *quota.QuotaService,
|
2022-03-11 01:38:04 +08:00
|
|
|
pluginDashboardService plugindashboards.Service, pluginStore plugins.Store,
|
2022-03-03 22:05:47 +08:00
|
|
|
libraryPanelService librarypanels.Service, dashboardService dashboards.DashboardService,
|
2022-03-10 19:58:18 +08:00
|
|
|
ac accesscontrol.AccessControl,
|
2022-03-03 22:05:47 +08:00
|
|
|
) *ImportDashboardService {
|
2022-01-28 17:28:33 +08:00
|
|
|
s := &ImportDashboardService{
|
2022-03-11 01:38:04 +08:00
|
|
|
pluginDashboardService: pluginDashboardService,
|
2022-03-10 19:58:18 +08:00
|
|
|
dashboardService: dashboardService,
|
|
|
|
|
libraryPanelService: libraryPanelService,
|
2022-01-28 17:28:33 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 08:52:44 +08:00
|
|
|
dashboardImportAPI := api.New(s, quotaService, pluginStore, ac)
|
2022-01-28 17:28:33 +08:00
|
|
|
dashboardImportAPI.RegisterAPIEndpoints(routeRegister)
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ImportDashboardService struct {
|
2022-03-11 01:38:04 +08:00
|
|
|
pluginDashboardService plugindashboards.Service
|
2022-03-10 19:58:18 +08:00
|
|
|
dashboardService dashboards.DashboardService
|
|
|
|
|
libraryPanelService librarypanels.Service
|
2022-01-28 17:28:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
2022-05-18 22:50:24 +08:00
|
|
|
var draftDashboard *models.Dashboard
|
2022-01-28 17:28:33 +08:00
|
|
|
if req.PluginId != "" {
|
2022-03-11 01:38:04 +08:00
|
|
|
loadReq := &plugindashboards.LoadPluginDashboardRequest{
|
|
|
|
|
PluginID: req.PluginId,
|
|
|
|
|
Reference: req.Path,
|
|
|
|
|
}
|
|
|
|
|
if resp, err := s.pluginDashboardService.LoadPluginDashboard(ctx, loadReq); err != nil {
|
2022-01-28 17:28:33 +08:00
|
|
|
return nil, err
|
2022-03-11 01:38:04 +08:00
|
|
|
} else {
|
2022-05-18 22:50:24 +08:00
|
|
|
draftDashboard = resp.Dashboard
|
2022-01-28 17:28:33 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2022-05-18 22:50:24 +08:00
|
|
|
draftDashboard = models.NewDashboardFromJson(req.Dashboard)
|
2022-01-28 17:28:33 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-18 22:50:24 +08:00
|
|
|
evaluator := utils.NewDashTemplateEvaluator(draftDashboard.Data, req.Inputs)
|
2022-01-28 17:28:33 +08:00
|
|
|
generatedDash, err := evaluator.Eval()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveCmd := models.SaveDashboardCommand{
|
|
|
|
|
Dashboard: generatedDash,
|
|
|
|
|
OrgId: req.User.OrgId,
|
|
|
|
|
UserId: req.User.UserId,
|
|
|
|
|
Overwrite: req.Overwrite,
|
|
|
|
|
PluginId: req.PluginId,
|
|
|
|
|
FolderId: req.FolderId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dto := &dashboards.SaveDashboardDTO{
|
|
|
|
|
OrgId: saveCmd.OrgId,
|
|
|
|
|
Dashboard: saveCmd.GetDashboardModel(),
|
|
|
|
|
Overwrite: saveCmd.Overwrite,
|
|
|
|
|
User: req.User,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 22:50:24 +08:00
|
|
|
savedDashboard, err := s.dashboardService.ImportDashboard(ctx, dto)
|
2022-01-28 17:28:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 22:50:24 +08:00
|
|
|
err = s.libraryPanelService.ImportLibraryPanelsForDashboard(ctx, req.User, savedDashboard, req.FolderId)
|
2022-01-28 17:28:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 22:50:24 +08:00
|
|
|
err = s.libraryPanelService.ConnectLibraryPanelsForDashboard(ctx, req.User, savedDashboard)
|
2022-01-28 17:28:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &dashboardimport.ImportDashboardResponse{
|
2022-05-18 22:50:24 +08:00
|
|
|
UID: savedDashboard.Uid,
|
2022-01-28 17:28:33 +08:00
|
|
|
PluginId: req.PluginId,
|
2022-05-18 22:50:24 +08:00
|
|
|
Title: savedDashboard.Title,
|
2022-01-28 17:28:33 +08:00
|
|
|
Path: req.Path,
|
2022-05-18 22:50:24 +08:00
|
|
|
Revision: savedDashboard.Data.Get("revision").MustInt64(1),
|
|
|
|
|
FolderId: savedDashboard.FolderId,
|
|
|
|
|
ImportedUri: "db/" + savedDashboard.Slug,
|
|
|
|
|
ImportedUrl: savedDashboard.GetUrl(),
|
|
|
|
|
ImportedRevision: savedDashboard.Data.Get("revision").MustInt64(1),
|
2022-01-28 17:28:33 +08:00
|
|
|
Imported: true,
|
2022-05-18 22:50:24 +08:00
|
|
|
DashboardId: savedDashboard.Id,
|
|
|
|
|
Slug: savedDashboard.Slug,
|
2022-01-28 17:28:33 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|