-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.go
309 lines (267 loc) · 7.52 KB
/
plot.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
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
306
307
308
309
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
"gopkg.in/yaml.v3"
)
var plotCommand = &cli.Command{
Name: "plot",
Usage: "Interactive command to generate a single plot",
Action: Plot,
Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "preview",
Required: false,
Usage: "Preview the plot in a browser window.",
Destination: &plotOpts.preview,
},
&cli.BoolFlag{
Name: "compact",
Required: false,
Usage: "Emit compact json instead of pretty-printed.",
Destination: &plotOpts.compact,
},
&cli.BoolFlag{
Name: "validate",
Required: false,
Usage: "Validate the input file without running queries.",
Destination: &plotOpts.validate,
},
&cli.StringSliceFlag{
Name: "source",
Aliases: []string{"s"},
Required: false,
Usage: "Specify the url of a data source, in the format name=url. May be repeated to specify multiple sources. Postgres urls take the form 'postgres://username:password@hostname:5432/database_name'",
Destination: &plotOpts.sources,
},
&cli.StringSliceFlag{
Name: "params",
Aliases: []string{"p"},
Required: false,
Usage: "Specify templating parameters, in the format key=value. May be repeated to specify multiple parameters.",
Destination: &plotOpts.params,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Required: false,
Usage: "Name of file JSON output should be written to. Output will be emitted to stdout by default.",
Destination: &plotOpts.output,
},
&cli.StringFlag{
Name: "conf",
Required: false,
Usage: "Path of directory containing configuration.",
Destination: &plotOpts.confDir,
},
}, loggingFlags...),
}
var plotOpts struct {
preview bool
compact bool
sources cli.StringSlice
params cli.StringSlice
output string
validate bool
confDir string
}
func Plot(cc *cli.Context) error {
ctx := cc.Context
setupLogging()
cfg := &PlotConfig{
BasisTime: time.Now().UTC(),
Sources: map[string]DataSource{
"static": &StaticDataSource{},
"demo": &DemoDataSource{},
},
TemplateParams: map[string]any{},
}
for _, sopt := range plotOpts.sources.Value() {
name, url, ok := strings.Cut(sopt, "=")
if !ok {
return fmt.Errorf("source option not valid, use format 'name=url'")
}
if _, exists := cfg.Sources[name]; exists {
return fmt.Errorf("duplicate source %q specified", name)
}
if strings.HasPrefix(url, "postgres:") {
cfg.Sources[name] = NewPgDataSource(url)
} else {
return fmt.Errorf("unsupported source url: %q", url)
}
}
for _, param := range plotOpts.params.Value() {
key, value, ok := strings.Cut(param, "=")
if !ok {
return fmt.Errorf("params option not valid, use format 'key=value'")
}
if _, exists := cfg.TemplateParams[key]; exists {
return fmt.Errorf("duplicate template parameter %q specified", key)
}
cfg.TemplateParams[key] = value
}
if plotOpts.confDir != "" {
conffs := os.DirFS(plotOpts.confDir)
colorConfContent, err := fs.ReadFile(conffs, "colors.yaml")
if err == nil {
slog.Info("Parsing colors.yaml", "filename", path.Join(plotOpts.confDir, "colors.yaml"))
var cd ColorDoc
if err := yaml.Unmarshal(colorConfContent, &cd); err != nil {
return fmt.Errorf("failed to unmarshal colors.yaml: %w", err)
}
cfg.DefaultColor = cd.Default
cfg.Colors = make(map[string]string, len(cd.Colors))
for _, nc := range cd.Colors {
cfg.Colors[nc.Name] = nc.Color
}
} else if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to read colors: %w", err)
}
}
if cc.NArg() != 1 {
return fmt.Errorf("plot definition must be supplied as an argument")
}
fname := cc.Args().Get(0)
fcontent, err := os.ReadFile(fname)
if err != nil {
return fmt.Errorf("failed to read plot definition: %w", err)
}
templated, err := ExecuteTemplate(ctx, string(fcontent), cfg)
if err != nil {
return fmt.Errorf("failed to execute templates for plot definition: %w", err)
}
pd, err := parsePlotDef(fname, []byte(templated))
if err != nil {
return fmt.Errorf("failed to parse plot definition: %w", err)
}
if plotOpts.validate {
fmt.Println("Name: " + pd.Name)
fmt.Println("Frequency: " + pd.Frequency)
fmt.Println("Datasets:")
for _, ds := range pd.Datasets {
fmt.Println(" Name: " + ds.Name)
fmt.Println(" Source: " + ds.Source)
fmt.Println(" Query:")
fmt.Println(indent(ds.Query, " "))
}
return nil
}
slog.Info("generating figure", "filename", fname)
fig, err := generateFig(ctx, pd, cfg)
if err != nil {
return fmt.Errorf("failed to generate plot: %w", err)
}
figDat := FigureData{
Fig: fig,
Params: pd.Parameters,
DynLayout: pd.DynLayout,
Config: pd.Config,
}
var data []byte
if plotOpts.compact {
data, err = json.Marshal(figDat)
} else {
data, err = json.MarshalIndent(figDat, "", " ")
}
if err != nil {
return fmt.Errorf("failed to marshal to json: %w", err)
}
var out io.Writer = os.Stdout
if plotOpts.output != "" {
f, err := os.Create(plotOpts.output)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer f.Close()
out = f
}
fmt.Fprintln(out, string(data))
if plotOpts.preview {
if err := preview(figDat); err != nil {
return fmt.Errorf("preview plot: %w", err)
}
}
return nil
}
type DemoDataSource struct{}
func (s *DemoDataSource) GetDataSet(_ context.Context, query string, params ...any) (DataSet, error) {
switch query {
case "populations":
return &StaticDataSet{Data: map[string][]any{
"creature": {"giraffes", "orangutans", "monkeys"},
"month1": {20, 14, 23},
"month2": {2, 18, 29},
}}, nil
default:
return nil, fmt.Errorf("unknown demo dataset: %s", query)
}
}
func indent(s string, prefix string) string {
s = strings.ReplaceAll(s, "\n", "\n"+prefix)
return prefix + s
}
func plotname(fname string) string {
base := filepath.Base(fname)
return strings.TrimSuffix(base, filepath.Ext(fname))
}
func parsePlotDef(fname string, content []byte) (*PlotDef, error) {
slog.Info("parsing plot definition file", "filename", fname)
var pd PlotDef
if err := yaml.Unmarshal(content, &pd); err != nil {
return nil, fmt.Errorf("failed to unmarshal plot definition: %w", err)
}
if pd.Name == "" {
pd.Name = plotname(fname)
}
for _, s := range pd.Series {
switch s.Type {
case SeriesTypeBar, SeriesTypeHBar, SeriesTypeLine, SeriesTypeScatter, SeriesTypeBox, SeriesTypeHBox:
default:
return nil, fmt.Errorf("unknown series type: %q", s.Type)
}
switch s.Fill {
case FillTypeNone, FillTypeToZero:
default:
return nil, fmt.Errorf("unknown series fill: %q", s.Fill)
}
}
for _, s := range pd.Scalars {
switch s.Type {
case ScalarTypeNumber, ScalarTypeGauge:
default:
return nil, fmt.Errorf("unknown scalar type: %q", s.Type)
}
switch s.DeltaType {
case DeltaTypeNone, DeltaTypeRelative, DeltaTypeAbsolute:
default:
return nil, fmt.Errorf("unknown scalar delta type: %q", s.DeltaType)
}
}
// annotate series with order in definition
for i := range pd.Series {
pd.Series[i].order = i
}
for _, t := range pd.Tables {
switch t.Type {
case TableTypeHeatmap, TableTypeCategoryBar, TableTypeMarkers:
default:
return nil, fmt.Errorf("unknown table type: %q", t.Type)
}
}
// annotate series with order in definition
for i := range pd.Tables {
pd.Tables[i].order = i
}
return &pd, nil
}