mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
Merge pull request #1855 from weaveworks/extract-pluralize
refactor: extract pluralization
This commit is contained in:
@@ -57,19 +57,26 @@ type NodeSummary struct {
|
||||
Adjacency report.IDList `json:"adjacency,omitempty"`
|
||||
}
|
||||
|
||||
var renderers = map[string]func(NodeSummary, report.Node) (NodeSummary, bool){
|
||||
render.Pseudo: pseudoNodeSummary,
|
||||
report.Process: processNodeSummary,
|
||||
report.Container: containerNodeSummary,
|
||||
report.ContainerImage: containerImageNodeSummary,
|
||||
report.Pod: podNodeSummary,
|
||||
report.Service: podGroupNodeSummary,
|
||||
report.Deployment: podGroupNodeSummary,
|
||||
report.ReplicaSet: podGroupNodeSummary,
|
||||
report.Host: hostNodeSummary,
|
||||
}
|
||||
|
||||
var templates = map[string]struct{ Label, LabelMinor string }{
|
||||
render.TheInternetID: {render.InboundMajor, ""},
|
||||
render.IncomingInternetID: {render.InboundMajor, render.InboundMinor},
|
||||
render.OutgoingInternetID: {render.OutboundMajor, render.OutboundMinor},
|
||||
}
|
||||
|
||||
// MakeNodeSummary summarizes a node, if possible.
|
||||
func MakeNodeSummary(r report.Report, n report.Node) (NodeSummary, bool) {
|
||||
renderers := map[string]func(NodeSummary, report.Node) (NodeSummary, bool){
|
||||
render.Pseudo: pseudoNodeSummary,
|
||||
report.Process: processNodeSummary,
|
||||
report.Container: containerNodeSummary,
|
||||
report.ContainerImage: containerImageNodeSummary,
|
||||
report.Pod: podNodeSummary,
|
||||
report.Service: serviceNodeSummary,
|
||||
report.Deployment: deploymentNodeSummary,
|
||||
report.ReplicaSet: replicaSetNodeSummary,
|
||||
report.Host: hostNodeSummary,
|
||||
}
|
||||
if renderer, ok := renderers[n.Topology]; ok {
|
||||
return renderer(baseNodeSummary(r, n), n)
|
||||
}
|
||||
@@ -108,11 +115,7 @@ func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
base.Pseudo = true
|
||||
base.Rank = n.ID
|
||||
|
||||
if template, ok := map[string]struct{ Label, LabelMinor string }{
|
||||
render.TheInternetID: {render.InboundMajor, ""},
|
||||
render.IncomingInternetID: {render.InboundMajor, render.InboundMinor},
|
||||
render.OutgoingInternetID: {render.OutboundMajor, render.OutboundMinor},
|
||||
}[n.ID]; ok {
|
||||
if template, ok := templates[n.ID]; ok {
|
||||
base.Label = template.Label
|
||||
base.LabelMinor = template.LabelMinor
|
||||
base.Shape = report.Cloud
|
||||
@@ -195,13 +198,8 @@ func containerImageNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bo
|
||||
}
|
||||
}
|
||||
|
||||
if c, ok := n.Counters.Lookup(report.Container); ok {
|
||||
if c == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d container", c)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d containers", c)
|
||||
}
|
||||
}
|
||||
base.LabelMinor = pluralize(n.Counters, report.Container, "container", "containers")
|
||||
|
||||
return base, true
|
||||
}
|
||||
|
||||
@@ -214,60 +212,18 @@ func addKubernetesLabelAndRank(base NodeSummary, n report.Node) NodeSummary {
|
||||
|
||||
func podNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
base = addKubernetesLabelAndRank(base, n)
|
||||
if c, ok := n.Counters.Lookup(report.Container); ok {
|
||||
if c == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d container", c)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d containers", c)
|
||||
}
|
||||
}
|
||||
base.LabelMinor = pluralize(n.Counters, report.Container, "container", "containers")
|
||||
|
||||
return base, true
|
||||
}
|
||||
|
||||
func serviceNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
func podGroupNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
base = addKubernetesLabelAndRank(base, n)
|
||||
base.Stack = true
|
||||
|
||||
// Services are always just a group of pods, so there's no counting multiple
|
||||
// services which might be grouped together.
|
||||
if p, ok := n.Counters.Lookup(report.Pod); ok {
|
||||
if p == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d pod", p)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d pods", p)
|
||||
}
|
||||
}
|
||||
|
||||
return base, true
|
||||
}
|
||||
|
||||
func deploymentNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
base = addKubernetesLabelAndRank(base, n)
|
||||
base.Stack = true
|
||||
|
||||
if p, ok := n.Counters.Lookup(report.Pod); ok {
|
||||
if p == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d pod", p)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d pods", p)
|
||||
}
|
||||
}
|
||||
|
||||
return base, true
|
||||
}
|
||||
|
||||
func replicaSetNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
|
||||
base = addKubernetesLabelAndRank(base, n)
|
||||
base.Stack = true
|
||||
|
||||
if p, ok := n.Counters.Lookup(report.Pod); ok {
|
||||
if p == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d pod", p)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d pods", p)
|
||||
}
|
||||
}
|
||||
// NB: pods are the highest aggregation level for which we display
|
||||
// counts.
|
||||
base.LabelMinor = pluralize(n.Counters, report.Pod, "pod", "pods")
|
||||
|
||||
return base, true
|
||||
}
|
||||
@@ -303,13 +259,7 @@ func groupNodeSummary(base NodeSummary, r report.Report, n report.Node) (NodeSum
|
||||
|
||||
t, ok := r.Topology(parts[1])
|
||||
if ok && t.Label != "" {
|
||||
if count, ok := n.Counters.Lookup(parts[1]); ok {
|
||||
if count == 1 {
|
||||
base.LabelMinor = fmt.Sprintf("%d %s", count, t.Label)
|
||||
} else {
|
||||
base.LabelMinor = fmt.Sprintf("%d %s", count, t.LabelPlural)
|
||||
}
|
||||
}
|
||||
base.LabelMinor = pluralize(n.Counters, parts[1], t.Label, t.LabelPlural)
|
||||
}
|
||||
|
||||
base.Shape = t.GetShape()
|
||||
@@ -317,6 +267,16 @@ func groupNodeSummary(base NodeSummary, r report.Report, n report.Node) (NodeSum
|
||||
return base, true
|
||||
}
|
||||
|
||||
func pluralize(counters report.Counters, key, singular, plural string) string {
|
||||
if c, ok := counters.Lookup(key); ok {
|
||||
if c == 1 {
|
||||
return fmt.Sprintf("%d %s", c, singular)
|
||||
}
|
||||
return fmt.Sprintf("%d %s", c, plural)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type nodeSummariesByID []NodeSummary
|
||||
|
||||
func (s nodeSummariesByID) Len() int { return len(s) }
|
||||
|
||||
Reference in New Issue
Block a user