From d99cd4610e8f18845455641bece8df064b04a73e Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 28 Sep 2024 16:11:01 +0000 Subject: [PATCH] stats: Return NULL if stats don't have any meaninhful data, yet Signed-off-by: Michael Tremer --- src/stats.c | 6 +++--- src/tui.c | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/stats.c b/src/stats.c index 1e95642..55c32ef 100644 --- a/src/stats.c +++ b/src/stats.c @@ -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; diff --git a/src/tui.c b/src/tui.c index 2dc4ffd..98c0c1c 100644 --- 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, '#'); } -- 2.47.2