json.c 11 KB

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