buffer.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_WEB_BUFFER_H
  3. #define NETDATA_WEB_BUFFER_H 1
  4. #include "../string/utf8.h"
  5. #include "../libnetdata.h"
  6. #ifdef ENABLE_H2O
  7. #include "h2o/memory.h"
  8. #endif
  9. #define WEB_DATA_LENGTH_INCREASE_STEP 1024
  10. #define BUFFER_JSON_MAX_DEPTH 32 // max is 255
  11. extern const char hex_digits[16];
  12. extern const char base64_digits[64];
  13. extern unsigned char hex_value_from_ascii[256];
  14. extern unsigned char base64_value_from_ascii[256];
  15. typedef enum __attribute__ ((__packed__)) {
  16. BUFFER_JSON_EMPTY = 0,
  17. BUFFER_JSON_OBJECT,
  18. BUFFER_JSON_ARRAY,
  19. } BUFFER_JSON_NODE_TYPE;
  20. typedef struct web_buffer_json_node {
  21. BUFFER_JSON_NODE_TYPE type;
  22. uint32_t count:24;
  23. } BUFFER_JSON_NODE;
  24. #define BUFFER_QUOTE_MAX_SIZE 7
  25. typedef enum __attribute__ ((__packed__)) {
  26. WB_CONTENT_CACHEABLE = (1 << 0),
  27. WB_CONTENT_NO_CACHEABLE = (1 << 1),
  28. } BUFFER_OPTIONS;
  29. typedef enum __attribute__ ((__packed__)) {
  30. CT_NONE = 0,
  31. CT_APPLICATION_JSON,
  32. CT_TEXT_PLAIN,
  33. CT_TEXT_HTML,
  34. CT_APPLICATION_X_JAVASCRIPT,
  35. CT_TEXT_CSS,
  36. CT_TEXT_XML,
  37. CT_APPLICATION_XML,
  38. CT_TEXT_XSL,
  39. CT_APPLICATION_OCTET_STREAM,
  40. CT_APPLICATION_X_FONT_TRUETYPE,
  41. CT_APPLICATION_X_FONT_OPENTYPE,
  42. CT_APPLICATION_FONT_WOFF,
  43. CT_APPLICATION_FONT_WOFF2,
  44. CT_APPLICATION_VND_MS_FONTOBJ,
  45. CT_IMAGE_SVG_XML,
  46. CT_IMAGE_PNG,
  47. CT_IMAGE_JPG,
  48. CT_IMAGE_GIF,
  49. CT_IMAGE_XICON,
  50. CT_IMAGE_ICNS,
  51. CT_IMAGE_BMP,
  52. CT_PROMETHEUS,
  53. CT_AUDIO_MPEG,
  54. CT_AUDIO_OGG,
  55. CT_VIDEO_MP4,
  56. CT_APPLICATION_PDF,
  57. CT_APPLICATION_ZIP,
  58. } HTTP_CONTENT_TYPE;
  59. typedef enum __attribute__ ((__packed__)) {
  60. BUFFER_JSON_OPTIONS_DEFAULT = 0,
  61. BUFFER_JSON_OPTIONS_MINIFY = (1 << 0),
  62. BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS = (1 << 1),
  63. } BUFFER_JSON_OPTIONS;
  64. typedef struct web_buffer {
  65. size_t size; // allocation size of buffer, in bytes
  66. size_t len; // current data length in buffer, in bytes
  67. char *buffer; // the buffer itself
  68. HTTP_CONTENT_TYPE content_type; // the content type of the data in the buffer
  69. BUFFER_OPTIONS options; // options related to the content
  70. time_t date; // the timestamp this content has been generated
  71. time_t expires; // the timestamp this content expires
  72. size_t *statistics;
  73. struct {
  74. char key_quote[BUFFER_QUOTE_MAX_SIZE + 1];
  75. char value_quote[BUFFER_QUOTE_MAX_SIZE + 1];
  76. int8_t depth;
  77. BUFFER_JSON_OPTIONS options;
  78. BUFFER_JSON_NODE stack[BUFFER_JSON_MAX_DEPTH];
  79. } json;
  80. } BUFFER;
  81. #define buffer_cacheable(wb) do { (wb)->options |= WB_CONTENT_CACHEABLE; if((wb)->options & WB_CONTENT_NO_CACHEABLE) (wb)->options &= ~WB_CONTENT_NO_CACHEABLE; } while(0)
  82. #define buffer_no_cacheable(wb) do { (wb)->options |= WB_CONTENT_NO_CACHEABLE; if((wb)->options & WB_CONTENT_CACHEABLE) (wb)->options &= ~WB_CONTENT_CACHEABLE; (wb)->expires = 0; } while(0)
  83. #define buffer_strlen(wb) ((wb)->len)
  84. #define BUFFER_OVERFLOW_EOF "EOF"
  85. #ifdef NETDATA_INTERNAL_CHECKS
  86. #define buffer_overflow_check(b) _buffer_overflow_check(b)
  87. #else
  88. #define buffer_overflow_check(b)
  89. #endif
  90. static inline void _buffer_overflow_check(BUFFER *b) {
  91. assert(b->len <= b->size &&
  92. "BUFFER: length is above buffer size.");
  93. assert(!(b->buffer && (b->buffer[b->size] != '\0' || strcmp(&b->buffer[b->size + 1], BUFFER_OVERFLOW_EOF) != 0)) &&
  94. "BUFFER: detected overflow.");
  95. }
  96. static inline void buffer_flush(BUFFER *wb) {
  97. wb->len = 0;
  98. wb->json.depth = 0;
  99. wb->json.stack[0].type = BUFFER_JSON_EMPTY;
  100. wb->json.stack[0].count = 0;
  101. if(wb->buffer)
  102. wb->buffer[0] = '\0';
  103. }
  104. void buffer_reset(BUFFER *wb);
  105. void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  106. void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  107. BUFFER *buffer_create(size_t size, size_t *statistics);
  108. void buffer_free(BUFFER *b);
  109. void buffer_increase(BUFFER *b, size_t free_size_required);
  110. void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...) PRINTFLIKE(3, 4);
  111. void buffer_vsprintf(BUFFER *wb, const char *fmt, va_list args);
  112. void buffer_sprintf(BUFFER *wb, const char *fmt, ...) PRINTFLIKE(2,3);
  113. void buffer_strcat_htmlescape(BUFFER *wb, const char *txt);
  114. void buffer_char_replace(BUFFER *wb, char from, char to);
  115. void buffer_print_sn_flags(BUFFER *wb, SN_FLAGS flags, bool send_anomaly_bit);
  116. #ifdef ENABLE_H2O
  117. h2o_iovec_t buffer_to_h2o_iovec(BUFFER *wb);
  118. #endif
  119. static inline void buffer_need_bytes(BUFFER *buffer, size_t needed_free_size) {
  120. if(unlikely(buffer->len + needed_free_size >= buffer->size))
  121. buffer_increase(buffer, needed_free_size + 1);
  122. }
  123. void buffer_json_initialize(BUFFER *wb, const char *key_quote, const char *value_quote, int depth,
  124. bool add_anonymous_object, BUFFER_JSON_OPTIONS options);
  125. void buffer_json_finalize(BUFFER *wb);
  126. static const char *buffer_tostring(BUFFER *wb)
  127. {
  128. buffer_need_bytes(wb, 1);
  129. wb->buffer[wb->len] = '\0';
  130. buffer_overflow_check(wb);
  131. return(wb->buffer);
  132. }
  133. static inline void _buffer_json_depth_push(BUFFER *wb, BUFFER_JSON_NODE_TYPE type) {
  134. #ifdef NETDATA_INTERNAL_CHECKS
  135. assert(wb->json.depth <= BUFFER_JSON_MAX_DEPTH && "BUFFER JSON: max nesting reached");
  136. #endif
  137. wb->json.depth++;
  138. wb->json.stack[wb->json.depth].count = 0;
  139. wb->json.stack[wb->json.depth].type = type;
  140. }
  141. static inline void _buffer_json_depth_pop(BUFFER *wb) {
  142. wb->json.depth--;
  143. }
  144. static inline void buffer_fast_charcat(BUFFER *wb, const char c) {
  145. buffer_need_bytes(wb, 2);
  146. *(&wb->buffer[wb->len]) = c;
  147. wb->len += 1;
  148. wb->buffer[wb->len] = '\0';
  149. buffer_overflow_check(wb);
  150. }
  151. static inline void buffer_fast_rawcat(BUFFER *wb, const char *txt, size_t len) {
  152. if(unlikely(!txt || !*txt || !len)) return;
  153. buffer_need_bytes(wb, len + 1);
  154. const char *t = txt;
  155. const char *e = &txt[len];
  156. char *d = &wb->buffer[wb->len];
  157. while(t != e)
  158. *d++ = *t++;
  159. wb->len += len;
  160. wb->buffer[wb->len] = '\0';
  161. buffer_overflow_check(wb);
  162. }
  163. static inline void buffer_fast_strcat(BUFFER *wb, const char *txt, size_t len) {
  164. if(unlikely(!txt || !*txt || !len)) return;
  165. buffer_need_bytes(wb, len + 1);
  166. const char *t = txt;
  167. const char *e = &txt[len];
  168. char *d = &wb->buffer[wb->len];
  169. while(t != e
  170. #ifdef NETDATA_INTERNAL_CHECKS
  171. && *t
  172. #endif
  173. )
  174. *d++ = *t++;
  175. #ifdef NETDATA_INTERNAL_CHECKS
  176. assert(!(t != e && !*t) && "BUFFER: source string is shorter than the length given.");
  177. #endif
  178. wb->len += len;
  179. wb->buffer[wb->len] = '\0';
  180. buffer_overflow_check(wb);
  181. }
  182. static inline void buffer_strcat(BUFFER *wb, const char *txt) {
  183. if(unlikely(!txt || !*txt)) return;
  184. const char *t = txt;
  185. while(*t) {
  186. buffer_need_bytes(wb, 100);
  187. char *s = &wb->buffer[wb->len];
  188. char *d = s;
  189. const char *e = &wb->buffer[wb->size];
  190. while(*t && d < e)
  191. *d++ = *t++;
  192. wb->len += d - s;
  193. }
  194. buffer_need_bytes(wb, 1);
  195. wb->buffer[wb->len] = '\0';
  196. buffer_overflow_check(wb);
  197. }
  198. static inline void buffer_contents_replace(BUFFER *wb, const char *txt, size_t len) {
  199. wb->len = 0;
  200. buffer_need_bytes(wb, len + 1);
  201. memcpy(wb->buffer, txt, len);
  202. wb->len = len;
  203. wb->buffer[wb->len] = '\0';
  204. buffer_overflow_check(wb);
  205. }
  206. static inline void buffer_strncat(BUFFER *wb, const char *txt, size_t len) {
  207. if(unlikely(!txt || !*txt)) return;
  208. buffer_need_bytes(wb, len + 1);
  209. memcpy(&wb->buffer[wb->len], txt, len);
  210. wb->len += len;
  211. wb->buffer[wb->len] = '\0';
  212. buffer_overflow_check(wb);
  213. }
  214. static inline void buffer_json_strcat(BUFFER *wb, const char *txt) {
  215. if(unlikely(!txt || !*txt)) return;
  216. const unsigned char *t = (const unsigned char *)txt;
  217. while(*t) {
  218. buffer_need_bytes(wb, 110);
  219. unsigned char *s = (unsigned char *)&wb->buffer[wb->len];
  220. unsigned char *d = s;
  221. const unsigned char *e = (unsigned char *)&wb->buffer[wb->size - 10]; // make room for the max escape sequence
  222. while(*t && d < e) {
  223. #ifdef BUFFER_JSON_ESCAPE_UTF
  224. if(unlikely(IS_UTF8_STARTBYTE(*t) && IS_UTF8_BYTE(t[1]))) {
  225. // UTF-8 multi-byte encoded character
  226. // find how big this character is (2-4 bytes)
  227. size_t utf_character_size = 2;
  228. while(utf_character_size < 4 && t[utf_character_size] && IS_UTF8_BYTE(t[utf_character_size]) && !IS_UTF8_STARTBYTE(t[utf_character_size]))
  229. utf_character_size++;
  230. uint32_t code_point = 0;
  231. for (size_t i = 0; i < utf_character_size; i++) {
  232. code_point <<= 6;
  233. code_point |= (t[i] & 0x3F);
  234. }
  235. t += utf_character_size;
  236. // encode as \u escape sequence
  237. *d++ = '\\';
  238. *d++ = 'u';
  239. *d++ = hex_digits[(code_point >> 12) & 0xf];
  240. *d++ = hex_digits[(code_point >> 8) & 0xf];
  241. *d++ = hex_digits[(code_point >> 4) & 0xf];
  242. *d++ = hex_digits[code_point & 0xf];
  243. }
  244. else
  245. #endif
  246. if(unlikely(*t < ' ')) {
  247. uint32_t v = *t++;
  248. *d++ = '\\';
  249. *d++ = 'u';
  250. *d++ = hex_digits[(v >> 12) & 0xf];
  251. *d++ = hex_digits[(v >> 8) & 0xf];
  252. *d++ = hex_digits[(v >> 4) & 0xf];
  253. *d++ = hex_digits[v & 0xf];
  254. }
  255. else {
  256. if (unlikely(*t == '\\' || *t == '\"'))
  257. *d++ = '\\';
  258. *d++ = *t++;
  259. }
  260. }
  261. wb->len += d - s;
  262. }
  263. buffer_need_bytes(wb, 1);
  264. wb->buffer[wb->len] = '\0';
  265. buffer_overflow_check(wb);
  266. }
  267. static inline void buffer_json_quoted_strcat(BUFFER *wb, const char *txt) {
  268. if(unlikely(!txt || !*txt)) return;
  269. if(*txt == '"')
  270. txt++;
  271. const char *t = txt;
  272. while(*t) {
  273. buffer_need_bytes(wb, 100);
  274. char *s = &wb->buffer[wb->len];
  275. char *d = s;
  276. const char *e = &wb->buffer[wb->size - 1]; // remove 1 to make room for the escape character
  277. while(*t && d < e) {
  278. if(unlikely(*t == '"' && !t[1])) {
  279. t++;
  280. continue;
  281. }
  282. if(unlikely(*t == '\\' || *t == '"'))
  283. *d++ = '\\';
  284. *d++ = *t++;
  285. }
  286. wb->len += d - s;
  287. }
  288. buffer_need_bytes(wb, 1);
  289. wb->buffer[wb->len] = '\0';
  290. buffer_overflow_check(wb);
  291. }
  292. // This trick seems to give an 80% speed increase in 32bit systems
  293. // print_number_llu_r() will just print the digits up to the
  294. // point the remaining value fits in 32 bits, and then calls
  295. // print_number_lu_r() to print the rest with 32 bit arithmetic.
  296. static inline char *print_uint32_reversed(char *dst, uint32_t value) {
  297. char *d = dst;
  298. do *d++ = (char)('0' + (value % 10)); while((value /= 10));
  299. return d;
  300. }
  301. static inline char *print_uint64_reversed(char *dst, uint64_t value) {
  302. #ifdef ENV32BIT
  303. if(value <= (uint64_t)0xffffffff)
  304. return print_uint32_reversed(dst, value);
  305. char *d = dst;
  306. do *d++ = (char)('0' + (value % 10)); while((value /= 10) && value > (uint64_t)0xffffffff);
  307. if(value) return print_uint32_reversed(d, value);
  308. return d;
  309. #else
  310. char *d = dst;
  311. do *d++ = (char)('0' + (value % 10)); while((value /= 10));
  312. return d;
  313. #endif
  314. }
  315. static inline char *print_uint32_hex_reversed(char *dst, uint32_t value) {
  316. static const char *digits = "0123456789ABCDEF";
  317. char *d = dst;
  318. do *d++ = digits[value & 0xf]; while((value >>= 4));
  319. return d;
  320. }
  321. static inline char *print_uint64_hex_reversed(char *dst, uint64_t value) {
  322. #ifdef ENV32BIT
  323. if(value <= (uint64_t)0xffffffff)
  324. return print_uint32_hex_reversed(dst, value);
  325. char *d = dst;
  326. do *d++ = hex_digits[value & 0xf]; while((value >>= 4) && value > (uint64_t)0xffffffff);
  327. if(value) return print_uint32_hex_reversed(d, value);
  328. return d;
  329. #else
  330. char *d = dst;
  331. do *d++ = hex_digits[value & 0xf]; while((value >>= 4));
  332. return d;
  333. #endif
  334. }
  335. static inline char *print_uint64_base64_reversed(char *dst, uint64_t value) {
  336. char *d = dst;
  337. do *d++ = base64_digits[value & 63]; while ((value >>= 6));
  338. return d;
  339. }
  340. static inline void char_array_reverse(char *from, char *to) {
  341. // from and to are inclusive
  342. char *begin = from, *end = to, aux;
  343. while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
  344. }
  345. static inline int print_netdata_double(char *dst, NETDATA_DOUBLE value) {
  346. char *s = dst;
  347. if(unlikely(value < 0)) {
  348. *s++ = '-';
  349. value = fabsndd(value);
  350. }
  351. uint64_t fractional_precision = 10000000ULL; // fractional part 7 digits
  352. int fractional_wanted_digits = 7;
  353. int exponent = 0;
  354. if(unlikely(value >= (NETDATA_DOUBLE)(UINT64_MAX / 10))) {
  355. // the number is too big to print using 64bit numbers
  356. // so, let's convert it to exponential notation
  357. exponent = (int)(floorndd(log10ndd(value)));
  358. value /= powndd(10, exponent);
  359. // the max precision we can support is 18 digits
  360. // (UINT64_MAX is 20, but the first is 1)
  361. fractional_precision = 1000000000000000000ULL; // fractional part 18 digits
  362. fractional_wanted_digits = 18;
  363. }
  364. char *d = s;
  365. NETDATA_DOUBLE integral_d, fractional_d;
  366. fractional_d = modfndd(value, &integral_d);
  367. // get the integral and the fractional parts as 64-bit integers
  368. uint64_t integral = (uint64_t)integral_d;
  369. uint64_t fractional = (uint64_t)llrintndd(fractional_d * (NETDATA_DOUBLE)fractional_precision);
  370. if(unlikely(fractional >= fractional_precision)) {
  371. integral++;
  372. fractional -= fractional_precision;
  373. }
  374. // convert the integral part to string (reversed)
  375. d = print_uint64_reversed(d, integral);
  376. char_array_reverse(s, d - 1); // copy reversed the integral string
  377. if(likely(fractional != 0)) {
  378. *d++ = '.'; // add the dot
  379. // convert the fractional part to string (reversed)
  380. d = print_uint64_reversed(s = d, fractional);
  381. while(d - s < fractional_wanted_digits) *d++ = '0'; // prepend zeros to reach precision
  382. char_array_reverse(s, d - 1); // copy reversed the fractional string
  383. // remove trailing zeros from the fractional part
  384. while(*(d - 1) == '0') d--;
  385. }
  386. if(unlikely(exponent != 0)) {
  387. *d++ = 'e';
  388. *d++ = '+';
  389. d = print_uint32_reversed(s = d, exponent);
  390. char_array_reverse(s, d - 1);
  391. }
  392. *d = '\0';
  393. return (int)(d - dst);
  394. }
  395. static inline void buffer_print_uint64(BUFFER *wb, uint64_t value) {
  396. buffer_need_bytes(wb, 50);
  397. char *s = &wb->buffer[wb->len];
  398. char *d = print_uint64_reversed(s, value);
  399. char_array_reverse(s, d - 1);
  400. *d = '\0';
  401. wb->len += d - s;
  402. buffer_overflow_check(wb);
  403. }
  404. static inline void buffer_print_int64(BUFFER *wb, int64_t value) {
  405. buffer_need_bytes(wb, 50);
  406. if(value < 0) {
  407. buffer_fast_strcat(wb, "-", 1);
  408. value = -value;
  409. }
  410. buffer_print_uint64(wb, (uint64_t)value);
  411. buffer_overflow_check(wb);
  412. }
  413. static inline void buffer_print_uint64_hex(BUFFER *wb, uint64_t value) {
  414. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1);
  415. buffer_fast_strcat(wb, HEX_PREFIX, sizeof(HEX_PREFIX) - 1);
  416. char *s = &wb->buffer[wb->len];
  417. char *d = print_uint64_hex_reversed(s, value);
  418. char_array_reverse(s, d - 1);
  419. *d = '\0';
  420. wb->len += d - s;
  421. buffer_overflow_check(wb);
  422. }
  423. static inline void buffer_print_uint64_base64(BUFFER *wb, uint64_t value) {
  424. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1);
  425. buffer_fast_strcat(wb, IEEE754_UINT64_B64_PREFIX, sizeof(IEEE754_UINT64_B64_PREFIX) - 1);
  426. char *s = &wb->buffer[wb->len];
  427. char *d = print_uint64_base64_reversed(s, value);
  428. char_array_reverse(s, d - 1);
  429. *d = '\0';
  430. wb->len += d - s;
  431. buffer_overflow_check(wb);
  432. }
  433. static inline void buffer_print_int64_hex(BUFFER *wb, int64_t value) {
  434. buffer_need_bytes(wb, 2);
  435. if(value < 0) {
  436. buffer_fast_strcat(wb, "-", 1);
  437. value = -value;
  438. }
  439. buffer_print_uint64_hex(wb, (uint64_t)value);
  440. buffer_overflow_check(wb);
  441. }
  442. static inline void buffer_print_int64_base64(BUFFER *wb, int64_t value) {
  443. buffer_need_bytes(wb, 2);
  444. if(value < 0) {
  445. buffer_fast_strcat(wb, "-", 1);
  446. value = -value;
  447. }
  448. buffer_print_uint64_base64(wb, (uint64_t)value);
  449. buffer_overflow_check(wb);
  450. }
  451. static inline void buffer_print_netdata_double(BUFFER *wb, NETDATA_DOUBLE value) {
  452. buffer_need_bytes(wb, 512 + 2);
  453. if(isnan(value) || isinf(value)) {
  454. buffer_fast_strcat(wb, "null", 4);
  455. return;
  456. }
  457. else
  458. wb->len += print_netdata_double(&wb->buffer[wb->len], value);
  459. // terminate it
  460. buffer_need_bytes(wb, 1);
  461. wb->buffer[wb->len] = '\0';
  462. buffer_overflow_check(wb);
  463. }
  464. static inline void buffer_print_netdata_double_hex(BUFFER *wb, NETDATA_DOUBLE value) {
  465. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1 + 1);
  466. uint64_t *ptr = (uint64_t *) (&value);
  467. buffer_fast_strcat(wb, IEEE754_DOUBLE_HEX_PREFIX, sizeof(IEEE754_DOUBLE_HEX_PREFIX) - 1);
  468. char *s = &wb->buffer[wb->len];
  469. char *d = print_uint64_hex_reversed(s, *ptr);
  470. char_array_reverse(s, d - 1);
  471. *d = '\0';
  472. wb->len += d - s;
  473. buffer_overflow_check(wb);
  474. }
  475. static inline void buffer_print_netdata_double_base64(BUFFER *wb, NETDATA_DOUBLE value) {
  476. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1 + 1);
  477. uint64_t *ptr = (uint64_t *) (&value);
  478. buffer_fast_strcat(wb, IEEE754_DOUBLE_B64_PREFIX, sizeof(IEEE754_DOUBLE_B64_PREFIX) - 1);
  479. char *s = &wb->buffer[wb->len];
  480. char *d = print_uint64_base64_reversed(s, *ptr);
  481. char_array_reverse(s, d - 1);
  482. *d = '\0';
  483. wb->len += d - s;
  484. buffer_overflow_check(wb);
  485. }
  486. typedef enum {
  487. NUMBER_ENCODING_DECIMAL,
  488. NUMBER_ENCODING_HEX,
  489. NUMBER_ENCODING_BASE64,
  490. } NUMBER_ENCODING;
  491. static inline void buffer_print_int64_encoded(BUFFER *wb, NUMBER_ENCODING encoding, int64_t value) {
  492. if(encoding == NUMBER_ENCODING_BASE64)
  493. return buffer_print_int64_base64(wb, value);
  494. if(encoding == NUMBER_ENCODING_HEX)
  495. return buffer_print_int64_hex(wb, value);
  496. return buffer_print_int64(wb, value);
  497. }
  498. static inline void buffer_print_uint64_encoded(BUFFER *wb, NUMBER_ENCODING encoding, uint64_t value) {
  499. if(encoding == NUMBER_ENCODING_BASE64)
  500. return buffer_print_uint64_base64(wb, value);
  501. if(encoding == NUMBER_ENCODING_HEX)
  502. return buffer_print_uint64_hex(wb, value);
  503. return buffer_print_uint64(wb, value);
  504. }
  505. static inline void buffer_print_netdata_double_encoded(BUFFER *wb, NUMBER_ENCODING encoding, NETDATA_DOUBLE value) {
  506. if(encoding == NUMBER_ENCODING_BASE64)
  507. return buffer_print_netdata_double_base64(wb, value);
  508. if(encoding == NUMBER_ENCODING_HEX)
  509. return buffer_print_netdata_double_hex(wb, value);
  510. return buffer_print_netdata_double(wb, value);
  511. }
  512. static inline void buffer_print_spaces(BUFFER *wb, size_t spaces) {
  513. buffer_need_bytes(wb, spaces * 4 + 1);
  514. char *d = &wb->buffer[wb->len];
  515. for(size_t i = 0; i < spaces; i++) {
  516. *d++ = ' ';
  517. *d++ = ' ';
  518. *d++ = ' ';
  519. *d++ = ' ';
  520. }
  521. *d = '\0';
  522. wb->len += spaces * 4;
  523. buffer_overflow_check(wb);
  524. }
  525. static inline void buffer_print_json_comma(BUFFER *wb) {
  526. if(wb->json.stack[wb->json.depth].count)
  527. buffer_fast_strcat(wb, ",", 1);
  528. }
  529. static inline void buffer_print_json_comma_newline_spacing(BUFFER *wb) {
  530. buffer_print_json_comma(wb);
  531. if((wb->json.options & BUFFER_JSON_OPTIONS_MINIFY) ||
  532. (wb->json.stack[wb->json.depth].type == BUFFER_JSON_ARRAY && !(wb->json.options & BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS)))
  533. return;
  534. buffer_fast_strcat(wb, "\n", 1);
  535. buffer_print_spaces(wb, wb->json.depth + 1);
  536. }
  537. static inline void buffer_print_json_key(BUFFER *wb, const char *key) {
  538. buffer_strcat(wb, wb->json.key_quote);
  539. buffer_json_strcat(wb, key);
  540. buffer_strcat(wb, wb->json.key_quote);
  541. }
  542. static inline void buffer_json_add_string_value(BUFFER *wb, const char *value) {
  543. if(value) {
  544. buffer_strcat(wb, wb->json.value_quote);
  545. buffer_json_strcat(wb, value);
  546. buffer_strcat(wb, wb->json.value_quote);
  547. }
  548. else
  549. buffer_fast_strcat(wb, "null", 4);
  550. }
  551. static inline void buffer_json_add_quoted_string_value(BUFFER *wb, const char *value) {
  552. if(value) {
  553. buffer_strcat(wb, wb->json.value_quote);
  554. buffer_json_quoted_strcat(wb, value);
  555. buffer_strcat(wb, wb->json.value_quote);
  556. }
  557. else
  558. buffer_fast_strcat(wb, "null", 4);
  559. }
  560. static inline void buffer_json_member_add_object(BUFFER *wb, const char *key) {
  561. buffer_print_json_comma_newline_spacing(wb);
  562. buffer_print_json_key(wb, key);
  563. buffer_fast_strcat(wb, ":{", 2);
  564. wb->json.stack[wb->json.depth].count++;
  565. _buffer_json_depth_push(wb, BUFFER_JSON_OBJECT);
  566. }
  567. static inline void buffer_json_object_close(BUFFER *wb) {
  568. #ifdef NETDATA_INTERNAL_CHECKS
  569. assert(wb->json.depth >= 0 && "BUFFER JSON: nothing is open to close it");
  570. assert(wb->json.stack[wb->json.depth].type == BUFFER_JSON_OBJECT && "BUFFER JSON: an object is not open to close it");
  571. #endif
  572. if(!(wb->json.options & BUFFER_JSON_OPTIONS_MINIFY)) {
  573. buffer_fast_strcat(wb, "\n", 1);
  574. buffer_print_spaces(wb, wb->json.depth);
  575. }
  576. buffer_fast_strcat(wb, "}", 1);
  577. _buffer_json_depth_pop(wb);
  578. }
  579. static inline void buffer_json_member_add_string(BUFFER *wb, const char *key, const char *value) {
  580. buffer_print_json_comma_newline_spacing(wb);
  581. buffer_print_json_key(wb, key);
  582. buffer_fast_strcat(wb, ":", 1);
  583. buffer_json_add_string_value(wb, value);
  584. wb->json.stack[wb->json.depth].count++;
  585. }
  586. static inline void buffer_json_member_add_string_or_omit(BUFFER *wb, const char *key, const char *value) {
  587. if(value && *value)
  588. buffer_json_member_add_string(wb, key, value);
  589. }
  590. static inline void buffer_json_member_add_string_or_empty(BUFFER *wb, const char *key, const char *value) {
  591. if(!value)
  592. value = "";
  593. buffer_json_member_add_string(wb, key, value);
  594. }
  595. static inline void buffer_json_member_add_quoted_string(BUFFER *wb, const char *key, const char *value) {
  596. buffer_print_json_comma_newline_spacing(wb);
  597. buffer_print_json_key(wb, key);
  598. buffer_fast_strcat(wb, ":", 1);
  599. if(!value || strcmp(value, "null") == 0)
  600. buffer_fast_strcat(wb, "null", 4);
  601. else
  602. buffer_json_add_quoted_string_value(wb, value);
  603. wb->json.stack[wb->json.depth].count++;
  604. }
  605. static inline void buffer_json_member_add_uuid(BUFFER *wb, const char *key, uuid_t *value) {
  606. buffer_print_json_comma_newline_spacing(wb);
  607. buffer_print_json_key(wb, key);
  608. buffer_fast_strcat(wb, ":", 1);
  609. if(value && !uuid_is_null(*value)) {
  610. char uuid[GUID_LEN + 1];
  611. uuid_unparse_lower(*value, uuid);
  612. buffer_json_add_string_value(wb, uuid);
  613. }
  614. else
  615. buffer_json_add_string_value(wb, NULL);
  616. wb->json.stack[wb->json.depth].count++;
  617. }
  618. static inline void buffer_json_member_add_boolean(BUFFER *wb, const char *key, bool value) {
  619. buffer_print_json_comma_newline_spacing(wb);
  620. buffer_print_json_key(wb, key);
  621. buffer_fast_strcat(wb, ":", 1);
  622. buffer_strcat(wb, value?"true":"false");
  623. wb->json.stack[wb->json.depth].count++;
  624. }
  625. static inline void buffer_json_member_add_array(BUFFER *wb, const char *key) {
  626. buffer_print_json_comma_newline_spacing(wb);
  627. buffer_print_json_key(wb, key);
  628. buffer_fast_strcat(wb, ":[", 2);
  629. wb->json.stack[wb->json.depth].count++;
  630. _buffer_json_depth_push(wb, BUFFER_JSON_ARRAY);
  631. }
  632. static inline void buffer_json_add_array_item_array(BUFFER *wb) {
  633. if(!(wb->json.options & BUFFER_JSON_OPTIONS_MINIFY) && wb->json.stack[wb->json.depth].type == BUFFER_JSON_ARRAY) {
  634. // an array inside another array
  635. buffer_print_json_comma(wb);
  636. buffer_fast_strcat(wb, "\n", 1);
  637. buffer_print_spaces(wb, wb->json.depth + 1);
  638. }
  639. else
  640. buffer_print_json_comma_newline_spacing(wb);
  641. buffer_fast_strcat(wb, "[", 1);
  642. wb->json.stack[wb->json.depth].count++;
  643. _buffer_json_depth_push(wb, BUFFER_JSON_ARRAY);
  644. }
  645. static inline void buffer_json_add_array_item_string(BUFFER *wb, const char *value) {
  646. buffer_print_json_comma_newline_spacing(wb);
  647. buffer_json_add_string_value(wb, value);
  648. wb->json.stack[wb->json.depth].count++;
  649. }
  650. static inline void buffer_json_add_array_item_double(BUFFER *wb, NETDATA_DOUBLE value) {
  651. buffer_print_json_comma_newline_spacing(wb);
  652. buffer_print_netdata_double(wb, value);
  653. wb->json.stack[wb->json.depth].count++;
  654. }
  655. static inline void buffer_json_add_array_item_int64(BUFFER *wb, int64_t value) {
  656. buffer_print_json_comma_newline_spacing(wb);
  657. buffer_print_int64(wb, value);
  658. wb->json.stack[wb->json.depth].count++;
  659. }
  660. static inline void buffer_json_add_array_item_uint64(BUFFER *wb, uint64_t value) {
  661. buffer_print_json_comma_newline_spacing(wb);
  662. buffer_print_uint64(wb, value);
  663. wb->json.stack[wb->json.depth].count++;
  664. }
  665. static inline void buffer_json_add_array_item_time_t(BUFFER *wb, time_t value) {
  666. buffer_print_json_comma_newline_spacing(wb);
  667. buffer_print_int64(wb, value);
  668. wb->json.stack[wb->json.depth].count++;
  669. }
  670. static inline void buffer_json_add_array_item_time_ms(BUFFER *wb, time_t value) {
  671. buffer_print_json_comma_newline_spacing(wb);
  672. buffer_print_int64(wb, value);
  673. buffer_fast_strcat(wb, "000", 3);
  674. wb->json.stack[wb->json.depth].count++;
  675. }
  676. static inline void buffer_json_add_array_item_time_t2ms(BUFFER *wb, time_t value) {
  677. buffer_print_json_comma_newline_spacing(wb);
  678. buffer_print_int64(wb, value);
  679. buffer_fast_strcat(wb, "000", 3);
  680. wb->json.stack[wb->json.depth].count++;
  681. }
  682. static inline void buffer_json_add_array_item_object(BUFFER *wb) {
  683. buffer_print_json_comma_newline_spacing(wb);
  684. buffer_fast_strcat(wb, "{", 1);
  685. wb->json.stack[wb->json.depth].count++;
  686. _buffer_json_depth_push(wb, BUFFER_JSON_OBJECT);
  687. }
  688. static inline void buffer_json_member_add_time_t(BUFFER *wb, const char *key, time_t value) {
  689. buffer_print_json_comma_newline_spacing(wb);
  690. buffer_print_json_key(wb, key);
  691. buffer_fast_strcat(wb, ":", 1);
  692. buffer_print_int64(wb, value);
  693. wb->json.stack[wb->json.depth].count++;
  694. }
  695. static inline void buffer_json_member_add_time_t2ms(BUFFER *wb, const char *key, time_t value) {
  696. buffer_print_json_comma_newline_spacing(wb);
  697. buffer_print_json_key(wb, key);
  698. buffer_fast_strcat(wb, ":", 1);
  699. buffer_print_int64(wb, value);
  700. buffer_fast_strcat(wb, "000", 3);
  701. wb->json.stack[wb->json.depth].count++;
  702. }
  703. static inline void buffer_json_member_add_uint64(BUFFER *wb, const char *key, uint64_t value) {
  704. buffer_print_json_comma_newline_spacing(wb);
  705. buffer_print_json_key(wb, key);
  706. buffer_fast_strcat(wb, ":", 1);
  707. buffer_print_uint64(wb, value);
  708. wb->json.stack[wb->json.depth].count++;
  709. }
  710. static inline void buffer_json_member_add_int64(BUFFER *wb, const char *key, int64_t value) {
  711. buffer_print_json_comma_newline_spacing(wb);
  712. buffer_print_json_key(wb, key);
  713. buffer_fast_strcat(wb, ":", 1);
  714. buffer_print_int64(wb, value);
  715. wb->json.stack[wb->json.depth].count++;
  716. }
  717. static inline void buffer_json_member_add_double(BUFFER *wb, const char *key, NETDATA_DOUBLE value) {
  718. buffer_print_json_comma_newline_spacing(wb);
  719. buffer_print_json_key(wb, key);
  720. buffer_fast_strcat(wb, ":", 1);
  721. buffer_print_netdata_double(wb, value);
  722. wb->json.stack[wb->json.depth].count++;
  723. }
  724. static inline void buffer_json_array_close(BUFFER *wb) {
  725. #ifdef NETDATA_INTERNAL_CHECKS
  726. assert(wb->json.depth >= 0 && "BUFFER JSON: nothing is open to close it");
  727. assert(wb->json.stack[wb->json.depth].type == BUFFER_JSON_ARRAY && "BUFFER JSON: an array is not open to close it");
  728. #endif
  729. if(wb->json.options & BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS) {
  730. buffer_fast_strcat(wb, "\n", 1);
  731. buffer_print_spaces(wb, wb->json.depth);
  732. }
  733. buffer_fast_strcat(wb, "]", 1);
  734. _buffer_json_depth_pop(wb);
  735. }
  736. typedef enum __attribute__((packed)) {
  737. RRDF_FIELD_OPTS_NONE = 0,
  738. RRDF_FIELD_OPTS_UNIQUE_KEY = (1 << 0), // the field is the unique key of the row
  739. RRDF_FIELD_OPTS_VISIBLE = (1 << 1), // the field should be visible by default
  740. RRDF_FIELD_OPTS_STICKY = (1 << 2), // the field should be sticky
  741. RRDF_FIELD_OPTS_FULL_WIDTH = (1 << 3), // the field should get full width
  742. RRDF_FIELD_OPTS_WRAP = (1 << 4), // the field should wrap
  743. RRDR_FIELD_OPTS_DUMMY = (1 << 5), // not a presentable field
  744. } RRDF_FIELD_OPTIONS;
  745. typedef enum __attribute__((packed)) {
  746. RRDF_FIELD_TYPE_NONE,
  747. RRDF_FIELD_TYPE_INTEGER,
  748. RRDF_FIELD_TYPE_STRING,
  749. RRDF_FIELD_TYPE_DETAIL_STRING,
  750. RRDF_FIELD_TYPE_BAR_WITH_INTEGER,
  751. RRDF_FIELD_TYPE_DURATION,
  752. RRDF_FIELD_TYPE_TIMESTAMP,
  753. RRDF_FIELD_TYPE_ARRAY,
  754. } RRDF_FIELD_TYPE;
  755. static inline const char *rrdf_field_type_to_string(RRDF_FIELD_TYPE type) {
  756. switch(type) {
  757. default:
  758. case RRDF_FIELD_TYPE_NONE:
  759. return "none";
  760. case RRDF_FIELD_TYPE_INTEGER:
  761. return "integer";
  762. case RRDF_FIELD_TYPE_STRING:
  763. return "string";
  764. case RRDF_FIELD_TYPE_DETAIL_STRING:
  765. return "detail-string";
  766. case RRDF_FIELD_TYPE_BAR_WITH_INTEGER:
  767. return "bar-with-integer";
  768. case RRDF_FIELD_TYPE_DURATION:
  769. return "duration";
  770. case RRDF_FIELD_TYPE_TIMESTAMP:
  771. return "timestamp";
  772. case RRDF_FIELD_TYPE_ARRAY:
  773. return "array";
  774. }
  775. }
  776. typedef enum __attribute__((packed)) {
  777. RRDF_FIELD_VISUAL_VALUE, // show the value, possibly applying a transformation
  778. RRDF_FIELD_VISUAL_BAR, // show the value and a bar, respecting the max field to fill the bar at 100%
  779. RRDF_FIELD_VISUAL_PILL, //
  780. RRDF_FIELD_VISUAL_RICH, //
  781. RRDR_FIELD_VISUAL_ROW_OPTIONS, // this is a dummy column that is used for row options
  782. } RRDF_FIELD_VISUAL;
  783. static inline const char *rrdf_field_visual_to_string(RRDF_FIELD_VISUAL visual) {
  784. switch(visual) {
  785. default:
  786. case RRDF_FIELD_VISUAL_VALUE:
  787. return "value";
  788. case RRDF_FIELD_VISUAL_BAR:
  789. return "bar";
  790. case RRDF_FIELD_VISUAL_PILL:
  791. return "pill";
  792. case RRDF_FIELD_VISUAL_RICH:
  793. return "richValue";
  794. case RRDR_FIELD_VISUAL_ROW_OPTIONS:
  795. return "rowOptions";
  796. }
  797. }
  798. typedef enum __attribute__((packed)) {
  799. RRDF_FIELD_TRANSFORM_NONE, // show the value as-is
  800. RRDF_FIELD_TRANSFORM_NUMBER, // show the value respecting the decimal_points
  801. RRDF_FIELD_TRANSFORM_DURATION_S, // transform as duration in second to a human-readable duration
  802. RRDF_FIELD_TRANSFORM_DATETIME_MS, // UNIX epoch timestamp in ms
  803. RRDF_FIELD_TRANSFORM_DATETIME_USEC, // UNIX epoch timestamp in usec
  804. } RRDF_FIELD_TRANSFORM;
  805. static inline const char *rrdf_field_transform_to_string(RRDF_FIELD_TRANSFORM transform) {
  806. switch(transform) {
  807. default:
  808. case RRDF_FIELD_TRANSFORM_NONE:
  809. return "none";
  810. case RRDF_FIELD_TRANSFORM_NUMBER:
  811. return "number";
  812. case RRDF_FIELD_TRANSFORM_DURATION_S:
  813. return "duration";
  814. case RRDF_FIELD_TRANSFORM_DATETIME_MS:
  815. return "datetime";
  816. case RRDF_FIELD_TRANSFORM_DATETIME_USEC:
  817. return "datetime_usec";
  818. }
  819. }
  820. typedef enum __attribute__((packed)) {
  821. RRDF_FIELD_SORT_ASCENDING = (1 << 0),
  822. RRDF_FIELD_SORT_DESCENDING = (1 << 1),
  823. RRDF_FIELD_SORT_FIXED = (1 << 7),
  824. } RRDF_FIELD_SORT;
  825. static inline const char *rrdf_field_sort_to_string(RRDF_FIELD_SORT sort) {
  826. if(sort & RRDF_FIELD_SORT_DESCENDING)
  827. return "descending";
  828. else
  829. return "ascending";
  830. }
  831. typedef enum __attribute__((packed)) {
  832. RRDF_FIELD_SUMMARY_UNIQUECOUNT, // Finds the number of unique values of a group of rows
  833. RRDF_FIELD_SUMMARY_SUM, // Sums the values of a group of rows
  834. RRDF_FIELD_SUMMARY_MIN, // Finds the minimum value of a group of rows
  835. RRDF_FIELD_SUMMARY_MAX, // Finds the maximum value of a group of rows
  836. // RRDF_FIELD_SUMMARY_EXTENT, // Finds the minimum and maximum values of a group of rows
  837. RRDF_FIELD_SUMMARY_MEAN, // Finds the mean/average value of a group of rows
  838. RRDF_FIELD_SUMMARY_MEDIAN, // Finds the median value of a group of rows
  839. // RRDF_FIELD_SUMMARY_UNIQUE, // Finds the unique values of a group of rows
  840. RRDF_FIELD_SUMMARY_COUNT, // Calculates the number of rows in a group
  841. } RRDF_FIELD_SUMMARY;
  842. static inline const char *rrdf_field_summary_to_string(RRDF_FIELD_SUMMARY summary) {
  843. switch(summary) {
  844. default:
  845. case RRDF_FIELD_SUMMARY_COUNT:
  846. return "count";
  847. case RRDF_FIELD_SUMMARY_UNIQUECOUNT:
  848. return "uniqueCount";
  849. case RRDF_FIELD_SUMMARY_SUM:
  850. return "sum";
  851. case RRDF_FIELD_SUMMARY_MIN:
  852. return "min";
  853. case RRDF_FIELD_SUMMARY_MEAN:
  854. return "mean";
  855. case RRDF_FIELD_SUMMARY_MEDIAN:
  856. return "median";
  857. case RRDF_FIELD_SUMMARY_MAX:
  858. return "max";
  859. }
  860. }
  861. typedef enum __attribute__((packed)) {
  862. RRDF_FIELD_FILTER_NONE,
  863. RRDF_FIELD_FILTER_RANGE,
  864. RRDF_FIELD_FILTER_MULTISELECT,
  865. RRDF_FIELD_FILTER_FACET,
  866. } RRDF_FIELD_FILTER;
  867. static inline const char *rrdf_field_filter_to_string(RRDF_FIELD_FILTER filter) {
  868. switch(filter) {
  869. case RRDF_FIELD_FILTER_RANGE:
  870. return "range";
  871. case RRDF_FIELD_FILTER_MULTISELECT:
  872. return "multiselect";
  873. case RRDF_FIELD_FILTER_FACET:
  874. return "facet";
  875. default:
  876. case RRDF_FIELD_FILTER_NONE:
  877. return "none";
  878. }
  879. }
  880. static inline void
  881. buffer_rrdf_table_add_field(BUFFER *wb, size_t field_id, const char *key, const char *name, RRDF_FIELD_TYPE type,
  882. RRDF_FIELD_VISUAL visual, RRDF_FIELD_TRANSFORM transform, size_t decimal_points,
  883. const char *units, NETDATA_DOUBLE max, RRDF_FIELD_SORT sort, const char *pointer_to,
  884. RRDF_FIELD_SUMMARY summary, RRDF_FIELD_FILTER filter, RRDF_FIELD_OPTIONS options,
  885. const char *default_value) {
  886. buffer_json_member_add_object(wb, key);
  887. {
  888. buffer_json_member_add_uint64(wb, "index", field_id);
  889. buffer_json_member_add_boolean(wb, "unique_key", options & RRDF_FIELD_OPTS_UNIQUE_KEY);
  890. buffer_json_member_add_string(wb, "name", name);
  891. buffer_json_member_add_boolean(wb, "visible", options & RRDF_FIELD_OPTS_VISIBLE);
  892. buffer_json_member_add_string(wb, "type", rrdf_field_type_to_string(type));
  893. buffer_json_member_add_string_or_omit(wb, "units", units);
  894. buffer_json_member_add_string(wb, "visualization", rrdf_field_visual_to_string(visual));
  895. buffer_json_member_add_object(wb, "value_options");
  896. {
  897. buffer_json_member_add_string_or_omit(wb, "units", units);
  898. buffer_json_member_add_string(wb, "transform", rrdf_field_transform_to_string(transform));
  899. buffer_json_member_add_uint64(wb, "decimal_points", decimal_points);
  900. buffer_json_member_add_string(wb, "default_value", default_value);
  901. }
  902. buffer_json_object_close(wb);
  903. if (!isnan((NETDATA_DOUBLE) (max)))
  904. buffer_json_member_add_double(wb, "max", (NETDATA_DOUBLE) (max));
  905. buffer_json_member_add_string_or_omit(wb, "pointer_to", pointer_to);
  906. buffer_json_member_add_string(wb, "sort", rrdf_field_sort_to_string(sort));
  907. buffer_json_member_add_boolean(wb, "sortable", !(sort & RRDF_FIELD_SORT_FIXED));
  908. buffer_json_member_add_boolean(wb, "sticky", options & RRDF_FIELD_OPTS_STICKY);
  909. buffer_json_member_add_string(wb, "summary", rrdf_field_summary_to_string(summary));
  910. buffer_json_member_add_string(wb, "filter", rrdf_field_filter_to_string(filter));
  911. buffer_json_member_add_boolean(wb, "full_width", options & RRDF_FIELD_OPTS_FULL_WIDTH);
  912. buffer_json_member_add_boolean(wb, "wrap", options & RRDF_FIELD_OPTS_WRAP);
  913. if(options & RRDR_FIELD_OPTS_DUMMY)
  914. buffer_json_member_add_boolean(wb, "dummy", true);
  915. }
  916. buffer_json_object_close(wb);
  917. }
  918. #endif /* NETDATA_WEB_BUFFER_H */