#ifndef PROTOCOLSTATS_HPP #define PROTOCOLSTATS_HPP #include "../packet/packet.hpp" #include "ftxui/dom/elements.hpp" #include #include #include #include #include struct protocolStats { uint32_t packets = 0; uint32_t bytes = 0; }; struct trafficStats { uint32_t total_packets = 0; uint32_t total_bytes = 0; }; struct IPStats { uint32_t bytes_sent = 0; uint32_t bytes_received = 0; uint32_t packets_sent = 0; uint32_t packets_received = 0; }; struct BandwidthPoint { double timestamp; double bytes_per_sec; }; struct StatsSnapshot { std::vector> transport_rows; std::vector> app_rows; std::vector> rows; std::vector> pairs_rows; std::vector> packets_rows; uint32_t total_p = 0, total_b = 0; // bandwidth std::vector bandwidth_history; double bandwidth = 0; double max_bandwidth = 0; }; /** * @brief Thread-safe statistics engine. * * Responsible for: * - Aggregating packet statistics * - Maintaining protocol distributions * - Tracking IP traffic * - Calculating bandwidth * - Providing snapshot for UI rendering * * All write operations are protected by mutex. */ class Stats { private: std::mutex mtx; uint32_t last_b = 0; std::chrono::steady_clock::time_point last_tick; std::unordered_map transport_map; std::unordered_map application_map; std::unordered_map ip_map; std::map, protocolStats> pairs; std::deque packets; int limit_packets = 10; StatsSnapshot snapshot; public: void push(const Packet &p) { std::lock_guard lock(mtx); if (packets.size() > static_cast(limit_packets)) { packets.pop_front(); } packets.push_back(p); } StatsSnapshot get_snapshot() { std::lock_guard lock(mtx); return snapshot; } void update_bandwidth(); double smooth_value(size_t i, size_t start); double smooth_bandwidth = 0.0; void set_packets_limit(int limit) { limit_packets = limit; } void add_packet(const Packet &packet); void update_transport_stats(); void update_application_stats(); void update_ip_stats(size_t limit); void update_pairs(size_t limit = 10); void update_packets(); void export_csv(const std::string &filename); void export_json(const std::string &filename); Stats(); }; #endif // PROTOCOLSTATS_HPP