-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_graphs.go
More file actions
164 lines (122 loc) · 3.75 KB
/
Copy pathget_graphs.go
File metadata and controls
164 lines (122 loc) · 3.75 KB
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
package main
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"go.uber.org/zap"
xfont "golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/vg"
)
//go:embed resources/fonts/Inter_18pt-Medium.ttf
var mediumFontTTF []byte
//go:embed resources/fonts/Inter_18pt-Bold.ttf
var boldFontTTF []byte
var mediumFont = font.Font{Typeface: "Medium", Size: vg.Points(10)}
var boldFont = font.Font{Typeface: "Bold", Weight: xfont.WeightBold, Size: vg.Points(10)}
func prepareGetGraphs(cfg *LumoConfig) error {
if err := validateGetGraphsFlags(cfg); err != nil {
return fmt.Errorf("error: %w", err)
}
// Register the embedded fonts used for the legend table before any rendering
if err := initFonts(); err != nil {
return fmt.Errorf("%w: %w", ErrInitFonts, err)
}
// Get info about this service
service, err := getServiceByName(cfg.Endpoint, cfg.Token, cfg.Service)
if err != nil {
return fmt.Errorf("%w: %w", ErrServiceNotFound, err)
}
if service.NodeName == "" {
return fmt.Errorf("%w: service '%s' has no node name. Use the -node flag to supply the correct node name for this service", ErrNodeNameEmpty, cfg.Service)
}
cfg.Node = service.NodeName
if cfg.ClusterName == "" {
cfg.ClusterName = service.ClusterName
}
// MongoDB replset graph group requires the -replset flag
if _, exists := cfg.Groups["replset"]; exists && cfg.ReplSet == "" {
return fmt.Errorf("%w: -replset flag is required", ErrFlagRequired)
}
if cfg.OutDir == "" {
cfg.OutDir = cfg.Service
}
if err := os.MkdirAll(cfg.OutDir, 0o750); err != nil {
return fmt.Errorf("%w: '%s'", ErrCreateOutput, cfg.OutDir)
}
return nil
}
func renderGraphGroup(cfg *LumoConfig, graphGroup string) {
graphConfigs, exists := LumoGraphs[graphGroup]
if !exists {
zap.S().Errorf("error: requested group '%s' does not exist in the predefined configurations", graphGroup)
return
}
if err := validateGraphConfigs(graphConfigs); err != nil {
zap.S().Errorf("validation error in group '%s': %v", graphGroup, err)
return
}
for _, graphConfig := range graphConfigs {
renderGraph(cfg, &graphConfig)
}
}
func renderGraph(cfg *LumoConfig, graphConfig *GraphConfig) {
zap.S().Debugf("Generating graph '%s'", graphConfig.Title)
graphConfig.Title = interpolateGraphConfig(graphConfig.Title, cfg)
outputFile := graphOutputPath(cfg.OutDir, graphConfig.Title)
zap.S().Infof("Generating graph '%s'", graphConfig.Title)
if err := generateGraph(cfg, graphConfig, outputFile); err != nil {
zap.S().Errorf("error generating graph: %v", err)
}
}
func graphOutputPath(outDir, title string) string {
nameBase := title
if parts := strings.Split(nameBase, " - "); len(parts) > 1 {
nameBase = parts[len(parts)-1]
}
if nameBase == "" {
nameBase = "untitled_graph"
}
return filepath.Join(outDir, toSnakeCase(nameBase)+".png")
}
func validateGetGraphsFlags(cfg *LumoConfig) error {
if cfg.Endpoint == "" {
return fmt.Errorf("%w: -endpoint flag is required", ErrFlagRequired)
}
if cfg.Service == "" {
return fmt.Errorf("%w: -service flag is required", ErrFlagRequired)
}
if cfg.Token == "" {
return fmt.Errorf("%w: -token flag is required", ErrFlagRequired)
}
if len(cfg.Groups) == 0 {
return fmt.Errorf("%w: -groups flag is required", ErrFlagRequired)
}
return nil
}
func initFonts() error {
ttf, err := opentype.Parse(mediumFontTTF)
if err != nil {
return fmt.Errorf("error parsing embedded font: %w", err)
}
ttfBold, err := opentype.Parse(boldFontTTF)
if err != nil {
return fmt.Errorf("error parsing embedded bold font: %w", err)
}
font.DefaultCache.Add([]font.Face{
{
Font: mediumFont,
Face: ttf,
},
{
Font: boldFont,
Face: ttfBold,
},
})
plot.DefaultFont = mediumFont
return nil
}