6
"github.com/pkg/errors"
7
"google.golang.org/protobuf/types/known/timestamppb"
9
"github.com/kumahq/kuma/api/generic"
10
util_proto "github.com/kumahq/kuma/pkg/util/proto"
13
var _ generic.Insight = &ZoneInsight{}
15
func NewSubscriptionStatus(now time.Time) *KDSSubscriptionStatus {
16
return &KDSSubscriptionStatus{
17
LastUpdateTime: util_proto.MustTimestampProto(now),
18
Total: &KDSServiceStats{},
19
Stat: map[string]*KDSServiceStats{},
23
func (x *ZoneInsight) GetSubscription(id string) generic.Subscription {
24
return generic.GetSubscription[*KDSSubscription](x, id)
27
func (x *ZoneInsight) GetLastSubscription() generic.Subscription {
28
if len(x.GetSubscriptions()) == 0 {
29
return (*KDSSubscription)(nil)
31
return x.GetSubscriptions()[len(x.GetSubscriptions())-1]
34
func (x *ZoneInsight) IsOnline() bool {
35
for _, s := range x.GetSubscriptions() {
36
if s.ConnectTime != nil && s.DisconnectTime == nil {
43
func (x *ZoneInsight) AllSubscriptions() []generic.Subscription {
44
return generic.AllSubscriptions[*KDSSubscription](x)
47
func (x *KDSSubscription) SetDisconnectTime(time time.Time) {
48
x.DisconnectTime = timestamppb.New(time)
51
func (x *KDSSubscription) IsOnline() bool {
52
return x.GetConnectTime() != nil && x.GetDisconnectTime() == nil
55
func (x *ZoneInsight) Sum(v func(*KDSSubscription) uint64) uint64 {
57
for _, s := range x.GetSubscriptions() {
63
func (x *ZoneInsight) UpdateSubscription(s generic.Subscription) error {
67
kdsSubscription, ok := s.(*KDSSubscription)
69
return errors.Errorf("invalid type %T for ZoneInsight", s)
71
for i, sub := range x.GetSubscriptions() {
72
if sub.GetId() == kdsSubscription.Id {
73
x.Subscriptions[i] = kdsSubscription
77
x.finalizeSubscriptions()
78
x.Subscriptions = append(x.Subscriptions, kdsSubscription)
82
// CompactFinished removes detailed information about finished subscriptions to trim the object size
83
// The last subscription always has details.
84
func (x *ZoneInsight) CompactFinished() {
85
for i := 0; i < len(x.GetSubscriptions())-1; i++ {
86
x.Subscriptions[i].Config = ""
87
if status := x.Subscriptions[i].Status; status != nil {
88
status.Stat = map[string]*KDSServiceStats{}
93
// If Global CP was killed ungracefully then we can get a subscription without a DisconnectTime.
94
// Because of the way we process subscriptions the lack of DisconnectTime on old subscription
95
// will cause wrong status.
96
func (x *ZoneInsight) finalizeSubscriptions() {
97
now := util_proto.Now()
98
for _, subscription := range x.GetSubscriptions() {
99
if subscription.DisconnectTime == nil {
100
subscription.DisconnectTime = now
105
func NewVersion() *Version {
107
KumaCp: &KumaCpVersion{