-
Notifications
You must be signed in to change notification settings - Fork 335
/
zone_ingress_insight_helpers.go
91 lines (81 loc) · 2.2 KB
/
zone_ingress_insight_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package v1alpha1
import (
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/kumahq/kuma/api/helpers"
)
func (x *ZoneIngressInsight) GetSubscription(id string) (int, *DiscoverySubscription) {
for i, s := range x.GetSubscriptions() {
if s.Id == id {
return i, s
}
}
return -1, nil
}
func (x *ZoneIngressInsight) UpdateSubscription(s helpers.Subscription) {
if x == nil {
return
}
discoverySubscription, ok := s.(*DiscoverySubscription)
if !ok {
return
}
i, old := x.GetSubscription(discoverySubscription.Id)
if old != nil {
x.Subscriptions[i] = discoverySubscription
} else {
x.finalizeSubscriptions()
x.Subscriptions = append(x.Subscriptions, discoverySubscription)
}
}
// If Kuma CP was killed ungracefully then we can get a subscription without a DisconnectTime.
// Because of the way we process subscriptions the lack of DisconnectTime on old subscription
// will cause wrong status.
func (x *ZoneIngressInsight) finalizeSubscriptions() {
now := timestamppb.Now()
for _, subscription := range x.GetSubscriptions() {
if subscription.DisconnectTime == nil {
subscription.DisconnectTime = now
}
}
}
func (x *ZoneIngressInsight) IsOnline() bool {
for _, s := range x.GetSubscriptions() {
if s.ConnectTime != nil && s.DisconnectTime == nil {
return true
}
}
return false
}
func (x *ZoneIngressInsight) GetLastSubscription() helpers.Subscription {
if len(x.GetSubscriptions()) == 0 {
return nil
}
return x.GetSubscriptions()[len(x.GetSubscriptions())-1]
}
// todo(lobkovilya): delete GetLatestSubscription, use GetLastSubscription instead
func (x *ZoneIngressInsight) GetLatestSubscription() (*DiscoverySubscription, *time.Time) {
if len(x.GetSubscriptions()) == 0 {
return nil, nil
}
var idx int = 0
var latest *time.Time
for i, s := range x.GetSubscriptions() {
if err := s.ConnectTime.CheckValid(); err != nil {
continue
}
t := s.ConnectTime.AsTime()
if latest == nil || latest.Before(t) {
idx = i
latest = &t
}
}
return x.Subscriptions[idx], latest
}
func (x *ZoneIngressInsight) Sum(v func(*DiscoverySubscription) uint64) uint64 {
var result uint64 = 0
for _, s := range x.GetSubscriptions() {
result += v(s)
}
return result
}