]> granicus.if.org Git - vnstat/commitdiff
implement daily and monthly outputs
authorTeemu Toivola <git@humdi.net>
Sun, 22 Jan 2017 17:50:15 +0000 (19:50 +0200)
committerTeemu Toivola <git@humdi.net>
Sun, 22 Jan 2017 17:50:15 +0000 (19:50 +0200)
README.md
src/dbshow.c
src/dbshow.h
src/dbsql.c
src/dbsql.h

index d2c4f0bc8e230dfb1c6e0171ffd75d65f73c4049..b416ef05bf6b3af0a21fc625e192c59b809b5bdd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -16,7 +16,11 @@ configurable durations. Yearly and 5 minute resolution statistics are also plann
     * implemented database dependent features
       * `--create`
       * `--delete`
-      * summary output
+    * implemented console outputs
+      * summary
+      * one line
+      * daily
+      * monthly
   * vnstati (image output) is disabled
   * not ready to replace vnStat 1.x due to lack of most outputs
 
index 2e2a91a7dec64e6248cbe73197ddf865a02ccf79..40a69ee8d323af8043a5c99b8cc16923f30dbbe1 100644 (file)
@@ -24,13 +24,13 @@ void showdb(const char *interface, int qmode)
                case 0:
                        showsummary(&info);
                        break;
-/*             case 1:
-                       showdays();
+               case 1:
+                       showlist(&info, "day");
                        break;
                case 2:
-                       showmonths();
+                       showlist(&info, "month");
                        break;
-               case 3:
+/*             case 3:
                        showtop();
                        break;
                case 4:
@@ -54,7 +54,7 @@ void showdb(const char *interface, int qmode)
        }
 }
 
-void showsummary(interfaceinfo *interface)
+void showsummary(const interfaceinfo *interface)
 {
        struct tm *d;
        char datebuff[DATEBUFFLEN];
@@ -259,7 +259,124 @@ void showsummary(interfaceinfo *interface)
        dbdatalistfree(&datalist);
 }
 
-void showoneline(interfaceinfo *interface)
+void showlist(const interfaceinfo *interface, const char *listname)
+{
+       int limit;
+       struct tm *d;
+       char datebuff[DATEBUFFLEN], titlename[8], stampformat[64];
+       uint64_t e_rx, e_tx;
+       dbdatalist *datalist = NULL, *datalist_i = NULL;
+       dbdatalistinfo datainfo;
+
+       if (strcmp(listname, "day") == 0) {
+               snprintf(titlename, 8, "daily");
+               strncpy_nt(stampformat, cfg.dformat, 64);
+               limit = 30;
+       } else if (strcmp(listname, "month") == 0) {
+               snprintf(titlename, 8, "monthly");
+               strncpy_nt(stampformat, cfg.mformat, 64);
+               limit = 12;
+       } else {
+               return;
+       }
+
+       e_rx = e_tx = 0;
+
+       printf("\n");
+       if (strcmp(interface->name, interface->alias) == 0 || strlen(interface->alias) == 0) {
+               printf(" %s", interface->name);
+       } else {
+               printf(" %s (%s)", interface->alias, interface->name);
+       }
+       if (interface->active == 0) {
+               printf(" [disabled]");
+       }
+       printf("  /  %s\n\n", titlename);
+
+       if (cfg.ostyle == 3) {
+               printf("     %8s        rx      |     tx      |    total    |   avg. rate\n", listname);
+               printf("     ------------------------+-------------+-------------+---------------\n");
+       } else {
+               printf(" %8s        rx      |     tx      |    total\n", listname);
+               if (cfg.ostyle != 0) {
+                       printf("-------------------------+-------------+---------------------------------------\n");
+               } else {
+                       printf("-------------------------+-------------+------------\n");
+               }
+       }
+
+       if (!db_getdata(&datalist, &datainfo, interface->name, listname, limit, 0)) {
+               /* TODO: match with other output style */
+               printf("Error: failed to fetch %s data\n", titlename);
+               return;
+       }
+
+       datalist_i = datalist;
+
+       while (datalist_i != NULL) {
+               d = localtime(&datalist_i->timestamp);
+               strftime(datebuff, DATEBUFFLEN, stampformat, d);
+               if (cfg.ostyle == 3) {
+                       printf("    ");
+               }
+               if (strlen(datebuff)<=9) {
+                       printf(" %*s   %s", getpadding(9, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1));
+               } else {
+                       printf(" %-*s %s", getpadding(11, datebuff), datebuff, getvalue(datalist_i->rx, 11, 1));
+               }
+               printf(" | %s", getvalue(datalist_i->tx, 11, 1));
+               printf(" | %s", getvalue(datalist_i->rx+datalist_i->tx, 11, 1));
+               if (cfg.ostyle == 3) {
+                       if (datalist_i->next == NULL) {
+                               d=localtime(&interface->updated);
+                               printf(" | %s", gettrafficrate(datalist_i->rx+datalist_i->tx, d->tm_sec+(d->tm_min*60)+(d->tm_hour*3600), 14));
+                       } else {
+                               printf(" | %s", gettrafficrate(datalist_i->rx+datalist_i->tx, 86400, 14));
+                       }
+               } else if (cfg.ostyle != 0) {
+                       showbar(datalist_i->rx, datalist_i->tx, datainfo.max, 24);
+               }
+               printf("\n");
+               if (datalist_i->next == NULL) {
+                       break;
+               }
+               datalist_i = datalist_i->next;
+       }
+       if (datainfo.count == 0)
+               printf("                           no data available\n");
+       if (cfg.ostyle == 3) {
+               printf("     ------------------------+-------------+-------------+---------------\n");
+       } else {
+               if (cfg.ostyle != 0) {
+                       printf("-------------------------+-------------+---------------------------------------\n");
+               } else {
+                       printf("-------------------------+-------------+------------\n");
+               }
+       }
+       if (datainfo.count > 0) {
+               /* use database update time for estimates */
+               d = localtime(&interface->updated);
+               if ( datalist_i->rx==0 || datalist_i->tx==0 || (d->tm_hour*60+d->tm_min)==0 ) {
+                       e_rx = e_tx = 0;
+               } else {
+                       e_rx = ((datalist_i->rx) / (float)(d->tm_hour*60+d->tm_min)) * 1440;
+                       e_tx = ((datalist_i->tx) / (float)(d->tm_hour*60+d->tm_min)) * 1440;
+               }
+               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 (cfg.ostyle == 3) {
+                       printf(" |");
+               }
+               printf("\n");
+       }
+       dbdatalistfree(&datalist);
+}
+
+void showoneline(const interfaceinfo *interface)
 {
        struct tm *d;
        char daytemp[DATEBUFFLEN];
@@ -324,7 +441,7 @@ void showoneline(interfaceinfo *interface)
 
 int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int len)
 {
-       int i, l, width;
+       int i, l, width = len;
 
        if ( (rx + tx) < max) {
                width = ( (rx + tx) / (float)max ) * len;
@@ -339,7 +456,7 @@ int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int
        printf("  ");
 
        if (tx > rx) {
-               l=rintf((rx/(float)(rx+tx)*width));
+               l = rintf((rx/(float)(rx+tx)*width));
 
                for (i=0; i<l; i++) {
                        printf("%c", cfg.rxchar[0]);
@@ -348,8 +465,9 @@ int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int
                        printf("%c", cfg.txchar[0]);
                }
        } else {
-               l=rintf((tx/(float)(rx+tx)*width));
-                       for (i=0;i<(width-l);i++) {
+               l = rintf((tx/(float)(rx+tx)*width));
+
+               for (i=0; i<(width-l); i++) {
                        printf("%c", cfg.rxchar[0]);
                }
                for (i=0; i<l; i++) {
index bea96db0d2f0a0e6f96d659233014aede80d3252..9a46660baff0bbbe195a00b406ab138969975897 100644 (file)
@@ -4,8 +4,9 @@
 #define DATEBUFFLEN 64
 
 void showdb(const char *interface, int qmode);
-void showsummary(interfaceinfo *interface);
-void showoneline(interfaceinfo *interface);
+void showsummary(const interfaceinfo *interface);
+void showlist(const interfaceinfo *interface, const char *listname);
+void showoneline(const interfaceinfo *interface);
 int showbar(uint64_t rx, uint64_t tx, uint64_t max, const int len);
 void indent(int i);
 
index 2f72ada87d2747feae9968a29582227c43bb0017..35a1c45dfef467b5a9fdc548ab1fe8f401211fa1 100644 (file)
@@ -868,6 +868,8 @@ void updatelistinfo(dbdatalistinfo *listinfo, const uint64_t rx, const uint64_t
                listinfo->minrx = rx;
                listinfo->maxtx = tx;
                listinfo->mintx = tx;
+               listinfo->min = rx + tx;
+               listinfo->max = rx + tx;
        } else {
                if (timestamp > listinfo->maxtime) {
                        listinfo->maxtime = timestamp;
@@ -887,6 +889,12 @@ void updatelistinfo(dbdatalistinfo *listinfo, const uint64_t rx, const uint64_t
                if (tx > listinfo->maxtx) {
                        listinfo->maxtx = tx;
                }
+               if (rx + tx > listinfo->max) {
+                       listinfo->max = rx + tx;
+               }
+               if (rx + tx < listinfo->min) {
+                       listinfo->min = rx + tx;
+               }
        }
        listinfo->count++;
 }
index 02032425ebc107376abced20d460fd9c71ed7bd2..4e08024f0072b792860ac64df6c67aafb4306657 100644 (file)
@@ -19,6 +19,7 @@ typedef struct dbdatalistinfo {
        time_t maxtime, mintime;
        uint64_t minrx, mintx;
        uint64_t maxrx, maxtx;
+       uint64_t min, max;
 } dbdatalistinfo;
 
 typedef struct interfaceinfo {