mirror of
https://github.com/grafana/grafana.git
synced 2025-01-26 08:16:59 -06:00
35a755fe50
* PanelLibrary: Adds uid and renames title to name * Chore: removing lines * Chore: updates comments * Chore: changes after PR comments
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package librarypanels
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
)
|
|
|
|
// createLibraryPanel adds a Library Panel
|
|
func (lps *LibraryPanelService) createLibraryPanel(c *models.ReqContext, cmd createLibraryPanelCommand) (LibraryPanel, error) {
|
|
libraryPanel := LibraryPanel{
|
|
OrgID: c.SignedInUser.OrgId,
|
|
FolderID: cmd.FolderID,
|
|
UID: util.GenerateShortUID(),
|
|
Name: cmd.Name,
|
|
Model: cmd.Model,
|
|
|
|
Created: time.Now(),
|
|
Updated: time.Now(),
|
|
|
|
CreatedBy: c.SignedInUser.UserId,
|
|
UpdatedBy: c.SignedInUser.UserId,
|
|
}
|
|
err := lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
if res, err := session.Query("SELECT 1 FROM library_panel WHERE org_id=? AND folder_id=? AND name=?",
|
|
c.SignedInUser.OrgId, cmd.FolderID, cmd.Name); err != nil {
|
|
return err
|
|
} else if len(res) == 1 {
|
|
return errLibraryPanelAlreadyAdded
|
|
}
|
|
|
|
if _, err := session.Insert(&libraryPanel); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return libraryPanel, err
|
|
}
|
|
|
|
// deleteLibraryPanel deletes a Library Panel
|
|
func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, uid string) error {
|
|
orgID := c.SignedInUser.OrgId
|
|
return lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
result, err := session.Exec("DELETE FROM library_panel WHERE uid=? and org_id=?", uid, orgID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if rowsAffected, err := result.RowsAffected(); err != nil {
|
|
return err
|
|
} else if rowsAffected != 1 {
|
|
return errLibraryPanelNotFound
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// getLibraryPanel gets a Library Panel.
|
|
func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, uid string) (LibraryPanel, error) {
|
|
orgID := c.SignedInUser.OrgId
|
|
var libraryPanel LibraryPanel
|
|
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
libraryPanels := make([]LibraryPanel, 0)
|
|
err := session.SQL("SELECT * FROM library_panel WHERE uid=? and org_id=?", uid, orgID).Find(&libraryPanels)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(libraryPanels) == 0 {
|
|
return errLibraryPanelNotFound
|
|
}
|
|
if len(libraryPanels) > 1 {
|
|
return fmt.Errorf("found %d panels, while expecting at most one", len(libraryPanels))
|
|
}
|
|
|
|
libraryPanel = libraryPanels[0]
|
|
|
|
return nil
|
|
})
|
|
|
|
return libraryPanel, err
|
|
}
|
|
|
|
// getAllLibraryPanels gets all library panels.
|
|
func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]LibraryPanel, error) {
|
|
orgID := c.SignedInUser.OrgId
|
|
libraryPanels := make([]LibraryPanel, 0)
|
|
|
|
err := lps.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
|
|
err := session.SQL("SELECT * FROM library_panel WHERE org_id=?", orgID).Find(&libraryPanels)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return libraryPanels, err
|
|
}
|