Skip to content

Commit 2fa7765

Browse files
committed
make runnable
Signed-off-by: cwen0 <[email protected]>
1 parent 898701c commit 2fa7765

File tree

11 files changed

+23
-44
lines changed

11 files changed

+23
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.test
1010

1111
web/node_modules
12+
web/build
1213

1314
# Output of the go coverage tool, specifically when used with LiteIDE
1415
*.out

.idea/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.idea/web-show.iml

Lines changed: 0 additions & 8 deletions
This file was deleted.

server/nework.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const (
1414
)
1515

1616
type PingD struct {
17-
Time int64 `json:"time"`
18-
Delay time.Duration `json:"delay"`
17+
Time int64 `json:"time"`
18+
Delay float64 `json:"delay"`
1919
}
2020

2121
func (p *PingD) Key() interface{} {
@@ -26,19 +26,21 @@ func (p *PingD) SetPosition(_ int) {}
2626

2727
func (s *Server) startPing() {
2828
ticker := time.NewTicker(10 * time.Second)
29+
log.Info("start ping")
2930
for {
3031
select {
3132
case <-ticker.C:
3233
pinger, err := ping.NewPinger(GoogleAddress)
3334
if err != nil {
3435
log.Error(err.Error())
35-
s.push(&PingD{Time: time.Now().Unix()})
36+
s.push(&PingD{Time: time.Now().UnixNano() / 1e6})
3637
continue
3738
}
39+
pinger.Timeout = 5 * time.Second
3840
pinger.Count = 1
3941
pinger.Run() // blocks until finished
4042
stats := pinger.Statistics()
41-
s.push(&PingD{Time: time.Now().Unix(), Delay: stats.AvgRtt})
43+
s.push(&PingD{Time: time.Now().UnixNano() / 1e6, Delay: float64(stats.AvgRtt.Microseconds()) / 1000})
4244
}
4345
}
4446
}
@@ -57,7 +59,7 @@ func (s *Server) network(w http.ResponseWriter, r *http.Request) {
5759
var data []PingD
5860

5961
for _, p := range s.pingData.All() {
60-
pd,ok := p.(*PingD)
62+
pd, ok := p.(*PingD)
6163
if !ok {
6264
continue
6365
}

server/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ func SetupServer() Server {
1818

1919
r.PathPrefix("/api/network").HandlerFunc(server.network)
2020

21-
r.PathPrefix("/").Handler(server.web("/", "/web"))
21+
r.PathPrefix("/").Handler(server.web("/", "./web/build"))
2222

2323
return server
2424
}
2525

2626
func (s *Server) Run() {
27+
go s.startPing()
2728
log.Info("Starting Server on 0.0.0.0:8081")
2829
err := http.ListenAndServe("0.0.0.0:8081", s.router)
2930
if err != nil {

web/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const App: React.FC = () => {
5252
<Banner />
5353
<BodyStyles className="body">
5454
<Row>
55-
<TrafficChartContainer title="Ping" />
55+
<TrafficChartContainer title="Ping www.google.com" />
5656
</Row>
5757
</BodyStyles>
5858
</AppStyles>

web/src/components/ChartContainer.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ const ChartContainer: React.FC<IChartProps> = props => {
3535
const fetchData = () => {
3636
console.log('on fetch data', new Date())
3737
// TODO: use real fetch data API, and update response data with setTraffic Hook
38-
setTraffic(data.traffic)
38+
fetch("/api/network").then(res => res.json()).then((data) => {
39+
setTraffic(data)
40+
}).catch(console.log)
41+
// setTraffic(data.traffic)
3942
}
4043

4144
useInterval(fetchData, 10 * 1000, true)

0 commit comments

Comments
 (0)