Skip to content

Commit

Permalink
finish linting status and handle issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
n8wb committed Apr 26, 2019
1 parent 40cd38a commit 23700d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
16 changes: 3 additions & 13 deletions status/build.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
/*
Handles functions related to the current state of the network
*/
// Package status handles functions related to the current state of the network
package status

import (
"../state"
"log"
)

type BuildStatus struct {
Error state.CustomError `json:"error"`
Progress float64 `json:"progress"`
Stage string `json:"stage"`
Frozen bool `json:"frozen"`
}

/*
Check the current status of the build
*/
// CheckBuildStatus checks the current status of the build relating to the
// given build id
func CheckBuildStatus(buildID string) (string, error) {
bs, err := state.GetBuildStateByID(buildID)
if err != nil {
Expand Down
19 changes: 3 additions & 16 deletions status/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,15 @@ import (
"log"
)

/*
Get the servers used in the latest testnet, populated with the
ips of all the nodes
*/
// GetLatestServers gets the servers used in the latest testnet, populated with the
// ips of all the nodes
func GetLatestServers(testnetID string) ([]db.Server, error) {
nodes, err := db.GetAllNodesByTestNet(testnetID)
if err != nil {
log.Println(err)
return nil, err
}
serverIDs := []int{}
for _, node := range nodes {
shouldAdd := true
for _, id := range serverIDs {
if id == node.Server {
shouldAdd = false
}
}
if shouldAdd {
serverIDs = append(serverIDs, node.Server)
}
}
serverIDs := db.GetUniqueServerIDs(nodes)

servers, err := db.GetServers(serverIDs)
if err != nil {
Expand Down
38 changes: 16 additions & 22 deletions status/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,34 @@ func init() {
conf = util.GetConfig()
}

// Comp represents the compuational resources currently in use
// by a node
type Comp struct {
Cpu float64 `json:"cpu"`
Vsz float64 `json:"virtualMemorySize"`
Rss float64 `json:"residentSetSize"`
CPU float64 `json:"cpu"`
VSZ float64 `json:"virtualMemorySize"`
RSS float64 `json:"residentSetSize"`
}

/*
Represents the status of the node
*/
// NodeStatus represents the status of the node
type NodeStatus struct {
Name string `json:"name"`
Server int `json:"server"`
Ip string `json:"ip"`
IP string `json:"ip"`
Up bool `json:"up"`
Resources Comp `json:"resourceUse"`
}

/*
Finds the index of a node by name and server id
*/
func FindNodeIndex(status []NodeStatus, name string, serverId int) int {
// FindNodeIndex finds the index of a node by name and server id
func FindNodeIndex(status []NodeStatus, name string, serverID int) int {
for i, stat := range status {
if stat.Name == name && serverId == stat.Server {
if stat.Name == name && serverID == stat.Server {
return i
}
}
return -1
}

/*
Gets the cpu usage of a node
*/
// SumResUsage gets the cpu usage of a node
func SumResUsage(c *ssh.Client, name string) (Comp, error) {
res, err := c.Run(fmt.Sprintf("docker exec %s ps aux --no-headers | grep -v nibbler | awk '{print $3,$5,$6}'", name))
if err != nil {
Expand All @@ -69,29 +65,27 @@ func SumResUsage(c *ssh.Client, name string) (Comp, error) {
log.Println(err)
return Comp{-1, -1, -1}, err
}
out.Cpu += cpu
out.CPU += cpu

vsz, err := strconv.ParseFloat(values[1], 64)
if err != nil {
log.Println(err)
return Comp{-1, -1, -1}, err
}
out.Vsz += vsz
out.VSZ += vsz

rss, err := strconv.ParseFloat(values[2], 64)
if err != nil {
log.Println(err)
return Comp{-1, -1, -1}, err
}
out.Rss += rss
out.RSS += rss

}
return out, nil
}

/*
Checks the status of the nodes in the current testnet
*/
// CheckNodeStatus checks the status of the nodes in the current testnet
func CheckNodeStatus(nodes []db.Node) ([]NodeStatus, error) {

serverIds := []int{}
Expand All @@ -110,7 +104,7 @@ func CheckNodeStatus(nodes []db.Node) ([]NodeStatus, error) {
//fmt.Printf("ABS = %d; REL=%d;NAME=%s%d\n", node.AbsoluteNum, node.LocalID, conf.NodePrefix, node.LocalID)
out[node.AbsoluteNum] = NodeStatus{
Name: fmt.Sprintf("%s%d", conf.NodePrefix, node.LocalID),
Ip: node.IP,
IP: node.IP,
Server: node.Server,
Up: false,
Resources: Comp{-1, -1, -1},
Expand Down
16 changes: 7 additions & 9 deletions status/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ var _clients = map[int]*ssh.Client{}

var _mux = sync.Mutex{}

/*
GetClient retrieves the ssh client for running a command
on a remote server based on server id. It will create one if it
does not exist.
*/
// GetClient retrieves the ssh client for running a command
// on a remote server based on server id. It will create one if it
// does not exist.
func GetClient(id int) (*ssh.Client, error) {
cli, ok := _clients[id]
if !ok || cli == nil {
Expand All @@ -36,10 +34,8 @@ func GetClient(id int) (*ssh.Client, error) {
return cli, nil
}

/*
GetClients functions similar to GetClient, except that it takes in
an array of server ids and outputs an array of clients
*/
// GetClients functions similar to GetClient, except that it takes in
// an array of server ids and outputs an array of clients
func GetClients(servers []int) ([]*ssh.Client, error) {

out := make([]*ssh.Client, len(servers))
Expand All @@ -54,6 +50,8 @@ func GetClients(servers []int) ([]*ssh.Client, error) {
return out, nil
}

// GetClientsFromNodes gets all of the ssh clients you need for
// communication with the given nodes
func GetClientsFromNodes(nodes []db.Node) ([]*ssh.Client, error) {
serverIds := db.GetUniqueServerIDs(nodes)
return GetClients(serverIds)
Expand Down

0 comments on commit 23700d0

Please sign in to comment.