/
stats.go
53 lines (47 loc) · 1.11 KB
/
stats.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
package store
import (
"fmt"
"strings"
"text/tabwriter"
"time"
"github.com/bonnefoa/kubectl-fzf/v3/internal/k8s/resources"
)
type Stats struct {
ResourceType resources.ResourceType
ItemPerNamespace map[string]int
LastDumped time.Time
}
func GetStatsFromStores(stores []*Store) []*Stats {
stats := make([]*Stats, 0)
for _, stores := range stores {
stats = append(stats, stores.GetStats())
}
return stats
}
func (s *Stats) toTabOutput() []string {
strings := make([]string, 0)
now := time.Now()
for namespace, numItems := range s.ItemPerNamespace {
deltaDate := now.Sub(s.LastDumped).Truncate(time.Second)
line := fmt.Sprintf("%s\t%s\t%d\t%s",
s.ResourceType.String(),
namespace,
numItems,
deltaDate,
)
strings = append(strings, line)
}
return strings
}
func GetStatsOutput(stats []*Stats) string {
b := new(strings.Builder)
w := tabwriter.NewWriter(b, 0, 0, 1, ' ', tabwriter.StripEscape)
fmt.Fprintln(w, "Resource\tNamespace\tNumber\tLast Dumped")
for _, s := range stats {
for _, line := range s.toTabOutput() {
fmt.Fprintln(w, line)
}
}
w.Flush()
return b.String()
}