json.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if(datatable) {
  27. dates = JSON_DATES_JS;
  28. if( options & RRDR_OPTION_GOOGLE_JSON ) {
  29. kq[0] = '\0';
  30. sq[0] = '\'';
  31. }
  32. else {
  33. kq[0] = '"';
  34. sq[0] = '"';
  35. }
  36. row_annotations = 1;
  37. snprintfz(pre_date, 100, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
  38. snprintfz(post_date, 100, "%s}", sq);
  39. snprintfz(pre_label, 100, ",\n {%sid%s:%s%s,%slabel%s:%s", kq, kq, sq, sq, kq, kq, sq);
  40. snprintfz(post_label, 100, "%s,%spattern%s:%s%s,%stype%s:%snumber%s}", sq, kq, kq, sq, sq, kq, kq, sq, sq);
  41. snprintfz(pre_value, 100, ",{%sv%s:", kq, kq);
  42. strcpy(post_value, "}");
  43. strcpy(post_line, "]}");
  44. snprintfz(data_begin, 100, "\n ],\n %srows%s:\n [\n", kq, kq);
  45. strcpy(finish, "\n ]\n}");
  46. 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);
  47. snprintfz(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
  48. buffer_sprintf(wb, "{\n %scols%s:\n [\n", kq, kq);
  49. 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);
  50. 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);
  51. 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);
  52. // remove the valueobjects flag
  53. // google wants its own keys
  54. if(options & RRDR_OPTION_OBJECTSROWS)
  55. options &= ~RRDR_OPTION_OBJECTSROWS;
  56. }
  57. else {
  58. kq[0] = '"';
  59. sq[0] = '"';
  60. if(options & RRDR_OPTION_GOOGLE_JSON) {
  61. dates = JSON_DATES_JS;
  62. dates_with_new = 1;
  63. }
  64. else {
  65. dates = JSON_DATES_TIMESTAMP;
  66. dates_with_new = 0;
  67. }
  68. if( options & RRDR_OPTION_OBJECTSROWS )
  69. strcpy(pre_date, " { ");
  70. else
  71. strcpy(pre_date, " [ ");
  72. strcpy(pre_label, ", \"");
  73. strcpy(post_label, "\"");
  74. strcpy(pre_value, ", ");
  75. if( options & RRDR_OPTION_OBJECTSROWS )
  76. strcpy(post_line, "}");
  77. else
  78. strcpy(post_line, "]");
  79. snprintfz(data_begin, 100, "],\n %sdata%s:\n [\n", kq, kq);
  80. strcpy(finish, "\n ]\n}");
  81. buffer_sprintf(wb, "{\n %slabels%s: [", kq, kq);
  82. buffer_sprintf(wb, "%stime%s", sq, sq);
  83. }
  84. // -------------------------------------------------------------------------
  85. // print the JSON header
  86. long c, i;
  87. RRDDIM *rd;
  88. // print the header lines
  89. for(c = 0, i = 0, rd = temp_rd?temp_rd:r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
  90. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  91. if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  92. buffer_strcat(wb, pre_label);
  93. buffer_strcat(wb, rd->name);
  94. // buffer_strcat(wb, ".");
  95. // buffer_strcat(wb, rd->rrdset->name);
  96. buffer_strcat(wb, post_label);
  97. i++;
  98. }
  99. if(!i) {
  100. buffer_strcat(wb, pre_label);
  101. buffer_strcat(wb, "no data");
  102. buffer_strcat(wb, post_label);
  103. }
  104. // print the begin of row data
  105. buffer_strcat(wb, data_begin);
  106. // if all dimensions are hidden, print a null
  107. if(!i) {
  108. buffer_strcat(wb, finish);
  109. return;
  110. }
  111. long start = 0, end = rrdr_rows(r), step = 1;
  112. if(!(options & RRDR_OPTION_REVERSED)) {
  113. start = rrdr_rows(r) - 1;
  114. end = -1;
  115. step = -1;
  116. }
  117. // for each line in the array
  118. calculated_number total = 1;
  119. for(i = start; i != end ;i += step) {
  120. calculated_number *cn = &r->v[ i * r->d ];
  121. RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
  122. time_t now = r->t[i];
  123. if(dates == JSON_DATES_JS) {
  124. // generate the local date time
  125. struct tm tmbuf, *tm = localtime_r(&now, &tmbuf);
  126. if(!tm) { error("localtime_r() failed."); continue; }
  127. if(likely(i != start)) buffer_strcat(wb, ",\n");
  128. buffer_strcat(wb, pre_date);
  129. if( options & RRDR_OPTION_OBJECTSROWS )
  130. buffer_sprintf(wb, "%stime%s: ", kq, kq);
  131. if(dates_with_new)
  132. buffer_strcat(wb, "new ");
  133. buffer_jsdate(wb, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  134. buffer_strcat(wb, post_date);
  135. if(row_annotations) {
  136. // google supports one annotation per row
  137. int annotation_found = 0;
  138. for(c = 0, rd = temp_rd?temp_rd:r->st->dimensions; rd ;c++, rd = rd->next) {
  139. if(unlikely(!(r->od[c] & RRDR_DIMENSION_SELECTED))) continue;
  140. if(co[c] & RRDR_VALUE_RESET) {
  141. buffer_strcat(wb, overflow_annotation);
  142. annotation_found = 1;
  143. break;
  144. }
  145. }
  146. if(!annotation_found)
  147. buffer_strcat(wb, normal_annotation);
  148. }
  149. }
  150. else {
  151. // print the timestamp of the line
  152. if(likely(i != start)) buffer_strcat(wb, ",\n");
  153. buffer_strcat(wb, pre_date);
  154. if( options & RRDR_OPTION_OBJECTSROWS )
  155. buffer_sprintf(wb, "%stime%s: ", kq, kq);
  156. buffer_rrd_value(wb, (calculated_number)r->t[i]);
  157. // in ms
  158. if(options & RRDR_OPTION_MILLISECONDS) buffer_strcat(wb, "000");
  159. buffer_strcat(wb, post_date);
  160. }
  161. int set_min_max = 0;
  162. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  163. total = 0;
  164. for(c = 0, rd = temp_rd?temp_rd:r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
  165. calculated_number n = cn[c];
  166. if(likely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  167. n = -n;
  168. total += n;
  169. }
  170. // prevent a division by zero
  171. if(total == 0) total = 1;
  172. set_min_max = 1;
  173. }
  174. // for each dimension
  175. for(c = 0, rd = temp_rd?temp_rd:r->st->dimensions; rd && c < r->d ;c++, rd = rd->next) {
  176. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  177. if(unlikely((options & RRDR_OPTION_NONZERO) && !(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  178. calculated_number n = cn[c];
  179. buffer_strcat(wb, pre_value);
  180. if( options & RRDR_OPTION_OBJECTSROWS )
  181. buffer_sprintf(wb, "%s%s%s: ", kq, rd->name, kq);
  182. if(co[c] & RRDR_VALUE_EMPTY) {
  183. if(options & RRDR_OPTION_NULL2ZERO)
  184. buffer_strcat(wb, "0");
  185. else
  186. buffer_strcat(wb, "null");
  187. }
  188. else {
  189. if(unlikely((options & RRDR_OPTION_ABSOLUTE) && n < 0))
  190. n = -n;
  191. if(unlikely(options & RRDR_OPTION_PERCENTAGE)) {
  192. n = n * 100 / total;
  193. if(unlikely(set_min_max)) {
  194. r->min = r->max = n;
  195. set_min_max = 0;
  196. }
  197. if(n < r->min) r->min = n;
  198. if(n > r->max) r->max = n;
  199. }
  200. buffer_rrd_value(wb, n);
  201. }
  202. buffer_strcat(wb, post_value);
  203. }
  204. buffer_strcat(wb, post_line);
  205. }
  206. buffer_strcat(wb, finish);
  207. //info("RRD2JSON(): %s: END", r->st->id);
  208. }