mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
public-dashboards: Add log statement when public dashboard enabled or disabled (#54133)
* refactor apis for consistent outputs * add dashboardUid validation at API layer * add check for empty dashboardUid on SavePublicDashboard * remove public dashboard errors from models package. Co-authored-by: Ezequiel Victorero <evictorero@gmail.com>
This commit is contained in:
@@ -86,11 +86,12 @@ func (pd *PublicDashboardServiceImpl) GetPublicDashboardConfig(ctx context.Conte
|
||||
|
||||
// SavePublicDashboardConfig is a helper method to persist the sharing config
|
||||
// to the database. It handles validations for sharing config and persistence
|
||||
func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Context, dto *SavePublicDashboardConfigDTO) (*PublicDashboard, error) {
|
||||
func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Context, u *user.SignedInUser, dto *SavePublicDashboardConfigDTO) (*PublicDashboard, error) {
|
||||
dashboard, err := pd.GetDashboard(ctx, dto.DashboardUid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = validation.ValidateSavePublicDashboard(dto, dashboard)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -101,22 +102,45 @@ func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Cont
|
||||
dto.PublicDashboard.TimeSettings = simplejson.New()
|
||||
}
|
||||
|
||||
if dto.PublicDashboard.Uid == "" {
|
||||
return pd.savePublicDashboardConfig(ctx, dto)
|
||||
}
|
||||
|
||||
return pd.updatePublicDashboardConfig(ctx, dto)
|
||||
}
|
||||
|
||||
func (pd *PublicDashboardServiceImpl) savePublicDashboardConfig(ctx context.Context, dto *SavePublicDashboardConfigDTO) (*PublicDashboard, error) {
|
||||
uid, err := pd.store.GenerateNewPublicDashboardUid(ctx)
|
||||
// get existing public dashboard if exists
|
||||
existingPubdash, err := pd.store.GetPublicDashboardByUid(ctx, dto.PublicDashboard.Uid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// save changes
|
||||
var pubdashUid string
|
||||
if existingPubdash == nil {
|
||||
pubdashUid, err = pd.savePublicDashboardConfig(ctx, dto)
|
||||
} else {
|
||||
pubdashUid, err = pd.updatePublicDashboardConfig(ctx, dto)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Get latest public dashboard to return
|
||||
newPubdash, err := pd.store.GetPublicDashboardByUid(ctx, pubdashUid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pd.logIsEnabledChanged(existingPubdash, newPubdash, u)
|
||||
|
||||
return newPubdash, err
|
||||
}
|
||||
|
||||
// Called by SavePublicDashboardConfig this handles business logic
|
||||
// to generate token and calls create at the database layer
|
||||
func (pd *PublicDashboardServiceImpl) savePublicDashboardConfig(ctx context.Context, dto *SavePublicDashboardConfigDTO) (string, error) {
|
||||
uid, err := pd.store.GenerateNewPublicDashboardUid(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
accessToken, err := GenerateAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
cmd := SavePublicDashboardConfigCommand{
|
||||
@@ -132,10 +156,17 @@ func (pd *PublicDashboardServiceImpl) savePublicDashboardConfig(ctx context.Cont
|
||||
},
|
||||
}
|
||||
|
||||
return pd.store.SavePublicDashboardConfig(ctx, cmd)
|
||||
err = pd.store.SavePublicDashboardConfig(ctx, cmd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func (pd *PublicDashboardServiceImpl) updatePublicDashboardConfig(ctx context.Context, dto *SavePublicDashboardConfigDTO) (*PublicDashboard, error) {
|
||||
// Called by SavePublicDashboard this handles business logic for updating a
|
||||
// dashboard and calls update at the database layer
|
||||
func (pd *PublicDashboardServiceImpl) updatePublicDashboardConfig(ctx context.Context, dto *SavePublicDashboardConfigDTO) (string, error) {
|
||||
cmd := SavePublicDashboardConfigCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
Uid: dto.PublicDashboard.Uid,
|
||||
@@ -146,17 +177,7 @@ func (pd *PublicDashboardServiceImpl) updatePublicDashboardConfig(ctx context.Co
|
||||
},
|
||||
}
|
||||
|
||||
err := pd.store.UpdatePublicDashboardConfig(ctx, cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
publicDashboard, err := pd.store.GetPublicDashboardConfig(ctx, dto.OrgId, dto.DashboardUid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return publicDashboard, nil
|
||||
return dto.PublicDashboard.Uid, pd.store.UpdatePublicDashboardConfig(ctx, cmd)
|
||||
}
|
||||
|
||||
// BuildPublicDashboardMetricRequest merges public dashboard parameters with
|
||||
@@ -218,3 +239,23 @@ func GenerateAccessToken() (string, error) {
|
||||
|
||||
return fmt.Sprintf("%x", token[:]), nil
|
||||
}
|
||||
|
||||
// Log when PublicDashboard.IsEnabled changed
|
||||
func (pd *PublicDashboardServiceImpl) logIsEnabledChanged(existingPubdash *PublicDashboard, newPubdash *PublicDashboard, u *user.SignedInUser) {
|
||||
if publicDashboardIsEnabledChanged(existingPubdash, newPubdash) {
|
||||
verb := "disabled"
|
||||
if newPubdash.IsEnabled {
|
||||
verb = "enabled"
|
||||
}
|
||||
pd.log.Info(fmt.Sprintf("Public dashboard %v: dashboardUid: %v, user:%v", verb, newPubdash.Uid, u.Login))
|
||||
}
|
||||
}
|
||||
|
||||
// Checks to see if PublicDashboard.Isenabled is true on create or changed on update
|
||||
func publicDashboardIsEnabledChanged(existingPubdash *PublicDashboard, newPubdash *PublicDashboard) bool {
|
||||
// creating dashboard, enabled true
|
||||
newDashCreated := existingPubdash == nil && newPubdash.IsEnabled
|
||||
// updating dashboard, enabled changed
|
||||
isEnabledChanged := existingPubdash != nil && newPubdash.IsEnabled != existingPubdash.IsEnabled
|
||||
return newDashCreated || isEnabledChanged
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user