-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats_page.go
121 lines (103 loc) · 2.92 KB
/
stats_page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"sort"
charts "github.com/cnguy/gopherjs-frappe-charts"
"github.com/divan/graphx/graph"
"github.com/divan/simulation/propagation"
"github.com/divan/whispervis/widgets"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
)
// StatsPage is stats view component.
type StatsPage struct {
vecty.Core
width, height string
chart1Data *charts.ChartData
chart2Data *charts.ChartData
}
// NewStatsPage creates and inits new stats page.
func NewStatsPage() *StatsPage {
width, height := PageViewSize()
return &StatsPage{
width: fmt.Sprintf("%dpx", width),
height: fmt.Sprintf("%dpx", height),
}
}
// Render implements the vecty.Component interface.
func (s *StatsPage) Render() vecty.ComponentOrHTML {
return elem.Div(
vecty.Markup(
vecty.Style("width", s.width),
vecty.Style("height", s.height),
),
elem.Div(
vecty.Markup(
vecty.Class("title", "has-text-centered"),
),
vecty.Text("Stats page"),
),
// consult this tile madness here https://bulma.io/documentation/layout/tiles/
elem.Div(vecty.Markup(vecty.Class("tile", "is-anscestor")),
elem.Div(vecty.Markup(vecty.Class("tile", "is-parent", "is-vertical")),
vecty.If(s.chart1Data != nil, Tile(widgets.NewChart("nodes", s.chart1Data))),
vecty.If(s.chart2Data != nil, Tile(widgets.NewChart("cumulative", s.chart2Data))),
),
),
)
}
// UpdateStats updates stats page with a new data.
func (s *StatsPage) UpdateStats(g *graph.Graph, plog *propagation.Log) {
sort.Sort(plog)
labels := make([]string, len(plog.Timestamps))
nodeCounts := make([]float64, len(plog.Timestamps))
linkCounts := make([]float64, len(plog.Timestamps))
nodeCum := make([]float64, len(plog.Timestamps))
linkCum := make([]float64, len(plog.Timestamps))
nodesMap := make(map[int]int)
linksMap := make(map[int]int)
for i, ts := range plog.Timestamps {
labels[i] = fmt.Sprintf("%d", ts)
// calculate unique new nodes
var nodes, links int
for _, nodeIdx := range plog.Nodes[i] {
if _, ok := nodesMap[nodeIdx]; !ok {
nodes++
}
}
for _, linkIdx := range plog.Links[i] {
if _, ok := linksMap[linkIdx]; !ok {
links++
}
}
nodeCounts[i] = float64(nodes)
linkCounts[i] = float64(links)
// calculate unique cumulative nodes
for _, nodeIdx := range plog.Nodes[i] {
nodesMap[nodeIdx]++
}
for _, linkIdx := range plog.Links[i] {
linksMap[linkIdx]++
}
nodeCum[i] = float64(len(nodesMap))
linkCum[i] = float64(len(linksMap))
}
data := charts.NewChartData()
data.Labels = labels
dataset1 := charts.NewDataset("Nodes", nodeCounts)
dataset2 := charts.NewDataset("Links", linkCounts)
data.Datasets = []*charts.Dataset{
dataset1,
dataset2,
}
s.chart1Data = data
data2 := charts.NewChartData()
data2.Labels = labels
dataset3 := charts.NewDataset("Nodes", nodeCum)
dataset4 := charts.NewDataset("Links", linkCum)
data2.Datasets = []*charts.Dataset{
dataset3,
dataset4,
}
s.chart2Data = data2
}