forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_summary.go
123 lines (101 loc) · 2.88 KB
/
process_summary.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
package v7action
import (
"fmt"
"sort"
"strings"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
log "github.com/sirupsen/logrus"
)
// ProcessSummary represents a process with instance details.
type ProcessSummary struct {
Process
InstanceDetails []ProcessInstance
}
type ProcessSummaries []ProcessSummary
func (p ProcessSummary) TotalInstanceCount() int {
return len(p.InstanceDetails)
}
func (p ProcessSummary) HealthyInstanceCount() int {
count := 0
for _, instance := range p.InstanceDetails {
if instance.State == constant.ProcessInstanceRunning {
count++
}
}
return count
}
func (ps ProcessSummaries) Sort() {
sort.Slice(ps, func(i int, j int) bool {
var iScore int
var jScore int
switch ps[i].Type {
case constant.ProcessTypeWeb:
iScore = 0
default:
iScore = 1
}
switch ps[j].Type {
case constant.ProcessTypeWeb:
jScore = 0
default:
jScore = 1
}
if iScore == 1 && jScore == 1 {
return ps[i].Type < ps[j].Type
}
return iScore < jScore
})
}
func (ps ProcessSummaries) String() string {
ps.Sort()
var summaries []string
for _, p := range ps {
summaries = append(summaries, fmt.Sprintf("%s:%d/%d", p.Type, p.HealthyInstanceCount(), p.TotalInstanceCount()))
}
return strings.Join(summaries, ", ")
}
func (actor Actor) getProcessSummariesForApp(appGUID string, withObfuscatedValues bool) (ProcessSummaries, Warnings, error) {
log.WithFields(log.Fields{
"appGUID": appGUID,
"withObfuscatedValues": withObfuscatedValues,
}).Info("retrieving process information")
ccv3Processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(appGUID)
allWarnings := Warnings(warnings)
if err != nil {
return nil, allWarnings, err
}
var processSummaries ProcessSummaries
for _, ccv3Process := range ccv3Processes {
process := Process(ccv3Process)
if withObfuscatedValues {
fullProcess, warnings, err := actor.GetProcess(ccv3Process.GUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
process = fullProcess
}
processSummary, warnings, err := actor.getProcessSummary(process)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return nil, allWarnings, err
}
processSummaries = append(processSummaries, processSummary)
}
processSummaries.Sort()
return processSummaries, allWarnings, nil
}
func (actor Actor) getProcessSummary(process Process) (ProcessSummary, Warnings, error) {
instances, warnings, err := actor.CloudControllerClient.GetProcessInstances(process.GUID)
allWarnings := Warnings(warnings)
if err != nil {
return ProcessSummary{}, allWarnings, err
}
processSummary := ProcessSummary{
Process: process,
}
for _, instance := range instances {
processSummary.InstanceDetails = append(processSummary.InstanceDetails, ProcessInstance(instance))
}
return processSummary, allWarnings, nil
}