-
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
Andrea Falzetti
committed
Jun 10, 2022
1 parent
dbd8d6b
commit 4bb1cb6
Showing
2 changed files
with
96 additions
and
0 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,70 @@ | ||
// 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/spf13/cobra" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
) | ||
|
||
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) | ||
|
||
cpuColor := getColor(cpuFraction) | ||
memoryColor := getColor(memFraction) | ||
|
||
table.Rich([]string{cpu, memory}, []tablewriter.Colors{{cpuColor}, {memoryColor}}) | ||
|
||
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 workspace resource (CPU and memory usage)", | ||
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() { | ||
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
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 | ||
} |