whispervis/widgets/stats.go

79 lines
1.5 KiB
Go
Raw Normal View History

2018-10-19 14:13:41 +02:00
package widgets
import (
"fmt"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"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{
2018-10-20 19:42:05 +02:00
/*
stats: &stats.Stats{
Time: 1234 * time.Millisecond,
NodeCoverage: stats.NewCoverage(100, 200),
LinkCoverage: stats.NewCoverage(100, 200),
},
*/
2018-10-19 14:13:41 +02:00
}
}
// Update updates the stats based on the propagation log info and current graph.
2018-10-19 16:01:49 +02:00
func (s *Stats) Update(stats *stats.Stats) {
s.stats = stats
2018-10-19 14:13:41 +02:00
vecty.Rerender(s)
}
// Render implements vecty.Component interface for Stats.
func (s *Stats) Render() vecty.ComponentOrHTML {
if s.stats == nil {
return elem.Div()
}
2018-10-20 14:47:19 +02:00
return Widget(
2018-10-19 22:10:56 +02:00
Header("Stats:"),
elem.Table(
vecty.Markup(
vecty.Class("table", "is-hoverable", "is-fullwidth"),
),
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),
2018-10-19 14:13:41 +02:00
),
),
)
}
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),
),
),
)
}
2018-10-24 12:54:48 +02:00
func (s *Stats) Reset() {
s.stats = nil
}