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-23 14:55:31 -05:00
|
|
|
// ChannelClientCount will return the number of clients for a channel
|
|
|
|
type ChannelClientCount func(channel string) (int, 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
|
2021-04-08 14:40:06 -05:00
|
|
|
// into a stream upon OnPublish but returning a data here
|
2021-04-02 11:41:45 -05:00
|
|
|
// 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 {
|
2021-04-23 14:55:31 -05:00
|
|
|
// Called when a dashboard is saved -- this includes the error so we can support a
|
|
|
|
// gitops workflow that knows if the value was saved to the local database or not
|
|
|
|
// in many cases all direct save requests will fail, but the request should be forwarded
|
|
|
|
// to any gitops observers
|
|
|
|
DashboardSaved(user *UserDisplayDTO, message string, dashboard *Dashboard, err error) error
|
|
|
|
|
|
|
|
// Called when a dashboard is deleted
|
|
|
|
DashboardDeleted(user *UserDisplayDTO, uid string) error
|
|
|
|
|
|
|
|
// Experimental! Indicate is GitOps is active. This really means
|
|
|
|
// someone is subscribed to the `grafana/dashboards/gitops` channel
|
|
|
|
HasGitOpsObserver() bool
|
2020-10-01 12:46:14 -05:00
|
|
|
}
|