Skip to content

Commit 17a45ce

Browse files
committed
Split the kind_registry compiler passes
1 parent bfee88f commit 17a45ce

14 files changed

Lines changed: 421 additions & 441 deletions

.cog/config.yaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ inputs:
1919
version: '%kind_registry_version%'
2020
transformations:
2121
- '%__config_dir%/resources/kind_registry.schema.transforms.yaml'
22+
- '%__config_dir%/resources/candlestick/schema.transforms.yaml'
23+
- '%__config_dir%/resources/cloudwatch/schema.transforms.yaml'
24+
- '%__config_dir%/resources/common/schema.transforms.yaml'
25+
- '%__config_dir%/resources/googlecloudmonitoring/schema.transforms.yaml'
26+
- '%__config_dir%/resources/heatmap/schema.transforms.yaml'
27+
- '%__config_dir%/resources/loki/schema.transforms.yaml'
28+
- '%__config_dir%/resources/piechart/schema.transforms.yaml'
29+
- '%__config_dir%/resources/timeseries/schema.transforms.yaml'
2230
- openapi:
2331
url: 'https://raw.githubusercontent.com/grafana/grafana/v11.6.0/public/openapi3.json'
2432
no_validate: true
@@ -92,7 +100,7 @@ inputs:
92100
transformations:
93101
- '%__config_dir%/resources/prometheus/schema.transforms.yaml'
94102

95-
# The schema for bigquery queries
103+
# Bigquery queries
96104
- cue:
97105
entrypoint: '%__config_dir%/resources/bigquery'
98106
metadata:
@@ -102,7 +110,7 @@ inputs:
102110
cue_imports:
103111
- '%kind_registry_path%/grafana/%kind_registry_version%/common:github.com/grafana/grafana/packages/grafana-schema/src/common'
104112

105-
# The schema for athena queries
113+
# Athena queries
106114
- cue:
107115
entrypoint: '%__config_dir%/resources/athena'
108116
metadata:
@@ -114,7 +122,7 @@ inputs:
114122
transformations:
115123
- '%__config_dir%/resources/athena/schema.transforms.yaml'
116124

117-
# The schema for "datasource" queries
125+
# "datasource" queries
118126
- cue:
119127
entrypoint: '%__config_dir%/resources/datasource'
120128
metadata:
@@ -133,18 +141,19 @@ inputs:
133141
transformations:
134142
- '%__config_dir%/resources/dashboardv2beta1/schema.transforms.yaml'
135143

136-
# The schema for units
144+
# Units
137145
- cue:
138146
entrypoint: '%__config_dir%/resources/units'
139147

140-
# The schema for resource manifests
148+
# Resource manifests
141149
- cue:
142150
entrypoint: '%__config_dir%/resources/resource'
143151

144152
transformations:
145153
schemas:
146154
- { path: '%__config_dir%/resources/common.schema.transforms.yaml' }
147155
- { path: '%__config_dir%/resources/dataqueries.schema.transforms.yaml' }
156+
- { path: '%__config_dir%/resources/panels.schema.transforms.yaml' }
148157
- { path: '%__config_dir%/resources/dashboard/schema.transforms.yaml' }
149158
# To avoid it to conflict with the refid in the dataquery schema.
150159
- { path: '%__config_dir%/resources/dashboardv2beta1/dataqueries_refid.schema.transforms.yaml' }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/grafana/cog/main/schemas/compiler_passes.json
2+
3+
passes:
4+
# This field is parsed incorrectly from the CUE schema: its definition and
5+
# defaults are merged for some reason.
6+
# TODO: remove once the parser is fixed.
7+
# See: https://github.com/grafana/cog/issues/292
8+
- retype_field:
9+
field: candlestick.Options.colors
10+
as:
11+
kind: ref
12+
ref: { referred_pkg: candlestick, referred_type: CandlestickColors }
13+
default: { down: "red", up: "green", flat: "gray" }
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/grafana/cog/main/schemas/compiler_passes.json
2+
3+
passes:
4+
# Simplify the definition of the `QueryEditorArrayExpression.expressions` field from
5+
# `[...#QueryEditorExpression] | [...#QueryEditorArrayExpression]` to
6+
# `[...#QueryEditorExpression]`.
7+
# This should be semantically equivalent since `QueryEditorExpression` is a
8+
# union type that includes `QueryEditorArrayExpression`.
9+
- retype_field:
10+
field: cloudwatch.QueryEditorArrayExpression.expressions
11+
as:
12+
kind: array
13+
array:
14+
value_type:
15+
kind: ref
16+
ref: { referred_pkg: cloudwatch, referred_type: QueryEditorExpression }
17+
18+
# Retype this object to manually define its disjunction mapping.
19+
# TODO: check if we need to do this, or if the DisjunctionInferMapping would
20+
# be able to infer it on its own.
21+
- retype_object:
22+
object: cloudwatch.QueryEditorExpression
23+
as:
24+
kind: disjunction
25+
disjunction:
26+
branches:
27+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorArrayExpression } }
28+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorPropertyExpression } }
29+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorGroupByExpression } }
30+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorFunctionExpression } }
31+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorFunctionParameterExpression } }
32+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: QueryEditorOperatorExpression } }
33+
discriminator: type
34+
discriminator_mapping:
35+
and: QueryEditorArrayExpression
36+
or: QueryEditorArrayExpression
37+
property: QueryEditorPropertyExpression
38+
groupBy: QueryEditorGroupByExpression
39+
function: QueryEditorFunctionExpression
40+
functionParameter: QueryEditorFunctionParameterExpression
41+
operator: QueryEditorOperatorExpression
42+
43+
- add_object:
44+
object: cloudwatch.CloudWatchQuery
45+
as:
46+
kind: disjunction
47+
hints: { implements_variant: dataquery }
48+
disjunction:
49+
branches:
50+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: CloudWatchMetricsQuery } }
51+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: CloudWatchLogsQuery } }
52+
- { kind: ref, ref: { referred_pkg: cloudwatch, referred_type: CloudWatchAnnotationQuery } }
53+
discriminator: queryMode
54+
discriminator_mapping:
55+
Metrics: CloudWatchMetricsQuery
56+
Logs: CloudWatchLogsQuery
57+
Annotations: CloudWatchAnnotationQuery
58+
59+
- schema_set_entry_point:
60+
package: cloudwatch
61+
entry_point: CloudWatchQuery
62+
63+
- fields_set_default:
64+
defaults:
65+
cloudwatch.CloudWatchMetricsQuery.queryMode: Metrics
66+
cloudwatch.CloudWatchLogsQuery.queryMode: Logs
67+
cloudwatch.CloudWatchAnnotationQuery.queryMode: Annotations
68+
69+
- hint_object:
70+
object: cloudwatch.CloudWatchMetricsQuery
71+
hints:
72+
skip_variant_plugin_registration: true
73+
74+
- hint_object:
75+
object: cloudwatch.CloudWatchLogsQuery
76+
hints:
77+
skip_variant_plugin_registration: true
78+
79+
- hint_object:
80+
object: cloudwatch.CloudWatchAnnotationQuery
81+
hints:
82+
skip_variant_plugin_registration: true
83+
84+
- fields_set_required:
85+
fields:
86+
- cloudwatch.CloudWatchMetricsQuery.queryMode
87+
- cloudwatch.CloudWatchLogsQuery.queryMode
88+
- cloudwatch.CloudWatchAnnotationQuery.queryMode
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/grafana/cog/main/schemas/compiler_passes.json
2+
3+
passes:
4+
- omit_fields:
5+
fields: [ common.DataSourceRef.apiVersion ]
6+
7+
- retype_object:
8+
object: common.TimeZone
9+
as:
10+
kind: scalar
11+
scalar: { scalar_kind: string }
12+
default: browser
13+
comments:
14+
- 'A specific timezone from https://en.wikipedia.org/wiki/Tz_database'
15+
16+
###########################
17+
# common.VizLegendOptions #
18+
###########################
19+
20+
# Without these values, some panels might misbehave when displayed in a dashboard
21+
- fields_set_default:
22+
defaults:
23+
common.VizLegendOptions.displayMode: "list"
24+
common.VizLegendOptions.placement: "bottom"
25+
common.VizLegendOptions.calcs: []
26+
27+
###########################
28+
# common.GraphFieldConfig #
29+
###########################
30+
31+
# The `insertNulls` field is missing.
32+
# Added upstream here: https://github.com/grafana/grafana/pull/85861
33+
- add_fields:
34+
to: common.GraphFieldConfig
35+
fields:
36+
- name: insertNulls
37+
type:
38+
kind: disjunction
39+
disjunction:
40+
branches:
41+
- { kind: scalar, scalar: { scalar_kind: bool } }
42+
- { kind: scalar, scalar: { scalar_kind: uint32 } }
43+
44+
###############
45+
# table panel #
46+
###############
47+
48+
- fields_set_not_required:
49+
fields: [ common.TableFieldOptions.CellOptions ]

.cog/resources/dashboard/schema.transforms.yaml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,164 @@ passes:
1111
as:
1212
kind: ref
1313
ref: { referred_pkg: common, referred_type: DataSourceRef }
14+
15+
#############
16+
# Dashboard #
17+
#############
18+
19+
# These fields are incorrectly set as non-required in the CUE schema.
20+
# Loading a dashboard with these set to null will fail.
21+
- fields_set_required:
22+
fields: [
23+
dashboard.Dashboard.annotations,
24+
dashboard.Dashboard.templating,
25+
]
26+
27+
# Defined as an empty type in the CUE schemas.
28+
# It is also not used at all: references to this type are replaced by a "composable slot"
29+
- omit:
30+
objects: [
31+
dashboard.Target
32+
]
33+
34+
- name_anonymous_struct:
35+
field: dashboard.Dashboard.timepicker
36+
as: TimePicker
37+
38+
# From: `panels: [...#Panel | #RowPanel | #GraphPanel | #HeatmapPanel]`
39+
# To: `panels: [...#Panel | #GraphPanel | #HeatmapPanel]`
40+
- retype_field:
41+
field: dashboard.Dashboard.panels
42+
as:
43+
kind: array
44+
array:
45+
value_type:
46+
kind: disjunction
47+
disjunction:
48+
branches:
49+
- { kind: ref, ref: { referred_pkg: dashboard, referred_type: Panel } }
50+
- { kind: ref, ref: { referred_pkg: dashboard, referred_type: RowPanel } }
51+
discriminator: type
52+
discriminator_mapping:
53+
row: RowPanel
54+
cog_discriminator_catch_all: Panel
55+
56+
- retype_field:
57+
field: dashboard.Panel.options
58+
as:
59+
kind: scalar
60+
scalar: { scalar_kind: any }
61+
62+
- retype_field:
63+
field: dashboard.FieldConfig.custom
64+
as:
65+
kind: scalar
66+
scalar: { scalar_kind: any }
67+
68+
- retype_field:
69+
field: dashboard.Panel.Targets
70+
as:
71+
kind: array
72+
array:
73+
value_type:
74+
kind: composable_slot
75+
composable_slot: { variant: dataquery }
76+
77+
# typed as `links?: [...]` in the original schema
78+
- retype_field:
79+
field: dashboard.FieldConfig.links
80+
as:
81+
kind: array
82+
array:
83+
value_type:
84+
kind: ref
85+
ref: { referred_pkg: dashboard, referred_type: DashboardLink }
86+
87+
# Add a few missing fields.
88+
# This is meant to be removed once the schema is updated.
89+
- add_fields:
90+
to: dashboard.AnnotationQuery
91+
fields:
92+
- name: expr
93+
type:
94+
kind: scalar
95+
scalar: { scalar_kind: string }
96+
97+
#######################
98+
# Dashboard variables #
99+
#######################
100+
101+
- add_fields:
102+
to: dashboard.VariableModel
103+
fields:
104+
- name: auto
105+
comments: [ 'Dynamically calculates interval by dividing time range by the count specified.' ]
106+
type:
107+
kind: scalar
108+
scalar: { scalar_kind: bool }
109+
default: false
110+
- name: auto_min
111+
comments: [ 'The minimum threshold below which the step count intervals will not divide the time.' ]
112+
type:
113+
kind: scalar
114+
default: '10s'
115+
scalar: { scalar_kind: string }
116+
- name: auto_count
117+
comments:
118+
- 'How many times the current time range should be divided to calculate the value, similar to the Max data points query option.'
119+
- 'For example, if the current visible time range is 30 minutes, then the auto interval groups the data into 30 one-minute increments.'
120+
type:
121+
kind: scalar
122+
default: 30
123+
scalar:
124+
scalar_kind: int32
125+
constraints:
126+
- { op: '>', args: [0] }
127+
128+
#############
129+
# row panel #
130+
#############
131+
132+
# From: `panels: [...#Panel | #RowPanel]`
133+
# To: `panels: [...#Panel]`
134+
- retype_field:
135+
field: dashboard.RowPanel.panels
136+
as:
137+
kind: array
138+
array:
139+
value_type:
140+
kind: ref
141+
ref: { referred_pkg: dashboard, referred_type: Panel }
142+
143+
############
144+
# snapshot #
145+
############
146+
147+
- add_fields:
148+
to: dashboard.Snapshot
149+
fields:
150+
- name: dashboard
151+
type:
152+
kind: ref
153+
ref: { referred_pkg: dashboard, referred_type: Dashboard }
154+
155+
# In the original `librarypanel.PanelModel` schema, the "model" field is left
156+
# mainly undefined but a comment indicates that it should be the same panel
157+
# schema defined in dashboard with a few fields omitted:
158+
# model: Omit<dashboard.Panel, 'gridPos' | 'id' | 'libraryPanel'>
159+
- duplicate_object:
160+
object: dashboard.Panel
161+
as: librarypanel.PanelModel
162+
omit_fields:
163+
- id
164+
- gridPos
165+
- libraryPanel
166+
- retype_field:
167+
field: librarypanel.LibraryPanel.model
168+
as:
169+
kind: ref
170+
ref: { referred_pkg: librarypanel, referred_type: PanelModel }
171+
172+
- omit:
173+
objects:
174+
- dashboard.HeatmapPanel

0 commit comments

Comments
 (0)