-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstat.go
84 lines (74 loc) · 1.85 KB
/
stat.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
)
var quotaName string
var statCmd = &cobra.Command{
Use: "stat",
Short: "Show quota statistics using JSON input file",
Run: func(cmd *cobra.Command, args []string) {
input, err := parseInputFile(inputFilePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
printStat(*input, quotaName)
},
}
func init() {
initCommonFlags(statCmd)
statCmd.PersistentFlags().StringVar("aName, "quotaName", "", "quota name")
}
func printStat(input Input, quotaName string) {
hostsMap := input.Hosts
quotaMap := input.Quota
aliasesMap := input.Aliases
for qn, aliases := range aliasesMap {
if _, ok := quotaMap[qn]; ok {
for _, alias := range aliases {
quotaMap[alias] = quotaMap[qn]
}
} else {
fmt.Printf("Missing reference quota %s\n", qn)
os.Exit(1)
}
}
if len(quotaName) > 0 {
if _, ok := quotaMap[quotaName]; ok {
processJsonQuota(quotaName, quotaMap[quotaName], hostsMap)
} else {
fmt.Printf("Missing quota %s\n", quotaName)
os.Exit(1)
}
} else {
for qn, jsonQuota := range quotaMap {
processJsonQuota(qn, jsonQuota, hostsMap)
}
}
}
func processJsonQuota(quotaName string, quota Quota, hostsMap Hosts) {
fmt.Println(quotaName)
fmt.Println("---")
for browserName, browser := range quota {
for versionName, hostsRef := range browser.Versions {
regions := hostsMap[hostsRef]
if regions != nil {
regionsStat := ""
for regionName, region := range regions {
regionTotal := 0
for hostPattern, host := range region {
hostNames := parseHostPattern(hostPattern)
regionTotal += len(hostNames) * host.Count
}
regionsStat += fmt.Sprintf(" %s = %d", regionName, regionTotal)
}
if len(regionsStat) > 0 {
fmt.Printf("%s %s%s\n", browserName, versionName, regionsStat)
}
}
}
}
fmt.Println()
}