From 960d19d7fe60922f0bfa9d0ad6057993e089170b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Wed, 13 Nov 2024 18:04:06 -0300 Subject: [PATCH] Display scale down behavior just when is available --- tsuru/client/apps.go | 15 +++++++-------- tsuru/client/apps_test.go | 16 ++++++++-------- tsuru/client/apps_type.go | 24 ++++++++++-------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/tsuru/client/apps.go b/tsuru/client/apps.go index 89ab8e5e..c555a871 100644 --- a/tsuru/client/apps.go +++ b/tsuru/client/apps.go @@ -792,14 +792,13 @@ func (a *app) String(simplified bool) string { prometheusInfo, })) } - scaleDown := getParamsScaleDownJson(as.Behavior) - autoScaleTable.AddRow([]string{ - "Scale Down Behavior", - fmt.Sprintf("Units: %s\nPercentage: %s%%\nStabilization Window: %ss", - scaleDown.UnitsPolicyValue, - scaleDown.PercentagePolicyValue, - scaleDown.StabilizationWindow), - }) + scaleDownLines := getParamsScaleDownLines(as.Behavior) + if len(scaleDownLines) > 0 { + autoScaleTable.AddRow([]string{ + "Scale down behavior", + strings.Join(scaleDownLines, "\n"), + }) + } autoScaleTables = append(autoScaleTables, autoScaleTable) } diff --git a/tsuru/client/apps_test.go b/tsuru/client/apps_test.go index 951d0b0d..a59744e1 100644 --- a/tsuru/client/apps_test.go +++ b/tsuru/client/apps_test.go @@ -1627,9 +1627,9 @@ Process: web (v10), Min Units: 1, Max Units: 10 +---------------------+----------------------------+ | CPU | Target: 50% | +---------------------+----------------------------+ -| Scale Down Behavior | Units: 20 | +| Scale down behavior | Units: 20 | | | Percentage: 30% | -| | Stabilization Window: 100s | +| | Stabilization window: 100s | +---------------------+----------------------------+ Process: worker (v10), Min Units: 2, Max Units: 5 @@ -1638,9 +1638,9 @@ Process: worker (v10), Min Units: 2, Max Units: 5 +---------------------+---------------------------+ | CPU | Target: 200% | +---------------------+---------------------------+ -| Scale Down Behavior | Units: 10 | +| Scale down behavior | Units: 10 | | | Percentage: 10% | -| | Stabilization Window: 60s | +| | Stabilization window: 60s | +---------------------+---------------------------+ ` @@ -1804,9 +1804,9 @@ Process: web (v10), Min Units: 1, Max Units: 10 | | Threshold: 5 | | | PrometheusAddress: exemple.prometheus.com | +---------------------+-------------------------------------------+ -| Scale Down Behavior | Units: 25 | +| Scale down behavior | Units: 25 | | | Percentage: 21% | -| | Stabilization Window: 50s | +| | Stabilization window: 50s | +---------------------+-------------------------------------------+ Process: worker (v10), Min Units: 2, Max Units: 5 @@ -1820,9 +1820,9 @@ Process: worker (v10), Min Units: 2, Max Units: 5 | | Units: 1 | | | Timezone: America/Sao_Paulo | +---------------------+--------------------------------+ -| Scale Down Behavior | Units: 7 | +| Scale down behavior | Units: 7 | | | Percentage: 5% | -| | Stabilization Window: 60s | +| | Stabilization window: 60s | +---------------------+--------------------------------+ ` diff --git a/tsuru/client/apps_type.go b/tsuru/client/apps_type.go index e05cc0bb..fc42f045 100644 --- a/tsuru/client/apps_type.go +++ b/tsuru/client/apps_type.go @@ -21,31 +21,27 @@ type scaleDownJson struct { StabilizationWindow *int32 `json:"stabilizationWindow,omitempty"` } -type scaleDownOutput struct { - UnitsPolicyValue string `json:"unitsPolicyValue,omitempty"` - PercentagePolicyValue string `json:"percentagePolicyValue,omitempty"` - StabilizationWindow string `json:"stabilizationWindow,omitempty"` -} - -func getParamsScaleDownJson(behavior tsuru.AutoScaleSpecBehavior) scaleDownOutput { +func getParamsScaleDownLines(behavior tsuru.AutoScaleSpecBehavior) []string { b, err := json.Marshal(behavior) if err != nil { - return scaleDownOutput{} + return nil } var behaviorJson behaviorScaleDownJson err = json.Unmarshal(b, &behaviorJson) if err != nil { - return scaleDownOutput{} + return nil } - output := scaleDownOutput{} + + lines := []string{} + if behaviorJson.ScaleDown.UnitsPolicyValue != nil { - output.UnitsPolicyValue = fmt.Sprintf("%d", *behaviorJson.ScaleDown.UnitsPolicyValue) + lines = append(lines, fmt.Sprintf("Units: %d", *behaviorJson.ScaleDown.UnitsPolicyValue)) } if behaviorJson.ScaleDown.PercentagePolicyValue != nil { - output.PercentagePolicyValue = fmt.Sprintf("%d", *behaviorJson.ScaleDown.PercentagePolicyValue) + lines = append(lines, fmt.Sprintf("Percentage: %d%%", *behaviorJson.ScaleDown.PercentagePolicyValue)) } if behaviorJson.ScaleDown.StabilizationWindow != nil { - output.StabilizationWindow = fmt.Sprintf("%d", *behaviorJson.ScaleDown.StabilizationWindow) + lines = append(lines, fmt.Sprintf("Stabilization window: %ds", *behaviorJson.ScaleDown.StabilizationWindow)) } - return output + return lines }