-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
271 lines (221 loc) · 8.76 KB
/
model.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
package main
import (
"context"
"fmt"
"os"
"time"
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
)
// PlotConfig provides external configuration and context to the generation
// of a plot.
type PlotConfig struct {
// BasisTime is the time of execution of the queries in a plot
// Generally it is the current time but can be set to a time in the past
BasisTime time.Time
// Sources is a mapping of names to datasources. The names can be
// referenced in a dataset definition
Sources map[string]DataSource
// Template parameters can be provided on the command line. They
// are passed directly to the templating engine.
TemplateParams map[string]any
DefaultColor string
// Colors is a mapping of friendly names to hex values of colors
Colors map[string]string
// Profiles contains information about different variants of plot defs
Profiles []*ProcessingProfile
MatchGlob string
}
func (c *PlotConfig) MaybeLookupColor(name string, seriesName string) string {
// if name == "" {
// return c.DefaultColor
// }
v, ok := c.Colors[name]
if ok {
return v
}
return name
}
type PlotFrequency string
const (
PlotFrequencyWeekly PlotFrequency = "weekly"
PlotFrequencyDaily PlotFrequency = "daily"
PlotFrequencyHourly PlotFrequency = "hourly"
)
func (f PlotFrequency) String() string { return string(f) }
func (f PlotFrequency) Truncate(t time.Time) time.Time {
switch f {
case PlotFrequencyWeekly:
return t.Truncate(7 * 24 * time.Hour)
case PlotFrequencyDaily:
return t.Truncate(24 * time.Hour)
case PlotFrequencyHourly:
return t.Truncate(time.Hour)
default:
panic(fmt.Sprintf("unsupported plot frequency: %q", f))
}
}
type ProcessingProfile struct {
Source string `yaml:"source"`
OutTpl string `yaml:"output"`
Variants []map[string]any `yaml:"variants"`
}
func (p *ProcessingProfile) SourceIsDir() bool {
info, err := os.Stat(p.Source)
return err == nil && info.IsDir()
}
type PlotDef struct {
Name string `yaml:"name"`
Frequency PlotFrequency `yaml:"frequency"`
Datasets []DataSetDef `yaml:"datasets"`
Computed []ComputedDef `yaml:"computed"`
Series []SeriesDef `yaml:"series"`
Scalars []ScalarDef `yaml:"scalars"`
Tables []TableDef `yaml:"tables"`
Layout grob.Layout `yaml:"layout"`
Config map[string]any `yaml:"config"`
Parameters map[string]any `yaml:"params"`
DynLayout map[string]any `yaml:"dynamicLayout"`
}
type DataSetDef struct {
Name string `yaml:"name"`
Source string `yaml:"source"`
Query string `yaml:"query"`
}
type SeriesDef struct {
Type SeriesType `yaml:"type"`
Name string `yaml:"name"` // name of the series
Color string `yaml:"color"`
Marker MarkerType `yaml:"marker"`
Fill FillType `yaml:"fill"`
DataSet string `yaml:"dataset"`
Labels string `yaml:"labels"` // the name of the field the series should use for labels
Values string `yaml:"values"` // the name of the field the series should use for values
GroupField string `yaml:"groupfield"` // optional name of a field the series should use for grouping into related series
GroupValue string `yaml:"groupvalue"` // optional value of a field the series should use for grouping into related series
Percent bool `yaml:"percent"`
order int // used for retaining ordering of series
HoverTemplate string `yaml:"hovertemplate,omitempty"`
Visible *bool `yaml:"visible"`
Yaxis string `yaml:"yaxis"`
}
type SeriesType string
const (
SeriesTypeBar SeriesType = "bar" // vertical bars
SeriesTypeHBar SeriesType = "hbar" // horizontal bars
SeriesTypeLine SeriesType = "line" // lines
SeriesTypeScatter SeriesType = "scatter" // scatter
SeriesTypeBox SeriesType = "box" // vertical box plot
SeriesTypeHBox SeriesType = "hbox" // horizontal box plot
)
func (t SeriesType) String() string { return string(t) }
type FillType string
const (
FillTypeNone FillType = ""
FillTypeToZero FillType = "tozero"
)
func (t FillType) String() string { return string(t) }
type MarkerType string
const (
// Note: this is only a subset of what plotly supports
// see https://plotly.com/javascript/reference/scatter/#scatter-marker-symbol
MarkerTypeNone MarkerType = ""
MarkerTypeCircle MarkerType = "circle"
MarkerTypeSquare MarkerType = "square"
MarkerTypeDiamond MarkerType = "diamond"
MarkerTypeTriangle MarkerType = "triangle"
MarkerTypeHexagon MarkerType = "hexagon"
)
func (t MarkerType) String() string { return string(t) }
type ScalarDef struct {
Type ScalarType `yaml:"type"`
Name string `yaml:"name"` // name of the scalar
Color string `yaml:"color"`
DataSet string `yaml:"dataset"`
Value string `yaml:"value"` // the name of the field in the dataset that should be used for the scalar value
ValueSuffix string `yaml:"valueSuffix"` // a string to append after the value
ValuePrefix string `yaml:"valuePrefix"` // a string to prepend to the value
DeltaDataSet string `yaml:"deltaDataset"` // the name of a dataset to use for a delta value
DeltaValue string `yaml:"deltaValue"` // the name of the field in the delta dataset that should be used for the scalar value
DeltaType DeltaType `yaml:"deltaType"` // the type of delta contained in the value field
IncreaseColor string `yaml:"increaseColor"` // the color to use for delta that show an increase
DecreaseColor string `yaml:"decreaseColor"` // the color to use for delta that show an decrease
Visible *bool `yaml:"visible"` // if this trace should be shown
Gauge *grob.IndicatorGauge `yaml:"gauge"` // gauge configuration
Domain *grob.IndicatorDomain `yaml:"domain"`
}
type ScalarType string
const (
ScalarTypeNumber ScalarType = "number" // display the scalar value as a number
ScalarTypeGauge ScalarType = "gauge" // display the scalar value as a gauge
)
func (t ScalarType) String() string { return string(t) }
type DeltaType string
const (
DeltaTypeNone DeltaType = ""
DeltaTypeRelative DeltaType = "relative" // the delta is an absolute value and should be displayed with a relative % change to the scalar
DeltaTypeAbsolute DeltaType = "absolute" // the delta is an absolute value and should be displayed with a relative % change to the scalar
)
func (t DeltaType) String() string { return string(t) }
type DataSource interface {
GetDataSet(ctx context.Context, query string, params ...any) (DataSet, error)
}
type DataSeries struct {
Labels []string
Values []float64
}
type DataSet interface {
Next() bool
Err() error
Field(name string) any
ResetIterator()
}
// ColorDoc represents a document that defines a set of named colors
type ColorDoc struct {
Default string `yaml:"default"`
Colors []NamedColor `yaml:"colors"`
}
type NamedColor struct {
Name string `yaml:"name"`
Color string `yaml:"color"`
}
// ComputedDef defines a computed dataset from a combination of others
type ComputedDef struct {
Name string `yaml:"name"`
Function ComputeType `yaml:"function"`
DataSets []ComputeDataSetDef `yaml:"datasets"`
}
type ComputeDataSetDef struct {
DataSet string `yaml:"dataset"` // the name of the dataset
JoinField string `yaml:"joinField"` // the field name that will be used to join the datasets
ValueField string `yaml:"valueField"` // the field containing the value that will be used in the computation
}
type ComputeType string
const (
ComputeTypeDiff ComputeType = "diff" // compute the difference between the first series and the second (first-second)
)
func (t ComputeType) String() string { return string(t) }
type FigureData struct {
*grob.Fig
Params map[string]any `json:"params"`
DynLayout map[string]any `json:"dynamicLayout"`
Config map[string]any `json:"config"`
}
type TableDef struct {
Type TableType `yaml:"type"`
Name string `yaml:"name"`
DataSet string `yaml:"dataset"`
LabelsX string `yaml:"xLabels"`
LabelsY string `yaml:"yLabels"`
Values string `yaml:"values"`
Color string `yaml:"color"`
Colorbar *grob.HeatmapColorbar `yaml:"colorbar"`
Yaxis string `yaml:"yaxis"`
order int // used for retaining ordering of series
}
type TableType string
const (
TableTypeHeatmap TableType = "heatmap"
TableTypeCategoryBar TableType = "category+bar"
TableTypeMarkers TableType = "markers"
)
func (t TableType) String() string { return string(t) }