Add stats widget.

This commit is contained in:
Ivan Danyliuk 2018-10-19 14:13:41 +02:00
parent 5d46031d87
commit 37daa05e1c
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
4 changed files with 35956 additions and 69 deletions

17
page.go
View File

@ -9,6 +9,7 @@ import (
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/status-im/simulation/propagation"
"github.com/status-im/whispervis/widgets"
)
@ -26,6 +27,7 @@ type Page struct {
forceEditor *widgets.ForceEditor
network *NetworkSelector
simulationConf *widgets.Simulation
statsWidget *widgets.Stats
}
// NewPage creates and inits new app page.
@ -36,7 +38,8 @@ func NewPage() *Page {
}
page.network = NewNetworkSelector(page.onNetworkChange)
page.webgl = NewWebGLScene()
page.simulationConf = widgets.NewSimulation("localhost:8084", page.CurrentNetwork, page.webgl.AnimatePropagation)
page.simulationConf = widgets.NewSimulation("localhost:8084", page.CurrentNetwork, page.onSimulationFinish)
page.statsWidget = widgets.NewStats()
return page
}
@ -62,6 +65,8 @@ func (p *Page) Render() vecty.ComponentOrHTML {
elem.HorizontalRule(),
p.forceEditor,
p.updateButton(),
elem.HorizontalRule(),
p.statsWidget,
),
),
elem.Div(
@ -134,3 +139,13 @@ func (p *Page) CurrentNetwork() []byte {
}
return buf.Bytes()
}
// onSimulationFinish is called on the end of each simulation round.
func (p *Page) onSimulationFinish(plog *propagation.Log) {
net := p.network.current
nodesCount := len(net.Data.Nodes())
linksCount := len(net.Data.Links())
p.statsWidget.Update(plog, nodesCount, linksCount)
p.webgl.AnimatePropagation(plog)
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

77
widgets/stats.go Normal file
View File

@ -0,0 +1,77 @@
package widgets
import (
"fmt"
"time"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/status-im/simulation/propagation"
"github.com/status-im/simulation/stats"
)
// Stats represents widget with the statistics from the latest simulation.
type Stats struct {
vecty.Core
stats *stats.Stats
}
// NewStats create a new Stats widget.
func NewStats() *Stats {
return &Stats{
stats: &stats.Stats{
Time: 1234 * time.Millisecond,
NodeCoverage: stats.NewCoverage(100, 200),
LinkCoverage: stats.NewCoverage(100, 200),
},
}
}
// Update updates the stats based on the propagation log info and current graph.
func (s *Stats) Update(plog *propagation.Log, nodes, links int) {
s.stats = stats.Analyze(plog, nodes, links)
vecty.Rerender(s)
}
// Render implements vecty.Component interface for Stats.
func (s *Stats) Render() vecty.ComponentOrHTML {
if s.stats == nil {
return elem.Div()
}
return elem.Div(
elem.Div(
elem.Heading3(vecty.Text("Stats")),
elem.Table(
vecty.Markup(
vecty.Style("width", "90%"),
vecty.Style("margin", "10px"),
),
elem.TableBody(
s.tableRow("Elapsed time:", s.stats.Time),
s.tableRow("Nodes hit:", s.stats.NodeCoverage.Actual),
s.tableRow("Links hit:", s.stats.LinkCoverage.Actual),
),
),
),
)
}
func (s *Stats) tableRow(label string, value interface{}) *vecty.HTML {
return elem.TableRow(
elem.TableData(
vecty.Markup(
vecty.Style("font-weight", "bold"),
),
vecty.Text(label),
),
elem.TableData(
vecty.Markup(
vecty.Style("text-align", "right"),
),
vecty.Text(
fmt.Sprintf("%v", value),
),
),
)
}