Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show available resources in zui #1971

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmds/modules/zui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (f *signalFlag) Signal() {
atomic.SwapInt32((*int32)(f), 1)
}

//Signaled checks if flag was raised, and lowers the flag
// Signaled checks if flag was raised, and lowers the flag
func (f *signalFlag) Signaled() bool {
return atomic.SwapInt32((*int32)(f), 0) == 1
}
Expand Down Expand Up @@ -73,10 +73,10 @@ func action(ctx *cli.Context) error {
netgrid.Title = "Network"
netgrid.SetRect(0, 7, width, 14)

provision := ui.NewGrid()
provision.Title = "Provision"
provision.SetRect(0, 14, width, 20)
provision.Border = false
resources := ui.NewGrid()
resources.Title = "Provision"
resources.SetRect(0, 14, width, 22)
resources.Border = false

var flag signalFlag

Expand All @@ -88,12 +88,12 @@ func action(ctx *cli.Context) error {
log.Error().Err(err).Msg("failed to start net renderer")
}

if err := provisionRender(client, provision, &flag); err != nil {
log.Error().Err(err).Msg("failed to start net renderer")
if err := resourcesRender(client, resources, &flag); err != nil {
log.Error().Err(err).Msg("failed to start resources renderer")
}

render := func() {
ui.Render(header, netgrid, provision)
ui.Render(header, netgrid, resources)
}

render()
Expand Down
92 changes: 67 additions & 25 deletions cmds/modules/zui/prov.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,97 @@ import (
"github.com/gizak/termui/v3/widgets"
"github.com/pkg/errors"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/zos/pkg/stubs"
)

const (
gig = 1024 * 1024 * 1024.0
mb = 1024 * 1024.0
)

func provisionRender(client zbus.Client, grid *ui.Grid, render *signalFlag) error {
func resourcesRender(client zbus.Client, grid *ui.Grid, render *signalFlag) error {
prov := widgets.NewTable()
prov.Title = "System Used Capacity"
prov.RowSeparator = false

prov.Rows = [][]string{
{"CPU Usage", "", "Memory Usage", ""},
{"CRU Reserved", "", "MRU Reserved", ""},
{"SSD Reserved", "", "HDD Reserved", ""},
{"IPv4 Reserved", ""},
}
usage := widgets.NewTable()

grid.Set(
ui.NewRow(1.0,
ui.NewCol(1, prov),
ui.NewCol(.6, prov),
ui.NewCol(.4, usage),
),
)

ctx := context.Background()
if err := provRender(client, render, prov); err != nil {
return errors.Wrap(err, "failed to render system provisioned resources")
}

if err := usageRender(client, render, usage); err != nil {
return errors.Wrap(err, "failed to render system resources usage")
}

return nil
}

func provRender(client zbus.Client, render *signalFlag, prov *widgets.Table) error {
prov.Title = "System Resources"
prov.RowSeparator = false

prov.Rows = [][]string{
{"", "Total", "Reserved"},
{"CRU", "", ""},
{"Memory", "", ""},
{"SSD", "", ""},
{"HDD", "", ""},
{"IPv4", "", ""},
}

monitor := stubs.NewStatisticsStub(client)
counters, err := monitor.ReservedStream(ctx)

total := monitor.Total(context.Background())
assignTotalResources(prov, total)
render.Signal()

reserved, err := monitor.ReservedStream(context.Background())
if err != nil {
return errors.Wrap(err, "failed to start net monitor stream")
}

go func() {
for counter := range counters {
for counter := range reserved {
rows := prov.Rows
rows[1][1] = fmt.Sprint(counter.CRU)
rows[1][3] = fmt.Sprintf("%0.00f GB", float64(counter.MRU)/gig)
rows[2][1] = fmt.Sprintf("%0.00f GB", float64(counter.SRU)/gig)
rows[2][3] = fmt.Sprintf("%0.00f GB", float64(counter.HRU)/gig)
rows[3][1] = fmt.Sprint(counter.IPV4U)
rows[1][2] = fmt.Sprint(counter.CRU)
rows[2][2] = fmt.Sprintf("%0.00f GB", float64(counter.MRU)/gig)
rows[3][2] = fmt.Sprintf("%0.00f GB", float64(counter.SRU)/gig)
rows[4][2] = fmt.Sprintf("%0.00f GB", float64(counter.HRU)/gig)
rows[5][2] = fmt.Sprint(counter.IPV4U)

render.Signal()
}
}()

return nil
}

func usageRender(client zbus.Client, render *signalFlag, usage *widgets.Table) error {
usage.Title = "Usage"
usage.RowSeparator = false
usage.FillRow = true

usage.Rows = [][]string{
{"CPU", ""},
{"Memory", ""},
}

sysMonitor := stubs.NewSystemMonitorStub(client)
stream, err := sysMonitor.CPU(context.Background())
cpuStream, err := sysMonitor.CPU(context.Background())
if err != nil {
return errors.Wrap(err, "failed to start cpu monitor stream")
}

go func() {
for point := range stream {
prov.Mutex.Lock()
prov.Rows[0][1] = fmt.Sprintf("%0.00f%%", point.Percent)
for point := range cpuStream {
usage.Rows[0][1] = fmt.Sprintf("%0.00f%%", point.Percent)
render.Signal()
prov.Mutex.Unlock()
}
}()

Expand All @@ -76,10 +109,19 @@ func provisionRender(client zbus.Client, grid *ui.Grid, render *signalFlag) erro

go func() {
for point := range memStream {
prov.Rows[0][3] = fmt.Sprintf("%0.00f%%", point.UsedPercent)
usage.Rows[1][1] = fmt.Sprintf("%0.00f MB", float64(point.Used/mb))
render.Signal()
}
}()

return nil
}

func assignTotalResources(prov *widgets.Table, total gridtypes.Capacity) {
rows := prov.Rows
rows[1][1] = fmt.Sprint(total.CRU)
rows[2][1] = fmt.Sprintf("%0.00f GB", float64(total.MRU)/gig)
rows[3][1] = fmt.Sprintf("%0.00f GB", float64(total.SRU)/gig)
rows[4][1] = fmt.Sprintf("%0.00f GB", float64(total.HRU)/gig)
rows[5][1] = fmt.Sprint(total.IPV4U)
}
2 changes: 1 addition & 1 deletion qemu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ zinit:

kernel:
@echo "Download 0-OS kernel"
wget https://bootstrap.grid.tf/kernel/zero-os-development-zos-v3-generic-7e587e499a.efi
wget https://bootstrap.grid.tf/kernel/zero-os-development-zos-v3-generic-0ad6c11534.efi

start:
bash vm.sh -n node1 -c "runmode=dev farmer_id=$(FARMERID)"
Expand Down