From 32b74e75a30a253602c630728d46ef2ae141d2c3 Mon Sep 17 00:00:00 2001 From: Alexander Emelin Date: Fri, 23 Jul 2021 19:47:08 +0300 Subject: [PATCH] live: various code cleanups (#37165) --- pkg/services/live/features/dashboard.go | 28 ++++++++++++------------ pkg/services/live/live.go | 2 -- pkg/services/live/livecontext/context.go | 16 -------------- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/pkg/services/live/features/dashboard.go b/pkg/services/live/features/dashboard.go index 54f1029580b..cd8be5f3cc1 100644 --- a/pkg/services/live/features/dashboard.go +++ b/pkg/services/live/features/dashboard.go @@ -16,12 +16,12 @@ import ( type actionType string const ( - ACTION_SAVED actionType = "saved" - ACTION_DELETED actionType = "deleted" - EDITING_STARTED actionType = "editing-started" - EDITING_FINISHED actionType = "editing-finished" + ActionSaved actionType = "saved" + ActionDeleted actionType = "deleted" + EditingStarted actionType = "editing-started" + //EditingFinished actionType = "editing-finished" - GITOPS_CHANNEL = "grafana/dashboard/gitops" + GitopsChannel = "grafana/dashboard/gitops" ) // DashboardEvent events related to dashboards @@ -42,7 +42,7 @@ type DashboardHandler struct { } // GetHandlerForPath called on init -func (h *DashboardHandler) GetHandlerForPath(path string) (models.ChannelHandler, error) { +func (h *DashboardHandler) GetHandlerForPath(_ string) (models.ChannelHandler, error) { return h, nil // all dashboards share the same handler } @@ -85,7 +85,7 @@ func (h *DashboardHandler) OnSubscribe(_ context.Context, user *models.SignedInU } // OnPublish is called when someone begins to edit a dashboard -func (h *DashboardHandler) OnPublish(ctx context.Context, user *models.SignedInUser, e models.PublishEvent) (models.PublishReply, backend.PublishStreamStatus, error) { +func (h *DashboardHandler) OnPublish(_ context.Context, user *models.SignedInUser, e models.PublishEvent) (models.PublishReply, backend.PublishStreamStatus, error) { parts := strings.Split(e.Path, "/") if parts[0] == "gitops" { // gitops gets all changes for everything, so lets make sure it is an admin user @@ -104,7 +104,7 @@ func (h *DashboardHandler) OnPublish(ctx context.Context, user *models.SignedInU if err != nil || event.UID != parts[1] { return models.PublishReply{}, backend.PublishStreamStatusNotFound, fmt.Errorf("bad request") } - if event.Action != EDITING_STARTED { + if event.Action != EditingStarted { // just ignore the event return models.PublishReply{}, backend.PublishStreamStatusNotFound, fmt.Errorf("ignore???") } @@ -114,8 +114,8 @@ func (h *DashboardHandler) OnPublish(ctx context.Context, user *models.SignedInU return models.PublishReply{}, backend.PublishStreamStatusNotFound, nil } - guardian := guardian.New(query.Result.Id, user.OrgId, user) - canEdit, err := guardian.CanEdit() + guard := guardian.New(query.Result.Id, user.OrgId, user) + canEdit, err := guard.CanEdit() if err != nil { return models.PublishReply{}, backend.PublishStreamStatusNotFound, fmt.Errorf("internal error") } @@ -154,7 +154,7 @@ func (h *DashboardHandler) publish(orgID int64, event dashboardEvent) error { } // Send everything to the gitops channel - return h.Publisher(orgID, GITOPS_CHANNEL, msg) + return h.Publisher(orgID, GitopsChannel, msg) } // DashboardSaved will broadcast to all connected dashboards @@ -165,7 +165,7 @@ func (h *DashboardHandler) DashboardSaved(orgID int64, user *models.UserDisplayD msg := dashboardEvent{ UID: dashboard.Uid, - Action: ACTION_SAVED, + Action: ActionSaved, User: user, Message: message, Dashboard: dashboard, @@ -182,14 +182,14 @@ func (h *DashboardHandler) DashboardSaved(orgID int64, user *models.UserDisplayD func (h *DashboardHandler) DashboardDeleted(orgID int64, user *models.UserDisplayDTO, uid string) error { return h.publish(orgID, dashboardEvent{ UID: uid, - Action: ACTION_DELETED, + Action: ActionDeleted, User: user, }) } // HasGitOpsObserver will return true if anyone is listening to the `gitops` channel func (h *DashboardHandler) HasGitOpsObserver(orgID int64) bool { - count, err := h.ClientCount(orgID, GITOPS_CHANNEL) + count, err := h.ClientCount(orgID, GitopsChannel) if err != nil { logger.Error("error getting client count", "error", err) return false diff --git a/pkg/services/live/live.go b/pkg/services/live/live.go index 41297552fa5..71a1791814f 100644 --- a/pkg/services/live/live.go +++ b/pkg/services/live/live.go @@ -346,7 +346,6 @@ func (g *GrafanaLive) Init() error { } newCtx := centrifuge.SetCredentials(ctx.Req.Context(), cred) newCtx = livecontext.SetContextSignedUser(newCtx, user) - newCtx = livecontext.SetContextValues(newCtx, ctx.Req.URL.Query()) r := ctx.Req.Request r = r.WithContext(newCtx) wsHandler.ServeHTTP(ctx.Resp, r) @@ -355,7 +354,6 @@ func (g *GrafanaLive) Init() error { g.pushWebsocketHandler = func(ctx *models.ReqContext) { user := ctx.SignedInUser newCtx := livecontext.SetContextSignedUser(ctx.Req.Context(), user) - newCtx = livecontext.SetContextValues(newCtx, ctx.Req.URL.Query()) newCtx = livecontext.SetContextStreamID(newCtx, ctx.Params(":streamId")) r := ctx.Req.Request r = r.WithContext(newCtx) diff --git a/pkg/services/live/livecontext/context.go b/pkg/services/live/livecontext/context.go index e459294595c..44503ff2b2c 100644 --- a/pkg/services/live/livecontext/context.go +++ b/pkg/services/live/livecontext/context.go @@ -2,7 +2,6 @@ package livecontext import ( "context" - "net/url" "github.com/grafana/grafana/pkg/models" ) @@ -24,21 +23,6 @@ func GetContextSignedUser(ctx context.Context) (*models.SignedInUser, bool) { return nil, false } -type valuesContextKey struct{} - -func SetContextValues(ctx context.Context, values url.Values) context.Context { - ctx = context.WithValue(ctx, valuesContextKey{}, values) - return ctx -} - -func GetContextValues(ctx context.Context) (url.Values, bool) { - if val := ctx.Value(valuesContextKey{}); val != nil { - values, ok := val.(url.Values) - return values, ok - } - return nil, false -} - type streamIDContextKey struct{} func SetContextStreamID(ctx context.Context, streamID string) context.Context {