/**
 * @file UDPSourceStats.h
 * @brief Per-source statistics snapshot for StreamHub (Go hub StatInfo parity).
 */

#ifndef STREAMHUB_UDP_SOURCE_STATS_H_
#define STREAMHUB_UDP_SOURCE_STATS_H_

#include "CompilerTypes.h"
#include "StreamString.h"

namespace StreamHub {

using MARTe::uint32;
using MARTe::uint64;
using MARTe::float64;
using MARTe::StreamString;

/** Number of bins in the cycle-time histogram. */
static const uint32 UDPS_STAT_HIST_BINS = 20u;

/**
 * @brief Statistics snapshot for one UDPStreamer source.
 *
 * Field set mirrors the Go hub's StatInfo (stats.go) — same JSON names are
 * emitted by StreamHub::PushStats — plus a connection state string.
 */
struct UDPSourceStats {

    UDPSourceStats();

    uint64  totalReceived;   ///< Total complete DATA packets received
    uint64  totalLost;       ///< Estimated lost DATA packets (counter gaps)

    float64 rateHz;          ///< Mean cycle rate (1/avg cycle time)
    float64 rateStdHz;       ///< Std-dev of the cycle rate
    float64 fragsPerCycle;   ///< Mean UDP datagrams per DATA cycle
    float64 bytesPerCycle;   ///< Mean raw bytes per DATA cycle

    float64 cycleAvgMs;      ///< Mean cycle time (ms)
    float64 cycleStdMs;      ///< Std-dev of cycle time (ms)
    float64 cycleMinMs;      ///< Min cycle time in window (ms)
    float64 cycleMaxMs;      ///< Max cycle time in window (ms)

    uint32  cycleHist[UDPS_STAT_HIST_BINS]; ///< Cycle-time histogram counts
    float64 cycleHistMin;    ///< Histogram lower bound (ms)
    float64 cycleHistMax;    ///< Histogram upper bound (ms)
    bool    histValid;       ///< True if the histogram has data

    StreamString state;      ///< "disconnected" | "connecting" | "connected" | "error"
};

inline UDPSourceStats::UDPSourceStats()
    : totalReceived(0u),
      totalLost(0u),
      rateHz(0.0),
      rateStdHz(0.0),
      fragsPerCycle(0.0),
      bytesPerCycle(0.0),
      cycleAvgMs(0.0),
      cycleStdMs(0.0),
      cycleMinMs(0.0),
      cycleMaxMs(0.0),
      cycleHistMin(0.0),
      cycleHistMax(0.0),
      histValid(false) {
    for (uint32 i = 0u; i < UDPS_STAT_HIST_BINS; i++) {
        cycleHist[i] = 0u;
    }
    state = "disconnected";
}

} /* namespace StreamHub */

#endif /* STREAMHUB_UDP_SOURCE_STATS_H_ */
