Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Rebecca Roberts <rebecca.roberts@broadcom.com>
  • Loading branch information
ctlong authored and rroberts2222 committed Apr 1, 2024
1 parent 96b9cb0 commit 5fc0821
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 84 deletions.
13 changes: 9 additions & 4 deletions api/cloudcontroller/ccv3/process_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
"code.cloudfoundry.org/cli/types"
)

// ProcessInstance represents a single process instance for a particular
// application.
type ProcessInstance struct {
// CPU is the current CPU usage of the instance.
CPU float64
// CPU Entitlement is the current CPU entitlement usage of the instance.
CPUEntitlement types.NullFloat64
// Details is information about errors placing the instance.
Details string
// DiskQuota is the maximum disk the instance is allowed to use.
Expand Down Expand Up @@ -56,10 +59,11 @@ func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
Type string `json:"type"`
Uptime int64 `json:"uptime"`
Usage struct {
CPU float64 `json:"cpu"`
Mem uint64 `json:"mem"`
Disk uint64 `json:"disk"`
LogRate uint64 `json:"log_rate"`
CPU float64 `json:"cpu"`
CPUEntitlement types.NullFloat64 `json:"cpu_entitlement"`
Mem uint64 `json:"mem"`
Disk uint64 `json:"disk"`
LogRate uint64 `json:"log_rate"`
} `json:"usage"`
}

Expand All @@ -69,6 +73,7 @@ func (instance *ProcessInstance) UnmarshalJSON(data []byte) error {
}

instance.CPU = inputInstance.Usage.CPU
instance.CPUEntitlement = inputInstance.Usage.CPUEntitlement
instance.Details = inputInstance.Details
instance.DiskQuota = inputInstance.DiskQuota
instance.DiskUsage = inputInstance.Usage.Disk
Expand Down
5 changes: 5 additions & 0 deletions api/cloudcontroller/ccv3/process_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/ghttp"
Expand Down Expand Up @@ -92,6 +93,7 @@ var _ = Describe("ProcessInstance", func() {
"state": "RUNNING",
"usage": {
"cpu": 0.01,
"cpu_entitlement": 0.02,
"mem": 1000000,
"disk": 2000000,
"log_rate": 5000
Expand All @@ -109,6 +111,7 @@ var _ = Describe("ProcessInstance", func() {
"state": "RUNNING",
"usage": {
"cpu": 0.02,
"cpu_entitlement": 0.04,
"mem": 8000000,
"disk": 16000000,
"log_rate": 32000
Expand Down Expand Up @@ -136,6 +139,7 @@ var _ = Describe("ProcessInstance", func() {
Expect(processes).To(ConsistOf(
ProcessInstance{
CPU: 0.01,
CPUEntitlement: types.NullFloat64{IsSet: true, Value: 0.02},
Details: "some details",
DiskQuota: 4000000,
DiskUsage: 2000000,
Expand All @@ -151,6 +155,7 @@ var _ = Describe("ProcessInstance", func() {
},
ProcessInstance{
CPU: 0.02,
CPUEntitlement: types.NullFloat64{IsSet: true, Value: 0.04},
DiskQuota: 32000000,
DiskUsage: 16000000,
Index: 1,
Expand Down
10 changes: 10 additions & 0 deletions command/v7/shared/app_summary_displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/cli/util/ui"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -80,6 +81,13 @@ func formatLogRateLimit(limit int64) string {
}
}

func formatCPUEntitlement(cpuEntitlement types.NullFloat64) string {
if !cpuEntitlement.IsSet {
return ""
}
return fmt.Sprintf("%.1f%%", cpuEntitlement.Value*100)
}

func (display AppSummaryDisplayer) displayAppInstancesTable(processSummary v7action.ProcessSummary) {
table := [][]string{
{
Expand All @@ -90,6 +98,7 @@ func (display AppSummaryDisplayer) displayAppInstancesTable(processSummary v7act
display.UI.TranslateText("memory"),
display.UI.TranslateText("disk"),
display.UI.TranslateText("logging"),
display.UI.TranslateText("cpu entitlement"),
display.UI.TranslateText("details"),
},
}
Expand All @@ -112,6 +121,7 @@ func (display AppSummaryDisplayer) displayAppInstancesTable(processSummary v7act
"LogRate": bytefmt.ByteSize(instance.LogRate),
"LogRateLimit": formatLogRateLimit(instance.LogRateLimit),
}),
formatCPUEntitlement(instance.CPUEntitlement),
instance.Details,
})
}
Expand Down
67 changes: 37 additions & 30 deletions command/v7/shared/app_summary_displayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var _ = Describe("app summary displayer", func() {

const instanceStatsTitles = `state\s+since\s+cpu\s+memory\s+disk\s+logging\s+details`
const instanceStatsTitles = `state\s+since\s+cpu\s+memory\s+disk\s+logging\s+cpu entitlement\s+details`

var (
appSummaryDisplayer *AppSummaryDisplayer
Expand Down Expand Up @@ -65,39 +65,42 @@ var _ = Describe("app summary displayer", func() {
Sidecars: []resources.Sidecar{},
InstanceDetails: []v7action.ProcessInstance{
v7action.ProcessInstance{
Index: 0,
State: constant.ProcessInstanceRunning,
MemoryUsage: 1000000,
DiskUsage: 1000000,
LogRate: 1024,
MemoryQuota: 33554432,
DiskQuota: 2000000,
LogRateLimit: 1024 * 5,
Uptime: uptime,
Details: "Some Details 1",
Index: 0,
State: constant.ProcessInstanceRunning,
CPUEntitlement: types.NullFloat64{Value: 0, IsSet: true},
MemoryUsage: 1000000,
DiskUsage: 1000000,
LogRate: 1024,
MemoryQuota: 33554432,
DiskQuota: 2000000,
LogRateLimit: 1024 * 5,
Uptime: uptime,
Details: "Some Details 1",
},
v7action.ProcessInstance{
Index: 1,
State: constant.ProcessInstanceRunning,
MemoryUsage: 2000000,
DiskUsage: 2000000,
LogRate: 1024 * 2,
MemoryQuota: 33554432,
DiskQuota: 4000000,
LogRateLimit: 1024 * 5,
Uptime: time.Since(time.Unix(330480000, 0)),
Details: "Some Details 2",
Index: 1,
State: constant.ProcessInstanceRunning,
CPUEntitlement: types.NullFloat64{Value: 0, IsSet: false},
MemoryUsage: 2000000,
DiskUsage: 2000000,
LogRate: 1024 * 2,
MemoryQuota: 33554432,
DiskQuota: 4000000,
LogRateLimit: 1024 * 5,
Uptime: time.Since(time.Unix(330480000, 0)),
Details: "Some Details 2",
},
v7action.ProcessInstance{
Index: 2,
State: constant.ProcessInstanceRunning,
MemoryUsage: 3000000,
DiskUsage: 3000000,
LogRate: 1024 * 3,
MemoryQuota: 33554432,
DiskQuota: 6000000,
LogRateLimit: 1024 * 5,
Uptime: time.Since(time.Unix(1277164800, 0)),
Index: 2,
State: constant.ProcessInstanceRunning,
CPUEntitlement: types.NullFloat64{Value: 0.03, IsSet: true},
MemoryUsage: 3000000,
DiskUsage: 3000000,
LogRate: 1024 * 3,
MemoryQuota: 33554432,
DiskQuota: 6000000,
LogRateLimit: 1024 * 5,
Uptime: time.Since(time.Unix(1277164800, 0)),
},
},
},
Expand Down Expand Up @@ -143,18 +146,21 @@ var _ = Describe("app summary displayer", func() {
Expect(time.Parse(time.RFC3339, webProcessSummary.Instances[0].Since)).To(BeTemporally("~", time.Now().Add(-uptime), 2*time.Second))
Expect(webProcessSummary.Instances[0].Disk).To(Equal("976.6K of 1.9M"))
Expect(webProcessSummary.Instances[0].CPU).To(Equal("0.0%"))
Expect(webProcessSummary.Instances[0].CPUEntitlement).To(Equal("0.0%"))
Expect(webProcessSummary.Instances[0].LogRate).To(Equal("1K/s of 5K/s"))
Expect(webProcessSummary.Instances[0].Details).To(Equal("Some Details 1"))

Expect(webProcessSummary.Instances[1].Memory).To(Equal("1.9M of 32M"))
Expect(webProcessSummary.Instances[1].Disk).To(Equal("1.9M of 3.8M"))
Expect(webProcessSummary.Instances[1].CPU).To(Equal("0.0%"))
Expect(webProcessSummary.Instances[1].CPUEntitlement).To(Equal(""))
Expect(webProcessSummary.Instances[1].LogRate).To(Equal("2K/s of 5K/s"))
Expect(webProcessSummary.Instances[1].Details).To(Equal("Some Details 2"))

Expect(webProcessSummary.Instances[2].Memory).To(Equal("2.9M of 32M"))
Expect(webProcessSummary.Instances[2].Disk).To(Equal("2.9M of 5.7M"))
Expect(webProcessSummary.Instances[2].CPU).To(Equal("0.0%"))
Expect(webProcessSummary.Instances[2].CPUEntitlement).To(Equal("3.0%"))
Expect(webProcessSummary.Instances[2].LogRate).To(Equal("3K/s of 5K/s"))

consoleProcessSummary := processTable.Processes[1]
Expand All @@ -166,6 +172,7 @@ var _ = Describe("app summary displayer", func() {
Expect(consoleProcessSummary.Instances[0].Memory).To(Equal("976.6K of 32M"))
Expect(consoleProcessSummary.Instances[0].Disk).To(Equal("976.6K of 7.6M"))
Expect(consoleProcessSummary.Instances[0].CPU).To(Equal("0.0%"))
Expect(consoleProcessSummary.Instances[0].CPUEntitlement).To(Equal(""))
Expect(consoleProcessSummary.Instances[0].LogRate).To(Equal("128B/s of 256B/s"))
})
})
Expand Down
51 changes: 31 additions & 20 deletions integration/helpers/app_instance_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
// AppInstanceRow represents an instance of a V3 app's process,
// as displayed in the 'cf app' output.
type AppInstanceRow struct {
Index string
State string
Since string
CPU string
Memory string
Disk string
LogRate string
Details string
Index string
State string
Since string
CPU string
Memory string
Disk string
LogRate string
CPUEntitlement string
Details string
}

// AppProcessTable represents a process of a V3 app, as displayed in the 'cf
Expand Down Expand Up @@ -56,24 +57,28 @@ func ParseV3AppProcessTable(input []byte) AppTable {

switch {
case strings.HasPrefix(row, "#"):
const columnCount = 8
const columnCount = 9

// instance row
columns := splitColumns(row)
details := ""
cpuEntitlement, details := "", ""
if len(columns) >= columnCount-1 {
cpuEntitlement = columns[columnCount-2]
}
if len(columns) >= columnCount {
details = columns[7]
details = columns[columnCount-1]
}

instanceRow := AppInstanceRow{
Index: columns[0],
State: columns[1],
Since: columns[2],
CPU: columns[3],
Memory: columns[4],
Disk: columns[5],
LogRate: columns[6],
Details: details,
Index: columns[0],
State: columns[1],
Since: columns[2],
CPU: columns[3],
Memory: columns[4],
Disk: columns[5],
LogRate: columns[6],
CPUEntitlement: cpuEntitlement,
Details: details,
}
lastProcessIndex := len(appTable.Processes) - 1
appTable.Processes[lastProcessIndex].Instances = append(
Expand Down Expand Up @@ -107,6 +112,12 @@ func ParseV3AppProcessTable(input []byte) AppTable {
}

func splitColumns(row string) []string {
s := strings.TrimSpace(row)
// uses 3 spaces between columns
return regexp.MustCompile(`\s{3,}`).Split(strings.TrimSpace(row), -1)
result := regexp.MustCompile(`\s{3,}`).Split(s, -1)
// 21 spaces should only occur if cpu entitlement is empty but details is filled in
if regexp.MustCompile(`\s{21}`).MatchString(s) {
result = append(result[:len(result)-1], "", result[len(result)-1])
}
return result
}
42 changes: 21 additions & 21 deletions integration/helpers/app_instance_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ buildpacks: ruby 1.6.44
type: web
instances: 4/4
memory usage: 32M
state since cpu memory disk logging
#0 running 2017-08-02 17:12:10 PM 0.0% 21.2M of 32M 84.5M of 1G 5B/s of 1K/s
#1 running 2017-08-03 09:39:25 AM 0.2% 19.3M of 32M 84.5M of 1G 7B/s of 1K/s
#2 running 2017-08-03 03:29:25 AM 0.1% 22.8M of 32M 84.5M of 1G 10B/s of 1K/s
#3 running 2017-08-02 17:12:10 PM 0.2% 22.9M of 32M 84.5M of 1G 8B/s of 1K/s
state since cpu memory disk logging cpu entitlement
#0 running 2017-08-02 17:12:10 PM 0.0% 21.2M of 32M 84.5M of 1G 5B/s of 1K/s 0.0%
#1 running 2017-08-03 09:39:25 AM 0.2% 19.3M of 32M 84.5M of 1G 7B/s of 1K/s 0.4%
#2 running 2017-08-03 03:29:25 AM 0.1% 22.8M of 32M 84.5M of 1G 10B/s of 1K/s 0.2%
#3 running 2017-08-02 17:12:10 PM 0.2% 22.9M of 32M 84.5M of 1G 8B/s of 1K/s 0.4%
type: worker
instances: 1/1
memory usage: 32M
state since cpu memory disk logging
#0 stopped 2017-08-02 17:12:10 PM 0.0% 0M of 32M 0M of 1G 0B/s of 1K/s
state since cpu memory disk logging cpu entitlement
#0 stopped 2017-08-02 17:12:10 PM 0.0% 0M of 32M 0M of 1G 0B/s of 1K/s 0.0%
`
appInstanceTable := ParseV3AppProcessTable([]byte(input))
Expect(appInstanceTable).To(Equal(AppTable{
Expand All @@ -41,18 +41,18 @@ memory usage: 32M
InstanceCount: "4/4",
MemUsage: "32M",
Instances: []AppInstanceRow{
{Index: "#0", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "21.2M of 32M", Disk: "84.5M of 1G", LogRate: "5B/s of 1K/s"},
{Index: "#1", State: "running", Since: "2017-08-03 09:39:25 AM", CPU: "0.2%", Memory: "19.3M of 32M", Disk: "84.5M of 1G", LogRate: "7B/s of 1K/s"},
{Index: "#2", State: "running", Since: "2017-08-03 03:29:25 AM", CPU: "0.1%", Memory: "22.8M of 32M", Disk: "84.5M of 1G", LogRate: "10B/s of 1K/s"},
{Index: "#3", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.2%", Memory: "22.9M of 32M", Disk: "84.5M of 1G", LogRate: "8B/s of 1K/s"},
{Index: "#0", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "21.2M of 32M", Disk: "84.5M of 1G", LogRate: "5B/s of 1K/s", CPUEntitlement: "0.0%"},
{Index: "#1", State: "running", Since: "2017-08-03 09:39:25 AM", CPU: "0.2%", Memory: "19.3M of 32M", Disk: "84.5M of 1G", LogRate: "7B/s of 1K/s", CPUEntitlement: "0.4%"},
{Index: "#2", State: "running", Since: "2017-08-03 03:29:25 AM", CPU: "0.1%", Memory: "22.8M of 32M", Disk: "84.5M of 1G", LogRate: "10B/s of 1K/s", CPUEntitlement: "0.2%"},
{Index: "#3", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.2%", Memory: "22.9M of 32M", Disk: "84.5M of 1G", LogRate: "8B/s of 1K/s", CPUEntitlement: "0.4%"},
},
},
{
Type: "worker",
InstanceCount: "1/1",
MemUsage: "32M",
Instances: []AppInstanceRow{
{Index: "#0", State: "stopped", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "0M of 32M", Disk: "0M of 1G", LogRate: "0B/s of 1K/s"},
{Index: "#0", State: "stopped", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "0M of 32M", Disk: "0M of 1G", LogRate: "0B/s of 1K/s", CPUEntitlement: "0.0%"},
},
},
},
Expand All @@ -66,11 +66,11 @@ Showing health and status for app dora in org wut / space wut as admin...
type: web
instances: 4/4
memory usage: 32M
state since cpu memory disk logging
#0 running 2017-08-02 17:12:10 PM 0.0% 21.2M of 32M 84.5M of 1G 1.3K/s of 5K/s
#1 running 2017-08-03 09:39:25 AM 0.2% 19.3M of 32M 84.5M of 1G 1.2K/s of 5K/s
#2 running 2017-08-03 03:29:25 AM 0.1% 22.8M of 32M 84.5M of 1G 1.1K/s of 5K/s
#3 running 2017-08-02 17:12:10 PM 0.2% 22.9M of 32M 84.5M of 1G 1.2K/s of 5K/s
state since cpu memory disk logging cpu entitlement
#0 running 2017-08-02 17:12:10 PM 0.0% 21.2M of 32M 84.5M of 1G 1.3K/s of 5K/s 0.0%
#1 running 2017-08-03 09:39:25 AM 0.2% 19.3M of 32M 84.5M of 1G 1.2K/s of 5K/s 0.4%
#2 running 2017-08-03 03:29:25 AM 0.1% 22.8M of 32M 84.5M of 1G 1.1K/s of 5K/s 0.2%
#3 running 2017-08-02 17:12:10 PM 0.2% 22.9M of 32M 84.5M of 1G 1.2K/s of 5K/s 0.4%
`
appInstanceTable := ParseV3AppProcessTable([]byte(input))
Expect(appInstanceTable).To(Equal(AppTable{
Expand All @@ -80,10 +80,10 @@ memory usage: 32M
InstanceCount: "4/4",
MemUsage: "32M",
Instances: []AppInstanceRow{
{Index: "#0", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "21.2M of 32M", Disk: "84.5M of 1G", LogRate: "1.3K/s of 5K/s"},
{Index: "#1", State: "running", Since: "2017-08-03 09:39:25 AM", CPU: "0.2%", Memory: "19.3M of 32M", Disk: "84.5M of 1G", LogRate: "1.2K/s of 5K/s"},
{Index: "#2", State: "running", Since: "2017-08-03 03:29:25 AM", CPU: "0.1%", Memory: "22.8M of 32M", Disk: "84.5M of 1G", LogRate: "1.1K/s of 5K/s"},
{Index: "#3", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.2%", Memory: "22.9M of 32M", Disk: "84.5M of 1G", LogRate: "1.2K/s of 5K/s"},
{Index: "#0", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.0%", Memory: "21.2M of 32M", Disk: "84.5M of 1G", LogRate: "1.3K/s of 5K/s", CPUEntitlement: "0.0%"},
{Index: "#1", State: "running", Since: "2017-08-03 09:39:25 AM", CPU: "0.2%", Memory: "19.3M of 32M", Disk: "84.5M of 1G", LogRate: "1.2K/s of 5K/s", CPUEntitlement: "0.4%"},
{Index: "#2", State: "running", Since: "2017-08-03 03:29:25 AM", CPU: "0.1%", Memory: "22.8M of 32M", Disk: "84.5M of 1G", LogRate: "1.1K/s of 5K/s", CPUEntitlement: "0.2%"},
{Index: "#3", State: "running", Since: "2017-08-02 17:12:10 PM", CPU: "0.2%", Memory: "22.9M of 32M", Disk: "84.5M of 1G", LogRate: "1.2K/s of 5K/s", CPUEntitlement: "0.4%"},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion integration/v7/isolated/app_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ applications:
Eventually(session).Should(Say(`type:\s+web`))
Eventually(session).Should(Say(`instances:\s+\d/2`))
Eventually(session).Should(Say(`memory usage:\s+128M`))
Eventually(session).Should(Say(`\s+state\s+since\s+cpu\s+memory\s+disk\s+logging\s+details`))
Eventually(session).Should(Say(`\s+state\s+since\s+cpu\s+memory\s+disk\s+logging\s+cpu entitlement\s+details`))
Eventually(session).Should(Say(`#0\s+(starting|running)\s+\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`))

Eventually(session).Should(Exit(0))
Expand Down
Loading

0 comments on commit 5fc0821

Please sign in to comment.