Commit 179f35a5 by Hugo Häggmark Committed by GitHub

PanelLibrary: Adds library panel meta information to dashboard json (#30770)

parent 07e7f47d
...@@ -12,6 +12,21 @@ import ( ...@@ -12,6 +12,21 @@ import (
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
var (
sqlStatmentLibrayPanelDTOWithMeta = `
SELECT lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by
, 0 AS can_edit
, u1.login AS created_by_name
, u1.email AS created_by_email
, u2.login AS updated_by_name
, u2.email AS updated_by_email
, (SELECT COUNT(dashboard_id) FROM library_panel_dashboard WHERE librarypanel_id = lp.id) AS connected_dashboards
FROM library_panel AS lp
LEFT JOIN user AS u1 ON lp.created_by = u1.id
LEFT JOIN user AS u2 ON lp.updated_by = u2.id
`
)
// createLibraryPanel adds a Library Panel. // createLibraryPanel adds a Library Panel.
func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd createLibraryPanelCommand) (LibraryPanelDTO, error) { func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd createLibraryPanelCommand) (LibraryPanelDTO, error) {
libraryPanel := LibraryPanel{ libraryPanel := LibraryPanel{
...@@ -46,6 +61,7 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd cre ...@@ -46,6 +61,7 @@ func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd cre
Model: libraryPanel.Model, Model: libraryPanel.Model,
Meta: LibraryPanelDTOMeta{ Meta: LibraryPanelDTOMeta{
CanEdit: true, CanEdit: true,
ConnectedDashboards: 0,
Created: libraryPanel.Created, Created: libraryPanel.Created,
Updated: libraryPanel.Updated, Updated: libraryPanel.Updated,
CreatedBy: LibraryPanelDTOMetaUser{ CreatedBy: LibraryPanelDTOMetaUser{
...@@ -179,18 +195,7 @@ func (lps *LibraryPanelService) disconnectLibraryPanelsForDashboard(dashboardID ...@@ -179,18 +195,7 @@ func (lps *LibraryPanelService) disconnectLibraryPanelsForDashboard(dashboardID
func getLibraryPanel(session *sqlstore.DBSession, uid string, orgID int64) (LibraryPanelWithMeta, error) { func getLibraryPanel(session *sqlstore.DBSession, uid string, orgID int64) (LibraryPanelWithMeta, error) {
libraryPanels := make([]LibraryPanelWithMeta, 0) libraryPanels := make([]LibraryPanelWithMeta, 0)
sql := `SELECT sql := sqlStatmentLibrayPanelDTOWithMeta + "WHERE lp.uid=? AND lp.org_id=?"
lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by
, 0 AS can_edit
, u1.login AS created_by_name
, u1.email AS created_by_email
, u2.login AS updated_by_name
, u2.email AS updated_by_email
FROM library_panel AS lp
LEFT JOIN user AS u1 ON lp.created_by = u1.id
LEFT JOIN user AS u2 ON lp.updated_by = u2.id
WHERE lp.uid=? AND lp.org_id=?`
sess := session.SQL(sql, uid, orgID) sess := session.SQL(sql, uid, orgID)
err := sess.Find(&libraryPanels) err := sess.Find(&libraryPanels)
if err != nil { if err != nil {
...@@ -224,6 +229,7 @@ func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, uid string ...@@ -224,6 +229,7 @@ func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, uid string
Model: libraryPanel.Model, Model: libraryPanel.Model,
Meta: LibraryPanelDTOMeta{ Meta: LibraryPanelDTOMeta{
CanEdit: true, CanEdit: true,
ConnectedDashboards: libraryPanel.ConnectedDashboards,
Created: libraryPanel.Created, Created: libraryPanel.Created,
Updated: libraryPanel.Updated, Updated: libraryPanel.Updated,
CreatedBy: LibraryPanelDTOMetaUser{ CreatedBy: LibraryPanelDTOMetaUser{
...@@ -247,18 +253,7 @@ func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]Lib ...@@ -247,18 +253,7 @@ func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]Lib
orgID := c.SignedInUser.OrgId orgID := c.SignedInUser.OrgId
libraryPanels := make([]LibraryPanelWithMeta, 0) libraryPanels := make([]LibraryPanelWithMeta, 0)
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error { err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
sql := `SELECT sql := sqlStatmentLibrayPanelDTOWithMeta + "WHERE lp.org_id=?"
lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by
, 0 AS can_edit
, u1.login AS created_by_name
, u1.email AS created_by_email
, u2.login AS updated_by_name
, u2.email AS updated_by_email
FROM library_panel AS lp
LEFT JOIN user AS u1 ON lp.created_by = u1.id
LEFT JOIN user AS u2 ON lp.updated_by = u2.id
WHERE lp.org_id=?`
sess := session.SQL(sql, orgID) sess := session.SQL(sql, orgID)
err := sess.Find(&libraryPanels) err := sess.Find(&libraryPanels)
if err != nil { if err != nil {
...@@ -279,6 +274,7 @@ func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]Lib ...@@ -279,6 +274,7 @@ func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]Lib
Model: panel.Model, Model: panel.Model,
Meta: LibraryPanelDTOMeta{ Meta: LibraryPanelDTOMeta{
CanEdit: true, CanEdit: true,
ConnectedDashboards: panel.ConnectedDashboards,
Created: panel.Created, Created: panel.Created,
Updated: panel.Updated, Updated: panel.Updated,
CreatedBy: LibraryPanelDTOMetaUser{ CreatedBy: LibraryPanelDTOMetaUser{
...@@ -325,17 +321,11 @@ func (lps *LibraryPanelService) getConnectedDashboards(c *models.ReqContext, uid ...@@ -325,17 +321,11 @@ func (lps *LibraryPanelService) getConnectedDashboards(c *models.ReqContext, uid
return connectedDashboardIDs, err return connectedDashboardIDs, err
} }
func (lps *LibraryPanelService) getLibraryPanelsForDashboardID(dashboardID int64) (map[string]LibraryPanel, error) { func (lps *LibraryPanelService) getLibraryPanelsForDashboardID(dashboardID int64) (map[string]LibraryPanelDTO, error) {
libraryPanelMap := make(map[string]LibraryPanel) libraryPanelMap := make(map[string]LibraryPanelDTO)
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error { err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
sql := `SELECT var libraryPanels []LibraryPanelWithMeta
lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by sql := sqlStatmentLibrayPanelDTOWithMeta + "INNER JOIN library_panel_dashboard AS lpd ON lpd.librarypanel_id = lp.id AND lpd.dashboard_id=?"
FROM
library_panel_dashboard AS lpd
INNER JOIN
library_panel AS lp ON lpd.librarypanel_id = lp.id AND lpd.dashboard_id=?`
var libraryPanels []LibraryPanel
sess := session.SQL(sql, dashboardID) sess := session.SQL(sql, dashboardID)
err := sess.Find(&libraryPanels) err := sess.Find(&libraryPanels)
if err != nil { if err != nil {
...@@ -343,7 +333,30 @@ func (lps *LibraryPanelService) getLibraryPanelsForDashboardID(dashboardID int64 ...@@ -343,7 +333,30 @@ func (lps *LibraryPanelService) getLibraryPanelsForDashboardID(dashboardID int64
} }
for _, panel := range libraryPanels { for _, panel := range libraryPanels {
libraryPanelMap[panel.UID] = panel libraryPanelMap[panel.UID] = LibraryPanelDTO{
ID: panel.ID,
OrgID: panel.OrgID,
FolderID: panel.FolderID,
UID: panel.UID,
Name: panel.Name,
Model: panel.Model,
Meta: LibraryPanelDTOMeta{
CanEdit: panel.CanEdit,
ConnectedDashboards: panel.ConnectedDashboards,
Created: panel.Created,
Updated: panel.Updated,
CreatedBy: LibraryPanelDTOMetaUser{
ID: panel.CreatedBy,
Name: panel.CreatedByName,
AvatarUrl: dtos.GetGravatarUrl(panel.CreatedByEmail),
},
UpdatedBy: LibraryPanelDTOMetaUser{
ID: panel.UpdatedBy,
Name: panel.UpdatedByName,
AvatarUrl: dtos.GetGravatarUrl(panel.UpdatedByEmail),
},
},
}
} }
return nil return nil
...@@ -402,10 +415,11 @@ func (lps *LibraryPanelService) patchLibraryPanel(c *models.ReqContext, cmd patc ...@@ -402,10 +415,11 @@ func (lps *LibraryPanelService) patchLibraryPanel(c *models.ReqContext, cmd patc
Model: libraryPanel.Model, Model: libraryPanel.Model,
Meta: LibraryPanelDTOMeta{ Meta: LibraryPanelDTOMeta{
CanEdit: true, CanEdit: true,
ConnectedDashboards: panelInDB.ConnectedDashboards,
Created: libraryPanel.Created, Created: libraryPanel.Created,
Updated: libraryPanel.Updated, Updated: libraryPanel.Updated,
CreatedBy: LibraryPanelDTOMetaUser{ CreatedBy: LibraryPanelDTOMetaUser{
ID: libraryPanel.CreatedBy, ID: panelInDB.CreatedBy,
Name: panelInDB.CreatedByName, Name: panelInDB.CreatedByName,
AvatarUrl: dtos.GetGravatarUrl(panelInDB.CreatedByEmail), AvatarUrl: dtos.GetGravatarUrl(panelInDB.CreatedByEmail),
}, },
......
...@@ -104,6 +104,22 @@ func (lps *LibraryPanelService) LoadLibraryPanelsForDashboard(dash *models.Dashb ...@@ -104,6 +104,22 @@ func (lps *LibraryPanelService) LoadLibraryPanelsForDashboard(dash *models.Dashb
elem.Set("libraryPanel", map[string]interface{}{ elem.Set("libraryPanel", map[string]interface{}{
"uid": libraryPanelInDB.UID, "uid": libraryPanelInDB.UID,
"name": libraryPanelInDB.Name, "name": libraryPanelInDB.Name,
"meta": map[string]interface{}{
"canEdit": libraryPanelInDB.Meta.CanEdit,
"connectedDashboards": libraryPanelInDB.Meta.ConnectedDashboards,
"created": libraryPanelInDB.Meta.Created,
"updated": libraryPanelInDB.Meta.Updated,
"createdBy": map[string]interface{}{
"id": libraryPanelInDB.Meta.CreatedBy.ID,
"name": libraryPanelInDB.Meta.CreatedBy.Name,
"avatarUrl": libraryPanelInDB.Meta.CreatedBy.AvatarUrl,
},
"updatedBy": map[string]interface{}{
"id": libraryPanelInDB.Meta.UpdatedBy.ID,
"name": libraryPanelInDB.Meta.UpdatedBy.Name,
"avatarUrl": libraryPanelInDB.Meta.UpdatedBy.AvatarUrl,
},
},
}) })
} }
......
...@@ -35,6 +35,7 @@ type LibraryPanelWithMeta struct { ...@@ -35,6 +35,7 @@ type LibraryPanelWithMeta struct {
Updated time.Time Updated time.Time
CanEdit bool CanEdit bool
ConnectedDashboards int64
CreatedBy int64 CreatedBy int64
UpdatedBy int64 UpdatedBy int64
CreatedByName string CreatedByName string
...@@ -57,6 +58,7 @@ type LibraryPanelDTO struct { ...@@ -57,6 +58,7 @@ type LibraryPanelDTO struct {
// LibraryPanelDTOMeta is the meta information for LibraryPanelDTO. // LibraryPanelDTOMeta is the meta information for LibraryPanelDTO.
type LibraryPanelDTOMeta struct { type LibraryPanelDTOMeta struct {
CanEdit bool `json:"canEdit"` CanEdit bool `json:"canEdit"`
ConnectedDashboards int64 `json:"connectedDashboards"`
Created time.Time `json:"created"` Created time.Time `json:"created"`
Updated time.Time `json:"updated"` Updated time.Time `json:"updated"`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment