json.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "json.h"
  3. #define JSON_DATES_JS 1
  4. #define JSON_DATES_TIMESTAMP 2
  5. void rrdr2json(RRDR *r, BUFFER *wb, RRDR_OPTIONS options, int datatable) {
  6. //info("RRD2JSON(): %s: BEGIN", r->st->id);
  7. int row_annotations = 0, dates, dates_with_new = 0;
  8. char kq[2] = "", // key quote
  9. sq[2] = "", // string quote
  10. pre_label[101] = "", // before each label
  11. post_label[101] = "", // after each label
  12. pre_date[101] = "", // the beginning of line, to the date
  13. post_date[101] = "", // closing the date
  14. pre_value[101] = "", // before each value
  15. post_value[101] = "", // after each value
  16. post_line[101] = "", // at the end of each row
  17. normal_annotation[201] = "", // default row annotation
  18. overflow_annotation[201] = "", // overflow row annotation
  19. data_begin[101] = "", // between labels and values
  20. finish[101] = "", // at the end of everything
  21. object_rows_time[101] = "";
  22. if(datatable) {
  23. dates = JSON_DATES_JS;
  24. if( options & RRDR_OPTION_GOOGLE_JSON ) {
  25. kq[0] = '\0';
  26. sq[0] = '\'';
  27. }
  28. else {
  29. kq[0] = '"';
  30. sq[0] = '"';
  31. }
  32. row_annotations = 1;
  33. snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
  34. snprintfz(post_date, 100, "%s}", sq);
  35. snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
  36. snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
  37. snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
  38. strcpy(post_value, "}");
  39. strcpy(post_line, "]}");
  40. snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
  41. strcpy(finish, "\n]\n}");
  42. 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);
  43. snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
  44. buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
  45. 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);
  46. 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);
  47. 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);
  48. // remove the valueobjects flag
  49. // google wants its own keys
  50. if(options & RRDR_OPTION_OBJECTSROWS)
  51. options &= ~RRDR_OPTION_OBJECTSROWS;
  52. }
  53. else {
  54. kq[0] = '"';
  55. sq[0] = '"';
  56. if(options & RRDR_OPTION_GOOGLE_JSON) {
  57. dates = JSON_DATES_JS;
  58. dates_with_new = 1;
  59. }
  60. else {
  61. dates = JSON_DATES_TIMESTAMP;
  62. dates_with_new = 0;
  63. }
  64. if( options & RRDR_OPTION_OBJECTSROWS )
  65. strcpy(pre_date, " { ");
  66. else
  67. strcpy(pre_date, " [ ");
  68. strcpy(pre_label, ",\"");
  69. strcpy(post_label, "\"");
  70. strcpy(pre_value, ",");
  71. if( options & RRDR_OPTION_OBJECTSROWS )
  72. strcpy(post_line, "}");
  73. else
  74. strcpy(post_line, "]");
  75. snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
  76. strcpy(finish, "\n]\n}");
  77. buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
  78. buffer_sprintf(wb, "%stime%s", sq, sq);
  79. if( options & RRDR_OPTION_OBJECTSROWS )
  80. snprintfz(object_rows_time, 100, "%stime%s: ", kq, kq);
  81. }
  82. size_t pre_value_len = strlen(pre_value);
  83. size_t post_value_len = strlen(post_value);
  84. size_t pre_label_len = strlen(pre_label);
  85. size_t post_label_len = strlen(post_label);
  86. size_t pre_date_len = strlen(pre_date);
  87. size_t post_date_len = strlen(post_date);
  88. size_t post_line_len = strlen(post_line);
  89. size_t normal_annotation_len = strlen(normal_annotation);
  90. size_t overflow_annotation_len = strlen(overflow_annotation);
  91. size_t object_rows_time_len = strlen(object_rows_time);
  92. // -------------------------------------------------------------------------
  93. // print the JSON header
  94. QUERY_TARGET *qt = r->internal.qt;
  95. long c, i;
  96. const long used = qt->query.used;
  97. // print the header lines
  98. for(c = 0, i = 0; c < used ; c++) {
  99. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  100. if(unlikely(!(r->od[c] & RRDR_DIMENSION_QUERIED))) continue;
  101. if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  102. buffer_fast_strcat(wb, pre_label, pre_label_len);
  103. buffer_strcat(wb, string2str(qt->query.array[c].dimension.name));
  104. buffer_fast_strcat(wb, post_label, post_label_len);
  105. i++;
  106. }
  107. if(!i) {
  108. buffer_fast_strcat(wb, pre_label, pre_label_len);
  109. buffer_fast_strcat(wb, "no data", 7);
  110. buffer_fast_strcat(wb, post_label, post_label_len);
  111. }
  112. size_t total_number_of_dimensions = i;
  113. // print the beginning of row data
  114. buffer_strcat(wb, data_begin);
  115. // if all dimensions are hidden, print a null
  116. if(!i) {
  117. buffer_strcat(wb, finish);
  118. return;
  119. }
  120. long start = 0, end = rrdr_rows(r), step = 1;
  121. if(!(options & RRDR_OPTION_REVERSED)) {
  122. start = rrdr_rows(r) - 1;
  123. end = -1;
  124. step = -1;
  125. }
  126. // pre-allocate a large enough buffer for us
  127. // this does not need to be accurate - it is just a hint to avoid multiple realloc().
  128. buffer_need_bytes(wb,
  129. ( 20 * rrdr_rows(r)) // timestamp + json overhead
  130. + ( (pre_value_len + post_value_len + 4) * total_number_of_dimensions * rrdr_rows(r) ) // number
  131. );
  132. // for each line in the array
  133. NETDATA_DOUBLE total = 1;
  134. for(i = start; i != end ;i += step) {
  135. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  136. RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
  137. NETDATA_DOUBLE *ar = &r->ar[ i * r->d ];
  138. time_t now = r->t[i];
  139. if(dates == JSON_DATES_JS) {
  140. // generate the local date time
  141. struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
  142. if(!tm) { error("localtime_r() failed."); continue; }
  143. if(likely(i != start)) buffer_fast_strcat(wb, ",\n", 2);
  144. buffer_fast_strcat(wb, pre_date, pre_date_len);
  145. if( options & RRDR_OPTION_OBJECTSROWS )
  146. buffer_fast_strcat(wb, object_rows_time, object_rows_time_len);
  147. if(unlikely(dates_with_new))
  148. buffer_fast_strcat(wb, "new ", 4);
  149. buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  150. buffer_fast_strcat(wb, post_date, post_date_len);
  151. if(unlikely(row_annotations)) {
  152. // google supports one annotation per row
  153. int annotation_found = 0;
  154. for(c = 0; c < used ; c++) {
  155. if(unlikely(!(r->od[c] & RRDR_DIMENSION_QUERIED))) continue;
  156. if(unlikely(co[c] & RRDR_VALUE_RESET)) {
  157. buffer_fast_strcat(wb, overflow_annotation, overflow_annotation_len);
  158. annotation_found = 1;
  159. break;
  160. }
  161. }
  162. if(likely(!annotation_found))
  163. buffer_fast_strcat(wb, normal_annotation, normal_annotation_len);
  164. }
  165. }
  166. else {
  167. // print the timestamp of the line
  168. if(likely(i != start))
  169. buffer_fast_strcat(wb, ",\n", 2);
  170. buffer_fast_strcat(wb, pre_date, pre_date_len);
  171. if(unlikely( options & RRDR_OPTION_OBJECTSROWS ))
  172. buffer_fast_strcat(wb, object_rows_time, object_rows_time_len);
  173. buffer_rrd_value(wb, (NETDATA_DOUBLE)r->t[i]);
  174. // in ms
  175. if(unlikely(options & RRDR_OPTION_MILLISECONDS))
  176. buffer_fast_strcat(wb, "000", 3);
  177. buffer_fast_strcat(wb, post_date, post_date_len);
  178. }
  179. int set_min_max = 0;
  180. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  181. total = 0;
  182. for(c = 0; c < used ;c++) {
  183. if(unlikely(!(r->od[c] & RRDR_DIMENSION_QUERIED))) continue;
  184. NETDATA_DOUBLE n;
  185. if(unlikely(options & RRDR_OPTION_INTERNAL_AR))
  186. n = ar[c];
  187. else
  188. n = cn[c];
  189. if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  190. n = -n;
  191. total += n;
  192. }
  193. // prevent a division by zero
  194. if(total == 0) total = 1;
  195. set_min_max = 1;
  196. }
  197. // for each dimension
  198. for(c = 0; c < used ;c++) {
  199. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  200. if(unlikely(!(r->od[c] & RRDR_DIMENSION_QUERIED))) continue;
  201. if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  202. NETDATA_DOUBLE n;
  203. if(unlikely(options & RRDR_OPTION_INTERNAL_AR))
  204. n = ar[c];
  205. else
  206. n = cn[c];
  207. buffer_fast_strcat(wb, pre_value, pre_value_len);
  208. if(unlikely( options & RRDR_OPTION_OBJECTSROWS ))
  209. buffer_sprintf(wb, "%s%s%s: ", kq, string2str(qt->query.array[c].dimension.name), kq);
  210. if(co[c] & RRDR_VALUE_EMPTY && !(options & RRDR_OPTION_INTERNAL_AR)) {
  211. if(unlikely(options & RRDR_OPTION_NULL2ZERO))
  212. buffer_fast_strcat(wb, "0", 1);
  213. else
  214. buffer_fast_strcat(wb, "null", 4);
  215. }
  216. else {
  217. if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  218. n = -n;
  219. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  220. n = n * 100 / total;
  221. if(unlikely(set_min_max)) {
  222. r->min = r->max = n;
  223. set_min_max = 0;
  224. }
  225. if(n < r->min) r->min = n;
  226. if(n > r->max) r->max = n;
  227. }
  228. buffer_rrd_value(wb, n);
  229. }
  230. buffer_fast_strcat(wb, post_value, post_value_len);
  231. }
  232. buffer_fast_strcat(wb, post_line, post_line_len);
  233. }
  234. buffer_strcat(wb, finish);
  235. //info("RRD2JSON(): %s: END", r->st->id);
  236. }