]> git.ipfire.org Git - fireperf.git/commitdiff
stats: Return NULL if stats don't have any meaninhful data, yet
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 16:11:01 +0000 (16:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 16:11:01 +0000 (16:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/stats.c
src/tui.c

index 1e95642ab8b86837a25d7c365828ddea7fc780aa..55c32efbc2e34482051c8d3d5a23730b48df96ad 100644 (file)
@@ -33,11 +33,11 @@ struct fireperf_stats fireperf_stats_step(
        struct fireperf_stats stats = {
                .t                    = new->t,
                .open_connections     = new->open_connections,
-               .connections          = new->connections - old->connections,
+               .connections          = new->connections - ((old) ? old->connections : 0),
                .total_bytes_received = new->total_bytes_received,
-               .bytes_received       = new->total_bytes_received - old->total_bytes_received,
+               .bytes_received       = new->total_bytes_received - ((old) ? old->total_bytes_received : 0),
                .total_bytes_sent     = new->total_bytes_sent,
-               .bytes_sent           = new->total_bytes_sent - old->total_bytes_sent,
+               .bytes_sent           = new->total_bytes_sent - ((old) ? old->total_bytes_sent : 0),
        };
 
        return stats;
index 2dc4ffd7a97c833a554f900bc3cfbeccfabac519..98c0c1c17e1740edced4f0e6ab905611cb40c6f5 100644 (file)
--- a/src/tui.c
+++ b/src/tui.c
@@ -95,7 +95,16 @@ void fireperf_tui_log(void* data, int priority, const char* file,
 }
 
 static const struct fireperf_stats* fireperf_tui_get_stats(struct fireperf_tui* tui, int i) {
-       return &tui->stats[(tui->s - i) % MAX_STATS];
+       // Select the i'th most recent index
+       i = (tui->s - i) % MAX_STATS;
+
+       const struct fireperf_stats* stats = &tui->stats[i];
+
+       // Return NULL if we have no meaningful data
+       if (!stats->total_bytes_received && !stats->total_bytes_sent)
+               return NULL;
+
+       return stats;
 }
 
 static size_t fireperf_tui_get_peak_bps(struct fireperf_tui* tui, int max) {
@@ -104,6 +113,8 @@ static size_t fireperf_tui_get_peak_bps(struct fireperf_tui* tui, int max) {
 
        for (unsigned int i = 0; i < max; i++) {
                stats = fireperf_tui_get_stats(tui, i);
+               if (!stats)
+                       continue;
 
                if (stats->bytes_sent > peak)
                        peak = stats->bytes_sent;
@@ -456,6 +467,8 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
        // Draw any sent traffic
        for (unsigned int x = 0; x < max_x; x++) {
                stats = fireperf_tui_get_stats(tui, x);
+               if (!stats)
+                       continue;
 
                mvwaddch(tui->graph, max_y - (stats->bytes_sent / step), x, '#');
        }
@@ -468,6 +481,8 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
        // Draw any received traffic
        for (unsigned int x = 0; x < max_x; x++) {
                stats = fireperf_tui_get_stats(tui, x);
+               if (!stats)
+                       continue;
 
                mvwaddch(tui->graph, max_y - (stats->bytes_received / step), x, '#');
        }