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"
|
2022-10-14 14:33:06 -05:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
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.
|
2021-05-11 14:03:04 -05:00
|
|
|
type ChannelPublisher func(orgID int64, channel string, data []byte) error
|
2020-10-01 12:46:14 -05:00
|
|
|
|
2021-04-23 14:55:31 -05:00
|
|
|
// ChannelClientCount will return the number of clients for a channel
|
2021-05-11 14:03:04 -05:00
|
|
|
type ChannelClientCount func(orgID int64, channel string) (int, error)
|
2021-04-23 14:55:31 -05:00
|
|
|
|
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-12-14 11:12:00 -06:00
|
|
|
Data json.RawMessage
|
2021-03-30 05:23:29 -05:00
|
|
|
}
|
|
|
|
|
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
|
2022-08-10 04:56:48 -05:00
|
|
|
OnSubscribe(ctx context.Context, user *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.
|
2022-08-10 04:56:48 -05:00
|
|
|
OnPublish(ctx context.Context, user *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
|
2022-08-10 04:56:48 -05:00
|
|
|
DashboardSaved(orgID int64, user *user.UserDisplayDTO, message string, dashboard *Dashboard, err error) error
|
2021-04-23 14:55:31 -05:00
|
|
|
|
|
|
|
// Called when a dashboard is deleted
|
2022-08-10 04:56:48 -05:00
|
|
|
DashboardDeleted(orgID int64, user *user.UserDisplayDTO, uid string) error
|
2021-04-23 14:55:31 -05:00
|
|
|
|
|
|
|
// Experimental! Indicate is GitOps is active. This really means
|
|
|
|
// someone is subscribed to the `grafana/dashboards/gitops` channel
|
2021-05-11 14:03:04 -05:00
|
|
|
HasGitOpsObserver(orgID int64) bool
|
2020-10-01 12:46:14 -05:00
|
|
|
}
|
2021-04-30 13:06:33 -05:00
|
|
|
|
|
|
|
type LiveMessage struct {
|
|
|
|
Id int64
|
|
|
|
OrgId int64
|
|
|
|
Channel string
|
|
|
|
Data json.RawMessage
|
|
|
|
Published time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type SaveLiveMessageQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
Channel string
|
|
|
|
Data json.RawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetLiveMessageQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
Channel string
|
|
|
|
}
|