mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 08:13:47 +08:00
feat(apps): progress on app dashboard sync
This commit is contained in:
@ -96,6 +96,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
|||||||
|
|
||||||
dash.UpdatedBy = cmd.UserId
|
dash.UpdatedBy = cmd.UserId
|
||||||
dash.OrgId = cmd.OrgId
|
dash.OrgId = cmd.OrgId
|
||||||
|
dash.PluginId = cmd.PluginId
|
||||||
dash.UpdateSlug()
|
dash.UpdateSlug()
|
||||||
return dash
|
return dash
|
||||||
}
|
}
|
||||||
@ -120,6 +121,7 @@ type SaveDashboardCommand struct {
|
|||||||
UserId int64 `json:"userId"`
|
UserId int64 `json:"userId"`
|
||||||
OrgId int64 `json:"-"`
|
OrgId int64 `json:"-"`
|
||||||
Overwrite bool `json:"overwrite"`
|
Overwrite bool `json:"overwrite"`
|
||||||
|
PluginId string `json:"-"`
|
||||||
|
|
||||||
Result *Dashboard
|
Result *Dashboard
|
||||||
}
|
}
|
||||||
|
@ -86,3 +86,9 @@ type GetPluginSettingByIdQuery struct {
|
|||||||
OrgId int64
|
OrgId int64
|
||||||
Result *PluginSetting
|
Result *PluginSetting
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PluginStateChangedEvent struct {
|
||||||
|
PluginId string
|
||||||
|
OrgId int64
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
@ -68,6 +68,7 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
|
|||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
UserId: cmd.UserId,
|
UserId: cmd.UserId,
|
||||||
Overwrite: cmd.Overwrite,
|
Overwrite: cmd.Overwrite,
|
||||||
|
PluginId: cmd.PluginId,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&saveCmd); err != nil {
|
if err := bus.Dispatch(&saveCmd); err != nil {
|
||||||
|
@ -18,7 +18,7 @@ type PluginDashboardInfoDTO struct {
|
|||||||
Revision int64 `json:"revision"`
|
Revision int64 `json:"revision"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Removed bool
|
Removed bool `json:"removed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {
|
func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {
|
||||||
|
@ -7,6 +7,10 @@ import (
|
|||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
bus.AddEventListener(handlePluginStateChanged)
|
||||||
|
}
|
||||||
|
|
||||||
func updateAppDashboards() {
|
func updateAppDashboards() {
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
|
|
||||||
@ -20,9 +24,14 @@ func updateAppDashboards() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, pluginSetting := range query.Result {
|
for _, pluginSetting := range query.Result {
|
||||||
if appDef, exist := Apps[pluginSetting.PluginId]; exist {
|
// ignore disabled plugins
|
||||||
if appDef.Info.Version != pluginSetting.PluginVersion {
|
if !pluginSetting.Enabled {
|
||||||
handleAppPluginUpdated(appDef, pluginSetting.OrgId)
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if pluginDef, exist := Plugins[pluginSetting.PluginId]; exist {
|
||||||
|
if pluginDef.Info.Version != pluginSetting.PluginVersion {
|
||||||
|
syncPluginDashboards(pluginDef, pluginSetting.OrgId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,11 +58,11 @@ func autoUpdateAppDashboard(pluginDashInfo *PluginDashboardInfoDTO, orgId int64)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
|
func syncPluginDashboards(pluginDef *PluginBase, orgId int64) {
|
||||||
plog.Info("App update detected", "pluginId", appDef.Id)
|
plog.Info("Syncing plugin dashboards to DB", "pluginId", pluginDef.Id)
|
||||||
|
|
||||||
// Get plugin dashboards
|
// Get plugin dashboards
|
||||||
if dashboards, err := GetPluginDashboards(orgId, appDef.Id); err != nil {
|
if dashboards, err := GetPluginDashboards(orgId, pluginDef.Id); err != nil {
|
||||||
plog.Error("Failed to load app dashboards", "error", err)
|
plog.Error("Failed to load app dashboards", "error", err)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
@ -61,7 +70,7 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
|
|||||||
for _, dash := range dashboards {
|
for _, dash := range dashboards {
|
||||||
if dash.ImportedRevision != dash.Revision {
|
if dash.ImportedRevision != dash.Revision {
|
||||||
if err := autoUpdateAppDashboard(dash, orgId); err != nil {
|
if err := autoUpdateAppDashboard(dash, orgId); err != nil {
|
||||||
plog.Error("Failed to auto update app dashboard", "pluginId", appDef.Id, "error", err)
|
plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,7 +78,7 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update version in plugin_setting table to mark that we have processed the update
|
// update version in plugin_setting table to mark that we have processed the update
|
||||||
query := m.GetPluginSettingByIdQuery{PluginId: appDef.Id, OrgId: orgId}
|
query := m.GetPluginSettingByIdQuery{PluginId: pluginDef.Id, OrgId: orgId}
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
plog.Error("Failed to read plugin setting by id", "error", err)
|
plog.Error("Failed to read plugin setting by id", "error", err)
|
||||||
return
|
return
|
||||||
@ -79,10 +88,36 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
|
|||||||
cmd := m.UpdatePluginSettingVersionCmd{
|
cmd := m.UpdatePluginSettingVersionCmd{
|
||||||
OrgId: appSetting.OrgId,
|
OrgId: appSetting.OrgId,
|
||||||
PluginId: appSetting.PluginId,
|
PluginId: appSetting.PluginId,
|
||||||
PluginVersion: appDef.Info.Version,
|
PluginVersion: pluginDef.Info.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
plog.Error("Failed to update plugin setting version", "error", err)
|
plog.Error("Failed to update plugin setting version", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handlePluginStateChanged(event *m.PluginStateChangedEvent) error {
|
||||||
|
plog.Info("Plugin state changed", "pluginId", event.PluginId, "enabled", event.Enabled)
|
||||||
|
|
||||||
|
if event.Enabled {
|
||||||
|
syncPluginDashboards(Plugins[event.PluginId], event.OrgId)
|
||||||
|
} else {
|
||||||
|
query := m.GetDashboardsByPluginIdQuery{PluginId: event.PluginId, OrgId: event.OrgId}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
for _, dash := range query.Result {
|
||||||
|
deleteCmd := m.DeleteDashboardCommand{OrgId: dash.OrgId, Slug: dash.Slug}
|
||||||
|
|
||||||
|
plog.Info("Deleting plugin dashboard", "pluginId", event.PluginId, "dashboard", dash.Slug)
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&deleteCmd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -62,17 +62,36 @@ func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error {
|
|||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add state change event on commit success
|
||||||
|
sess.events = append(sess.events, &m.PluginStateChangedEvent{
|
||||||
|
PluginId: cmd.PluginId,
|
||||||
|
OrgId: cmd.OrgId,
|
||||||
|
Enabled: cmd.Enabled,
|
||||||
|
})
|
||||||
|
|
||||||
_, err = sess.Insert(&pluginSetting)
|
_, err = sess.Insert(&pluginSetting)
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
for key, data := range cmd.SecureJsonData {
|
for key, data := range cmd.SecureJsonData {
|
||||||
pluginSetting.SecureJsonData[key] = util.Encrypt([]byte(data), setting.SecretKey)
|
pluginSetting.SecureJsonData[key] = util.Encrypt([]byte(data), setting.SecretKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add state change event on commit success
|
||||||
|
if pluginSetting.Enabled != cmd.Enabled {
|
||||||
|
sess.events = append(sess.events, &m.PluginStateChangedEvent{
|
||||||
|
PluginId: cmd.PluginId,
|
||||||
|
OrgId: cmd.OrgId,
|
||||||
|
Enabled: cmd.Enabled,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pluginSetting.Updated = time.Now()
|
pluginSetting.Updated = time.Now()
|
||||||
pluginSetting.Enabled = cmd.Enabled
|
pluginSetting.Enabled = cmd.Enabled
|
||||||
pluginSetting.JsonData = cmd.JsonData
|
pluginSetting.JsonData = cmd.JsonData
|
||||||
pluginSetting.Pinned = cmd.Pinned
|
pluginSetting.Pinned = cmd.Pinned
|
||||||
pluginSetting.PluginVersion = cmd.PluginVersion
|
pluginSetting.PluginVersion = cmd.PluginVersion
|
||||||
|
|
||||||
_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
|
_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,14 @@
|
|||||||
<span>
|
<span>
|
||||||
</td>
|
</td>
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
<button class="btn btn-secondary" ng-click="ctrl.import(dash, false)" ng-show="!dash.imported">
|
<button class="btn btn-secondary btn-small" ng-click="ctrl.import(dash, false)" ng-show="!dash.imported">
|
||||||
Import
|
Import
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-secondary" ng-click="ctrl.import(dash, true)" ng-show="dash.imported">
|
<button class="btn btn-secondary btn-small" ng-click="ctrl.import(dash, true)" ng-show="dash.imported">
|
||||||
Update
|
Update
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-danger" ng-click="ctrl.remove(dash)" ng-show="dash.imported">
|
<button class="btn btn-danger btn-small" ng-click="ctrl.remove(dash)" ng-show="dash.imported">
|
||||||
Delete
|
<i class="fa fa-trash"></i>
|
||||||
</button>
|
</button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -97,28 +97,7 @@ export class PluginEditCtrl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
importDashboards() {
|
importDashboards() {
|
||||||
// move to dashboards tab
|
return Promise.resolve();
|
||||||
this.tabIndex = 2;
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
if (!this.$scope.$$phase) {
|
|
||||||
this.$scope.$digest();
|
|
||||||
}
|
|
||||||
|
|
||||||
// let angular load dashboards tab
|
|
||||||
setTimeout(() => {
|
|
||||||
resolve();
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
}).then(() => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// send event to import list component
|
|
||||||
appEvents.emit('dashboard-list-import-all', {
|
|
||||||
resolve: resolve,
|
|
||||||
reject: reject
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setPreUpdateHook(callback: () => any) {
|
setPreUpdateHook(callback: () => any) {
|
||||||
|
Reference in New Issue
Block a user