2020-10-01 12:46:14 -05:00
|
|
|
package models
|
|
|
|
|
2021-03-30 05:23:29 -05:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2021-04-02 11:41:45 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2021-03-30 05:23:29 -05:00
|
|
|
)
|
2020-10-01 12:46:14 -05:00
|
|
|
|
2021-04-02 11:41:45 -05:00
|
|
|
// ChannelPublisher writes data into a channel. Note that permissions are not checked.
|
2020-10-01 12:46:14 -05:00
|
|
|
type ChannelPublisher func(channel string, data []byte) error
|
|
|
|
|
2021-04-02 11:41:45 -05:00
|
|
|
// SubscribeEvent contains subscription data.
|
2021-03-30 05:23:29 -05:00
|
|
|
type SubscribeEvent struct {
|
|
|
|
Channel string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:41:45 -05:00
|
|
|
// SubscribeReply is a reaction to SubscribeEvent.
|
2021-03-30 05:23:29 -05:00
|
|
|
type SubscribeReply struct {
|
|
|
|
Presence bool
|
|
|
|
JoinLeave bool
|
|
|
|
Recover bool
|
2021-04-02 11:41:45 -05:00
|
|
|
Data json.RawMessage
|
2021-03-30 05:23:29 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:41:45 -05:00
|
|
|
// PublishEvent contains publication data.
|
2021-03-30 05:23:29 -05:00
|
|
|
type PublishEvent struct {
|
|
|
|
Channel string
|
|
|
|
Path string
|
|
|
|
Data json.RawMessage
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:41:45 -05:00
|
|
|
// PublishReply is a reaction to PublishEvent.
|
2021-03-30 05:23:29 -05:00
|
|
|
type PublishReply struct {
|
2021-04-02 11:41:45 -05:00
|
|
|
// By default, it's a handler responsibility to publish data
|
|
|
|
// into a stream upon OnPublish but setting Fallthrough to true
|
|
|
|
// will make Grafana Live publish data itself (i.e. stream handler
|
|
|
|
// just works as permission proxy in this case).
|
|
|
|
Data json.RawMessage
|
|
|
|
// HistorySize sets a stream history size.
|
2021-03-30 05:23:29 -05:00
|
|
|
HistorySize int
|
2021-04-02 11:41:45 -05:00
|
|
|
// HistoryTTL is a time that messages will live in stream history.
|
|
|
|
HistoryTTL time.Duration
|
2021-03-30 05:23:29 -05:00
|
|
|
}
|
|
|
|
|
2020-10-01 12:46:14 -05:00
|
|
|
// ChannelHandler defines the core channel behavior
|
|
|
|
type ChannelHandler interface {
|
2020-11-05 12:37:04 -06:00
|
|
|
// OnSubscribe is called when a client wants to subscribe to a channel
|
2021-04-02 11:41:45 -05:00
|
|
|
OnSubscribe(ctx context.Context, user *SignedInUser, e SubscribeEvent) (SubscribeReply, backend.SubscribeStreamStatus, error)
|
2020-10-01 12:46:14 -05:00
|
|
|
|
2020-11-05 12:37:04 -06:00
|
|
|
// OnPublish is called when a client writes a message to the channel websocket.
|
2021-04-02 11:41:45 -05:00
|
|
|
OnPublish(ctx context.Context, user *SignedInUser, e PublishEvent) (PublishReply, backend.PublishStreamStatus, error)
|
2020-10-01 12:46:14 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:10:26 -05:00
|
|
|
// ChannelHandlerFactory should be implemented by all core features.
|
|
|
|
type ChannelHandlerFactory interface {
|
|
|
|
// GetHandlerForPath gets a ChannelHandler for a path.
|
|
|
|
// This is called fast and often -- it must be synchronized
|
2020-10-01 12:46:14 -05:00
|
|
|
GetHandlerForPath(path string) (ChannelHandler, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DashboardActivityChannel is a service to advertise dashboard activity
|
|
|
|
type DashboardActivityChannel interface {
|
|
|
|
DashboardSaved(uid string, userID int64) error
|
|
|
|
DashboardDeleted(uid string, userID int64) error
|
|
|
|
}
|