-
Notifications
You must be signed in to change notification settings - Fork 3
/
dashboard.go
173 lines (136 loc) · 4.48 KB
/
dashboard.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
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
package main
import (
"fmt"
"time"
"github.com/gizak/termui"
)
func dashboard(stopTheUI, stopTheCrawler chan bool) {
if err := termui.Init(); err != nil {
panic(err)
}
defer termui.Close()
var snapshots []Snapshot
logWindow := termui.NewList()
logWindow.ItemFgColor = termui.ColorYellow
logWindow.BorderLabel = "Log"
logWindow.Height = 22
logWindow.Y = 0
totalBytesDownloaded := termui.NewPar("")
totalBytesDownloaded.Height = 3
totalBytesDownloaded.TextFgColor = termui.ColorWhite
totalBytesDownloaded.BorderLabel = "Data downloaded"
totalBytesDownloaded.BorderFg = termui.ColorCyan
totalNumberOfRequests := termui.NewPar("")
totalNumberOfRequests.Height = 3
totalNumberOfRequests.TextFgColor = termui.ColorWhite
totalNumberOfRequests.BorderLabel = "URLs crawled"
totalNumberOfRequests.BorderFg = termui.ColorCyan
requestsPerSecond := termui.NewPar("")
requestsPerSecond.Height = 3
requestsPerSecond.TextFgColor = termui.ColorWhite
requestsPerSecond.BorderLabel = "URLs/second"
requestsPerSecond.BorderFg = termui.ColorCyan
averageResponseTime := termui.NewPar("")
averageResponseTime.Height = 3
averageResponseTime.TextFgColor = termui.ColorWhite
averageResponseTime.BorderLabel = "Average response time"
averageResponseTime.BorderFg = termui.ColorCyan
numberOfWorkers := termui.NewPar("")
numberOfWorkers.Height = 3
numberOfWorkers.TextFgColor = termui.ColorWhite
numberOfWorkers.BorderLabel = "Number of workers"
numberOfWorkers.BorderFg = termui.ColorCyan
averageSizeInBytes := termui.NewPar("")
averageSizeInBytes.Height = 3
averageSizeInBytes.TextFgColor = termui.ColorWhite
averageSizeInBytes.BorderLabel = "Average response size"
averageSizeInBytes.BorderFg = termui.ColorCyan
numberOfErrors := termui.NewPar("")
numberOfErrors.Height = 3
numberOfErrors.TextFgColor = termui.ColorWhite
numberOfErrors.BorderLabel = "Number of 4xx errors"
numberOfErrors.BorderFg = termui.ColorCyan
elapsedTime := termui.NewPar("")
elapsedTime.Height = 3
elapsedTime.TextFgColor = termui.ColorWhite
elapsedTime.BorderLabel = "Elapsed time"
elapsedTime.BorderFg = termui.ColorCyan
draw := func() {
snapshot := stats.LastSnapshot()
// ignore empty updates
if snapshot.Timestamp().IsZero() {
return
}
// don't update if there is no new snapshot available
if len(snapshots) > 0 {
previousSnapShot := snapshots[len(snapshots)-1]
if snapshot.Timestamp() == previousSnapShot.Timestamp() {
return
}
}
// capture the latest snapshot
snapshots = append(snapshots, snapshot)
// log messages
logWindow.Items = stats.LastLogMessages(20)
// total number of requests
totalNumberOfRequests.Text = fmt.Sprintf("%d", snapshot.TotalNumberOfRequests())
// total number of bytes downloaded
totalBytesDownloaded.Text = formatBytes(snapshot.TotalSizeInBytes())
// requests per second
requestsPerSecond.Text = fmt.Sprintf("%.1f", snapshot.RequestsPerSecond())
// average response time
averageResponseTime.Text = fmt.Sprintf("%s", snapshot.AverageResponseTime())
// number of workers
numberOfWorkers.Text = fmt.Sprintf("%d", snapshot.NumberOfWorkers())
// average request size
averageSizeInBytes.Text = formatBytes(snapshot.AverageSizeInBytes())
// number of errors
numberOfErrors.Text = fmt.Sprintf("%d", snapshot.NumberOfErrors())
// time since first snapshot
timeSinceStart := time.Now().Sub(snapshots[0].Timestamp())
elapsedTime.Text = fmt.Sprintf("%s", timeSinceStart)
termui.Render(termui.Body)
}
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(12, 0, logWindow),
),
termui.NewRow(
termui.NewCol(3, 0, totalBytesDownloaded),
termui.NewCol(3, 0, totalNumberOfRequests),
termui.NewCol(3, 0, requestsPerSecond),
termui.NewCol(3, 0, averageResponseTime),
),
termui.NewRow(
termui.NewCol(3, 0, numberOfWorkers),
termui.NewCol(3, 0, numberOfErrors),
termui.NewCol(3, 0, averageSizeInBytes),
termui.NewCol(3, 0, elapsedTime),
),
)
termui.Body.Align()
termui.Render(termui.Body)
termui.Handle("/sys/wnd/resize", func(e termui.Event) {
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Clear()
termui.Render(termui.Body)
})
termui.Handle("/sys/kbd/q", func(termui.Event) {
stopTheCrawler <- true
termui.StopLoop()
})
termui.Handle("/timer/1s", func(e termui.Event) {
draw()
})
// stop when the crawler is done
go func() {
select {
case <-stopTheUI:
// wait 10 seconds before closing the ui
time.Sleep(time.Second * 10)
termui.StopLoop()
}
}()
termui.Loop()
}