From edf6ce7bc75c2fc09135a9b21f8149b8fc68b2ef Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Mon, 26 Feb 2018 22:58:01 +0200 Subject: [PATCH] add options for controlling the number of decimals used in outputs, reduce the default number of decimals used in hourly output from 2 to 1 in order to improve readability --- cfg/vnstat.conf | 4 ++++ man/vnstat.conf.5 | 10 ++++++++++ src/cfg.c | 20 ++++++++++++++++++++ src/common.h | 5 +++++ src/dbshow.c | 2 +- src/misc.c | 4 ++-- tests/misc_tests.c | 12 ++++++++++++ 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/cfg/vnstat.conf b/cfg/vnstat.conf index 66bb3cc..2873960 100644 --- a/cfg/vnstat.conf +++ b/cfg/vnstat.conf @@ -44,6 +44,10 @@ OutputStyle 3 # used rate unit (0 = bytes, 1 = bits) RateUnit 1 +# number of decimals to use in outputs +DefaultDecimals 2 +HourlyDecimals 1 + # try to detect interface maximum bandwidth, 0 = disable feature # MaxBandwidth will be used as fallback value when enabled BandwidthDetection 1 diff --git a/man/vnstat.conf.5 b/man/vnstat.conf.5 index 8005ba6..f31f990 100644 --- a/man/vnstat.conf.5 +++ b/man/vnstat.conf.5 @@ -62,6 +62,16 @@ Formatting of date in available outputs. Uses the same format as .BR date (1). (vnstat and vnstati only) +.TP +.B DefaultDecimals +Number of decimals to use in outputs. Value range: 0..2 +(vnstat and vnstati only) + +.TP +.B HourlyDecimals +Number of decimals to use in hourly output. Value range: 0..2 +(vnstat only) + .TP .B Interface Default interface used when no other interface is specified on diff --git a/src/cfg.c b/src/cfg.c index 272e110..81e4582 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -51,6 +51,10 @@ void printcfgfile(void) printf("# used rate unit (0 = bytes, 1 = bits)\n"); printf("RateUnit %d\n\n", cfg.rateunit); + printf("# number of decimals to use in outputs\n"); + printf("DefaultDecimals %d\n", cfg.defaultdecimals); + printf("HourlyDecimals %d\n\n", cfg.hourlydecimals); + printf("# try to detect interface maximum bandwidth, 0 = disable feature\n"); printf("# MaxBandwidth will be used as fallback value when enabled\n"); printf("BandwidthDetection %d\n\n", cfg.bwdetection); @@ -196,6 +200,8 @@ int loadcfg(const char *cfgfile) { "RateUnitMode", 0, &cfg.rateunitmode, 0, 0 }, { "OutputStyle", 0, &cfg.ostyle, 0, 0 }, { "RateUnit", 0, &cfg.rateunit, 0, 0 }, + { "DefaultDecimals", 0, &cfg.defaultdecimals, 0, 0 }, + { "HourlyDecimals", 0, &cfg.hourlydecimals, 0, 0 }, { "BandwidthDetection", 0, &cfg.bwdetection, 0, 0 }, { "MaxBandwidth", 0, &cfg.maxbw, 0, 0 }, { "Sampletime", 0, &cfg.sampletime, 0, 0 }, @@ -320,6 +326,18 @@ void validatecfg(void) printe(PT_Config); } + if (cfg.defaultdecimals<0 || cfg.defaultdecimals>2) { + cfg.defaultdecimals = DEFAULTDECIMALS; + snprintf(errorstring, 512, "Invalid value for DefaultDecimals, resetting to \"%d\".", cfg.defaultdecimals); + printe(PT_Config); + } + + if (cfg.hourlydecimals<0 || cfg.hourlydecimals>2) { + cfg.hourlydecimals = HOURLYDECIMALS; + snprintf(errorstring, 512, "Invalid value for HourlyDecimals, resetting to \"%d\".", cfg.hourlydecimals); + printe(PT_Config); + } + if (cfg.bvar<0 || cfg.bvar>300) { cfg.bvar = BVAR; snprintf(errorstring, 512, "Invalid value for BootVariation, resetting to \"%d\".", cfg.bvar); @@ -495,6 +513,8 @@ void defaultcfg(void) cfg.rateunitmode = RATEUNITMODE; cfg.ostyle = OSTYLE; cfg.rateunit = RATEUNIT; + cfg.defaultdecimals = DEFAULTDECIMALS; + cfg.hourlydecimals = HOURLYDECIMALS; cfg.bwdetection = BWDETECT; cfg.bwdetectioninterval = BWDETECTINTERVAL; cfg.maxbw = DEFMAXBW; diff --git a/src/common.h b/src/common.h index 3dae00b..73c60fd 100644 --- a/src/common.h +++ b/src/common.h @@ -102,6 +102,10 @@ and most can be changed later from the config file. /* 0 = bytes, 1 = bits */ #define RATEUNIT 1 +/* number of decimals */ +#define DEFAULTDECIMALS 2 +#define HOURLYDECIMALS 1 + /* default interface */ #ifndef DEFIFACE #define DEFIFACE "eth0" @@ -237,6 +241,7 @@ typedef struct { char cline[8], clinel[8], cvnstat[8], crx[8], crxd[8], ctx[8], ctxd[8]; int32_t unitmode, rateunitmode, rateunit, bvar, qmode, sampletime, hourlyrate, summaryrate; int32_t monthrotate, maxbw, flock, spacecheck, traflessday, transbg, slayout, ostyle; + int32_t defaultdecimals, hourlydecimals; char cfgfile[512], logfile[512], pidfile[512]; char daemonuser[33], daemongroup[33]; int32_t timesyncwait, updateinterval, pollinterval, saveinterval, offsaveinterval, savestatus; diff --git a/src/dbshow.c b/src/dbshow.c index 1a3cf69..3d3aea3 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -842,7 +842,7 @@ void showweeks(void) void showhours(void) { - int i, k, s=0, hour, minute, declen=2, div=1; + int i, k, s=0, hour, minute, declen = cfg.hourlydecimals, div=1; unsigned int j, tmax=0, dots=0; uint64_t max=1; char matrix[24][81]; /* width is one over 80 so that snprintf can write the end char */ diff --git a/src/misc.c b/src/misc.c index 84c22ce..302526c 100644 --- a/src/misc.c +++ b/src/misc.c @@ -199,7 +199,7 @@ uint64_t getbtime(void) char *getvalue(uint64_t mb, uint64_t kb, int len, int type) { static char buffer[64]; - int declen=2; + int declen = cfg.defaultdecimals; uint64_t kB; /* request types: 1) normal 2) estimate 3) image scale */ @@ -248,7 +248,7 @@ char *getrate(uint64_t mb, uint64_t kb, uint32_t interval, int len) char *gettrafficrate(uint64_t bytes, uint32_t interval, int len) { static char buffer[64]; - int unitmode, declen = 2; + int unitmode, declen = cfg.defaultdecimals; uint64_t b; if (interval==0) { diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 3bad42a..cfe86f2 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -75,6 +75,7 @@ END_TEST START_TEST(getvalue_normal) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; ck_assert_str_eq(getvalue(0, 100, 0, 1), "100 KiB"); ck_assert_str_eq(getvalue(1, 0, 0, 1), "1.00 MiB"); @@ -90,6 +91,7 @@ END_TEST START_TEST(getvalue_estimate) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; ck_assert_str_eq(getvalue(0, 100, 0, 2), "100 KiB"); ck_assert_str_eq(getvalue(1, 0, 0, 2), "1 MiB"); @@ -105,6 +107,7 @@ END_TEST START_TEST(getvalue_imagescale) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; ck_assert_str_eq(getvalue(0, 100, 0, 3), "100 KiB"); ck_assert_str_eq(getvalue(1, 0, 0, 3), "1 MiB"); @@ -129,6 +132,7 @@ END_TEST START_TEST(getvalue_mb_kb_mixed) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; ck_assert_str_eq(getvalue(1, 3210, 0, 1), "4.13 MiB"); cfg.unitmode = 1; @@ -165,6 +169,7 @@ END_TEST START_TEST(getrate_bytes) { + cfg.defaultdecimals = 2; cfg.rateunit = 0; cfg.unitmode = 0; ck_assert_str_eq(getrate(0, 100, 1, 0), "100.00 KiB/s"); @@ -181,6 +186,7 @@ END_TEST START_TEST(getrate_bits) { + cfg.defaultdecimals = 2; cfg.rateunit = 1; cfg.rateunitmode = 1; @@ -211,6 +217,7 @@ END_TEST START_TEST(getrate_interval_divides) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; cfg.rateunitmode = 1; cfg.rateunit = 0; @@ -226,6 +233,7 @@ END_TEST START_TEST(getrate_padding) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; cfg.rateunitmode = 1; cfg.rateunit = 0; @@ -251,6 +259,7 @@ END_TEST START_TEST(gettrafficrate_bytes) { + cfg.defaultdecimals = 2; cfg.rateunit = 0; cfg.unitmode = 0; ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s"); @@ -267,6 +276,7 @@ END_TEST START_TEST(gettrafficrate_bits) { + cfg.defaultdecimals = 2; cfg.rateunit = 1; cfg.rateunitmode = 1; @@ -297,6 +307,7 @@ END_TEST START_TEST(gettrafficrate_interval_divides) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; cfg.rateunitmode = 1; cfg.rateunit = 0; @@ -312,6 +323,7 @@ END_TEST START_TEST(gettrafficrate_padding) { + cfg.defaultdecimals = 2; cfg.unitmode = 0; cfg.rateunit = 0; ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s"); -- 2.40.0