-
Notifications
You must be signed in to change notification settings - Fork 20
/
query.libsonnet
159 lines (147 loc) · 3.8 KB
/
query.libsonnet
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
local a = import 'github.com/crdsonnet/astsonnet/main.libsonnet';
local crdsonnet = import 'github.com/crdsonnet/crdsonnet/crdsonnet/main.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
local refactor = import './refactor.libsonnet';
local utils = import './utils.libsonnet';
{
local root = self,
titleMapping: {
azuremonitor: 'azureMonitor',
cloudwatch: 'cloudWatch',
googlecloudmonitoring: 'googleCloudMonitoring',
grafanapyroscope: 'grafanaPyroscope',
testdata: 'testData',
},
render(schemas):
local files = self.getFilesForSchemas(schemas);
{ 'query.libsonnet': root.queryIndex(files) }
+ {
[file.path]: file.content
for file in files
},
getFilesForSchemas(schemas):
std.foldl(
function(acc, schema)
local title = std.get(root.titleMapping, std.asciiLower(schema.info.title), schema.info.title);
acc + [{
title: title,
path: 'query/' + title + '.libsonnet',
content: root.generateLib(schema),
}],
schemas,
[],
),
generateLib(schema):
local title = std.get(root.titleMapping, std.asciiLower(schema.info.title), schema.info.title);
local customSchema =
schema + {
components+: {
schemas+: std.get(fixes, title, {}),
},
};
local subSchema = customSchema.components.schemas.dataquery;
local ast =
utils.unwrapFromCRDsonnet(
crdsonnet.openapi.render(
title,
subSchema,
customSchema,
refactor.ASTProcessor,
addNewFunction=false,
),
title,
);
utils.addDoc(
ast,
title,
'query.'
).toString()
+ (if std.member(self.hasCustom, title)
then
'\n +'
+ a.parenthesis.new(
a.import_statement.new('../custom/query/' + title + '.libsonnet'),
).toString()
else ''),
// FIXME: Some schemas follow a different structure, temporarily covering for this.
local fixes = {
cloudWatch: {
dataquery: {
type: 'object',
oneOf: [
{ '$ref': '#/components/schemas/CloudWatchAnnotationQuery' },
{ '$ref': '#/components/schemas/CloudWatchLogsQuery' },
{ '$ref': '#/components/schemas/CloudWatchMetricsQuery' },
],
},
QueryEditorArrayExpression+: {
properties+: {
// Prevent infinite recursion
expressions+: { items: {} },
},
},
},
azureMonitor: {
dataquery: {
'$ref': '#/components/schemas/AzureMonitorQuery',
},
},
tempo: {
dataquery: {
'$ref': '#/components/schemas/TempoQuery',
},
},
googleCloudMonitoring: {
dataquery: {
'$ref': '#/components/schemas/CloudMonitoringQuery',
},
},
testData: {
dataquery+: {
properties+: {
// `points` is an array of arrays, this renders awkwardly with CRDsonnet
points: { type: 'array' },
},
},
},
expr: {
dataquery: {
'$ref': '#/components/schemas/expr',
},
},
},
queryIndex(files):
a.object.new(
[
a.field.new(
a.string.new('#'),
a.literal.new( // render docsonnet as literal to avoid docsonnet dependency
d.package.newSub(
'query',
'grafonnet.query'
),
),
),
]
+ [
a.field.new(
a.string.new(file.title),
a.import_statement.new(file.path),
)
for file in files
]
).toString(),
hasCustom: [
'azureMonitor',
'cloudWatch',
'elasticsearch',
'expr',
'googleCloudMonitoring',
'grafanaPyroscope',
'loki',
'parca',
'prometheus',
'tempo',
'testData',
],
}