From: Teemu Toivola <git@humdi.net> Date: Thu, 20 Sep 2018 18:22:45 +0000 (+0300) Subject: fix drawbar() handling when there's no traffic, fix image size and alignment when... X-Git-Tag: v2.0~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a070d06934428ecf2064a577adf1794d93857b8f;p=vnstat fix drawbar() handling when there's no traffic, fix image size and alignment when there's no data --- diff --git a/src/dbshow.c b/src/dbshow.c index 5e928c2..9fa49ee 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -832,11 +832,11 @@ int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int if ( (rx + tx) < max) { width = (int)( ((rx + tx) / (float)max) * len ); - } else if ((rx + tx) > max) { + } else if ((rx + tx) > max || max == 0) { return 0; } - if (len <= 0) { + if (width <= 0) { return 0; } diff --git a/src/image.c b/src/image.c index 533c642..fb44527 100644 --- a/src/image.c +++ b/src/image.c @@ -188,30 +188,33 @@ void drawbar(IMAGECONTENT *ic, const int x, const int y, const int len, const ui { int l, width = len; - if ((rx+tx)!=max) { + if ((rx+tx) < max) { width=(int)(((rx+tx)/(float)max)*len); + } else if ((rx+tx) > max || max == 0) { + return; } - if (width!=0) { + if (width <= 0) { + return; + } - if (tx>rx) { - l=(int)(rintf((rx/(float)(rx+tx)*width))); + if (tx > rx) { + l=(int)(rintf((rx/(float)(rx+tx)*width))); - gdImageFilledRectangle(ic->im, x, y+YBEGINOFFSET, x+l, y+YENDOFFSET, ic->crx); - gdImageRectangle(ic->im, x, y+YBEGINOFFSET, x+l, y+YENDOFFSET, ic->crxd); + gdImageFilledRectangle(ic->im, x, y+YBEGINOFFSET, x+l, y+YENDOFFSET, ic->crx); + gdImageRectangle(ic->im, x, y+YBEGINOFFSET, x+l, y+YENDOFFSET, ic->crxd); - gdImageFilledRectangle(ic->im, x+l, y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctx); - gdImageRectangle(ic->im, x+l, y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctxd); + gdImageFilledRectangle(ic->im, x+l, y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctx); + gdImageRectangle(ic->im, x+l, y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctxd); - } else { - l=(int)(rintf((tx/(float)(rx+tx)*width))); + } else { + l=(int)(rintf((tx/(float)(rx+tx)*width))); - gdImageFilledRectangle(ic->im, x, y+YBEGINOFFSET, x+(width-l), y+YENDOFFSET, ic->crx); - gdImageRectangle(ic->im, x, y+YBEGINOFFSET, x+(width-l), y+YENDOFFSET, ic->crxd); + gdImageFilledRectangle(ic->im, x, y+YBEGINOFFSET, x+(width-l), y+YENDOFFSET, ic->crx); + gdImageRectangle(ic->im, x, y+YBEGINOFFSET, x+(width-l), y+YENDOFFSET, ic->crxd); - gdImageFilledRectangle(ic->im, x+(width-l), y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctx); - gdImageRectangle(ic->im, x+(width-l), y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctxd); - } + gdImageFilledRectangle(ic->im, x+(width-l), y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctx); + gdImageRectangle(ic->im, x+(width-l), y+YBEGINOFFSET, x+width, y+YENDOFFSET, ic->ctxd); } } @@ -534,6 +537,11 @@ void drawlist(IMAGECONTENT *ic, const char *listname) } height += 12 * rowcount; + if (!datainfo.count) { + height = 98; + offsety = -24; + } + if (!ic->showheader) { headermod = 26; height -= 22; @@ -575,7 +583,7 @@ void drawlist(IMAGECONTENT *ic, const char *listname) } else { // everything else snprintf(buffer, 512, " %8s rx tx total", colname); } - if (cfg.ostyle>2) { + if (cfg.ostyle > 2) { strcat(buffer, " avg. rate"); gdImageString(ic->im, gdFontGetSmall(), textx, texty, (unsigned char*)buffer, ic->ctext); gdImageLine(ic->im, textx+2, texty+16, textx+392+offsetx, texty+16, ic->cline); @@ -677,11 +685,15 @@ void drawlist(IMAGECONTENT *ic, const char *listname) } if (!datainfo.count) { - gdImageString(ic->im, gdFontGetSmall(), textx, texty, (unsigned char*)" no data available", ic->ctext); + i = 102; + if (cfg.ostyle > 2) { + i += 46; + } + gdImageString(ic->im, gdFontGetSmall(), textx+i, texty, (unsigned char*)"no data available", ic->ctext); texty += 12; } - if (cfg.ostyle>2) { + if (cfg.ostyle > 2) { gdImageLine(ic->im, textx+2, texty+5, textx+392+offsetx, texty+5, ic->cline); } else { gdImageLine(ic->im, textx+2, texty+5, textx+296+offsetx, texty+5, ic->cline); diff --git a/tests/database_tests.c b/tests/database_tests.c index f82695e..e54aa62 100644 --- a/tests/database_tests.c +++ b/tests/database_tests.c @@ -329,6 +329,15 @@ START_TEST(showbar_with_zero_len_is_nothing) } END_TEST +START_TEST(showbar_with_zero_max) +{ + int len; + suppress_output(); + len = showbar(0, 0, 0, 10); + ck_assert_int_eq(len, 0); +} +END_TEST + START_TEST(showbar_with_big_max_and_small_numbers) { int len; @@ -659,6 +668,7 @@ void add_database_tests(Suite *s) tcase_add_test(tc_db, database_outputs_do_not_crash); tcase_add_test(tc_db, database_outputs_do_not_crash_without_data); tcase_add_test(tc_db, showbar_with_zero_len_is_nothing); + tcase_add_test(tc_db, showbar_with_zero_max); tcase_add_test(tc_db, showbar_with_big_max_and_small_numbers); tcase_add_test(tc_db, showbar_with_all_rx); tcase_add_test(tc_db, showbar_with_all_tx);