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;
}
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) {
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;
// 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, '#');
}
// 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, '#');
}