mirror of https://github.com/grafana/grafana
Live: pipeline subscriber interface (#39299)
parent
73873f99cd
commit
e1f1773036
@ -0,0 +1,38 @@ |
|||||||
|
package pipeline |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models" |
||||||
|
"github.com/grafana/grafana/pkg/services/live/livecontext" |
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/live" |
||||||
|
) |
||||||
|
|
||||||
|
type BuiltinSubscriber struct { |
||||||
|
channelHandlerGetter ChannelHandlerGetter |
||||||
|
} |
||||||
|
|
||||||
|
type ChannelHandlerGetter interface { |
||||||
|
GetChannelHandler(user *models.SignedInUser, channel string) (models.ChannelHandler, live.Channel, error) |
||||||
|
} |
||||||
|
|
||||||
|
func NewBuiltinSubscriber(channelHandlerGetter ChannelHandlerGetter) *BuiltinSubscriber { |
||||||
|
return &BuiltinSubscriber{channelHandlerGetter: channelHandlerGetter} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *BuiltinSubscriber) Subscribe(ctx context.Context, vars Vars) (models.SubscribeReply, backend.SubscribeStreamStatus, error) { |
||||||
|
u, ok := livecontext.GetContextSignedUser(ctx) |
||||||
|
if !ok { |
||||||
|
return models.SubscribeReply{}, backend.SubscribeStreamStatusPermissionDenied, nil |
||||||
|
} |
||||||
|
handler, _, err := m.channelHandlerGetter.GetChannelHandler(u, vars.Channel) |
||||||
|
if err != nil { |
||||||
|
return models.SubscribeReply{}, 0, err |
||||||
|
} |
||||||
|
return handler.OnSubscribe(ctx, u, models.SubscribeEvent{ |
||||||
|
Channel: vars.Channel, |
||||||
|
Path: vars.Path, |
||||||
|
}) |
||||||
|
} |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
package pipeline |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/live/livecontext" |
||||||
|
"github.com/grafana/grafana/pkg/services/live/managedstream" |
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
||||||
|
"github.com/grafana/grafana/pkg/models" |
||||||
|
) |
||||||
|
|
||||||
|
type ManagedStreamSubscriber struct { |
||||||
|
managedStream *managedstream.Runner |
||||||
|
} |
||||||
|
|
||||||
|
func NewManagedStreamSubscriber(managedStream *managedstream.Runner) *ManagedStreamSubscriber { |
||||||
|
return &ManagedStreamSubscriber{managedStream: managedStream} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *ManagedStreamSubscriber) Subscribe(ctx context.Context, vars Vars) (models.SubscribeReply, backend.SubscribeStreamStatus, error) { |
||||||
|
stream, err := m.managedStream.GetOrCreateStream(vars.OrgID, vars.Scope, vars.Namespace) |
||||||
|
if err != nil { |
||||||
|
logger.Error("Error getting managed stream", "error", err) |
||||||
|
return models.SubscribeReply{}, 0, err |
||||||
|
} |
||||||
|
u, ok := livecontext.GetContextSignedUser(ctx) |
||||||
|
if !ok { |
||||||
|
return models.SubscribeReply{}, backend.SubscribeStreamStatusPermissionDenied, nil |
||||||
|
} |
||||||
|
return stream.OnSubscribe(ctx, u, models.SubscribeEvent{ |
||||||
|
Channel: vars.Channel, |
||||||
|
Path: vars.Path, |
||||||
|
}) |
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
package pipeline |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
||||||
|
"github.com/grafana/grafana/pkg/models" |
||||||
|
) |
||||||
|
|
||||||
|
type MultipleSubscriber struct { |
||||||
|
Subscribers []Subscriber |
||||||
|
} |
||||||
|
|
||||||
|
func NewMultipleSubscriber(subscribers ...Subscriber) *MultipleSubscriber { |
||||||
|
return &MultipleSubscriber{Subscribers: subscribers} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *MultipleSubscriber) Subscribe(ctx context.Context, vars Vars) (models.SubscribeReply, backend.SubscribeStreamStatus, error) { |
||||||
|
finalReply := models.SubscribeReply{} |
||||||
|
|
||||||
|
for _, s := range m.Subscribers { |
||||||
|
reply, status, err := s.Subscribe(ctx, vars) |
||||||
|
if err != nil { |
||||||
|
return models.SubscribeReply{}, 0, err |
||||||
|
} |
||||||
|
if status != backend.SubscribeStreamStatusOK { |
||||||
|
return models.SubscribeReply{}, status, nil |
||||||
|
} |
||||||
|
if finalReply.Data == nil { |
||||||
|
finalReply.Data = reply.Data |
||||||
|
} |
||||||
|
if !finalReply.JoinLeave { |
||||||
|
finalReply.JoinLeave = reply.JoinLeave |
||||||
|
} |
||||||
|
if !finalReply.Presence { |
||||||
|
finalReply.Presence = reply.Presence |
||||||
|
} |
||||||
|
if !finalReply.Recover { |
||||||
|
finalReply.Recover = reply.Recover |
||||||
|
} |
||||||
|
} |
||||||
|
return finalReply, backend.SubscribeStreamStatusOK, nil |
||||||
|
} |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
package pipeline |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
||||||
|
"github.com/grafana/grafana/pkg/models" |
||||||
|
) |
||||||
|
|
||||||
|
type PermissionDeniedSubscriber struct{} |
||||||
|
|
||||||
|
func NewPermissionDeniedSubscriber() *PermissionDeniedSubscriber { |
||||||
|
return &PermissionDeniedSubscriber{} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *PermissionDeniedSubscriber) Subscribe(ctx context.Context, vars Vars) (models.SubscribeReply, backend.SubscribeStreamStatus, error) { |
||||||
|
return models.SubscribeReply{}, backend.SubscribeStreamStatusPermissionDenied, nil |
||||||
|
} |
||||||
Loading…
Reference in new issue