| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "json.h" |
| 4 | #include "../jsonwrap-internal.h" |
| 5 | #include "web/mcp/mcp.h" |
| 6 | #include "libnetdata/json/json-keys.h" |
| 7 | |
| 8 | #define JSON_DATES_JS 1 |
| 9 | #define JSON_DATES_TIMESTAMP 2 |
| 10 | |
| 11 | void rrdr2json(RRDR *r, BUFFER *wb, RRDR_OPTIONS options, int datatable) { |
| 12 | json_keys_init((options & RRDR_OPTION_LONG_JSON_KEYS) ? JSON_KEYS_OPTION_LONG_KEYS : 0); |
| 13 | |
| 14 | //netdata_log_info("RRD2JSON(): %s: BEGIN", r->st->id); |
| 15 | int row_annotations = 0, dates, dates_with_new = 0; |
| 16 | char kq[2] = "", // key quote |
| 17 | sq[2] = "", // string quote |
| 18 | pre_label[101] = "", // before each label |
| 19 | post_label[101] = "", // after each label |
| 20 | pre_date[101] = "", // the beginning of line, to the date |
| 21 | post_date[101] = "", // closing the date |
| 22 | pre_value[101] = "", // before each value |
| 23 | post_value[101] = "", // after each value |
| 24 | post_line[101] = "", // at the end of each row |
| 25 | normal_annotation[201] = "", // default row annotation |
| 26 | overflow_annotation[201] = "", // overflow row annotation |
| 27 | data_begin[101] = "", // between labels and values |
| 28 | finish[101] = "", // at the end of everything |
| 29 | object_rows_time[101] = ""; |
| 30 | |
| 31 | if(datatable) { |
| 32 | dates = JSON_DATES_JS; |
| 33 | if( options & RRDR_OPTION_GOOGLE_JSON ) { |
| 34 | kq[0] = '\0'; |
| 35 | sq[0] = '\''; |
| 36 | } |
| 37 | else { |
| 38 | kq[0] = '"'; |
| 39 | sq[0] = '"'; |
| 40 | } |
| 41 | row_annotations = 1; |
| 42 | snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq); |
| 43 | snprintfz(post_date, 100, "%s}", sq); |
| 44 | snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq); |
| 45 | snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq); |
| 46 | snprintfz(pre_value, 100, ",{%sv%s:", kq, kq); |
| 47 | strcpy(post_value, "}"); |
| 48 | strcpy(post_line, "]}"); |
| 49 | snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq); |
| 50 | strcpy(finish, "\n ]\n }"); |
| 51 | |
| 52 | 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); |
| 53 | snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq); |
| 54 | |
| 55 | buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq); |
| 56 | 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); |
| 57 | 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); |
| 58 | 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); |
| 59 | |
| 60 | // remove the valueobjects flag |
| 61 | // google wants its own keys |
| 62 | if(options & RRDR_OPTION_OBJECTSROWS) |
| 63 | options &= ~RRDR_OPTION_OBJECTSROWS; |
| 64 | } |
| 65 | else { |
| 66 | kq[0] = '"'; |
| 67 | sq[0] = '"'; |
| 68 | if(options & RRDR_OPTION_GOOGLE_JSON) { |
| 69 | dates = JSON_DATES_JS; |
| 70 | dates_with_new = 1; |
| 71 | } |
| 72 | else { |
| 73 | dates = JSON_DATES_TIMESTAMP; |
| 74 | dates_with_new = 0; |
| 75 | } |
| 76 | if( options & RRDR_OPTION_OBJECTSROWS ) |
| 77 | strcpy(pre_date, " {"); |
| 78 | else |
| 79 | strcpy(pre_date, " ["); |
| 80 | strcpy(pre_label, ",\""); |
| 81 | strcpy(post_label, "\""); |
| 82 | strcpy(pre_value, ","); |
| 83 | if( options & RRDR_OPTION_OBJECTSROWS ) |
| 84 | strcpy(post_line, "}"); |
| 85 | else |
| 86 | strcpy(post_line, "]"); |
| 87 | snprintfz(data_begin, 100, "],\n %sdata%s:[\n", kq, kq); |
| 88 | strcpy(finish, "\n ]\n }"); |
| 89 | |
| 90 | buffer_sprintf(wb, "{\n %slabels%s:[", kq, kq); |
| 91 | buffer_sprintf(wb, "%stime%s", sq, sq); |
| 92 | |
| 93 | if( options & RRDR_OPTION_OBJECTSROWS ) |
| 94 | snprintfz(object_rows_time, 100, "%stime%s: ", kq, kq); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | size_t pre_value_len = strlen(pre_value); |
| 99 | size_t post_value_len = strlen(post_value); |
| 100 | size_t pre_label_len = strlen(pre_label); |
| 101 | size_t post_label_len = strlen(post_label); |
| 102 | size_t pre_date_len = strlen(pre_date); |
| 103 | size_t post_date_len = strlen(post_date); |
| 104 | size_t post_line_len = strlen(post_line); |
| 105 | size_t normal_annotation_len = strlen(normal_annotation); |
| 106 | size_t overflow_annotation_len = strlen(overflow_annotation); |
| 107 | size_t object_rows_time_len = strlen(object_rows_time); |
| 108 | |
| 109 | // RFC3339 buffer - allocated once and reused |
| 110 | char rfc3339_buf[RFC3339_MAX_LENGTH]; |
| 111 | |
| 112 | // ------------------------------------------------------------------------- |
| 113 | // print the JSON header |
| 114 | |
| 115 | long c, i; |
| 116 | const long used = (long)r->d; |
| 117 | |
| 118 | // print the header lines |
| 119 | for(c = 0, i = 0; c < used ; c++) { |
| 120 | if(!rrdr_dimension_should_be_exposed(r->od[c], options)) |
| 121 | continue; |
| 122 | |
| 123 | buffer_fast_strcat(wb, pre_label, pre_label_len); |
| 124 | buffer_strcat(wb, string2str(r->dn[c])); |
| 125 | buffer_fast_strcat(wb, post_label, post_label_len); |
| 126 | i++; |
| 127 | } |
| 128 | |
| 129 | if(!i) { |
| 130 | buffer_fast_strcat(wb, pre_label, pre_label_len); |
| 131 | buffer_fast_strcat(wb, "no data", 7); |
| 132 | buffer_fast_strcat(wb, post_label, post_label_len); |
| 133 | } |
| 134 | size_t total_number_of_dimensions = i; |
| 135 | |
| 136 | // print the beginning of row data |
| 137 | buffer_strcat(wb, data_begin); |
| 138 | |
| 139 | // if all dimensions are hidden, print a null |
| 140 | if(!i) { |
| 141 | buffer_strcat(wb, finish); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | long start = 0, end = rrdr_rows(r), step = 1; |
| 146 | if(!(options & RRDR_OPTION_REVERSED)) { |
| 147 | start = rrdr_rows(r) - 1; |
| 148 | end = -1; |
| 149 | step = -1; |
| 150 | } |
| 151 | |
| 152 | // pre-allocate a large enough buffer for us |
| 153 | // this does not need to be accurate - it is just a hint to avoid multiple realloc(). |
| 154 | buffer_need_bytes(wb, |
| 155 | ( 20 * rrdr_rows(r)) // timestamp + json overhead |
| 156 | + ( (pre_value_len + post_value_len + 4) * total_number_of_dimensions * rrdr_rows(r) ) // number |
| 157 | ); |
| 158 | |
| 159 | // for each line in the array |
| 160 | for(i = start; i != end ;i += step) { |
| 161 | NETDATA_DOUBLE *cn = &r->v[ i * r->d ]; |
| 162 | RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ]; |
| 163 | NETDATA_DOUBLE *ar = &r->ar[ i * r->d ]; |
| 164 | |
| 165 | time_t now = r->t[i]; |
| 166 | |
| 167 | if(dates == JSON_DATES_JS) { |
| 168 | // generate the local date time |
| 169 | struct tm tmbuf, *tm = localtime_r(&now, &tmbuf); |
| 170 | if(!tm) { |
| 171 | netdata_log_error("localtime_r() failed."); continue; } |
| 172 | |
| 173 | if(likely(i != start)) buffer_fast_strcat(wb, ",\n", 2); |
| 174 | buffer_fast_strcat(wb, pre_date, pre_date_len); |
| 175 | |
| 176 | if( options & RRDR_OPTION_OBJECTSROWS ) |
| 177 | buffer_fast_strcat(wb, object_rows_time, object_rows_time_len); |
| 178 | |
| 179 | if(unlikely(dates_with_new)) |
| 180 | buffer_fast_strcat(wb, "new ", 4); |
| 181 | |
| 182 | buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 183 | |
| 184 | buffer_fast_strcat(wb, post_date, post_date_len); |
| 185 | |
| 186 | if(unlikely(row_annotations)) { |
| 187 | // google supports one annotation per row |
| 188 | int annotation_found = 0; |
| 189 | for(c = 0; c < used ; c++) { |
| 190 | if(unlikely(!(r->od[c] & RRDR_DIMENSION_QUERIED))) continue; |
| 191 | |
| 192 | if(unlikely(co[c] & RRDR_VALUE_RESET)) { |
| 193 | buffer_fast_strcat(wb, overflow_annotation, overflow_annotation_len); |
| 194 | annotation_found = 1; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | if(likely(!annotation_found)) |
| 199 | buffer_fast_strcat(wb, normal_annotation, normal_annotation_len); |
| 200 | } |
| 201 | } |
| 202 | else { |
| 203 | // print the timestamp of the line |
| 204 | if(likely(i != start)) |
| 205 | buffer_fast_strcat(wb, ",\n", 2); |
| 206 | |
| 207 | buffer_fast_strcat(wb, pre_date, pre_date_len); |
| 208 | |
| 209 | if(unlikely( options & RRDR_OPTION_OBJECTSROWS )) |
| 210 | buffer_fast_strcat(wb, object_rows_time, object_rows_time_len); |
| 211 | |
| 212 | if(unlikely(options & RRDR_OPTION_RFC3339)) { |
| 213 | // Output RFC3339 timestamp |
| 214 | usec_t timestamp_ut = (usec_t)r->t[i] * USEC_PER_SEC; |
| 215 | rfc3339_datetime_ut(rfc3339_buf, sizeof(rfc3339_buf), timestamp_ut, 0, true); |
| 216 | buffer_strcat(wb, "\""); |
| 217 | buffer_strcat(wb, rfc3339_buf); |
| 218 | buffer_strcat(wb, "\""); |
| 219 | } |
| 220 | else { |
| 221 | buffer_print_netdata_double(wb, (NETDATA_DOUBLE) r->t[i]); |
| 222 | |
| 223 | // in ms |
| 224 | if(unlikely(options & RRDR_OPTION_MILLISECONDS)) |
| 225 | buffer_fast_strcat(wb, "000", 3); |
| 226 | } |
| 227 | |
| 228 | buffer_fast_strcat(wb, post_date, post_date_len); |
| 229 | } |
| 230 | |
| 231 | // for each dimension |
| 232 | for(c = 0; c < used ;c++) { |
| 233 | if(!rrdr_dimension_should_be_exposed(r->od[c], options)) |
| 234 | continue; |
| 235 | |
| 236 | NETDATA_DOUBLE n; |
| 237 | if(unlikely(options & RRDR_OPTION_INTERNAL_AR)) |
| 238 | n = ar[c]; |
| 239 | else |
| 240 | n = cn[c]; |
| 241 | |
| 242 | buffer_fast_strcat(wb, pre_value, pre_value_len); |
| 243 | |
| 244 | if(unlikely( options & RRDR_OPTION_OBJECTSROWS )) |
| 245 | buffer_sprintf(wb, "%s%s%s: ", kq, string2str(r->dn[c]), kq); |
| 246 | |
| 247 | if(co[c] & RRDR_VALUE_EMPTY && !(options & (RRDR_OPTION_INTERNAL_AR))) { |
| 248 | if(unlikely(options & RRDR_OPTION_NULL2ZERO)) |
| 249 | buffer_fast_strcat(wb, "0", 1); |
| 250 | else |
| 251 | buffer_fast_strcat(wb, "null", 4); |
| 252 | } |
| 253 | else |
| 254 | buffer_print_netdata_double(wb, n); |
| 255 | |
| 256 | buffer_fast_strcat(wb, post_value, post_value_len); |
| 257 | } |
| 258 | |
| 259 | buffer_fast_strcat(wb, post_line, post_line_len); |
| 260 | } |
| 261 | |
| 262 | buffer_strcat(wb, finish); |
| 263 | //netdata_log_info("RRD2JSON(): %s: END", r->st->id); |
| 264 | } |
| 265 | |
| 266 | void rrdr2json_v2(RRDR *r, BUFFER *wb) { |
| 267 | QUERY_TARGET *qt = r->internal.qt; |
| 268 | RRDR_OPTIONS options = qt->window.options; |
| 269 | |
| 270 | bool send_count = query_target_aggregatable(qt); |
| 271 | bool send_hidden = send_count && r->vh && query_has_group_by_aggregation_percentage(qt); |
| 272 | |
| 273 | buffer_json_member_add_object(wb, "result"); |
| 274 | |
| 275 | if(options & RRDR_OPTION_MCP_INFO) |
| 276 | buffer_json_member_add_string(wb, "info", MCP_QUERY_INFO_RESULT_SECTION); |
| 277 | |
| 278 | buffer_json_member_add_array(wb, "labels"); |
| 279 | buffer_json_add_array_item_string(wb, "time"); |
| 280 | long d, i; |
| 281 | const long used = (long)r->d; |
| 282 | for(d = 0, i = 0; d < used ; d++) { |
| 283 | if(!rrdr_dimension_should_be_exposed(r->od[d], options)) |
| 284 | continue; |
| 285 | |
| 286 | buffer_json_add_array_item_string(wb, string2str(r->di[d])); |
| 287 | i++; |
| 288 | } |
| 289 | buffer_json_array_close(wb); // labels |
| 290 | |
| 291 | buffer_json_member_add_object(wb, JSKEY(point_schema)); |
| 292 | { |
| 293 | size_t point_count = 0; |
| 294 | buffer_json_member_add_uint64(wb, "value", point_count++); |
| 295 | buffer_json_member_add_uint64(wb, JSKEY(anomaly_rate), point_count++); |
| 296 | buffer_json_member_add_uint64(wb, JSKEY(point_annotations), point_count++); |
| 297 | if (send_count) |
| 298 | buffer_json_member_add_uint64(wb, "count", point_count++); |
| 299 | if (send_hidden) |
| 300 | buffer_json_member_add_uint64(wb, "hidden", point_count++); |
| 301 | } |
| 302 | buffer_json_object_close(wb); // point |
| 303 | |
| 304 | buffer_json_member_add_array(wb, "data"); |
| 305 | if(i) { |
| 306 | long start = 0, end = rrdr_rows(r), step = 1; |
| 307 | if (!(options & RRDR_OPTION_REVERSED)) { |
| 308 | start = rrdr_rows(r) - 1; |
| 309 | end = -1; |
| 310 | step = -1; |
| 311 | } |
| 312 | |
| 313 | // for each line in the array |
| 314 | for (i = start; i != end; i += step) { |
| 315 | NETDATA_DOUBLE *cn = &r->v[ i * r->d ]; |
| 316 | NETDATA_DOUBLE *ch = send_hidden ? &r->vh[i * r->d ] : NULL; |
| 317 | RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ]; |
| 318 | NETDATA_DOUBLE *ar = &r->ar[ i * r->d ]; |
| 319 | uint32_t *gbc = &r->gbc [ i * r->d ]; |
| 320 | time_t now = r->t[i]; |
| 321 | |
| 322 | buffer_json_add_array_item_array(wb); // row |
| 323 | |
| 324 | if (options & RRDR_OPTION_MILLISECONDS) |
| 325 | buffer_json_add_array_item_time_ms(wb, now); // the time |
| 326 | else |
| 327 | buffer_json_add_array_item_time_t_formatted(wb, now, options & RRDR_OPTION_RFC3339); // the time |
| 328 | |
| 329 | for (d = 0; d < used; d++) { |
| 330 | if (!rrdr_dimension_should_be_exposed(r->od[d], options)) |
| 331 | continue; |
| 332 | |
| 333 | RRDR_VALUE_FLAGS o = co[d]; |
| 334 | |
| 335 | buffer_json_add_array_item_array(wb); // point |
| 336 | |
| 337 | // add the value |
| 338 | NETDATA_DOUBLE n = cn[d]; |
| 339 | |
| 340 | if(o & RRDR_VALUE_EMPTY) { |
| 341 | if (unlikely(options & RRDR_OPTION_NULL2ZERO)) |
| 342 | buffer_json_add_array_item_double(wb, 0); |
| 343 | else |
| 344 | buffer_json_add_array_item_double(wb, NAN); |
| 345 | } |
| 346 | else |
| 347 | buffer_json_add_array_item_double(wb, n); |
| 348 | |
| 349 | // add the anomaly |
| 350 | buffer_json_add_array_item_double(wb, ar[d]); |
| 351 | |
| 352 | // add the point annotations |
| 353 | buffer_json_add_array_item_uint64(wb, o); |
| 354 | |
| 355 | // add the count |
| 356 | if(send_count) |
| 357 | buffer_json_add_array_item_uint64(wb, gbc[d]); |
| 358 | if(send_hidden) |
| 359 | buffer_json_add_array_item_double(wb, ch[d]); |
| 360 | |
| 361 | buffer_json_array_close(wb); // point |
| 362 | } |
| 363 | |
| 364 | buffer_json_array_close(wb); // row |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | buffer_json_array_close(wb); // data |
| 369 | |
| 370 | buffer_json_object_close(wb); // annotations |
| 371 | } |