Add debug info for propagation log

This commit is contained in:
Ivan Danyliuk 2018-09-19 21:06:30 +03:00
parent 8a8428975b
commit db7f2791e2
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
4 changed files with 90865 additions and 131 deletions

26
page.go
View File

@ -1,9 +1,10 @@
package main
import (
"bytes"
"fmt"
"github.com/divan/graphx/graph"
"github.com/divan/graphx/formats"
"github.com/divan/graphx/layout"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
@ -25,19 +26,17 @@ type Page struct {
forceEditor *widgets.ForceEditor
network *NetworkSelector
simulationConf *widgets.Simulation
data *graph.Graph
}
// NewPage creates and inits new app page.
func NewPage() *Page {
page := &Page{
loader: widgets.NewLoader(),
forceEditor: widgets.NewForceEditor(),
simulationConf: widgets.NewSimulation(""),
loader: widgets.NewLoader(),
forceEditor: widgets.NewForceEditor(),
}
page.network = NewNetworkSelector(page.onNetworkChange)
page.webgl = NewWebGLScene()
page.simulationConf = widgets.NewSimulation("localhost:8084", page.CurrentNetwork)
return page
}
@ -119,8 +118,19 @@ func (p *Page) onUpdateClick(e *vecty.Event) {
func (p *Page) onNetworkChange(network *Network) {
fmt.Println("Network changed:", network)
p.data = network.Data
config := p.forceEditor.Config()
p.layout = layout.NewFromConfig(p.data, config.Config)
p.layout = layout.NewFromConfig(network.Data, config.Config)
go p.UpdateGraph()
}
// CurrentNetwork returns JSON encoded description of the current graph/network.
func (p *Page) CurrentNetwork() []byte {
net := p.network.current.Data
var buf bytes.Buffer
err := formats.NewD3JSON(&buf, true).ExportGraph(net)
if err != nil {
fmt.Println("[ERROR] Can't export graph:", err)
return nil
}
return buf.Bytes()
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,29 +1,36 @@
package widgets
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
"github.com/status-im/simulation/propagation"
)
// Simulation represents configuration panel for propagation simulation.
type Simulation struct {
vecty.Core
networkFn func() []byte // function that returns current network JSON description
address string // backend host address
}
// NewSimulation creates new simulation configuration panel. If simulation
// backend host address is not specified, it'll use 'localhost:8084' as a default.
func NewSimulation(address string) *Simulation {
func NewSimulation(address string, networkFn func() []byte) *Simulation {
if address == "" {
address = "http://localhost:8084"
}
return &Simulation{
address: address,
address: address,
networkFn: networkFn,
}
}
@ -78,6 +85,42 @@ func (s *Simulation) Address() string {
}
func (s *Simulation) onSimulateClick(e *vecty.Event) {
// TODO(divan): connect to backend and run simulation
fmt.Println("Start simulation")
if s.networkFn == nil {
return
}
go s.runSimulation()
}
// runSimulation starts whisper message propagation simulation,
// remotely talking to simulation backend.
func (s *Simulation) runSimulation() {
payload := s.networkFn()
buf := bytes.NewBuffer(payload)
url := "http://" + s.address + "/"
resp, err := http.Post(url, "application/json", buf)
if err != nil {
fmt.Println("[ERROR] POST request to simulation backend:", err)
return
}
var plog propagation.Log
err = json.NewDecoder(resp.Body).Decode(&plog)
if err != nil {
fmt.Println("[ERROR] decoding response from simulation backend:", err)
return
}
var max int
for _, ts := range plog.Timestamps {
if ts > max {
max = ts
}
}
timespan := time.Duration(max) * time.Millisecond
fmt.Printf("Whoa! Got results! %d timestamps over %v\n", len(plog.Timestamps), timespan)
for i, ts := range plog.Timestamps {
fmt.Printf("[%dms] %d / %d links/nodes\n", ts, len(plog.Indices[i]), len(plog.Nodes[i]))
}
}