web_buffer_svg.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_buffer_svg.h"
  3. #define BADGE_HORIZONTAL_PADDING 4
  4. #define VERDANA_KERNING 0.2
  5. #define VERDANA_PADDING 1.0
  6. /*
  7. * verdana11_widths[] has been generated with this method:
  8. * https://github.com/badges/shields/blob/master/measure-text.js
  9. */
  10. static double verdana11_widths[128] = {
  11. [0] = 0.0,
  12. [1] = 0.0,
  13. [2] = 0.0,
  14. [3] = 0.0,
  15. [4] = 0.0,
  16. [5] = 0.0,
  17. [6] = 0.0,
  18. [7] = 0.0,
  19. [8] = 0.0,
  20. [9] = 0.0,
  21. [10] = 0.0,
  22. [11] = 0.0,
  23. [12] = 0.0,
  24. [13] = 0.0,
  25. [14] = 0.0,
  26. [15] = 0.0,
  27. [16] = 0.0,
  28. [17] = 0.0,
  29. [18] = 0.0,
  30. [19] = 0.0,
  31. [20] = 0.0,
  32. [21] = 0.0,
  33. [22] = 0.0,
  34. [23] = 0.0,
  35. [24] = 0.0,
  36. [25] = 0.0,
  37. [26] = 0.0,
  38. [27] = 0.0,
  39. [28] = 0.0,
  40. [29] = 0.0,
  41. [30] = 0.0,
  42. [31] = 0.0,
  43. [32] = 3.8671874999999996, //
  44. [33] = 4.3291015625, // !
  45. [34] = 5.048828125, // "
  46. [35] = 9.001953125, // #
  47. [36] = 6.9931640625, // $
  48. [37] = 11.837890625, // %
  49. [38] = 7.992187499999999, // &
  50. [39] = 2.9541015625, // '
  51. [40] = 4.9951171875, // (
  52. [41] = 4.9951171875, // )
  53. [42] = 6.9931640625, // *
  54. [43] = 9.001953125, // +
  55. [44] = 4.00146484375, // ,
  56. [45] = 4.9951171875, // -
  57. [46] = 4.00146484375, // .
  58. [47] = 4.9951171875, // /
  59. [48] = 6.9931640625, // 0
  60. [49] = 6.9931640625, // 1
  61. [50] = 6.9931640625, // 2
  62. [51] = 6.9931640625, // 3
  63. [52] = 6.9931640625, // 4
  64. [53] = 6.9931640625, // 5
  65. [54] = 6.9931640625, // 6
  66. [55] = 6.9931640625, // 7
  67. [56] = 6.9931640625, // 8
  68. [57] = 6.9931640625, // 9
  69. [58] = 4.9951171875, // :
  70. [59] = 4.9951171875, // ;
  71. [60] = 9.001953125, // <
  72. [61] = 9.001953125, // =
  73. [62] = 9.001953125, // >
  74. [63] = 5.99951171875, // ?
  75. [64] = 11.0, // @
  76. [65] = 7.51953125, // A
  77. [66] = 7.541015625, // B
  78. [67] = 7.680664062499999, // C
  79. [68] = 8.4755859375, // D
  80. [69] = 6.95556640625, // E
  81. [70] = 6.32177734375, // F
  82. [71] = 8.529296875, // G
  83. [72] = 8.26611328125, // H
  84. [73] = 4.6298828125, // I
  85. [74] = 5.00048828125, // J
  86. [75] = 7.62158203125, // K
  87. [76] = 6.123046875, // L
  88. [77] = 9.2705078125, // M
  89. [78] = 8.228515625, // N
  90. [79] = 8.658203125, // O
  91. [80] = 6.63330078125, // P
  92. [81] = 8.658203125, // Q
  93. [82] = 7.6484375, // R
  94. [83] = 7.51953125, // S
  95. [84] = 6.7783203125, // T
  96. [85] = 8.05126953125, // U
  97. [86] = 7.51953125, // V
  98. [87] = 10.87646484375, // W
  99. [88] = 7.53564453125, // X
  100. [89] = 6.767578125, // Y
  101. [90] = 7.53564453125, // Z
  102. [91] = 4.9951171875, // [
  103. [92] = 4.9951171875, // backslash
  104. [93] = 4.9951171875, // ]
  105. [94] = 9.001953125, // ^
  106. [95] = 6.9931640625, // _
  107. [96] = 6.9931640625, // `
  108. [97] = 6.6064453125, // a
  109. [98] = 6.853515625, // b
  110. [99] = 5.73095703125, // c
  111. [100] = 6.853515625, // d
  112. [101] = 6.552734375, // e
  113. [102] = 3.8671874999999996, // f
  114. [103] = 6.853515625, // g
  115. [104] = 6.9609375, // h
  116. [105] = 3.0185546875, // i
  117. [106] = 3.78662109375, // j
  118. [107] = 6.509765625, // k
  119. [108] = 3.0185546875, // l
  120. [109] = 10.69921875, // m
  121. [110] = 6.9609375, // n
  122. [111] = 6.67626953125, // o
  123. [112] = 6.853515625, // p
  124. [113] = 6.853515625, // q
  125. [114] = 4.6943359375, // r
  126. [115] = 5.73095703125, // s
  127. [116] = 4.33447265625, // t
  128. [117] = 6.9609375, // u
  129. [118] = 6.509765625, // v
  130. [119] = 9.001953125, // w
  131. [120] = 6.509765625, // x
  132. [121] = 6.509765625, // y
  133. [122] = 5.779296875, // z
  134. [123] = 6.982421875, // {
  135. [124] = 4.9951171875, // |
  136. [125] = 6.982421875, // }
  137. [126] = 9.001953125, // ~
  138. [127] = 0.0
  139. };
  140. // find the width of the string using the verdana 11points font
  141. static inline double verdana11_width(const char *s, float em_size) {
  142. double w = 0.0;
  143. while(*s) {
  144. // if UTF8 multibyte char found and guess it's width equal 1em
  145. // as label width will be updated with JavaScript this is not so important
  146. // TODO: maybe move UTF8 functions from url.c to separate util in libnetdata
  147. // then use url_utf8_get_byte_length etc.
  148. if(IS_UTF8_STARTBYTE(*s)) {
  149. s++;
  150. while(IS_UTF8_BYTE(*s) && !IS_UTF8_STARTBYTE(*s)){
  151. s++;
  152. }
  153. w += em_size;
  154. }
  155. else {
  156. if(likely(!(*s & 0x80))){ // Byte 1XXX XXXX is not valid in UTF8
  157. double t = verdana11_widths[(unsigned char)*s];
  158. if(t != 0.0)
  159. w += t + VERDANA_KERNING;
  160. }
  161. s++;
  162. }
  163. }
  164. w -= VERDANA_KERNING;
  165. w += VERDANA_PADDING;
  166. return w;
  167. }
  168. static inline size_t escape_xmlz(char *dst, const char *src, size_t len) {
  169. size_t i = len;
  170. // required escapes from
  171. // https://github.com/badges/shields/blob/master/badge.js
  172. while(*src && i) {
  173. switch(*src) {
  174. case '\\':
  175. *dst++ = '/';
  176. src++;
  177. i--;
  178. break;
  179. case '&':
  180. if(i > 5) {
  181. strcpy(dst, "&amp;");
  182. i -= 5;
  183. dst += 5;
  184. src++;
  185. }
  186. else goto cleanup;
  187. break;
  188. case '<':
  189. if(i > 4) {
  190. strcpy(dst, "&lt;");
  191. i -= 4;
  192. dst += 4;
  193. src++;
  194. }
  195. else goto cleanup;
  196. break;
  197. case '>':
  198. if(i > 4) {
  199. strcpy(dst, "&gt;");
  200. i -= 4;
  201. dst += 4;
  202. src++;
  203. }
  204. else goto cleanup;
  205. break;
  206. case '"':
  207. if(i > 6) {
  208. strcpy(dst, "&quot;");
  209. i -= 6;
  210. dst += 6;
  211. src++;
  212. }
  213. else goto cleanup;
  214. break;
  215. case '\'':
  216. if(i > 6) {
  217. strcpy(dst, "&apos;");
  218. i -= 6;
  219. dst += 6;
  220. src++;
  221. }
  222. else goto cleanup;
  223. break;
  224. default:
  225. i--;
  226. *dst++ = *src++;
  227. break;
  228. }
  229. }
  230. cleanup:
  231. *dst = '\0';
  232. return len - i;
  233. }
  234. static inline char *format_value_with_precision_and_unit(char *value_string, size_t value_string_len, calculated_number value, const char *units, int precision) {
  235. if(unlikely(isnan(value) || isinf(value)))
  236. value = 0.0;
  237. char *separator = "";
  238. if(unlikely(isalnum(*units)))
  239. separator = " ";
  240. if(precision < 0) {
  241. int len, lstop = 0, trim_zeros = 1;
  242. calculated_number abs = value;
  243. if(isless(value, 0)) {
  244. lstop = 1;
  245. abs = calculated_number_fabs(value);
  246. }
  247. if(isgreaterequal(abs, 1000)) {
  248. len = snprintfz(value_string, value_string_len, "%0.0" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  249. trim_zeros = 0;
  250. }
  251. else if(isgreaterequal(abs, 10)) len = snprintfz(value_string, value_string_len, "%0.1" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  252. else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, value_string_len, "%0.2" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  253. else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, value_string_len, "%0.2" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  254. else if(isgreaterequal(abs, 0.01)) len = snprintfz(value_string, value_string_len, "%0.4" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  255. else if(isgreaterequal(abs, 0.001)) len = snprintfz(value_string, value_string_len, "%0.5" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  256. else if(isgreaterequal(abs, 0.0001)) len = snprintfz(value_string, value_string_len, "%0.6" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  257. else len = snprintfz(value_string, value_string_len, "%0.7" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
  258. if(unlikely(trim_zeros)) {
  259. int l;
  260. // remove trailing zeros from the decimal part
  261. for(l = len - 1; l > lstop; l--) {
  262. if(likely(value_string[l] == '0')) {
  263. value_string[l] = '\0';
  264. len--;
  265. }
  266. else if(unlikely(value_string[l] == '.')) {
  267. value_string[l] = '\0';
  268. len--;
  269. break;
  270. }
  271. else
  272. break;
  273. }
  274. }
  275. if(unlikely(len <= 0)) len = 1;
  276. snprintfz(&value_string[len], value_string_len - len, "%s%s", separator, units);
  277. }
  278. else {
  279. if(precision > 50) precision = 50;
  280. snprintfz(value_string, value_string_len, "%0.*" LONG_DOUBLE_MODIFIER "%s%s", precision, (LONG_DOUBLE) value, separator, units);
  281. }
  282. return value_string;
  283. }
  284. typedef enum badge_units_format {
  285. UNITS_FORMAT_NONE,
  286. UNITS_FORMAT_SECONDS,
  287. UNITS_FORMAT_SECONDS_AGO,
  288. UNITS_FORMAT_MINUTES,
  289. UNITS_FORMAT_MINUTES_AGO,
  290. UNITS_FORMAT_HOURS,
  291. UNITS_FORMAT_HOURS_AGO,
  292. UNITS_FORMAT_ONOFF,
  293. UNITS_FORMAT_UPDOWN,
  294. UNITS_FORMAT_OKERROR,
  295. UNITS_FORMAT_OKFAILED,
  296. UNITS_FORMAT_EMPTY,
  297. UNITS_FORMAT_PERCENT
  298. } UNITS_FORMAT;
  299. static struct units_formatter {
  300. const char *units;
  301. uint32_t hash;
  302. UNITS_FORMAT format;
  303. } badge_units_formatters[] = {
  304. { "seconds", 0, UNITS_FORMAT_SECONDS },
  305. { "seconds ago", 0, UNITS_FORMAT_SECONDS_AGO },
  306. { "minutes", 0, UNITS_FORMAT_MINUTES },
  307. { "minutes ago", 0, UNITS_FORMAT_MINUTES_AGO },
  308. { "hours", 0, UNITS_FORMAT_HOURS },
  309. { "hours ago", 0, UNITS_FORMAT_HOURS_AGO },
  310. { "on/off", 0, UNITS_FORMAT_ONOFF },
  311. { "on-off", 0, UNITS_FORMAT_ONOFF },
  312. { "onoff", 0, UNITS_FORMAT_ONOFF },
  313. { "up/down", 0, UNITS_FORMAT_UPDOWN },
  314. { "up-down", 0, UNITS_FORMAT_UPDOWN },
  315. { "updown", 0, UNITS_FORMAT_UPDOWN },
  316. { "ok/error", 0, UNITS_FORMAT_OKERROR },
  317. { "ok-error", 0, UNITS_FORMAT_OKERROR },
  318. { "okerror", 0, UNITS_FORMAT_OKERROR },
  319. { "ok/failed", 0, UNITS_FORMAT_OKFAILED },
  320. { "ok-failed", 0, UNITS_FORMAT_OKFAILED },
  321. { "okfailed", 0, UNITS_FORMAT_OKFAILED },
  322. { "empty", 0, UNITS_FORMAT_EMPTY },
  323. { "null", 0, UNITS_FORMAT_EMPTY },
  324. { "percentage", 0, UNITS_FORMAT_PERCENT },
  325. { "percent", 0, UNITS_FORMAT_PERCENT },
  326. { "pcent", 0, UNITS_FORMAT_PERCENT },
  327. // terminator
  328. { NULL, 0, UNITS_FORMAT_NONE }
  329. };
  330. inline char *format_value_and_unit(char *value_string, size_t value_string_len, calculated_number value, const char *units, int precision) {
  331. static int max = -1;
  332. int i;
  333. if(unlikely(max == -1)) {
  334. for(i = 0; badge_units_formatters[i].units; i++)
  335. badge_units_formatters[i].hash = simple_hash(badge_units_formatters[i].units);
  336. max = i;
  337. }
  338. if(unlikely(!units)) units = "";
  339. uint32_t hash_units = simple_hash(units);
  340. UNITS_FORMAT format = UNITS_FORMAT_NONE;
  341. for(i = 0; i < max; i++) {
  342. struct units_formatter *ptr = &badge_units_formatters[i];
  343. if(hash_units == ptr->hash && !strcmp(units, ptr->units)) {
  344. format = ptr->format;
  345. break;
  346. }
  347. }
  348. if(unlikely(format == UNITS_FORMAT_SECONDS || format == UNITS_FORMAT_SECONDS_AGO)) {
  349. if(value == 0.0) {
  350. snprintfz(value_string, value_string_len, "%s", "now");
  351. return value_string;
  352. }
  353. else if(isnan(value) || isinf(value)) {
  354. snprintfz(value_string, value_string_len, "%s", "undefined");
  355. return value_string;
  356. }
  357. const char *suffix = (format == UNITS_FORMAT_SECONDS_AGO)?" ago":"";
  358. size_t s = (size_t)value;
  359. size_t d = s / 86400;
  360. s = s % 86400;
  361. size_t h = s / 3600;
  362. s = s % 3600;
  363. size_t m = s / 60;
  364. s = s % 60;
  365. if(d)
  366. snprintfz(value_string, value_string_len, "%zu %s %02zu:%02zu:%02zu%s", d, (d == 1)?"day":"days", h, m, s, suffix);
  367. else
  368. snprintfz(value_string, value_string_len, "%02zu:%02zu:%02zu%s", h, m, s, suffix);
  369. return value_string;
  370. }
  371. else if(unlikely(format == UNITS_FORMAT_MINUTES || format == UNITS_FORMAT_MINUTES_AGO)) {
  372. if(value == 0.0) {
  373. snprintfz(value_string, value_string_len, "%s", "now");
  374. return value_string;
  375. }
  376. else if(isnan(value) || isinf(value)) {
  377. snprintfz(value_string, value_string_len, "%s", "undefined");
  378. return value_string;
  379. }
  380. const char *suffix = (format == UNITS_FORMAT_MINUTES_AGO)?" ago":"";
  381. size_t m = (size_t)value;
  382. size_t d = m / (60 * 24);
  383. m = m % (60 * 24);
  384. size_t h = m / 60;
  385. m = m % 60;
  386. if(d)
  387. snprintfz(value_string, value_string_len, "%zud %02zuh %02zum%s", d, h, m, suffix);
  388. else
  389. snprintfz(value_string, value_string_len, "%zuh %zum%s", h, m, suffix);
  390. return value_string;
  391. }
  392. else if(unlikely(format == UNITS_FORMAT_HOURS || format == UNITS_FORMAT_HOURS_AGO)) {
  393. if(value == 0.0) {
  394. snprintfz(value_string, value_string_len, "%s", "now");
  395. return value_string;
  396. }
  397. else if(isnan(value) || isinf(value)) {
  398. snprintfz(value_string, value_string_len, "%s", "undefined");
  399. return value_string;
  400. }
  401. const char *suffix = (format == UNITS_FORMAT_HOURS_AGO)?" ago":"";
  402. size_t h = (size_t)value;
  403. size_t d = h / 24;
  404. h = h % 24;
  405. if(d)
  406. snprintfz(value_string, value_string_len, "%zud %zuh%s", d, h, suffix);
  407. else
  408. snprintfz(value_string, value_string_len, "%zuh%s", h, suffix);
  409. return value_string;
  410. }
  411. else if(unlikely(format == UNITS_FORMAT_ONOFF)) {
  412. snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"on":"off");
  413. return value_string;
  414. }
  415. else if(unlikely(format == UNITS_FORMAT_UPDOWN)) {
  416. snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"up":"down");
  417. return value_string;
  418. }
  419. else if(unlikely(format == UNITS_FORMAT_OKERROR)) {
  420. snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"error");
  421. return value_string;
  422. }
  423. else if(unlikely(format == UNITS_FORMAT_OKFAILED)) {
  424. snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"failed");
  425. return value_string;
  426. }
  427. else if(unlikely(format == UNITS_FORMAT_EMPTY))
  428. units = "";
  429. else if(unlikely(format == UNITS_FORMAT_PERCENT))
  430. units = "%";
  431. if(unlikely(isnan(value) || isinf(value))) {
  432. strcpy(value_string, "-");
  433. return value_string;
  434. }
  435. return format_value_with_precision_and_unit(value_string, value_string_len, value, units, precision);
  436. }
  437. static struct badge_color {
  438. const char *name;
  439. uint32_t hash;
  440. const char *color;
  441. } badge_colors[] = {
  442. // colors from:
  443. // https://github.com/badges/shields/blob/master/colorscheme.json
  444. { "brightgreen", 0, "4c1" },
  445. { "green", 0, "97CA00" },
  446. { "yellow", 0, "dfb317" },
  447. { "yellowgreen", 0, "a4a61d" },
  448. { "orange", 0, "fe7d37" },
  449. { "red", 0, "e05d44" },
  450. { "blue", 0, "007ec6" },
  451. { "grey", 0, "555" },
  452. { "gray", 0, "555" },
  453. { "lightgrey", 0, "9f9f9f" },
  454. { "lightgray", 0, "9f9f9f" },
  455. // terminator
  456. { NULL, 0, NULL }
  457. };
  458. static inline const char *color_map(const char *color, const char *def) {
  459. static int max = -1;
  460. int i;
  461. if(unlikely(max == -1)) {
  462. for(i = 0; badge_colors[i].name ;i++)
  463. badge_colors[i].hash = simple_hash(badge_colors[i].name);
  464. max = i;
  465. }
  466. uint32_t hash = simple_hash(color);
  467. for(i = 0; i < max; i++) {
  468. struct badge_color *ptr = &badge_colors[i];
  469. if(hash == ptr->hash && !strcmp(color, ptr->name))
  470. return ptr->color;
  471. }
  472. return def;
  473. }
  474. typedef enum color_comparison {
  475. COLOR_COMPARE_EQUAL,
  476. COLOR_COMPARE_NOTEQUAL,
  477. COLOR_COMPARE_LESS,
  478. COLOR_COMPARE_LESSEQUAL,
  479. COLOR_COMPARE_GREATER,
  480. COLOR_COMPARE_GREATEREQUAL,
  481. } BADGE_COLOR_COMPARISON;
  482. static inline void calc_colorz(const char *color, char *final, size_t len, calculated_number value) {
  483. if(isnan(value) || isinf(value))
  484. value = NAN;
  485. char color_buffer[256 + 1] = "";
  486. char value_buffer[256 + 1] = "";
  487. BADGE_COLOR_COMPARISON comparison = COLOR_COMPARE_GREATER;
  488. // example input:
  489. // color<max|color>min|color:null...
  490. const char *c = color;
  491. while(*c) {
  492. char *dc = color_buffer, *dv = NULL;
  493. size_t ci = 0, vi = 0;
  494. const char *t = c;
  495. while(*t && *t != '|') {
  496. switch(*t) {
  497. case '!':
  498. if(t[1] == '=') t++;
  499. comparison = COLOR_COMPARE_NOTEQUAL;
  500. dv = value_buffer;
  501. break;
  502. case '=':
  503. case ':':
  504. comparison = COLOR_COMPARE_EQUAL;
  505. dv = value_buffer;
  506. break;
  507. case '}':
  508. case ')':
  509. case '>':
  510. if(t[1] == '=') {
  511. comparison = COLOR_COMPARE_GREATEREQUAL;
  512. t++;
  513. }
  514. else
  515. comparison = COLOR_COMPARE_GREATER;
  516. dv = value_buffer;
  517. break;
  518. case '{':
  519. case '(':
  520. case '<':
  521. if(t[1] == '=') {
  522. comparison = COLOR_COMPARE_LESSEQUAL;
  523. t++;
  524. }
  525. else if(t[1] == '>' || t[1] == ')' || t[1] == '}') {
  526. comparison = COLOR_COMPARE_NOTEQUAL;
  527. t++;
  528. }
  529. else
  530. comparison = COLOR_COMPARE_LESS;
  531. dv = value_buffer;
  532. break;
  533. default:
  534. if(dv) {
  535. if(vi < 256) {
  536. vi++;
  537. *dv++ = *t;
  538. }
  539. }
  540. else {
  541. if(ci < 256) {
  542. ci++;
  543. *dc++ = *t;
  544. }
  545. }
  546. break;
  547. }
  548. t++;
  549. }
  550. // prepare for next iteration
  551. if(*t == '|') t++;
  552. c = t;
  553. // do the math
  554. *dc = '\0';
  555. if(dv) {
  556. *dv = '\0';
  557. calculated_number v;
  558. if(!*value_buffer || !strcmp(value_buffer, "null")) {
  559. v = NAN;
  560. }
  561. else {
  562. v = str2l(value_buffer);
  563. if(isnan(v) || isinf(v))
  564. v = NAN;
  565. }
  566. if(unlikely(isnan(value) || isnan(v))) {
  567. if(isnan(value) && isnan(v))
  568. break;
  569. }
  570. else {
  571. if (unlikely(comparison == COLOR_COMPARE_LESS && isless(value, v))) break;
  572. else if (unlikely(comparison == COLOR_COMPARE_LESSEQUAL && islessequal(value, v))) break;
  573. else if (unlikely(comparison == COLOR_COMPARE_GREATER && isgreater(value, v))) break;
  574. else if (unlikely(comparison == COLOR_COMPARE_GREATEREQUAL && isgreaterequal(value, v))) break;
  575. else if (unlikely(comparison == COLOR_COMPARE_EQUAL && !islessgreater(value, v))) break;
  576. else if (unlikely(comparison == COLOR_COMPARE_NOTEQUAL && islessgreater(value, v))) break;
  577. }
  578. }
  579. else
  580. break;
  581. }
  582. const char *b;
  583. if(color_buffer[0])
  584. b = color_buffer;
  585. else
  586. b = color;
  587. strncpyz(final, b, len);
  588. }
  589. // value + units
  590. #define VALUE_STRING_SIZE 100
  591. // label
  592. #define LABEL_STRING_SIZE 200
  593. // colors
  594. #define COLOR_STRING_SIZE 100
  595. static inline int allowed_hexa_char(char x) {
  596. return ( (x >= '0' && x <= '9') ||
  597. (x >= 'a' && x <= 'f') ||
  598. (x >= 'A' && x <= 'F')
  599. );
  600. }
  601. static int html_color_check(const char *str) {
  602. int i = 0;
  603. while(str[i]) {
  604. if(!allowed_hexa_char(str[i]))
  605. return 0;
  606. if(unlikely(i >= 6))
  607. return 0;
  608. i++;
  609. }
  610. // want to allow either RGB or RRGGBB
  611. return ( i == 6 || i == 3 );
  612. }
  613. // Will parse color arg as #RRGGBB or #RGB or one of the colors
  614. // from color_map hash table
  615. // if parsing fails (argument error) it will return default color
  616. // given as default parameter (def)
  617. // in any case it will return either color in "RRGGBB" or "RGB" format as string
  618. // or whatever is given as def (without checking - caller responsible to give sensible
  619. // safely escaped default) as default if it fails
  620. // in any case this function must always return something we can put directly in XML
  621. // so no escaping is necessary anymore (with exception of default where caller is responsible)
  622. // to give sensible default
  623. #define BADGE_SVG_COLOR_ARG_MAXLEN 20
  624. static const char *parse_color_argument(const char *arg, const char *def)
  625. {
  626. if( !arg )
  627. return def;
  628. size_t len = strnlen(arg, BADGE_SVG_COLOR_ARG_MAXLEN);
  629. if( len < 2 || len >= BADGE_SVG_COLOR_ARG_MAXLEN )
  630. return def;
  631. if( html_color_check(arg) )
  632. return arg;
  633. return color_map(arg, def);
  634. }
  635. void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int precision, int scale, uint32_t options, int fixed_width_lbl, int fixed_width_val, const char* text_color_lbl, const char* text_color_val) {
  636. char value_color_buffer[COLOR_STRING_SIZE + 1]
  637. , value_string[VALUE_STRING_SIZE + 1]
  638. , label_escaped[LABEL_STRING_SIZE + 1]
  639. , value_escaped[VALUE_STRING_SIZE + 1];
  640. const char *label_color_parsed;
  641. const char *value_color_parsed;
  642. double label_width = (double)fixed_width_lbl, value_width = (double)fixed_width_val, total_width;
  643. double height = 20.0, font_size = 11.0, text_offset = 5.8, round_corner = 3.0;
  644. if(scale < 100) scale = 100;
  645. if(unlikely(!value_color || !*value_color))
  646. value_color = (isnan(value) || isinf(value))?"999":"4c1";
  647. calc_colorz(value_color, value_color_buffer, COLOR_STRING_SIZE, value);
  648. format_value_and_unit(value_string, VALUE_STRING_SIZE, (options & RRDR_OPTION_DISPLAY_ABS)?calculated_number_fabs(value):value, units, precision);
  649. if(fixed_width_lbl <= 0 || fixed_width_val <= 0) {
  650. label_width = verdana11_width(label, font_size) + (BADGE_HORIZONTAL_PADDING * 2);
  651. value_width = verdana11_width(value_string, font_size) + (BADGE_HORIZONTAL_PADDING * 2);
  652. }
  653. total_width = label_width + value_width;
  654. escape_xmlz(label_escaped, label, LABEL_STRING_SIZE);
  655. escape_xmlz(value_escaped, value_string, VALUE_STRING_SIZE);
  656. label_color_parsed = parse_color_argument(label_color, "555");
  657. value_color_parsed = parse_color_argument(value_color_buffer, "555");
  658. wb->contenttype = CT_IMAGE_SVG_XML;
  659. total_width = total_width * scale / 100.0;
  660. height = height * scale / 100.0;
  661. font_size = font_size * scale / 100.0;
  662. text_offset = text_offset * scale / 100.0;
  663. label_width = label_width * scale / 100.0;
  664. value_width = value_width * scale / 100.0;
  665. round_corner = round_corner * scale / 100.0;
  666. // svg template from:
  667. // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
  668. buffer_sprintf(wb,
  669. "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%0.2f\" height=\"%0.2f\">"
  670. "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
  671. "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
  672. "<stop offset=\"1\" stop-opacity=\".1\"/>"
  673. "</linearGradient>"
  674. "<mask id=\"round\">"
  675. "<rect class=\"bdge-ttl-width\" width=\"%0.2f\" height=\"%0.2f\" rx=\"%0.2f\" fill=\"#fff\"/>"
  676. "</mask>"
  677. "<g mask=\"url(#round)\">"
  678. "<rect class=\"bdge-rect-lbl\" width=\"%0.2f\" height=\"%0.2f\" fill=\"#%s\"/>",
  679. total_width, height,
  680. total_width, height, round_corner,
  681. label_width, height, label_color_parsed); //<rect class="bdge-rect-lbl"
  682. if(fixed_width_lbl > 0 && fixed_width_val > 0) {
  683. buffer_sprintf(wb,
  684. "<clipPath id=\"lbl-rect\">"
  685. "<rect class=\"bdge-rect-lbl\" width=\"%0.2f\" height=\"%0.2f\"/>"
  686. "</clipPath>",
  687. label_width, height); //<clipPath id="lbl-rect"> <rect class="bdge-rect-lbl"
  688. }
  689. buffer_sprintf(wb,
  690. "<rect class=\"bdge-rect-val\" x=\"%0.2f\" width=\"%0.2f\" height=\"%0.2f\" fill=\"#%s\"/>",
  691. label_width, value_width, height, value_color_parsed);
  692. if(fixed_width_lbl > 0 && fixed_width_val > 0) {
  693. buffer_sprintf(wb,
  694. "<clipPath id=\"val-rect\">"
  695. "<rect class=\"bdge-rect-val\" x=\"%0.2f\" width=\"%0.2f\" height=\"%0.2f\"/>"
  696. "</clipPath>",
  697. label_width, value_width, height);
  698. }
  699. buffer_sprintf(wb,
  700. "<rect class=\"bdge-ttl-width\" width=\"%0.2f\" height=\"%0.2f\" fill=\"url(#smooth)\"/>"
  701. "</g>"
  702. "<g text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"%0.2f\">"
  703. "<text class=\"bdge-lbl-lbl\" x=\"%0.2f\" y=\"%0.0f\" fill=\"#010101\" fill-opacity=\".3\" clip-path=\"url(#lbl-rect)\">%s</text>"
  704. "<text class=\"bdge-lbl-lbl\" x=\"%0.2f\" y=\"%0.0f\" fill=\"#%s\" clip-path=\"url(#lbl-rect)\">%s</text>"
  705. "<text class=\"bdge-lbl-val\" x=\"%0.2f\" y=\"%0.0f\" fill=\"#010101\" fill-opacity=\".3\" clip-path=\"url(#val-rect)\">%s</text>"
  706. "<text class=\"bdge-lbl-val\" x=\"%0.2f\" y=\"%0.0f\" fill=\"#%s\" clip-path=\"url(#val-rect)\">%s</text>"
  707. "</g>",
  708. total_width, height,
  709. font_size,
  710. label_width / 2, ceil(height - text_offset), label_escaped,
  711. label_width / 2, ceil(height - text_offset - 1.0), parse_color_argument(text_color_lbl, "fff"), label_escaped,
  712. label_width + value_width / 2 -1, ceil(height - text_offset), value_escaped,
  713. label_width + value_width / 2 -1, ceil(height - text_offset - 1.0), parse_color_argument(text_color_val, "fff"), value_escaped);
  714. if(fixed_width_lbl <= 0 || fixed_width_val <= 0){
  715. buffer_sprintf(wb,
  716. "<script type=\"text/javascript\">"
  717. "var bdg_horiz_padding = %d;"
  718. "function netdata_bdge_each(list, attr, value){"
  719. "Array.prototype.forEach.call(list, function(el){"
  720. "el.setAttribute(attr, value);"
  721. "});"
  722. "};"
  723. "var this_svg = document.currentScript.closest(\"svg\");"
  724. "var elem_lbl = this_svg.getElementsByClassName(\"bdge-lbl-lbl\");"
  725. "var elem_val = this_svg.getElementsByClassName(\"bdge-lbl-val\");"
  726. "var lbl_size = elem_lbl[0].getBBox();"
  727. "var val_size = elem_val[0].getBBox();"
  728. "var width_total = lbl_size.width + bdg_horiz_padding*2;"
  729. "this_svg.getElementsByClassName(\"bdge-rect-lbl\")[0].setAttribute(\"width\", width_total);"
  730. "netdata_bdge_each(elem_lbl, \"x\", (lbl_size.width / 2) + bdg_horiz_padding);"
  731. "netdata_bdge_each(elem_val, \"x\", width_total + (val_size.width / 2) + bdg_horiz_padding);"
  732. "var val_rect = this_svg.getElementsByClassName(\"bdge-rect-val\")[0];"
  733. "val_rect.setAttribute(\"width\", val_size.width + bdg_horiz_padding*2);"
  734. "val_rect.setAttribute(\"x\", width_total);"
  735. "width_total += val_size.width + bdg_horiz_padding*2;"
  736. "var width_update_elems = this_svg.getElementsByClassName(\"bdge-ttl-width\");"
  737. "netdata_bdge_each(width_update_elems, \"width\", width_total);"
  738. "this_svg.setAttribute(\"width\", width_total);"
  739. "</script>",
  740. BADGE_HORIZONTAL_PADDING);
  741. }
  742. buffer_sprintf(wb, "</svg>");
  743. }
  744. #define BADGE_URL_ARG_LBL_COLOR "text_color_lbl"
  745. #define BADGE_URL_ARG_VAL_COLOR "text_color_val"
  746. int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *url) {
  747. int ret = HTTP_RESP_BAD_REQUEST;
  748. buffer_flush(w->response.data);
  749. BUFFER *dimensions = NULL;
  750. const char *chart = NULL
  751. , *before_str = NULL
  752. , *after_str = NULL
  753. , *points_str = NULL
  754. , *multiply_str = NULL
  755. , *divide_str = NULL
  756. , *label = NULL
  757. , *units = NULL
  758. , *label_color = NULL
  759. , *value_color = NULL
  760. , *refresh_str = NULL
  761. , *precision_str = NULL
  762. , *scale_str = NULL
  763. , *alarm = NULL
  764. , *fixed_width_lbl_str = NULL
  765. , *fixed_width_val_str = NULL
  766. , *text_color_lbl_str = NULL
  767. , *text_color_val_str = NULL;
  768. int group = RRDR_GROUPING_AVERAGE;
  769. uint32_t options = 0x00000000;
  770. while(url) {
  771. char *value = mystrsep(&url, "&");
  772. if(!value || !*value) continue;
  773. char *name = mystrsep(&value, "=");
  774. if(!name || !*name) continue;
  775. if(!value || !*value) continue;
  776. debug(D_WEB_CLIENT, "%llu: API v1 badge.svg query param '%s' with value '%s'", w->id, name, value);
  777. // name and value are now the parameters
  778. // they are not null and not empty
  779. if(!strcmp(name, "chart")) chart = value;
  780. else if(!strcmp(name, "dimension") || !strcmp(name, "dim") || !strcmp(name, "dimensions") || !strcmp(name, "dims")) {
  781. if(!dimensions)
  782. dimensions = buffer_create(100);
  783. buffer_strcat(dimensions, "|");
  784. buffer_strcat(dimensions, value);
  785. }
  786. else if(!strcmp(name, "after")) after_str = value;
  787. else if(!strcmp(name, "before")) before_str = value;
  788. else if(!strcmp(name, "points")) points_str = value;
  789. else if(!strcmp(name, "group")) {
  790. group = web_client_api_request_v1_data_group(value, RRDR_GROUPING_AVERAGE);
  791. }
  792. else if(!strcmp(name, "options")) {
  793. options |= web_client_api_request_v1_data_options(value);
  794. }
  795. else if(!strcmp(name, "label")) label = value;
  796. else if(!strcmp(name, "units")) units = value;
  797. else if(!strcmp(name, "label_color")) label_color = value;
  798. else if(!strcmp(name, "value_color")) value_color = value;
  799. else if(!strcmp(name, "multiply")) multiply_str = value;
  800. else if(!strcmp(name, "divide")) divide_str = value;
  801. else if(!strcmp(name, "refresh")) refresh_str = value;
  802. else if(!strcmp(name, "precision")) precision_str = value;
  803. else if(!strcmp(name, "scale")) scale_str = value;
  804. else if(!strcmp(name, "fixed_width_lbl")) fixed_width_lbl_str = value;
  805. else if(!strcmp(name, "fixed_width_val")) fixed_width_val_str = value;
  806. else if(!strcmp(name, "alarm")) alarm = value;
  807. else if(!strcmp(name, BADGE_URL_ARG_LBL_COLOR)) text_color_lbl_str = value;
  808. else if(!strcmp(name, BADGE_URL_ARG_VAL_COLOR)) text_color_val_str = value;
  809. }
  810. int fixed_width_lbl = -1;
  811. int fixed_width_val = -1;
  812. if(fixed_width_lbl_str && *fixed_width_lbl_str
  813. && fixed_width_val_str && *fixed_width_val_str) {
  814. fixed_width_lbl = str2i(fixed_width_lbl_str);
  815. fixed_width_val = str2i(fixed_width_val_str);
  816. }
  817. if(!chart || !*chart) {
  818. buffer_no_cacheable(w->response.data);
  819. buffer_sprintf(w->response.data, "No chart id is given at the request.");
  820. goto cleanup;
  821. }
  822. int scale = (scale_str && *scale_str)?str2i(scale_str):100;
  823. RRDSET *st = rrdset_find(host, chart);
  824. if(!st) st = rrdset_find_byname(host, chart);
  825. if(!st) {
  826. buffer_no_cacheable(w->response.data);
  827. buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1, scale, 0, -1, -1, NULL, NULL);
  828. ret = HTTP_RESP_OK;
  829. goto cleanup;
  830. }
  831. st->last_accessed_time = now_realtime_sec();
  832. RRDCALC *rc = NULL;
  833. if(alarm) {
  834. rc = rrdcalc_find(st, alarm);
  835. if (!rc) {
  836. buffer_no_cacheable(w->response.data);
  837. buffer_svg(w->response.data, "alarm not found", NAN, "", NULL, NULL, -1, scale, 0, -1, -1, NULL, NULL);
  838. ret = HTTP_RESP_OK;
  839. goto cleanup;
  840. }
  841. }
  842. long long multiply = (multiply_str && *multiply_str )?str2l(multiply_str):1;
  843. long long divide = (divide_str && *divide_str )?str2l(divide_str):1;
  844. long long before = (before_str && *before_str )?str2l(before_str):0;
  845. long long after = (after_str && *after_str )?str2l(after_str):-st->update_every;
  846. int points = (points_str && *points_str )?str2i(points_str):1;
  847. int precision = (precision_str && *precision_str)?str2i(precision_str):-1;
  848. if(!multiply) multiply = 1;
  849. if(!divide) divide = 1;
  850. int refresh = 0;
  851. if(refresh_str && *refresh_str) {
  852. if(!strcmp(refresh_str, "auto")) {
  853. if(rc) refresh = rc->update_every;
  854. else if(options & RRDR_OPTION_NOT_ALIGNED)
  855. refresh = st->update_every;
  856. else {
  857. refresh = (int)(before - after);
  858. if(refresh < 0) refresh = -refresh;
  859. }
  860. }
  861. else {
  862. refresh = str2i(refresh_str);
  863. if(refresh < 0) refresh = -refresh;
  864. }
  865. }
  866. if(!label) {
  867. if(alarm) {
  868. char *s = (char *)alarm;
  869. while(*s) {
  870. if(*s == '_') *s = ' ';
  871. s++;
  872. }
  873. label = alarm;
  874. }
  875. else if(dimensions) {
  876. const char *dim = buffer_tostring(dimensions);
  877. if(*dim == '|') dim++;
  878. label = dim;
  879. }
  880. else
  881. label = st->name;
  882. }
  883. if(!units) {
  884. if(alarm) {
  885. if(rc->units)
  886. units = rc->units;
  887. else
  888. units = "";
  889. }
  890. else if(options & RRDR_OPTION_PERCENTAGE)
  891. units = "%";
  892. else
  893. units = st->units;
  894. }
  895. debug(D_WEB_CLIENT, "%llu: API command 'badge.svg' for chart '%s', alarm '%s', dimensions '%s', after '%lld', before '%lld', points '%d', group '%d', options '0x%08x'"
  896. , w->id
  897. , chart
  898. , alarm?alarm:""
  899. , (dimensions)?buffer_tostring(dimensions):""
  900. , after
  901. , before
  902. , points
  903. , group
  904. , options
  905. );
  906. if(rc) {
  907. if (refresh > 0) {
  908. buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);
  909. w->response.data->expires = now_realtime_sec() + refresh;
  910. }
  911. else buffer_no_cacheable(w->response.data);
  912. if(!value_color) {
  913. switch(rc->status) {
  914. case RRDCALC_STATUS_CRITICAL:
  915. value_color = "red";
  916. break;
  917. case RRDCALC_STATUS_WARNING:
  918. value_color = "orange";
  919. break;
  920. case RRDCALC_STATUS_CLEAR:
  921. value_color = "brightgreen";
  922. break;
  923. case RRDCALC_STATUS_UNDEFINED:
  924. value_color = "lightgrey";
  925. break;
  926. case RRDCALC_STATUS_UNINITIALIZED:
  927. value_color = "#000";
  928. break;
  929. default:
  930. value_color = "grey";
  931. break;
  932. }
  933. }
  934. buffer_svg(w->response.data,
  935. label,
  936. (isnan(rc->value)||isinf(rc->value)) ? rc->value : rc->value * multiply / divide,
  937. units,
  938. label_color,
  939. value_color,
  940. precision,
  941. scale,
  942. options,
  943. fixed_width_lbl,
  944. fixed_width_val,
  945. text_color_lbl_str,
  946. text_color_val_str
  947. );
  948. ret = HTTP_RESP_OK;
  949. }
  950. else {
  951. time_t latest_timestamp = 0;
  952. int value_is_null = 1;
  953. calculated_number n = NAN;
  954. ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
  955. // if the collected value is too old, don't calculate its value
  956. if (rrdset_last_entry_t(st) >= (now_realtime_sec() - (st->update_every * st->gap_when_lost_iterations_above)))
  957. ret = rrdset2value_api_v1(st, w->response.data, &n, (dimensions) ? buffer_tostring(dimensions) : NULL
  958. , points, after, before, group, 0, options, NULL, &latest_timestamp, &value_is_null, 0);
  959. // if the value cannot be calculated, show empty badge
  960. if (ret != HTTP_RESP_OK) {
  961. buffer_no_cacheable(w->response.data);
  962. value_is_null = 1;
  963. n = 0;
  964. ret = HTTP_RESP_OK;
  965. }
  966. else if (refresh > 0) {
  967. buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);
  968. w->response.data->expires = now_realtime_sec() + refresh;
  969. }
  970. else buffer_no_cacheable(w->response.data);
  971. // render the badge
  972. buffer_svg(w->response.data,
  973. label,
  974. (value_is_null)?NAN:(n * multiply / divide),
  975. units,
  976. label_color,
  977. value_color,
  978. precision,
  979. scale,
  980. options,
  981. fixed_width_lbl,
  982. fixed_width_val,
  983. text_color_lbl_str,
  984. text_color_val_str
  985. );
  986. }
  987. cleanup:
  988. buffer_free(dimensions);
  989. return ret;
  990. }