-
Notifications
You must be signed in to change notification settings - Fork 67
/
statistic.go
127 lines (103 loc) · 2.9 KB
/
statistic.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package statistic
import (
"github.com/gardener/controller-manager-library/pkg/resources"
)
type (
WalkingState interface{}
OwnerWalker func(state WalkingState, owner, ptype string, pname resources.ObjectName, count int) WalkingState
)
type ProviderStatistic map[resources.ObjectName]int
func (this ProviderStatistic) Inc(name resources.ObjectName) {
if name == nil {
name = resources.NewObjectName("")
}
this[name]++
}
func (this ProviderStatistic) Count() int {
sum := 0
for _, e := range this {
sum += e
}
return sum
}
func (this ProviderStatistic) Walk(state WalkingState, walker OwnerWalker, owner, ptype string) WalkingState {
for pname, e := range this {
state = walker(state, owner, ptype, pname, e)
}
return state
}
////////////////////////////////////////////////////////////////////////////////
type ProviderTypeStatistic map[string]ProviderStatistic
func (this ProviderTypeStatistic) Inc(ptype string, pname resources.ObjectName) {
this.Assure(ptype).Inc(pname)
}
func (this ProviderTypeStatistic) Count() int {
sum := 0
for _, e := range this {
sum += e.Count()
}
return sum
}
func (this ProviderTypeStatistic) Get(ptype string) ProviderStatistic {
if ps := this[ptype]; ps != nil {
return ps
}
return ProviderStatistic{}
}
func (this ProviderTypeStatistic) Assure(ptype string) ProviderStatistic {
cur := this[ptype]
if cur == nil {
cur = ProviderStatistic{}
this[ptype] = cur
}
return cur
}
func (this ProviderTypeStatistic) Walk(state WalkingState, walker OwnerWalker, owner string) WalkingState {
for ptype, pts := range this {
state = pts.Walk(state, walker, owner, ptype)
}
return state
}
////////////////////////////////////////////////////////////////////////////////
type OwnerStatistic map[string]ProviderTypeStatistic
func (this OwnerStatistic) Inc(owner, ptype string, pname resources.ObjectName) {
this.Assure(owner).Inc(ptype, pname)
}
func (this OwnerStatistic) Count() int {
sum := 0
for _, e := range this {
sum += e.Count()
}
return sum
}
func (this OwnerStatistic) Get(owner string) ProviderTypeStatistic {
if pts := this[owner]; pts != nil {
return pts
}
return ProviderTypeStatistic{}
}
func (this OwnerStatistic) Assure(owner string) ProviderTypeStatistic {
cur := this[owner]
if cur == nil {
cur = ProviderTypeStatistic{}
this[owner] = cur
}
return cur
}
func (this OwnerStatistic) Walk(state WalkingState, walker OwnerWalker) WalkingState {
for o, os := range this {
state = os.Walk(state, walker, o)
}
return state
}
////////////////////////////////////////////////////////////////////////////////
type EntryStatistic struct {
Providers ProviderTypeStatistic
Owners OwnerStatistic
}
func NewEntryStatistic() *EntryStatistic {
return &EntryStatistic{ProviderTypeStatistic{}, OwnerStatistic{}}
}