buffer.h 36 KB

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