@cryptotaxi247 / netdata-1 / commits / f1f2a2e5e

badges can be scaled by appending scale=X (x >= 100); https://github.com/firehol/netdata/issues/1921#issuecomment-368028528

Costa Tsaousis (ktsaou) committed Feb 26, 2018 at 01:45 UTC f1f2a2e5e0cbe17f49aed44262eaf489075f14e9
3 files changed +43 -25
src/web_api_v1.c
+8 -2
@@ -361,6 +361,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
361 , *value_color = NULL
362 , *refresh_str = NULL
363 , *precision_str = NULL
364 + , *scale_str = NULL
365 , *alarm = NULL;
366
367 int group = GROUP_AVERAGE;
@@ -404,6 +405,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
405 else if(!strcmp(name, "divide")) divide_str = value;
406 else if(!strcmp(name, "refresh")) refresh_str = value;
407 else if(!strcmp(name, "precision")) precision_str = value;
408 + else if(!strcmp(name, "scale")) scale_str = value;
409 else if(!strcmp(name, "alarm")) alarm = value;
410 }
411
@@ -413,11 +415,13 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
415 goto cleanup;
416 }
417
418 + int scale = (scale_str && *scale_str)?str2i(scale_str):100;
419 +
420 RRDSET *st = rrdset_find(host, chart);
421 if(!st) st = rrdset_find_byname(host, chart);
422 if(!st) {
423 buffer_no_cacheable(w->response.data);
420 - buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1, 0);
424 + buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1, scale, 0);
425 ret = 200;
426 goto cleanup;
427 }
@@ -428,7 +432,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
432 rc = rrdcalc_find(st, alarm);
433 if (!rc) {
434 buffer_no_cacheable(w->response.data);
431 - buffer_svg(w->response.data, "alarm not found", NAN, "", NULL, NULL, -1, 0);
435 + buffer_svg(w->response.data, "alarm not found", NAN, "", NULL, NULL, -1, scale, 0);
436 ret = 200;
437 goto cleanup;
438 }
@@ -545,6 +549,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
549 label_color,
550 value_color,
551 precision,
552 + scale,
553 options
554 );
555 ret = 200;
@@ -581,6 +586,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
586 label_color,
587 value_color,
588 precision,
589 + scale,
590 options
591 );
592 }
src/web_buffer_svg.c
+34 -22
@@ -270,7 +270,7 @@ double verdana11_widths[256] = {
270
271 // find the width of the string using the verdana 11points font
272 // re-write the string in place, skiping zero-length characters
273 -static inline int verdana11_width(char *s) {
273 +static inline double verdana11_width(char *s) {
274 double w = 0.0;
275 char *d = s;
276
@@ -290,7 +290,7 @@ static inline int verdana11_width(char *s) {
290 *d = '\0';
291 w -= VERDANA_KERNING;
292 w += VERDANA_PADDING;
293 - return (int)ceil(w);
293 + return w;
294 }
295
296 static inline size_t escape_xmlz(char *dst, const char *src, size_t len) {
@@ -752,7 +752,7 @@ static inline void calc_colorz(const char *color, char *final, size_t len, calcu
752 // colors
753 #define COLOR_STRING_SIZE 100
754
755 -void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int precision, uint32_t options) {
755 +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) {
756 char label_buffer[LABEL_STRING_SIZE + 1]
757 , value_color_buffer[COLOR_STRING_SIZE + 1]
758 , value_string[VALUE_STRING_SIZE + 1]
@@ -761,7 +761,9 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
761 , label_color_escaped[COLOR_STRING_SIZE + 1]
762 , value_color_escaped[COLOR_STRING_SIZE + 1];
763
764 - int label_width, value_width, total_width;
764 + double label_width, value_width, total_width, height = 20.0, font_size = 11.0, text_offset = 5.8, round_corner = 3.0;
765 +
766 + if(scale < 100) scale = 100;
767
768 if(unlikely(!label_color || !*label_color))
769 label_color = "#555";
@@ -786,35 +788,45 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
788
789 wb->contenttype = CT_IMAGE_SVG_XML;
790
791 + total_width = total_width * scale / 100.0;
792 + height = height * scale / 100.0;
793 + font_size = font_size * scale / 100.0;
794 + text_offset = text_offset * scale / 100.0;
795 + label_width = label_width * scale / 100.0;
796 + value_width = value_width * scale / 100.0;
797 + round_corner = round_corner * scale / 100.0;
798 +
799 // svg template from:
800 // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
801 buffer_sprintf(wb,
792 - "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
802 + "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%0.2f\" height=\"%0.2f\">"
803 "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
804 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
805 "<stop offset=\"1\" stop-opacity=\".1\"/>"
806 "</linearGradient>"
807 "<mask id=\"round\">"
798 - "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
808 + "<rect width=\"%0.2f\" height=\"%0.2f\" rx=\"%0.2f\" fill=\"#fff\"/>"
809 "</mask>"
810 "<g mask=\"url(#round)\">"
801 - "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
802 - "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
803 - "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
811 + "<rect width=\"%0.2f\" height=\"%0.2f\" fill=\"%s\"/>"
812 + "<rect x=\"%0.2f\" width=\"%0.2f\" height=\"%0.2f\" fill=\"%s\"/>"
813 + "<rect width=\"%0.2f\" height=\"%0.2f\" fill=\"url(#smooth)\"/>"
814 "</g>"
805 - "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
806 - "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
807 - "<text x=\"%d\" y=\"14\">%s</text>"
808 - "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
809 - "<text x=\"%d\" y=\"14\">%s</text>"
815 + "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"%0.2f\">"
816 + "<text x=\"%0.2f\" y=\"%0.0f\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
817 + "<text x=\"%0.2f\" y=\"%0.0f\">%s</text>"
818 + "<text x=\"%0.2f\" y=\"%0.0f\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
819 + "<text x=\"%0.2f\" y=\"%0.0f\">%s</text>"
820 "</g>"
821 "</svg>",
812 - total_width, total_width,
813 - label_width, label_color_escaped,
814 - label_width, value_width, value_color_escaped,
815 - total_width,
816 - label_width / 2, label_escaped,
817 - label_width / 2, label_escaped,
818 - label_width + value_width / 2 -1, value_escaped,
819 - label_width + value_width / 2 -1, value_escaped);
822 + total_width, height,
823 + total_width, height, round_corner,
824 + label_width, height, label_color_escaped,
825 + label_width, value_width, height, value_color_escaped,
826 + total_width, height,
827 + font_size,
828 + label_width / 2, ceil(height - text_offset), label_escaped,
829 + label_width / 2, ceil(height - text_offset - 1.0), label_escaped,
830 + label_width + value_width / 2 -1, ceil(height - text_offset), value_escaped,
831 + label_width + value_width / 2 -1, ceil(height - text_offset - 1.0), value_escaped);
832 }
src/web_buffer_svg.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef NETDATA_WEB_BUFFER_SVG_H
2 #define NETDATA_WEB_BUFFER_SVG_H 1
3
4 -extern void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int precision, uint32_t options);
4 +extern 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);
5 extern char *format_value_and_unit(char *value_string, size_t value_string_len, calculated_number value, const char *units, int precision);
6
7 #endif /* NETDATA_WEB_BUFFER_SVG_H */