-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper" | ||
supervisor "github.com/gitpod-io/gitpod/supervisor/api" | ||
|
||
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
) | ||
|
||
var noColor bool | ||
|
||
func outputTable(workspaceResources *supervisor.ResourcesStatusResponse) { | ||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetHeader([]string{"CPU (millicores)", "Memory (bytes)"}) | ||
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: false}) | ||
table.SetCenterSeparator("|") | ||
|
||
cpuFraction := int64((float64(workspaceResources.Cpu.Used) / float64(workspaceResources.Cpu.Limit)) * 100) | ||
memFraction := int64((float64(workspaceResources.Memory.Used) / float64(workspaceResources.Memory.Limit)) * 100) | ||
cpu := fmt.Sprintf("%dm/%dm (%d%%)", workspaceResources.Cpu.Used, workspaceResources.Cpu.Limit, cpuFraction) | ||
memory := fmt.Sprintf("%dMi/%dMi (%d%%)\n", workspaceResources.Memory.Used/(1024*1024), workspaceResources.Memory.Limit/(1024*1024), memFraction) | ||
|
||
colors := []tablewriter.Colors{} | ||
|
||
if !noColor && utils.ColorsEnabled() { | ||
cpuColor := getColor(cpuFraction) | ||
memoryColor := getColor(memFraction) | ||
colors = []tablewriter.Colors{{cpuColor}, {memoryColor}} | ||
} | ||
|
||
table.Rich([]string{cpu, memory}, colors) | ||
|
||
table.Render() | ||
} | ||
|
||
func getColor(value int64) int { | ||
switch { | ||
case value >= 85: | ||
return tablewriter.FgRedColor | ||
case value >= 65: | ||
return tablewriter.FgYellowColor | ||
default: | ||
return tablewriter.FgHiGreenColor | ||
} | ||
} | ||
|
||
var topCmd = &cobra.Command{ | ||
Use: "top", | ||
Short: "Display usage of workspace resources (CPU and memory)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
workspaceResources, err := supervisor_helper.GetWorkspaceResources(ctx) | ||
if err != nil { | ||
log.Fatalf("cannot get workspace resources: %s", err) | ||
} | ||
|
||
outputTable(workspaceResources) | ||
}, | ||
} | ||
|
||
func init() { | ||
topCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization") | ||
rootCmd.AddCommand(topCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package supervisor_helper | ||
|
||
import ( | ||
"context" | ||
|
||
supervisor "github.com/gitpod-io/gitpod/supervisor/api" | ||
) | ||
|
||
func GetWorkspaceResources(ctx context.Context) (*supervisor.ResourcesStatusResponse, error) { | ||
conn, err := Dial(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := supervisor.NewStatusServiceClient(conn) | ||
workspaceResources, workspaceResourcesError := client.ResourcesStatus(ctx, &supervisor.ResourcesStatuRequest{}) | ||
|
||
if workspaceResourcesError != nil { | ||
return nil, workspaceResourcesError | ||
} | ||
|
||
return workspaceResources, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func isInteractiveTerminal() bool { | ||
return term.IsTerminal(syscall.Stdin) && term.IsTerminal(syscall.Stdout) | ||
} | ||
|
||
func userRequestsNoColor() bool { | ||
isDumbTerm := os.Getenv("TERM") == "dumb" | ||
_, noColorPresent := os.LookupEnv("NO_COLOR") | ||
_, gpNoColor := os.LookupEnv("GP_NO_COLOR") | ||
|
||
return isDumbTerm || noColorPresent || gpNoColor | ||
} | ||
|
||
func ColorsEnabled() bool { | ||
colorsDisabled := userRequestsNoColor() || !isInteractiveTerminal() | ||
return !colorsDisabled | ||
} |