Skip to content

Commit

Permalink
network-status: Add human friendly network status
Browse files Browse the repository at this point in the history
  • Loading branch information
amela committed Mar 8, 2024
1 parent 50f6c7c commit fdd68c5
Show file tree
Hide file tree
Showing 5 changed files with 780 additions and 621 deletions.
98 changes: 79 additions & 19 deletions cmd/network/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,100 @@ package network
import (
"context"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"

"github.com/oasisprotocol/cli/cmd/common"
cliConfig "github.com/oasisprotocol/cli/config"
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "Show the current status of the node and the network",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)
var (
showJson bool

Check warning on line 19 in cmd/network/status.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var showJson should be showJSON (revive)

// Establish connection with the target network.
ctx := context.Background()
conn, err := connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)
statusCmd = &cobra.Command{
Use: "status",
Short: "Show the current status of the node and the network",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)

ctrlConn := conn.Control()
// Establish connection with the target network.
ctx := context.Background()
conn, err := connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)

nodeStatus, err := ctrlConn.GetStatus(ctx)
cobra.CheckErr(err)
ctrlConn := conn.Control()

nodeStr, err := common.PrettyJSONMarshal(nodeStatus)
cobra.CheckErr(err)
nodeStatus, err := ctrlConn.GetStatus(ctx)
cobra.CheckErr(err)

fmt.Println(string(nodeStr))
},
}
if showJson {
nodeStr, err := common.PrettyJSONMarshal(nodeStatus)
cobra.CheckErr(err)

fmt.Println(string(nodeStr))
} else {
fmt.Println("=== NETWORK STATUS ===")
fmt.Printf("Network: %s", npa.PrettyPrintNetwork())
fmt.Println()

fmt.Printf("Core Version: %s", nodeStatus.SoftwareVersion)
fmt.Println()

fmt.Printf("Consensus:")
fmt.Println()

fmt.Printf(" Status: %s", nodeStatus.Consensus.Status.String())
fmt.Println()

fmt.Printf(" Consensus Version: %s", nodeStatus.Consensus.Version.String())
fmt.Println()

fmt.Printf(" Latest Height: %d", nodeStatus.Consensus.LatestHeight)
fmt.Println()

fmt.Printf(" Latest Epoch: %d", nodeStatus.Consensus.LatestEpoch)
fmt.Println()

for _, runtime := range nodeStatus.Runtimes {
fmt.Printf("%s: %s", strings.Title(runtime.Descriptor.Kind.String()), runtime.Descriptor.ID)

Check failure on line 68 in cmd/network/status.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead. (staticcheck)
fmt.Println()

fmt.Printf(" Status: %s", runtime.Committee.Status)
fmt.Println()

fmt.Printf(" Active version: %s", runtime.Committee.ActiveVersion)
fmt.Println()

for i, deployment := range runtime.Descriptor.Deployments {
fmt.Printf(" Deployment-%d: %s", i, deployment.Version)
fmt.Println()
}

fmt.Printf(" Latest round: %d", runtime.LatestRound)
fmt.Println()

date := time.Unix(int64(runtime.LatestTime), 0)
fmt.Printf(" Latest time: %s", date)
fmt.Println()

fmt.Printf(" Number of peers: %d", len(runtime.Committee.Peers))
fmt.Println()
}
}
},
}
)

func init() {
showJsonFlag := flag.NewFlagSet("", flag.ContinueOnError)

Check warning on line 98 in cmd/network/status.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var showJsonFlag should be showJSONFlag (revive)
showJsonFlag.BoolVar(&showJson, "json", false, "show network status in json")
statusCmd.Flags().AddFlagSet(showJsonFlag)
statusCmd.Flags().AddFlagSet(common.SelectorNFlags)
}
6 changes: 6 additions & 0 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ Mainnet was reported:

![code json](../examples/network/status.out.static)

You can also view output in json format by passing the `--json` parameter:

![code shell](../examples/network/status-json.in.static)

![code](../examples/network/status-json.out.static)

:::info

[Network](./account.md#npa) selector is available for the
Expand Down
1 change: 1 addition & 0 deletions examples/network/status-json.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis network status --json
Loading

0 comments on commit fdd68c5

Please sign in to comment.