aboutsummaryrefslogtreecommitdiffstats
path: root/dashboard/dashboard.go
blob: 10a36361921194f038cce3901ad99e23e45958bd (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package dashboard

//go:generate go-bindata -nometadata -o assets.go -prefix assets -pkg dashboard assets/public/...

import (
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "path/filepath"
    "sync"
    "sync/atomic"
    "time"

    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/rpc"
    "github.com/rcrowley/go-metrics"
    "golang.org/x/net/websocket"
)

const (
    memorySampleLimit  = 200 // Maximum number of memory data samples
    trafficSampleLimit = 200 // Maximum number of traffic data samples
)

var nextId uint32 // Next connection id

// Dashboard contains the dashboard internals.
type Dashboard struct {
    config *Config

    listener net.Listener
    conns    map[uint32]*client // Currently live websocket connections
    charts   charts             // The collected data samples to plot
    lock     sync.RWMutex       // Lock protecting the dashboard's internals

    quit chan chan error // Channel used for graceful exit
    wg   sync.WaitGroup
}

// message embraces the data samples of a client message.
type message struct {
    History *charts     `json:"history,omitempty"` // Past data samples
    Memory  *chartEntry `json:"memory,omitempty"`  // One memory sample
    Traffic *chartEntry `json:"traffic,omitempty"` // One traffic sample
    Log     string      `json:"log,omitempty"`     // One log
}

// client represents active websocket connection with a remote browser.
type client struct {
    conn   *websocket.Conn // Particular live websocket connection
    msg    chan message    // Message queue for the update messages
    logger log.Logger      // Logger for the particular live websocket connection
}

// charts contains the collected data samples.
type charts struct {
    Memory  []*chartEntry `json:"memorySamples,omitempty"`
    Traffic []*chartEntry `json:"trafficSamples,omitempty"`
}

// chartEntry represents one data sample
type chartEntry struct {
    Time  time.Time `json:"time,omitempty"`
    Value float64   `json:"value,omitempty"`
}

// New creates a new dashboard instance with the given configuration.
func New(config *Config) (*Dashboard, error) {
    return &Dashboard{
        conns:  make(map[uint32]*client),
        config: config,
        quit:   make(chan chan error),
    }, nil
}

// Protocols is a meaningless implementation of node.Service.
func (db *Dashboard) Protocols() []p2p.Protocol { return nil }

// APIs is a meaningless implementation of node.Service.
func (db *Dashboard) APIs() []rpc.API { return nil }

// Start implements node.Service, starting the data collection thread and the listening server of the dashboard.
func (db *Dashboard) Start(server *p2p.Server) error {
    db.wg.Add(2)
    go db.collectData()
    go db.collectLogs() // In case of removing this line change 2 back to 1 in wg.Add.

    http.HandleFunc("/", db.webHandler)
    http.Handle("/api", websocket.Handler(db.apiHandler))

    listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", db.config.Host, db.config.Port))
    if err != nil {
        return err
    }
    db.listener = listener

    go http.Serve(listener, nil)

    return nil
}

// Stop implements node.Service, stopping the data collection thread and the connection listener of the dashboard.
func (db *Dashboard) Stop() error {
    // Close the connection listener.
    var errs []error
    if err := db.listener.Close(); err != nil {
        errs = append(errs, err)
    }
    // Close the collectors.
    errc := make(chan error, 1)
    for i := 0; i < 2; i++ {
        db.quit <- errc
        if err := <-errc; err != nil {
            errs = append(errs, err)
        }
    }
    // Close the connections.
    db.lock.Lock()
    for _, c := range db.conns {
        if err := c.conn.Close(); err != nil {
            c.logger.Warn("Failed to close connection", "err", err)
        }
    }
    db.lock.Unlock()

    // Wait until every goroutine terminates.
    db.wg.Wait()
    log.Info("Dashboard stopped")

    var err error
    if len(errs) > 0 {
        err = fmt.Errorf("%v", errs)
    }

    return err
}

// webHandler handles all non-api requests, simply flattening and returning the dashboard website.
func (db *Dashboard) webHandler(w http.ResponseWriter, r *http.Request) {
    log.Debug("Request", "URL", r.URL)

    path := r.URL.String()
    if path == "/" {
        path = "/dashboard.html"
    }
    // If the path of the assets is manually set
    if db.config.Assets != "" {
        blob, err := ioutil.ReadFile(filepath.Join(db.config.Assets, path))
        if err != nil {
            log.Warn("Failed to read file", "path", path, "err", err)
            http.Error(w, "not found", http.StatusNotFound)
            return
        }
        w.Write(blob)
        return
    }
    blob, err := Asset(filepath.Join("public", path))
    if err != nil {
        log.Warn("Failed to load the asset", "path", path, "err", err)
        http.Error(w, "not found", http.StatusNotFound)
        return
    }
    w.Write(blob)
}

// apiHandler handles requests for the dashboard.
func (db *Dashboard) apiHandler(conn *websocket.Conn) {
    id := atomic.AddUint32(&nextId, 1)
    client := &client{
        conn:   conn,
        msg:    make(chan message, 128),
        logger: log.New("id", id),
    }
    done := make(chan struct{}) // Buffered channel as sender may exit early

    // Start listening for messages to send.
    db.wg.Add(1)
    go func() {
        defer db.wg.Done()

        for {
            select {
            case <-done:
                return
            case msg := <-client.msg:
                if err := websocket.JSON.Send(client.conn, msg); err != nil {
                    client.logger.Warn("Failed to send the message", "msg", msg, "err", err)
                    client.conn.Close()
                    return
                }
            }
        }
    }()
    // Send the past data.
    client.msg <- message{
        History: &db.charts,
    }
    // Start tracking the connection and drop at connection loss.
    db.lock.Lock()
    db.conns[id] = client
    db.lock.Unlock()
    defer func() {
        db.lock.Lock()
        delete(db.conns, id)
        db.lock.Unlock()
    }()
    for {
        fail := []byte{}
        if _, err := conn.Read(fail); err != nil {
            close(done)
            return
        }
        // Ignore all messages
    }
}

// collectData collects the required data to plot on the dashboard.
func (db *Dashboard) collectData() {
    defer db.wg.Done()

    for {
        select {
        case errc := <-db.quit:
            errc <- nil
            return
        case <-time.After(db.config.Refresh):
            inboundTraffic := metrics.DefaultRegistry.Get("p2p/InboundTraffic").(metrics.Meter).Rate1()
            memoryInUse := metrics.DefaultRegistry.Get("system/memory/inuse").(metrics.Meter).Rate1()
            now := time.Now()
            memory := &chartEntry{
                Time:  now,
                Value: memoryInUse,
            }
            traffic := &chartEntry{
                Time:  now,
                Value: inboundTraffic,
            }
            // Remove the first elements in case the samples' amount exceeds the limit.
            first := 0
            if len(db.charts.Memory) == memorySampleLimit {
                first = 1
            }
            db.charts.Memory = append(db.charts.Memory[first:], memory)
            first = 0
            if len(db.charts.Traffic) == trafficSampleLimit {
                first = 1
            }
            db.charts.Traffic = append(db.charts.Traffic[first:], traffic)

            db.sendToAll(&message{
                Memory:  memory,
                Traffic: traffic,
            })
        }
    }
}

// collectLogs collects and sends the logs to the active dashboards.
func (db *Dashboard) collectLogs() {
    defer db.wg.Done()

    // TODO (kurkomisi): log collection comes here.
    for {
        select {
        case errc := <-db.quit:
            errc <- nil
            return
        case <-time.After(db.config.Refresh / 2):
            db.sendToAll(&message{
                Log: "This is a fake log.",
            })
        }
    }
}

// sendToAll sends the given message to the active dashboards.
func (db *Dashboard) sendToAll(msg *message) {
    db.lock.Lock()
    for _, c := range db.conns {
        select {
        case c.msg <- *msg:
        default:
            c.conn.Close()
        }
    }
    db.lock.Unlock()
}