kuma

Форк
0
/
zone_insight_helpers.go 
114 строк · 2.9 Кб
1
package v1alpha1
2

3
import (
4
	"time"
5

6
	"github.com/pkg/errors"
7
	"google.golang.org/protobuf/types/known/timestamppb"
8

9
	"github.com/kumahq/kuma/api/generic"
10
	util_proto "github.com/kumahq/kuma/pkg/util/proto"
11
)
12

13
var _ generic.Insight = &ZoneInsight{}
14

15
func NewSubscriptionStatus(now time.Time) *KDSSubscriptionStatus {
16
	return &KDSSubscriptionStatus{
17
		LastUpdateTime: util_proto.MustTimestampProto(now),
18
		Total:          &KDSServiceStats{},
19
		Stat:           map[string]*KDSServiceStats{},
20
	}
21
}
22

23
func (x *ZoneInsight) GetSubscription(id string) generic.Subscription {
24
	return generic.GetSubscription[*KDSSubscription](x, id)
25
}
26

27
func (x *ZoneInsight) GetLastSubscription() generic.Subscription {
28
	if len(x.GetSubscriptions()) == 0 {
29
		return (*KDSSubscription)(nil)
30
	}
31
	return x.GetSubscriptions()[len(x.GetSubscriptions())-1]
32
}
33

34
func (x *ZoneInsight) IsOnline() bool {
35
	for _, s := range x.GetSubscriptions() {
36
		if s.ConnectTime != nil && s.DisconnectTime == nil {
37
			return true
38
		}
39
	}
40
	return false
41
}
42

43
func (x *ZoneInsight) AllSubscriptions() []generic.Subscription {
44
	return generic.AllSubscriptions[*KDSSubscription](x)
45
}
46

47
func (x *KDSSubscription) SetDisconnectTime(time time.Time) {
48
	x.DisconnectTime = timestamppb.New(time)
49
}
50

51
func (x *KDSSubscription) IsOnline() bool {
52
	return x.GetConnectTime() != nil && x.GetDisconnectTime() == nil
53
}
54

55
func (x *ZoneInsight) Sum(v func(*KDSSubscription) uint64) uint64 {
56
	var result uint64 = 0
57
	for _, s := range x.GetSubscriptions() {
58
		result += v(s)
59
	}
60
	return result
61
}
62

63
func (x *ZoneInsight) UpdateSubscription(s generic.Subscription) error {
64
	if x == nil {
65
		return nil
66
	}
67
	kdsSubscription, ok := s.(*KDSSubscription)
68
	if !ok {
69
		return errors.Errorf("invalid type %T for ZoneInsight", s)
70
	}
71
	for i, sub := range x.GetSubscriptions() {
72
		if sub.GetId() == kdsSubscription.Id {
73
			x.Subscriptions[i] = kdsSubscription
74
			return nil
75
		}
76
	}
77
	x.finalizeSubscriptions()
78
	x.Subscriptions = append(x.Subscriptions, kdsSubscription)
79
	return nil
80
}
81

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{}
89
		}
90
	}
91
}
92

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
101
		}
102
	}
103
}
104

105
func NewVersion() *Version {
106
	return &Version{
107
		KumaCp: &KumaCpVersion{
108
			Version:   "",
109
			GitTag:    "",
110
			GitCommit: "",
111
			BuildDate: "",
112
		},
113
	}
114
}
115

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.