-
Notifications
You must be signed in to change notification settings - Fork 100
/
parse_dashboard.go
159 lines (134 loc) · 4.39 KB
/
parse_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
package rillv1
import (
"errors"
"fmt"
"github.com/google/uuid"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"gopkg.in/yaml.v3"
)
type DashboardYAML struct {
commonYAML `yaml:",inline"` // Not accessed here, only setting it so we can use KnownFields for YAML parsing
Title string `yaml:"title"`
Columns uint32 `yaml:"columns"`
Gap uint32 `yaml:"gap"`
Items []*struct {
Component yaml.Node `yaml:"component"` // Can be a name (string) or inline component definition (map)
X *uint32 `yaml:"x"`
Y *uint32 `yaml:"y"`
Width *uint32 `yaml:"width"`
Height *uint32 `yaml:"height"`
FontSize uint32 `yaml:"font_size"`
} `yaml:"items"`
}
func (p *Parser) parseDashboard(node *Node) error {
// Parse YAML
tmp := &DashboardYAML{}
err := p.decodeNodeYAML(node, true, tmp)
if err != nil {
return err
}
// Validate SQL or connector isn't set
if node.SQL != "" {
return fmt.Errorf("dashboards cannot have SQL")
}
if !node.ConnectorInferred && node.Connector != "" {
return fmt.Errorf("dashboards cannot have a connector")
}
// Ensure there's at least one item
if len(tmp.Items) == 0 {
return errors.New(`at least one item must be configured`)
}
// Parse items.
// Each item can either reference an externally defined component by name or define a component inline.
items := make([]*runtimev1.DashboardItem, len(tmp.Items))
var inlineComponentDefs []*componentDef
for i, item := range tmp.Items {
if item == nil {
return fmt.Errorf("item at index %d is nil", i)
}
component, inlineComponentDef, err := p.parseDashboardItemComponent(node.Name, i, item.Component)
if err != nil {
return fmt.Errorf("invalid component at index %d: %w", i, err)
}
// Track inline component definitions so we can insert them after we have validated all components
if inlineComponentDef != nil {
inlineComponentDefs = append(inlineComponentDefs, inlineComponentDef)
}
items[i] = &runtimev1.DashboardItem{
Component: component,
X: item.X,
Y: item.Y,
Width: item.Width,
Height: item.Height,
FontSize: item.FontSize,
}
node.Refs = append(node.Refs, ResourceName{Kind: ResourceKindComponent, Name: component})
}
// Track dashboard
r, err := p.insertResource(ResourceKindDashboard, node.Name, node.Paths, node.Refs...)
if err != nil {
return err
}
// NOTE: After calling insertResource, an error must not be returned. Any validation should be done before calling it.
r.DashboardSpec.Title = tmp.Title
r.DashboardSpec.Columns = tmp.Columns
r.DashboardSpec.Gap = tmp.Gap
r.DashboardSpec.Items = items
// Track inline components
for _, def := range inlineComponentDefs {
r, err := p.insertResource(ResourceKindComponent, def.name, node.Paths, def.refs...)
if err != nil {
// Normally we could return the error, but we can't do that here because we've already inserted the dashboard.
// Since the component has been validated with insertDryRun in parseDashboardItemComponent, this error should never happen in practice.
// So let's panic.
panic(err)
}
r.ComponentSpec = def.spec
}
return nil
}
// parseDashboardItemComponent parses a dashboard item's "component" property.
// It may be a string (name of an externally defined component) or an inline component definition.
func (p *Parser) parseDashboardItemComponent(dashboardName string, idx int, n yaml.Node) (string, *componentDef, error) {
if n.Kind == yaml.ScalarNode {
var name string
err := n.Decode(&name)
if err != nil {
return "", nil, err
}
return name, nil, nil
}
if n.Kind != yaml.MappingNode {
return "", nil, errors.New("expected a component name or inline declaration")
}
tmp := &ComponentYAML{}
err := n.Decode(tmp)
if err != nil {
return "", nil, err
}
spec, refs, err := p.parseComponentYAML(tmp)
if err != nil {
return "", nil, err
}
spec.DefinedInDashboard = true
name := fmt.Sprintf("%s--component-%d", dashboardName, idx)
err = p.insertDryRun(ResourceKindComponent, name)
if err != nil {
name = fmt.Sprintf("%s--component-%d-%s", dashboardName, idx, uuid.New())
err = p.insertDryRun(ResourceKindComponent, name)
if err != nil {
return "", nil, err
}
}
def := &componentDef{
name: name,
refs: refs,
spec: spec,
}
return name, def, nil
}
type componentDef struct {
name string
refs []ResourceName
spec *runtimev1.ComponentSpec
}