#include "ratio.hpp" namespace ratio { Ledger::Ledger() : channels_{} { } bool Ledger::has_slot(int slot) { return slot >= 0 && slot < channel_count; } Configuration Ledger::configure(int slot, const Profile& profile) { Configuration response; if (!has_slot(slot)) { response.result = Result::bad_slot; return response; } if (!profile.valid()) { response.result = Result::bad_value; return response; } Channel& channel = channels_[static_cast(slot)]; channel.stats.clear(); channel.profile = profile; channel.configured = true; ++channel.generation; response.generation = channel.generation; return response; } Feed Ledger::feed(int slot, std::int64_t raw, std::int64_t weight) { Feed response; if (!has_slot(slot)) { response.result = Result::bad_slot; return response; } Channel& channel = channels_[static_cast(slot)]; if (!channel.configured) { response.result = Result::missing; return response; } if (raw < -raw_limit || raw > raw_limit || weight < 1 || weight > 1000) { response.result = Result::bad_value; return response; } if (channel.stats.samples >= sample_limit) { response.result = Result::full; return response; } const Mapped value = map_sample(channel.profile, raw); channel.stats.add(raw, value, static_cast(weight)); response.mapped = value.value; response.weight = static_cast(weight); return response; } Snapshot Ledger::snapshot(int slot) const { Snapshot response; if (!has_slot(slot)) { response.result = Result::bad_slot; return response; } const Channel& channel = channels_[static_cast(slot)]; response.generation = channel.generation; if (!channel.configured) { response.result = Result::missing; return response; } response.stats = channel.stats; return response; } Configuration Ledger::drop(int slot) { Configuration response; if (!has_slot(slot)) { response.result = Result::bad_slot; return response; } Channel& channel = channels_[static_cast(slot)]; response.generation = channel.generation; if (!channel.configured) { response.result = Result::missing; return response; } channel.stats.clear(); channel.profile = Profile{}; channel.configured = false; ++channel.generation; response.generation = channel.generation; return response; } Configuration Ledger::clear(int slot) { Configuration response; if (!has_slot(slot)) { response.result = Result::bad_slot; return response; } Channel& channel = channels_[static_cast(slot)]; response.generation = channel.generation; if (!channel.configured) { response.result = Result::missing; return response; } channel.stats.clear(); return response; } Totals Ledger::totals() const { Totals response; for (const Channel& channel : channels_) { if (!channel.configured) { continue; } ++response.configured; response.stats.samples += channel.stats.samples; response.stats.weight += channel.stats.weight; response.stats.raw_total += channel.stats.raw_total; response.stats.mapped_total += channel.stats.mapped_total; response.stats.clipped += channel.stats.clipped; } return response; } }