Source file grafana.ml
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
type config = {
url : Uri.t;
api_token : string option;
data_source : string;
timeout : float;
}
let config_of_json json =
{
url = JSON.(json |-> "url" |> as_string |> Uri.of_string);
api_token = JSON.(json |-> "api_token" |> as_string_opt);
data_source = JSON.(json |-> "data_source" |> as_string);
timeout =
JSON.(json |-> "timeout" |> as_float_opt |> Option.value ~default:20.);
}
type duration =
| Seconds of int
| Minutes of int
| Hours of int
| Days of int
| Weeks of int
| Month of int
| Years of int
let string_of_duration =
let f value unit = string_of_int value ^ unit in
function
| Seconds x -> f x "s"
| Minutes x -> f x "m"
| Hours x -> f x "h"
| Days x -> f x "d"
| Weeks x -> f x "w"
| Month x -> f x "M"
| Years x -> f x "y"
type alias = string
type yaxis = {format : string; label : string option}
type graph = {
title : string;
description : string;
queries : (InfluxDB.select * alias option) list;
interval : duration option;
yaxis_1 : yaxis option;
yaxis_2 : yaxis option;
}
type panel = Row of string | Graph of graph
type dashboard = {
uid : string;
title : string;
description : string;
panels : panel list;
}
let encode_target ((query : InfluxDB.select), alias) : JSON.u =
let label =
Option.map (fun a -> ("alias", `String a)) alias |> Option.to_list
in
`O
(label
@ [
("query", `String (InfluxDB.show_select ~grafana:true query));
("rawQuery", `Bool true);
("resultFormat", `String "time_series");
])
let encode_yaxis = function
| None -> `O [("show", `Bool false)]
| Some {format; label} ->
`O
(("format", `String format)
::
(match label with
| None -> []
| Some label -> [("label", `String label)])
@ [("show", `Bool true)])
let encode_panel config y panel : JSON.u =
match panel with
| Row title ->
`O
[
("type", `String "row");
("title", `String title);
( "gridPos",
`O
[
("h", `Float 1.);
("w", `Float 24.);
("x", `Float 0.);
( "y",
`Float
(let value = float !y in
y := !y + 1 ;
value) );
] );
]
| Graph {title; description; queries; interval; yaxis_1; yaxis_2} ->
let interval =
Option.map
(fun i -> ("interval", `String (string_of_duration i)))
interval
|> Option.to_list
in
let unit = match yaxis_1 with None -> "none" | Some x -> x.format in
`O
(interval
@ [
("type", `String "timeseries");
("datasource", `String config.data_source);
("title", `String title);
("description", `String description);
( "gridPos",
`O
[
("h", `Float 8.);
("w", `Float 24.);
("x", `Float 0.);
( "y",
`Float
(let value = float !y in
y := !y + 8 ;
value) );
] );
("targets", `A (List.map encode_target queries));
("yaxes", `A [encode_yaxis yaxis_1; encode_yaxis yaxis_2]);
( "fieldConfig",
`O
[
( "defaults",
`O
[
( "custom",
`O
[
("drawStyle", `String "line");
("lineInterpolation", `String "linear");
("showPoints", `String "always");
("pointSize", `Float 5.);
("spanNulls", `Bool true);
("lineWidth", `Float 1.);
("fillOpacity", `Float 10.);
("axisSoftMin", `Float 0.);
] );
("unit", `String unit);
] );
] );
])
let encode_dashboard config {uid; title; description; panels} : JSON.u =
`O
[
("uid", `String uid);
("title", `String title);
("description", `String description);
("panels", `A (List.map (encode_panel config (ref 0)) panels));
]
let make_url {url; _} path =
let path =
let base_path = Uri.path url in
if base_path <> "" && base_path.[String.length base_path - 1] = '/' then
base_path ^ path
else base_path ^ "/" ^ path
in
Uri.with_path url path
let with_timeout {timeout; _} p =
let timeout =
let* () = Lwt_unix.sleep timeout in
failwith "timeout"
in
Lwt.pick [p; timeout]
let uid_rex = rex "[a-zA-Z0-9._-]{1,128}"
type http_request = {
uri : Uri.t;
meth : Cohttp.Code.meth;
headers : Cohttp.Header.t;
body : Cohttp_lwt.Body.t option;
}
let http_call request config =
with_timeout config
@@ Cohttp_lwt_unix.Client.call
~headers:request.headers
?body:request.body
request.meth
request.uri
let string_of_http_request request =
let* body =
Option.map
(fun body ->
Lwt.map (fun s -> ", Body: " ^ s) @@ Cohttp_lwt.Body.to_string body)
request.body
|> Option.value ~default:(Lwt.return "")
in
return
@@ Format.sprintf
"Uri: %s, Method: %s, Headers: [%s]%s"
(Uri.to_string request.uri)
(Cohttp.Code.string_of_method request.meth)
(Cohttp.Header.to_string request.headers)
body
let handle_http_error resp_status resp_body request =
let* body = Cohttp_lwt.Body.to_string resp_body in
let* req = string_of_http_request request in
failwith
@@ sf
"Grafana responded with %s - %s for request (%s) "
(Cohttp.Code.string_of_status resp_status)
body
req
let update_dashboard config dashboard =
if dashboard.uid =~! uid_rex then
invalid_arg
(sf
"Grafana.update_dashboard: invalid UID: %s (must match: %s)"
dashboard.uid
(show_rex uid_rex)) ;
let authorization =
Option.map (fun t -> ("Authorization", "Bearer " ^ t)) config.api_token
|> Option.to_list
in
let* () =
let delete_request =
{
uri = make_url config ("dashboards/uid/" ^ dashboard.uid);
meth = `DELETE;
headers = Cohttp.Header.of_list authorization;
body = None;
}
in
let* response, body = http_call delete_request config in
match response.status with
| #Cohttp.Code.success_status | `Not_found ->
Cohttp_lwt.Body.drain_body body
| status -> handle_http_error status body delete_request
in
let body =
`O [("dashboard", encode_dashboard config dashboard)] |> JSON.encode_u
in
let create_request =
{
uri = make_url config "dashboards/db";
meth = `POST;
headers =
Cohttp.Header.of_list
@@ (("Content-Type", "application/json") :: authorization);
body = Option.some @@ Cohttp_lwt.Body.of_string body;
}
in
let* response, body = http_call create_request config in
match response.status with
| #Cohttp.Code.success_status -> Cohttp_lwt.Body.drain_body body
| status -> handle_http_error status body create_request
let where_clause_of_tag (tag_name, tag_label) =
InfluxDB.Tag (tag_name, EQ, tag_label)
let where_clause_of_tags hd tail =
List.fold_left
(fun clause tag -> InfluxDB.And (clause, where_clause_of_tag tag))
(where_clause_of_tag hd)
tail
let simple_query ?(tags = []) ~measurement ~field ~test () =
let where_clause =
InfluxDB.And (Grafana_time_filter, where_clause_of_tags ("test", test) tags)
in
InfluxDB.select
[Function (MEAN, Field field)]
~from:(Measurement measurement)
~where:where_clause
~group_by:(Time {interval = Grafana_interval; tag = None; fill = None})
let simple_graph ?title ?(description = "") ?(yaxis_format = "s") ?tags
?interval ~measurement ~field ~test () =
let title = Option.value title ~default:measurement in
Graph
{
title;
description;
queries = [(simple_query ?tags ~measurement ~field ~test (), None)];
interval;
yaxis_1 = Some {format = yaxis_format; label = Some field};
yaxis_2 = None;
}
let graphs_per_tags ?title ?(description = "") ?(yaxis_format = "s") ?interval
~measurement ~field ~test ~tags () =
let title = Option.value title ~default:measurement in
let queries =
List.map
(fun (key, value) ->
( simple_query ~tags:[(key, value)] ~measurement ~field ~test (),
Some (key ^ "=" ^ value) ))
tags
in
Graph
{
title;
description;
queries;
interval;
yaxis_1 = Some {format = yaxis_format; label = Some field};
yaxis_2 = None;
}