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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "json.h"
#define JSON_DATES_JS 1
#define JSON_DATES_TIMESTAMP 2
void rrdr2json(RRDR *r, BUFFER *wb, RRDR_OPTIONS options, int datatable, struct context_param *context_param_list)
{
RRDDIM *temp_rd = context_param_list ? context_param_list->rd : NULL;
int should_lock = (!context_param_list || !(context_param_list->flags & CONTEXT_FLAGS_ARCHIVE));
if (should_lock)
rrdset_check_rdlock(r->st);
//info("RRD2JSON(): %s: BEGIN", r->st->id);
int row_annotations = 0, dates, dates_with_new = 0;
char kq[2] = "", // key quote
sq[2] = "", // string quote
pre_label[101] = "", // before each label
post_label[101] = "", // after each label
pre_date[101] = "", // the beginning of line, to the date
post_date[101] = "", // closing the date
pre_value[101] = "", // before each value
post_value[101] = "", // after each value
post_line[101] = "", // at the end of each row
normal_annotation[201] = "", // default row annotation
overflow_annotation[201] = "", // overflow row annotation
data_begin[101] = "", // between labels and values
finish[101] = "", // at the end of everything
object_rows_time[101] = "";
if(datatable) {
dates = JSON_DATES_JS;
if( options & RRDR_OPTION_GOOGLE_JSON ) {
kq[0] = '\0';
sq[0] = '\'';
}
else {
kq[0] = '"';
sq[0] = '"';
}
row_annotations = 1;
snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
snprintfz(post_date, 100, "%s}", sq);
snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
strcpy(post_value, "}");
strcpy(post_line, "]}");
snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
strcpy(finish, "\n]\n}");
snprintfz(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
buffer_sprintf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotationText%s}}", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
// remove the valueobjects flag
// google wants its own keys
if(options & RRDR_OPTION_OBJECTSROWS)
options &= ~RRDR_OPTION_OBJECTSROWS;
}
else {
kq[0] = '"';
sq[0] = '"';
if(options & RRDR_OPTION_GOOGLE_JSON) {
dates = JSON_DATES_JS;
dates_with_new = 1;
}
else {
dates = JSON_DATES_TIMESTAMP;
dates_with_new = 0;
}
if( options & RRDR_OPTION_OBJECTSROWS )
strcpy(pre_date, " { ");
else
strcpy(pre_date, " [ ");
strcpy(pre_label, ",\"");
strcpy(post_label, "\"");
strcpy(pre_value, ",");
if( options & RRDR_OPTION_OBJECTSROWS )
strcpy(post_line, "}");
else
strcpy(post_line, "]");
snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
strcpy(finish, "\n]\n}");
buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
buffer_sprintf(wb, "%stime%s", sq, sq);
if( options & RRDR_OPTION_OBJECTSROWS )
snprintfz(object_rows_time, 100, "%stime%s: ", kq, kq);
}
size_t pre_value_len = strlen(pre_value);
size_t post_value_len = strlen(post_value);
size_t pre_label_len = strlen(pre_label);
size_t post_label_len = strlen(post_label);
size_t pre_date_len = strlen(pre_date);
size_t post_date_len = strlen(post_date);
size_t post_line_len = strlen(post_line);
size_t normal_annotation_len = strlen(normal_annotation);
size_t overflow_annotation_len = strlen(overflow_annotation);
size_t object_rows_time_len = strlen(object_rows_time);
// -------------------------------------------------------------------------
// print the JSON header
long c, i;
RRDDIM *rd;
// print the header lines
for(c = 0, i = 0, rd = temp_rd?temp_rd:r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
buffer_fast_strcat(wb, pre_label, pre_label_len);
buffer_strcat(wb, rd->name);
// buffer_strcat(wb, ".");
// buffer_strcat(wb, rd->rrdset->name);
buffer_fast_strcat(wb, post_label, post_label_len);
i++;
}
if(!i) {
buffer_fast_strcat(wb, pre_label, pre_label_len);
buffer_fast_strcat(wb, "no data", 7);
buffer_fast_strcat(wb, post_label, post_label_len);
}
size_t total_number_of_dimensions = i;
// print the begin of row data
buffer_strcat(wb, data_begin);
// if all dimensions are hidden, print a null
if(!i) {
buffer_strcat(wb, finish);
return;
}
long start = 0, end = rrdr_rows(r), step = 1;
if(!(options & RRDR_OPTION_REVERSED)) {
start = rrdr_rows(r) - 1;
end = -1;
step = -1;
}
// pre-allocate a large enough buffer for us
// this does not need to be accurate - it is just a hint to avoid multiple realloc().
buffer_need_bytes(wb,
( 20 * rrdr_rows(r)) // timestamp + json overhead
+ ( (pre_value_len + post_value_len + 4) * total_number_of_dimensions *
|