-
Notifications
You must be signed in to change notification settings - Fork 12k
/
service.go
129 lines (108 loc) · 3.65 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package service
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
pluginDashboardsManager "github.com/grafana/grafana/pkg/plugins/manager/dashboards"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/plugindashboards"
)
func ProvideService(pluginDashboardStore pluginDashboardsManager.FileStore, dashboardPluginService dashboards.PluginService) *Service {
return &Service{
pluginDashboardStore: pluginDashboardStore,
dashboardPluginService: dashboardPluginService,
logger: log.New("plugindashboards"),
}
}
type Service struct {
pluginDashboardStore pluginDashboardsManager.FileStore
dashboardPluginService dashboards.PluginService
logger log.Logger
}
func (s Service) ListPluginDashboards(ctx context.Context, req *plugindashboards.ListPluginDashboardsRequest) (*plugindashboards.ListPluginDashboardsResponse, error) {
if req == nil {
return nil, fmt.Errorf("req cannot be nil")
}
listArgs := &pluginDashboardsManager.ListPluginDashboardFilesArgs{
PluginID: req.PluginID,
}
listResp, err := s.pluginDashboardStore.ListPluginDashboardFiles(ctx, listArgs)
if err != nil {
return nil, err
}
result := make([]*plugindashboards.PluginDashboard, 0)
// load current dashboards
query := dashboards.GetDashboardsByPluginIDQuery{OrgID: req.OrgID, PluginID: req.PluginID}
queryResult, err := s.dashboardPluginService.GetDashboardsByPluginID(ctx, &query)
if err != nil {
return nil, err
}
existingMatches := make(map[int64]bool)
for _, reference := range listResp.FileReferences {
loadReq := &plugindashboards.LoadPluginDashboardRequest{
PluginID: req.PluginID,
Reference: reference,
}
loadResp, err := s.LoadPluginDashboard(ctx, loadReq)
if err != nil {
return nil, err
}
dashboard := loadResp.Dashboard
res := &plugindashboards.PluginDashboard{}
res.UID = dashboard.UID
res.Reference = reference
res.PluginId = req.PluginID
res.Title = dashboard.Title
res.Revision = dashboard.Data.Get("revision").MustInt64(1)
// find existing dashboard
for _, existingDash := range queryResult {
if existingDash.Slug == dashboard.Slug {
res.UID = existingDash.UID
res.DashboardId = existingDash.ID
res.Imported = true
res.ImportedUri = "db/" + existingDash.Slug
res.ImportedUrl = existingDash.GetURL()
res.ImportedRevision = existingDash.Data.Get("revision").MustInt64(1)
existingMatches[existingDash.ID] = true
break
}
}
result = append(result, res)
}
// find deleted dashboards
for _, dash := range queryResult {
if _, exists := existingMatches[dash.ID]; !exists {
result = append(result, &plugindashboards.PluginDashboard{
UID: dash.UID,
Slug: dash.Slug,
DashboardId: dash.ID,
Removed: true,
})
}
}
return &plugindashboards.ListPluginDashboardsResponse{
Items: result,
}, nil
}
func (s Service) LoadPluginDashboard(ctx context.Context, req *plugindashboards.LoadPluginDashboardRequest) (*plugindashboards.LoadPluginDashboardResponse, error) {
if req == nil {
return nil, fmt.Errorf("req cannot be nil")
}
args := &pluginDashboardsManager.GetPluginDashboardFileContentsArgs{
PluginID: req.PluginID,
FileReference: req.Reference,
}
resp, err := s.pluginDashboardStore.GetPluginDashboardFileContents(ctx, args)
if err != nil {
return nil, err
}
data, err := simplejson.NewJson(resp.Content)
if err != nil {
return nil, err
}
return &plugindashboards.LoadPluginDashboardResponse{
Dashboard: dashboards.NewDashboardFromJson(data),
}, nil
}
var _ plugindashboards.Service = &Service{}