]> granicus.if.org Git - vnstat/commitdiff
add hourly list and 5 minute list outputs, fix textual bar generation and write testc...
authorTeemu Toivola <git@humdi.net>
Mon, 12 Mar 2018 17:52:00 +0000 (19:52 +0200)
committerTeemu Toivola <git@humdi.net>
Mon, 12 Mar 2018 17:52:00 +0000 (19:52 +0200)
README.md
src/dbshow.c
src/vnstat.c
tests/database_tests.c

index 918badbcd788923f6ea8cdeb9e97e70541c3cc4d..83f9e793a948d020b2a97afbb93fc97333ce84ce 100644 (file)
--- a/README.md
+++ b/README.md
@@ -94,13 +94,11 @@ durations. Yearly and five-minute resolution statistics are now included.
 ##### TODO
 
   * `grep TODO src/* tests/*`
-  * rewrite version 1.18 feature which didn't merge properly
-  * output of 5 minute resolution statistics (vnstat and vnstati)
+  * output of 5 minute resolution statistics (vnstati)
     * possibly not included in first 2.x release
   * query of specific time range date
     * most likely not included in first 2.x release
     * extending the length of the current outputs is however already supported
-      * hourly output is the only exception
   * feature configurability
   * freeze database structure
     * plan ahead and figure out how to migrate data to new structure if necessary?
index 2db7521203f812ab5be9bdc67a3098a4a46d8aef..26783071eac1345f1171db767dcb2dd3aa757ab2 100644 (file)
@@ -48,6 +48,12 @@ void showdb(const char *interface, int qmode)
                case 9:
                        showoneline(&info);
                        break;
+               case 11:
+                       showlist(&info, "hour");
+                       break;
+               case 12:
+                       showlist(&info, "fiveminute");
+                       break;
                default:
                        printf("Error: Not such query mode: %d\n", qmode);
                        break;
@@ -279,7 +285,8 @@ void showlist(const interfaceinfo *interface, const char *listname)
        int32_t limit;
        int listtype, offset = 0, i = 1;
        struct tm *d;
-       char datebuff[DATEBUFFLEN], titlename[16], colname[8], stampformat[64];
+       char datebuff[DATEBUFFLEN], daybuff[DATEBUFFLEN];
+       char titlename[16], colname[8], stampformat[64];
        uint64_t e_rx, e_tx, e_secs, div, mult;
        dbdatalist *datalist = NULL, *datalist_i = NULL;
        dbdatalistinfo datainfo;
@@ -310,6 +317,18 @@ void showlist(const interfaceinfo *interface, const char *listname)
                strncpy_nt(stampformat, cfg.tformat, 64);
                limit = cfg.listtop;
                offset = 6;
+       } else if (strcmp(listname, "hour") == 0) {
+               listtype = 5;
+               strncpy_nt(colname, listname, 8);
+               snprintf(titlename, 16, "hourly");
+               strncpy_nt(stampformat, "%H:%M", 64);
+               limit = cfg.listhours;
+       } else if (strcmp(listname, "fiveminute") == 0) {
+               listtype = 6;
+               strncpy_nt(colname, "time", 8);
+               snprintf(titlename, 16, "5 minute");
+               strncpy_nt(stampformat, "%H:%M", 64);
+               limit = cfg.listfivemins;
        } else {
                return;
        }
@@ -318,6 +337,7 @@ void showlist(const interfaceinfo *interface, const char *listname)
                limit = 0;
        }
 
+       daybuff[0] = '\0';
        e_rx = e_tx = e_secs = 0;
 
        if (!db_getdata(&datalist, &datainfo, interface->name, listname, (uint32_t)limit)) {
@@ -385,6 +405,18 @@ void showlist(const interfaceinfo *interface, const char *listname)
 
        while (datalist_i != NULL) {
                d = localtime(&datalist_i->timestamp);
+
+               if (listtype == 5 || listtype == 6) {
+                       strftime(datebuff, DATEBUFFLEN, cfg.dformat, d);
+                       if (strcmp(daybuff, datebuff) != 0) {
+                               if (cfg.ostyle == 3) {
+                                       indent(4);
+                               }
+                               printf(" %s\n", datebuff);
+                               strcpy(daybuff, datebuff);
+                       }
+               }
+
                strftime(datebuff, DATEBUFFLEN, stampformat, d);
                if (cfg.ostyle == 3) {
                        indent(1);
@@ -415,6 +447,10 @@ void showlist(const interfaceinfo *interface, const char *listname)
                                        e_secs = (uint64_t)(d->tm_yday*86400+d->tm_sec+(d->tm_min*60)+(d->tm_hour*3600));
                                } else if (listtype == 4) { // top
                                        e_secs = 86400;
+                               } else if (listtype == 5) { // hour
+                                       e_secs = (uint64_t)(d->tm_sec+(d->tm_min*60));
+                               } else if (listtype == 6) { // 5min
+                                       e_secs = 300;
                                }
                        } else {
                                if (listtype == 1 || listtype == 4) { // day
@@ -423,6 +459,10 @@ void showlist(const interfaceinfo *interface, const char *listname)
                                        e_secs = (uint64_t)(dmonth(d->tm_mon) * 86400);
                                } else if (listtype == 3) { // year
                                        e_secs = (uint64_t)((365 + isleapyear(d->tm_year+1900)) * 86400);
+                               } else if (listtype == 5) { // hour
+                                       e_secs = 3600;
+                               } else if (listtype == 6) { // 5min
+                                       e_secs = 300;
                                }
                        }
                        printf(" | %s", gettrafficrate(datalist_i->rx+datalist_i->tx, (time_t)e_secs, 14));
@@ -458,7 +498,7 @@ void showlist(const interfaceinfo *interface, const char *listname)
                }
                printf("\n");
        }
-       if (datainfo.count > 0 && listtype != 4) {
+       if (datainfo.count > 0 && listtype < 4) {
                /* use database update time for estimates */
                d = localtime(&interface->updated);
                if ( datalist_i->rx==0 || datalist_i->tx==0 ) {
@@ -807,7 +847,7 @@ int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int
        int i, l, width = len;
 
        if ( (rx + tx) < max) {
-               width = (int)( (rx + tx) / (float)max ) * len;
+               width = (int)( ((rx + tx) / (float)max) * len );
        } else if ((rx + tx) > max) {
                return 0;
        }
index 12e28f73e2dd3ce3ce81a5bc0c0ca91c4a65230f..aff14d8eac8baa77dfaae60312e23efdf64f6e3f 100644 (file)
@@ -217,6 +217,26 @@ int main(int argc, char *argv[]) {
                        }
                } else if ((strcmp(argv[currentarg],"-h")==0) || (strcmp(argv[currentarg],"--hours")==0)) {
                        cfg.qmode=7;
+               } else if ((strcmp(argv[currentarg],"-hl")==0) || (strcmp(argv[currentarg],"--hourslist")==0)) {
+                       cfg.qmode=11;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listhours = atoi(argv[currentarg+1]);
+                               if (cfg.listhours < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
+               } else if ((strcmp(argv[currentarg],"-5")==0) || (strcmp(argv[currentarg],"--fiveminutes")==0)) {
+                       cfg.qmode=12;
+                       if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) {
+                               cfg.listfivemins = atoi(argv[currentarg+1]);
+                               if (cfg.listfivemins < 0) {
+                                       printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg+1], argv[currentarg]);
+                                       return 1;
+                               }
+                               currentarg++;
+                       }
                } else if ((strcmp(argv[currentarg],"--exportdb")==0) || (strcmp(argv[currentarg],"--dumpdb")==0)) {
                        cfg.qmode=4;
                } else if (strcmp(argv[currentarg],"--oneline")==0) {
@@ -449,7 +469,9 @@ void showhelp(PARAMS *p)
        printf(" vnStat %s by Teemu Toivola <tst at iki dot fi>\n\n", getversion());
 
        printf("         -q,  --query          query database\n");
+       printf("         -5,  --fiveminutes    show 5 minutes\n");
        printf("         -h,  --hours          show hours\n");
+       printf("         -hl, --hourslist      show hours list\n");
        printf("         -d,  --days           show days\n");
        printf("         -m,  --months         show months\n");
        printf("         -y,  --years          show years\n");
@@ -471,7 +493,9 @@ void showlonghelp(PARAMS *p)
 
        printf("   Query:\n");
        printf("         -q, --query           query database\n");
-       printf("         -h, --hours           show hours\n");
+       printf("         -5,  --fiveminutes    show 5 minutes\n");
+       printf("         -h,  --hours          show hours\n");
+       printf("         -hl, --hourslist      show hours list\n");
        printf("         -d, --days            show days\n");
        printf("         -m, --months          show months\n");
        printf("         -y, --years           show years\n");
index 6b496e97edc605456eab456bd94fdc787c4e1fff..59be17f96cb28d2ec01b5ee545b4981a0b027b39 100644 (file)
@@ -231,6 +231,9 @@ START_TEST(database_outputs_do_not_crash)
        showdb("something", 7);
        showdb("something", 8);
        showdb("something", 9);
+       showdb("something", 10);
+       showdb("something", 11);
+       showdb("something", 12);
 
        xmlheader();
        showxml("something", 'd');
@@ -276,6 +279,9 @@ START_TEST(database_outputs_do_not_crash_without_data)
        showdb("something", 7);
        showdb("something", 8);
        showdb("something", 9);
+       showdb("something", 10);
+       showdb("something", 11);
+       showdb("something", 12);
 
        xmlheader();
        showxml("something", 'd');
@@ -417,6 +423,25 @@ START_TEST(showbar_with_max_smaller_than_real_max)
 }
 END_TEST
 
+START_TEST(showbar_with_half_and_half_of_half)
+{
+       int pipe, len;
+       char buffer[512];
+       memset(&buffer, '\0', sizeof(buffer));
+
+       defaultcfg();
+       cfg.rxchar[0] = 'r';
+       cfg.txchar[0] = 't';
+       pipe = pipe_output();
+       len = showbar(1, 1, 4, 12);
+       ck_assert_int_eq(len, 6);
+       fflush(stdout);
+
+       len = (int)read(pipe, buffer, 512);
+       ck_assert_str_eq(buffer, "  rrrttt");
+}
+END_TEST
+
 void add_database_tests(Suite *s)
 {
        TCase *tc_db = tcase_create("Database");
@@ -443,6 +468,7 @@ void add_database_tests(Suite *s)
        tcase_add_test(tc_db, showbar_with_one_tenth);
        tcase_add_test(tc_db, showbar_with_small_rx_shows_all_tx);
        tcase_add_test(tc_db, showbar_with_max_smaller_than_real_max);
+       tcase_add_test(tc_db, showbar_with_half_and_half_of_half);
        suite_add_tcase(s, tc_db);
 }