From aebe139d00b44684158edb3616da5c37b12db6d1 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 26 Apr 2020 13:01:19 +0200 Subject: Added stats output --- statistics.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 11 deletions(-) (limited to 'statistics.cpp') diff --git a/statistics.cpp b/statistics.cpp index 19d0258..3fb99a3 100644 --- a/statistics.cpp +++ b/statistics.cpp @@ -5,13 +5,15 @@ #include namespace fs = std::filesystem; +using namespace std::string_literals; namespace { const fs::path statsfilepath{ "/var/lib/webserver/stats.db" }; } // anonymous namespace -Statistics::Statistics() +void Statistics::load() { + std::lock_guard lock(mMutex); std::cout << "Loading statistics..." << std::endl; std::ifstream file{statsfilepath, std::ios::in | std::ios::binary}; if (file.is_open()) { @@ -21,22 +23,38 @@ Statistics::Statistics() } else { std::cerr << "Warning: Couldn't read statistics" << std::endl; } + + mChanged = false; } -Statistics::~Statistics() +void Statistics::save() { - std::cout << "Saving statistics..." << std::endl; - std::lock_guard lock(mMutex); - std::ofstream file{statsfilepath, std::ios::out | std::ios::binary | std::ios::trunc}; - if (file.is_open()) { - Serialization::OArchive archive{file}; - - archive << mBins; - } else { - std::cerr << "Warning: Couldn't write statistics" << std::endl; + if (mChanged) { + std::lock_guard lock(mMutex); + std::cout << "Saving statistics..." << std::endl; + std::ofstream file{statsfilepath, std::ios::out | std::ios::binary | std::ios::trunc}; + if (file.is_open()) { + Serialization::OArchive archive{file}; + + archive << mBins; + } else { + std::cerr << "Warning: Couldn't write statistics" << std::endl; + } + + mChanged = false; } } +Statistics::Statistics() +{ + load(); +} + +Statistics::~Statistics() +{ + save(); +} + bool Statistics::Bin::expired() const { auto now {time(nullptr)}; @@ -57,6 +75,8 @@ void Statistics::count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, { std::lock_guard lock(mMutex); + mChanged = true; + if (mBins.empty() || mBins.back().expired()) { mBins.emplace_back(Bin{static_cast((time(nullptr) / binsize) * binsize), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); } @@ -85,3 +105,30 @@ void Statistics::count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, limit(); } +std::string Statistics::getValues() +{ + std::lock_guard lock(mMutex); + + std::string result; + + for (const auto& bin: mBins) { + result += std::to_string(bin.start_time) + ","s + + + std::to_string(bin.requests) + ","s + + std::to_string(bin.errors) + ","s + + std::to_string(bin.bytes_in) + ","s + + std::to_string(bin.bytes_out) + ","s + + + std::to_string(bin.requests_ipv6) + ","s + + std::to_string(bin.errors_ipv6) + ","s + + std::to_string(bin.bytes_in_ipv6) + ","s + + std::to_string(bin.bytes_out_ipv6) + ","s + + + std::to_string(bin.requests_https) + ","s + + std::to_string(bin.errors_https) + ","s + + std::to_string(bin.bytes_in_https) + ","s + + std::to_string(bin.bytes_out_https) + "\n"s; + } + + return result; +} -- cgit v1.2.3