-
Notifications
You must be signed in to change notification settings - Fork 2
/
viewer.go
191 lines (155 loc) · 5.02 KB
/
viewer.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package viewer
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"text/template"
"github.com/bygui86/go-csv-view/examples/dynamic-page/statics"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
"go.uber.org/zap"
)
const (
defaultWidth = "800px"
defaultHeight = "600px"
//defaultTheme = types.ThemeWesteros
defaultTheme = types.ThemeMacarons
defaultInterval = 2000
defaultMaxPoints = 30
)
type Viewer struct { // INFO fields globally visible because used by manager.Manager
Name string
Address string
AddressPath string
Graph *charts.Line
updater *Updater
}
// ViewerTemplate defines fields used in the default view template
// WARN: changing names or types, please remember to change also the content of statics.ViewTemplate const
type ViewerTemplate struct { // INFO fields globally visible because used in statics.ViewTemplate
Interval int
MaxPoints int
Address string
ViewPath string
ViewID string
}
type Metrics struct {
Values []float64 `json:"values"`
Time string `json:"time"`
}
func NewViewer(name, address, addressPath string,
ctx context.Context, interval, shutdownTimeout int) *Viewer {
zap.S().Debugf("New Viewer - name %s, address %s, addressPath %s, interval %d, shutdownTimeout %d",
name, address, addressPath, interval, shutdownTimeout)
return (&Viewer{}).
setupGeneral(name, address, addressPath).
setupGraph().
setupUpdater(ctx, interval, shutdownTimeout)
}
func (v *Viewer) setupGeneral(name, address, addressPath string) *Viewer {
v.Name = name
v.Address = address
v.AddressPath = addressPath
zap.S().Debugf("Setup general - name %s, address %s, addressPath %s",
name, address, addressPath)
return v
}
func (v *Viewer) setupGraph() *Viewer {
v.Graph = charts.NewLine()
v.Graph.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: "Line sample"}),
charts.WithLegendOpts(opts.Legend{Show: true}),
charts.WithTooltipOpts(opts.Tooltip{Show: true, Trigger: "axis"}),
charts.WithInitializationOpts(opts.Initialization{
Width: defaultWidth,
Height: defaultHeight,
Theme: defaultTheme,
}),
charts.WithXAxisOpts(opts.XAxis{Name: "Time"}),
charts.WithYAxisOpts(opts.YAxis{Name: "Size", AxisLabel: &opts.AxisLabel{Formatter: "{value} MB"}}),
)
v.Graph.SetXAxis([]string{}).SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: true}))
// v.Name NOT ENOUGH!!
v.Graph.AddJSFuncs(v.generateViewTemplate())
v.Graph.
AddSeries("Sys", []opts.LineData{}).
AddSeries("Inuse", []opts.LineData{}).
AddSeries("MSpan Sys", []opts.LineData{}).
AddSeries("MSpan Inuse", []opts.LineData{})
zap.S().Debugf("Setup graph")
return v
}
func (v *Viewer) setupUpdater(ctx context.Context, interval, shutdownTimeout int) *Viewer {
v.updater = newUpdater(ctx, interval, shutdownTimeout)
zap.S().Debugf("Setup updater")
return v
}
func (v *Viewer) Start() {
zap.S().Infof("%s Viewer start...", v.Name)
v.updater.Start()
}
func (v *Viewer) Stop() {
zap.S().Infof("%s Viewer stop...", v.Name)
v.updater.Stop()
}
func (v *Viewer) generateViewTemplate() string {
zap.S().Debugf("Generate view template")
tpl, tplErr := template.New("view").Parse(statics.ViewTemplate)
if tplErr != nil {
log.Fatalf("template parsing failed: %s" + tplErr.Error())
}
buf := bytes.Buffer{}
execErr := tpl.Execute(
&buf,
&ViewerTemplate{ // TODO what about & ?
Interval: defaultInterval,
MaxPoints: defaultMaxPoints,
Address: v.Address,
ViewPath: v.AddressPath,
ViewID: v.Graph.ChartID,
},
)
if execErr != nil {
log.Fatalf("template execution failed: %s" + execErr.Error())
}
return buf.String()
}
func (v *Viewer) Handler(w http.ResponseWriter, _ *http.Request) {
zap.S().Infof("%s Viewer handler", v.Name)
v.updater.ViewerRefresh() // INFO let the updater know last time the view was refreshed
metrics := Metrics{
Values: []float64{
fixedPrecision(float64(goMemStats.MemStats.StackSys)/1024/1024, 2), // "Sys" series
fixedPrecision(float64(goMemStats.MemStats.StackInuse)/1024/1024, 2), // "Inuse" series
fixedPrecision(float64(goMemStats.MemStats.MSpanSys)/1024/1024, 2), // "MSpan Sys" series
fixedPrecision(float64(goMemStats.MemStats.MSpanInuse)/1024/1024, 2), // "MSpan Inuse" series
},
Time: goMemStats.PointTime,
}
zap.S().Debugf("%s Viewer new metrics: Sys %.2f, Inuse %.2f, MSpan Sys %.2f, MSpan Inuse %.2f, time %s",
v.Name, metrics.Values[0], metrics.Values[1], metrics.Values[2], metrics.Values[3], metrics.Time)
metricsBytes, jsonErr := json.Marshal(metrics)
if jsonErr != nil {
zap.S().Errorf("view json marhal failed: %s", jsonErr.Error())
return
}
_, wrErr := w.Write(metricsBytes)
if wrErr != nil {
zap.S().Errorf("view rendering failed: %s", wrErr.Error())
}
}
func fixedPrecision(n float64, p int) float64 {
var r float64
switch p {
case 2:
r, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", n), 64)
case 6:
r, _ = strconv.ParseFloat(fmt.Sprintf("%.6f", n), 64)
}
return r
}