feat(apps): progress on app dashboard sync

This commit is contained in:
Torkel Ödegaard
2016-07-08 12:26:51 +02:00
parent d44325affd
commit 85be7dd902
8 changed files with 78 additions and 36 deletions

View File

@ -96,6 +96,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash.UpdatedBy = cmd.UserId
dash.OrgId = cmd.OrgId
dash.PluginId = cmd.PluginId
dash.UpdateSlug()
return dash
}
@ -120,6 +121,7 @@ type SaveDashboardCommand struct {
UserId int64 `json:"userId"`
OrgId int64 `json:"-"`
Overwrite bool `json:"overwrite"`
PluginId string `json:"-"`
Result *Dashboard
}

View File

@ -86,3 +86,9 @@ type GetPluginSettingByIdQuery struct {
OrgId int64
Result *PluginSetting
}
type PluginStateChangedEvent struct {
PluginId string
OrgId int64
Enabled bool
}

View File

@ -68,6 +68,7 @@ func ImportDashboard(cmd *ImportDashboardCommand) error {
OrgId: cmd.OrgId,
UserId: cmd.UserId,
Overwrite: cmd.Overwrite,
PluginId: cmd.PluginId,
}
if err := bus.Dispatch(&saveCmd); err != nil {

View File

@ -18,7 +18,7 @@ type PluginDashboardInfoDTO struct {
Revision int64 `json:"revision"`
Description string `json:"description"`
Path string `json:"path"`
Removed bool
Removed bool `json:"removed"`
}
func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {

View File

@ -7,6 +7,10 @@ import (
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddEventListener(handlePluginStateChanged)
}
func updateAppDashboards() {
time.Sleep(time.Second * 1)
@ -20,9 +24,14 @@ func updateAppDashboards() {
}
for _, pluginSetting := range query.Result {
if appDef, exist := Apps[pluginSetting.PluginId]; exist {
if appDef.Info.Version != pluginSetting.PluginVersion {
handleAppPluginUpdated(appDef, pluginSetting.OrgId)
// ignore disabled plugins
if !pluginSetting.Enabled {
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
}
func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
plog.Info("App update detected", "pluginId", appDef.Id)
func syncPluginDashboards(pluginDef *PluginBase, orgId int64) {
plog.Info("Syncing plugin dashboards to DB", "pluginId", pluginDef.Id)
// 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)
return
} else {
@ -61,7 +70,7 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
for _, dash := range dashboards {
if dash.ImportedRevision != dash.Revision {
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
}
}
@ -69,7 +78,7 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
}
// 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 {
plog.Error("Failed to read plugin setting by id", "error", err)
return
@ -79,10 +88,36 @@ func handleAppPluginUpdated(appDef *AppPlugin, orgId int64) {
cmd := m.UpdatePluginSettingVersionCmd{
OrgId: appSetting.OrgId,
PluginId: appSetting.PluginId,
PluginVersion: appDef.Info.Version,
PluginVersion: pluginDef.Info.Version,
}
if err := bus.Dispatch(&cmd); err != nil {
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
}

View File

@ -62,17 +62,36 @@ func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error {
Created: 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)
return err
} else {
for key, data := range cmd.SecureJsonData {
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.Enabled = cmd.Enabled
pluginSetting.JsonData = cmd.JsonData
pluginSetting.Pinned = cmd.Pinned
pluginSetting.PluginVersion = cmd.PluginVersion
_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
return err
}

View File

@ -19,14 +19,14 @@
<span>
</td>
<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
</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
</button>
<button class="btn btn-danger" ng-click="ctrl.remove(dash)" ng-show="dash.imported">
Delete
<button class="btn btn-danger btn-small" ng-click="ctrl.remove(dash)" ng-show="dash.imported">
<i class="fa fa-trash"></i>
</button>
</td>
</tr>

View File

@ -97,28 +97,7 @@ export class PluginEditCtrl {
}
importDashboards() {
// move to dashboards tab
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
});
});
});
return Promise.resolve();
}
setPreUpdateHook(callback: () => any) {