From: Teemu Toivola Date: Fri, 4 May 2018 22:20:45 +0000 (+0300) Subject: show sum of data instead of estimate in lists if end of time range has been specified X-Git-Tag: v2.0~20 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=72bbc3f982cc3b418ed78e28aa421732b371193e;p=vnstat show sum of data instead of estimate in lists if end of time range has been specified --- diff --git a/src/dbshow.c b/src/dbshow.c index d764d80..09e5d57 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -500,10 +500,10 @@ void showlist(const interfaceinfo *interface, const char *listname, const char * } printf("\n"); } - if (datainfo.count > 0 && listtype < 4) { + if ((strlen(dataend) == 0 && datainfo.count > 0 && listtype < 4) || (strlen(dataend) > 0 && datainfo.count > 1)) { /* use database update time for estimates */ d = localtime(&interface->updated); - if ( datalist_i->rx==0 || datalist_i->tx==0 ) { + if ( datalist_i->rx==0 || datalist_i->tx==0 || strlen(dataend)>0 ) { e_rx = e_tx = 0; } else { div = 0; @@ -528,9 +528,20 @@ void showlist(const interfaceinfo *interface, const char *listname, const char * if (cfg.ostyle == 3) { printf(" "); } - printf(" estimated %s", getvalue(e_rx, 11, 2)); - printf(" | %s", getvalue(e_tx, 11, 2)); - printf(" | %s", getvalue(e_rx + e_tx, 11, 2)); + if (strlen(dataend) == 0) { + printf(" estimated %s", getvalue(e_rx, 11, 2)); + printf(" | %s", getvalue(e_tx, 11, 2)); + printf(" | %s", getvalue(e_rx + e_tx, 11, 2)); + } else { + if (datainfo.count < 100) { + snprintf(datebuff, DATEBUFFLEN, "sum of %"PRIu32"", datainfo.count); + } else { + snprintf(datebuff, DATEBUFFLEN, "sum"); + } + printf(" %9s %s", datebuff, getvalue(datainfo.sumrx, 11, 2)); + printf(" | %s", getvalue(datainfo.sumtx, 11, 2)); + printf(" | %s", getvalue(datainfo.sumrx + datainfo.sumtx, 11, 2)); + } if (cfg.ostyle == 3) { printf(" |"); } diff --git a/src/dbsql.c b/src/dbsql.c index 41f196f..00001d4 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -1119,6 +1119,8 @@ void updatelistinfo(dbdatalistinfo *listinfo, const uint64_t rx, const uint64_t listinfo->mintx = tx; listinfo->min = rx + tx; listinfo->max = rx + tx; + listinfo->sumrx = rx; + listinfo->sumtx = tx; } else { if (timestamp > listinfo->maxtime) { listinfo->maxtime = timestamp; @@ -1144,6 +1146,8 @@ void updatelistinfo(dbdatalistinfo *listinfo, const uint64_t rx, const uint64_t if (rx + tx < listinfo->min) { listinfo->min = rx + tx; } + listinfo->sumrx += rx; + listinfo->sumtx += tx; } listinfo->count++; } diff --git a/src/dbsql.h b/src/dbsql.h index dab1623..fbaf309 100644 --- a/src/dbsql.h +++ b/src/dbsql.h @@ -21,6 +21,7 @@ typedef struct dbdatalistinfo { uint64_t minrx, mintx; uint64_t maxrx, maxtx; uint64_t min, max; + uint64_t sumrx, sumtx; } dbdatalistinfo; typedef struct interfaceinfo { diff --git a/src/image.c b/src/image.c index e5f6a30..022181c 100644 --- a/src/image.c +++ b/src/image.c @@ -526,7 +526,7 @@ void drawlist(IMAGECONTENT *ic, const char *listname) rowcount += datainfo.count; width = 500; - if (listtype >= 4) { // less space needed when no estimate is shown + if (listtype >= 4 && (datainfo.count < 2 || strlen(ic->dataend) == 0)) { // less space needed when no estimate or sum is shown height = 86; offsety = -16; } else { @@ -685,10 +685,10 @@ void drawlist(IMAGECONTENT *ic, const char *listname) gdImageLine(ic->im, textx+2, texty+5, textx+296+offsetx, texty+5, ic->cline); } - if (datainfo.count > 0 && listtype < 4) { + if ((strlen(ic->dataend) == 0 && datainfo.count > 0 && listtype < 4) || (strlen(ic->dataend) > 0 && datainfo.count > 1)) { d = localtime(&ic->interface.updated); - if ( datalist_i->rx==0 || datalist_i->tx==0 ) { + if ( datalist_i->rx==0 || datalist_i->tx==0 || strlen(ic->dataend)>0) { e_rx = e_tx = 0; } else { div = 0; @@ -710,12 +710,26 @@ void drawlist(IMAGECONTENT *ic, const char *listname) e_rx = e_tx = 0; } } - snprintf(buffer, 32, " estimated "); - strncat(buffer, getvalue(e_rx, 10, 2), 32); - strcat(buffer, " "); - strncat(buffer, getvalue(e_tx, 10, 2), 32); - strcat(buffer, " "); - strncat(buffer, getvalue(e_rx+e_tx, 10, 2), 32); + if (strlen(ic->dataend) == 0) { + snprintf(buffer, 32, " estimated "); + strncat(buffer, getvalue(e_rx, 10, 2), 32); + strcat(buffer, " "); + strncat(buffer, getvalue(e_tx, 10, 2), 32); + strcat(buffer, " "); + strncat(buffer, getvalue(e_rx+e_tx, 10, 2), 32); + } else { + if (datainfo.count < 100) { + snprintf(datebuff, 16, "sum of %"PRIu32"", datainfo.count); + } else { + snprintf(datebuff, 16, "sum"); + } + snprintf(buffer, 32, " %9s ", datebuff); + strncat(buffer, getvalue(datainfo.sumrx, 10, 2), 32); + strcat(buffer, " "); + strncat(buffer, getvalue(datainfo.sumtx, 10, 2), 32); + strcat(buffer, " "); + strncat(buffer, getvalue(datainfo.sumrx + datainfo.sumtx, 10, 2), 32); + } gdImageString(ic->im, gdFontGetSmall(), textx, texty+8, (unsigned char*)buffer, ic->ctext); } diff --git a/tests/dbsql_tests.c b/tests/dbsql_tests.c index 69e8af9..d64ebe6 100644 --- a/tests/dbsql_tests.c +++ b/tests/dbsql_tests.c @@ -823,8 +823,10 @@ START_TEST(db_data_can_be_retrieved) ck_assert_int_eq(datainfo.count, 2); ck_assert_int_eq(datainfo.minrx, 1); ck_assert_int_eq(datainfo.maxrx, 10); + ck_assert_int_eq(datainfo.sumrx, 11); ck_assert_int_eq(datainfo.mintx, 2); ck_assert_int_eq(datainfo.maxtx, 20); + ck_assert_int_eq(datainfo.sumtx, 22); /* db_insertdata rounds the timestamps to full hours */ ck_assert_int_eq((int)datainfo.maxtime, 7200); ck_assert_int_eq((int)datainfo.mintime, 0);