| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	bus.AddEventListener(handlePluginStateChanged) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | func updateAppDashboards() { | 
					
						
							| 
									
										
										
										
											2016-07-11 15:38:06 +08:00
										 |  |  | 	time.Sleep(time.Second * 5) | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	plog.Debug("Looking for App Dashboard Updates") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	query := m.GetPluginSettingsQuery{OrgId: 0} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		plog.Error("Failed to get all plugin settings", "error", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, pluginSetting := range query.Result { | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | 		// ignore disabled plugins
 | 
					
						
							|  |  |  | 		if !pluginSetting.Enabled { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if pluginDef, exist := Plugins[pluginSetting.PluginId]; exist { | 
					
						
							|  |  |  | 			if pluginDef.Info.Version != pluginSetting.PluginVersion { | 
					
						
							|  |  |  | 				syncPluginDashboards(pluginDef, pluginSetting.OrgId) | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func autoUpdateAppDashboard(pluginDashInfo *PluginDashboardInfoDTO, orgId int64) error { | 
					
						
							|  |  |  | 	if dash, err := loadPluginDashboard(pluginDashInfo.PluginId, pluginDashInfo.Path); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		plog.Info("Auto updating App dashboard", "dashboard", dash.Title, "newRev", pluginDashInfo.Revision, "oldRev", pluginDashInfo.ImportedRevision) | 
					
						
							|  |  |  | 		updateCmd := ImportDashboardCommand{ | 
					
						
							|  |  |  | 			OrgId:     orgId, | 
					
						
							|  |  |  | 			PluginId:  pluginDashInfo.PluginId, | 
					
						
							|  |  |  | 			Overwrite: true, | 
					
						
							|  |  |  | 			Dashboard: dash.Data, | 
					
						
							|  |  |  | 			UserId:    0, | 
					
						
							|  |  |  | 			Path:      pluginDashInfo.Path, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := bus.Dispatch(&updateCmd); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | func syncPluginDashboards(pluginDef *PluginBase, orgId int64) { | 
					
						
							|  |  |  | 	plog.Info("Syncing plugin dashboards to DB", "pluginId", pluginDef.Id) | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Get plugin dashboards
 | 
					
						
							| 
									
										
										
										
											2016-07-08 23:44:57 +08:00
										 |  |  | 	dashboards, err := GetPluginDashboards(orgId, pluginDef.Id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 		plog.Error("Failed to load app dashboards", "error", err) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-07-08 23:44:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update dashboards with updated revisions
 | 
					
						
							|  |  |  | 	for _, dash := range dashboards { | 
					
						
							|  |  |  | 		// remove removed ones
 | 
					
						
							|  |  |  | 		if dash.Removed { | 
					
						
							|  |  |  | 			plog.Info("Deleting plugin dashboard", "pluginId", pluginDef.Id, "dashboard", dash.Slug) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			deleteCmd := m.DeleteDashboardCommand{OrgId: orgId, Slug: dash.Slug} | 
					
						
							|  |  |  | 			if err := bus.Dispatch(&deleteCmd); err != nil { | 
					
						
							|  |  |  | 				plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// update updated ones
 | 
					
						
							|  |  |  | 		if dash.ImportedRevision != dash.Revision { | 
					
						
							|  |  |  | 			if err := autoUpdateAppDashboard(dash, orgId); err != nil { | 
					
						
							|  |  |  | 				plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err) | 
					
						
							|  |  |  | 				return | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// update version in plugin_setting table to mark that we have processed the update
 | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | 	query := m.GetPluginSettingByIdQuery{PluginId: pluginDef.Id, OrgId: orgId} | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		plog.Error("Failed to read plugin setting by id", "error", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	appSetting := query.Result | 
					
						
							|  |  |  | 	cmd := m.UpdatePluginSettingVersionCmd{ | 
					
						
							|  |  |  | 		OrgId:         appSetting.OrgId, | 
					
						
							|  |  |  | 		PluginId:      appSetting.PluginId, | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | 		PluginVersion: pluginDef.Info.Version, | 
					
						
							| 
									
										
										
										
											2016-07-08 00:11:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							|  |  |  | 		plog.Error("Failed to update plugin setting version", "error", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-08 18:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | } |