Skip to content

Commit

Permalink
En 3563 update term ui (#373)
Browse files Browse the repository at this point in the history
* updated gin to start in debug mode only if termui is not enabled
code refactoring in termui render
added version label in termui render

* minor code changes: renamed bool flag from elrond facade
  • Loading branch information
iulianpascalau committed Aug 14, 2019
1 parent ff6b430 commit 53f4cf7
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 166 deletions.
10 changes: 9 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type prometheus struct {
// MainApiHandler interface defines methods that can be used from `elrondFacade` context variable
type MainApiHandler interface {
RestApiPort() string
RestAPIServerDebugMode() bool
PprofEnabled() bool
PrometheusMonitoring() bool
PrometheusJoinURL() string
Expand All @@ -41,7 +42,14 @@ type MainApiHandler interface {

// Start will boot up the api and appropriate routes, handlers and validators
func Start(elrondFacade MainApiHandler) error {
ws := gin.Default()
var ws *gin.Engine
if elrondFacade.RestAPIServerDebugMode() {
ws = gin.Default()
} else {
ws = gin.New()
ws.Use(gin.Recovery())
gin.SetMode(gin.ReleaseMode)
}
ws.Use(cors.Default())

err := registerValidators()
Expand Down
6 changes: 4 additions & 2 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ var coreServiceContainer serviceContainer.Core
// windows:
// for /f %i in ('git describe --tags --long --dirty') do set VERS=%i
// go build -i -v -ldflags="-X main.appVersion=%VERS%"
var appVersion = "undefined"
var appVersion = core.UnVersionedAppString

func main() {
log := logger.DefaultLogger()
Expand Down Expand Up @@ -529,6 +529,7 @@ func startNode(ctx *cli.Context, log *logger.Logger, version string) error {
coreComponents.StatusHandler.SetUInt64Value(core.MetricShardId, uint64(shardCoordinator.SelfId()))
coreComponents.StatusHandler.SetStringValue(core.MetricNodeType, string(nodeType))
coreComponents.StatusHandler.SetUInt64Value(core.MetricRoundTime, nodesConfig.RoundDuration/milisecondsInSecond)
coreComponents.StatusHandler.SetStringValue(core.MetricAppVersion, version)

dataArgs := factory.NewDataComponentsFactoryArgs(generalConfig, shardCoordinator, coreComponents, uniqueDBFolder)
dataComponents, err := factory.DataComponentsFactory(dataArgs)
Expand Down Expand Up @@ -642,7 +643,8 @@ func startNode(ctx *cli.Context, log *logger.Logger, version string) error {
return err
}

ef := facade.NewElrondNodeFacade(currentNode, apiResolver)
restAPIServerDebugMode := !useTermui
ef := facade.NewElrondNodeFacade(currentNode, apiResolver, restAPIServerDebugMode)

efConfig := &config.FacadeConfig{
RestApiPort: ctx.GlobalString(restApiPort.Name),
Expand Down
7 changes: 7 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package core

// UnVersionedAppString represents the default app version that indicate that the binary wasn't build by setting
// the appVersion flag
const UnVersionedAppString = "undefined"

// NodeType represents the node's role in the network
type NodeType string

Expand Down Expand Up @@ -100,3 +104,6 @@ const MetricNetworkSentBpsPeak = "erd_network_sent_bps_peak"

// MetricRoundTime is the metric for round time in seconds
const MetricRoundTime = "erd_round_time"

// MetricAppVersion is the metric for the current app version
const MetricAppVersion = "erd_app_version"
24 changes: 15 additions & 9 deletions facade/elrondNodeFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ const DefaultRestPortOff = "off"

// ElrondNodeFacade represents a facade for grouping the functionality for node, transaction and address
type ElrondNodeFacade struct {
node NodeWrapper
apiResolver ApiResolver
syncer ntp.SyncTimer
log *logger.Logger
tpsBenchmark *statistics.TpsBenchmark
config *config.FacadeConfig
node NodeWrapper
apiResolver ApiResolver
syncer ntp.SyncTimer
log *logger.Logger
tpsBenchmark *statistics.TpsBenchmark
config *config.FacadeConfig
restAPIServerDebugMode bool
}

// NewElrondNodeFacade creates a new Facade with a NodeWrapper
func NewElrondNodeFacade(node NodeWrapper, apiResolver ApiResolver) *ElrondNodeFacade {
func NewElrondNodeFacade(node NodeWrapper, apiResolver ApiResolver, restAPIServerDebugMode bool) *ElrondNodeFacade {
if node == nil {
return nil
}
Expand All @@ -43,8 +44,9 @@ func NewElrondNodeFacade(node NodeWrapper, apiResolver ApiResolver) *ElrondNodeF
}

return &ElrondNodeFacade{
node: node,
apiResolver: apiResolver,
node: node,
apiResolver: apiResolver,
restAPIServerDebugMode: restAPIServerDebugMode,
}
}

Expand Down Expand Up @@ -100,6 +102,10 @@ func (ef *ElrondNodeFacade) IsNodeRunning() bool {
return ef.node.IsRunning()
}

func (ef *ElrondNodeFacade) RestAPIServerDebugMode() bool {
return ef.restAPIServerDebugMode
}

// RestApiPort returns the port on which the api should start on, based on the config file provided.
// The API will start on the DefaultRestPort value unless a correct value is passed or
// the value is explicitly set to off, in which case it will not start at all
Expand Down
9 changes: 5 additions & 4 deletions facade/elrondNodeFacade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
)

func createElrondNodeFacadeWithMockNodeAndResolver() *ElrondNodeFacade {
return NewElrondNodeFacade(&mock.NodeMock{}, &mock.ApiResolverStub{})
return NewElrondNodeFacade(&mock.NodeMock{}, &mock.ApiResolverStub{}, false)
}

func createElrondNodeFacadeWithMockResolver(node *mock.NodeMock) *ElrondNodeFacade {
return NewElrondNodeFacade(node, &mock.ApiResolverStub{})
return NewElrondNodeFacade(node, &mock.ApiResolverStub{}, false)
}

func TestNewElrondFacade_FromValidNodeShouldReturnNotNil(t *testing.T) {
Expand All @@ -30,12 +30,12 @@ func TestNewElrondFacade_FromValidNodeShouldReturnNotNil(t *testing.T) {
}

func TestNewElrondFacade_FromNilNodeShouldReturnNil(t *testing.T) {
ef := NewElrondNodeFacade(nil, &mock.ApiResolverStub{})
ef := NewElrondNodeFacade(nil, &mock.ApiResolverStub{}, false)
assert.Nil(t, ef)
}

func TestNewElrondFacade_FromNilApiResolverShouldReturnNil(t *testing.T) {
ef := NewElrondNodeFacade(&mock.NodeMock{}, nil)
ef := NewElrondNodeFacade(&mock.NodeMock{}, nil, false)
assert.Nil(t, ef)
}

Expand Down Expand Up @@ -507,6 +507,7 @@ func TestElrondNodeFacade_GetDataValue(t *testing.T) {
return make([]byte, 0), nil
},
},
false,
)

_, _ = ef.GetVmValue("", "")
Expand Down
55 changes: 6 additions & 49 deletions statusHandler/termuiStatusHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package statusHandler
import (
"sync"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/statusHandler/termuic"
)

Expand All @@ -15,8 +14,9 @@ type TermuiStatusHandler struct {

// NewTermuiStatusHandler will return an instance of the struct
func NewTermuiStatusHandler() *TermuiStatusHandler {
tsh := new(TermuiStatusHandler)
tsh.initMetricsMap()
tsh := &TermuiStatusHandler{
termuiConsoleMetrics: &sync.Map{},
}
tsh.tui = termuic.NewTermuiConsole(tsh.termuiConsoleMetrics)

return tsh
Expand All @@ -43,62 +43,19 @@ func (tsh *TermuiStatusHandler) Termui() *termuic.TermuiConsole {
return tsh.tui
}

// InitMetricsMap will init the map of prometheus metrics
func (tsh *TermuiStatusHandler) initMetricsMap() {
tsh.termuiConsoleMetrics = &sync.Map{}

tsh.termuiConsoleMetrics.Store(core.MetricPublicKeyTxSign, "")
tsh.termuiConsoleMetrics.Store(core.MetricPublicKeyBlockSign, "")
tsh.termuiConsoleMetrics.Store(core.MetricShardId, uint64(0))

tsh.termuiConsoleMetrics.Store(core.MetricNodeType, "")
tsh.termuiConsoleMetrics.Store(core.MetricCountConsensus, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricCountLeader, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricCountAcceptedBlocks, uint64(0))

tsh.termuiConsoleMetrics.Store(core.MetricIsSyncing, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNonce, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricProbableHighestNonce, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricCurrentRound, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricSynchronizedRound, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricRoundTime, uint64(0))

tsh.termuiConsoleMetrics.Store(core.MetricLiveValidatorNodes, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricConnectedNodes, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNumConnectedPeers, uint64(0))

tsh.termuiConsoleMetrics.Store(core.MetricCpuLoadPercent, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricMemLoadPercent, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricTotalMem, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkRecvBps, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkRecvBpsPeak, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkRecvPercent, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkSentBps, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkSentBpsPeak, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricNetworkSentPercent, uint64(0))
tsh.termuiConsoleMetrics.Store(core.MetricTxPoolLoad, uint64(0))

}

// SetInt64Value method - will update the value for a key
func (tsh *TermuiStatusHandler) SetInt64Value(key string, value int64) {
if _, ok := tsh.termuiConsoleMetrics.Load(key); ok {
tsh.termuiConsoleMetrics.Store(key, value)
}
tsh.termuiConsoleMetrics.Store(key, value)
}

// SetUInt64Value method - will update the value for a key
func (tsh *TermuiStatusHandler) SetUInt64Value(key string, value uint64) {
if _, ok := tsh.termuiConsoleMetrics.Load(key); ok {
tsh.termuiConsoleMetrics.Store(key, value)
}
tsh.termuiConsoleMetrics.Store(key, value)
}

// SetStringValue method - will update the value of a key
func (tsh *TermuiStatusHandler) SetStringValue(key string, value string) {
if _, ok := tsh.termuiConsoleMetrics.Load(key); ok {
tsh.termuiConsoleMetrics.Store(key, value)
}
tsh.termuiConsoleMetrics.Store(key, value)
}

// Increment - will increment the value of a key
Expand Down
34 changes: 9 additions & 25 deletions statusHandler/termuiStatusHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,14 @@ func TestTermuiStatusHandler_TermuiShouldPass(t *testing.T) {
assert.NotNil(t, termuiConsole)
}

func TestTermuiStatusHandler_TestIfMetricsAreInitialized(t *testing.T) {
t.Parallel()

termuiStatusHandler := statusHandler.NewTermuiStatusHandler()

// check if nonce metric for example was initialized
_, err := termuiStatusHandler.GetTermuiMetricByKey(core.MetricNonce)

assert.Nil(t, err)
assert.Equal(t, 26, termuiStatusHandler.GetMetricsCount())
}

func TestTermuiStatusHandler_TestIncrement(t *testing.T) {
t.Parallel()

var metricKey = core.MetricNonce

termuiStatusHandler := statusHandler.NewTermuiStatusHandler()

termuiStatusHandler.Increment(metricKey)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(metricKey)
termuiStatusHandler.SetUInt64Value(core.MetricNonce, 0)
termuiStatusHandler.Increment(core.MetricNonce)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(core.MetricNonce)
assert.Nil(t, err)

result := valueI.(uint64)
Expand All @@ -55,13 +42,12 @@ func TestTermuiStatusHandler_TestIncrement(t *testing.T) {
func TestTermuiStatusHandler_TestSetInt64(t *testing.T) {
t.Parallel()

var metricKey = core.MetricNonce
var intValue = int64(100)

termuiStatusHandler := statusHandler.NewTermuiStatusHandler()

termuiStatusHandler.SetInt64Value(metricKey, intValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(metricKey)
termuiStatusHandler.SetInt64Value(core.MetricNonce, intValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(core.MetricNonce)
assert.Nil(t, err)

result := valueI.(int64)
Expand All @@ -72,13 +58,12 @@ func TestTermuiStatusHandler_TestSetInt64(t *testing.T) {
func TestTermuiStatusHandler_TestSetUInt64(t *testing.T) {
t.Parallel()

var metricKey = core.MetricNonce
var intValue = uint64(200)

termuiStatusHandler := statusHandler.NewTermuiStatusHandler()

termuiStatusHandler.SetUInt64Value(metricKey, intValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(metricKey)
termuiStatusHandler.SetUInt64Value(core.MetricNonce, intValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(core.MetricNonce)
assert.Nil(t, err)

result := valueI.(uint64)
Expand All @@ -89,13 +74,12 @@ func TestTermuiStatusHandler_TestSetUInt64(t *testing.T) {
func TestTermuiStatusHandler_TestSetString(t *testing.T) {
t.Parallel()

var metricKey = core.MetricPublicKeyBlockSign
var stringValue = "KEY"

termuiStatusHandler := statusHandler.NewTermuiStatusHandler()

termuiStatusHandler.SetStringValue(metricKey, stringValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(metricKey)
termuiStatusHandler.SetStringValue(core.MetricPublicKeyBlockSign, stringValue)
valueI, err := termuiStatusHandler.GetTermuiMetricByKey(core.MetricPublicKeyBlockSign)
assert.Nil(t, err)

result := valueI.(string)
Expand Down
4 changes: 2 additions & 2 deletions statusHandler/termuic/termuiConsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ func (tc *TermuiConsole) eventLoop() {
termWidth, termHeight := ui.TerminalDimensions()
tc.grid.SetRectangle(0, 0, termWidth, termHeight)

time.Sleep(1 * time.Second)

uiEvents := ui.PollEvents()
// handles kill signal sent to gotop
sigTerm := make(chan os.Signal, 2)
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)

tc.consoleRender.RefreshData(tc.logLines)

for {
select {
case <-time.After(refreshInterval):
Expand Down

0 comments on commit 53f4cf7

Please sign in to comment.