From 63fc8e14be5e450df8ccc18fe76e02c5f0827660 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 7 Jan 2023 14:07:13 +0100 Subject: Test statistics --- config.cpp | 11 +++++++- config.h | 3 +++ debian/webserver.conf | 1 + server.cpp | 2 +- statistics.cpp | 13 ++++++--- statistics.h | 4 +++ tests/test-config.cpp | 1 + tests/test-statistics.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++-- tests/test-webserver.cpp | 1 + webserver.conf | 1 + 10 files changed, 98 insertions(+), 8 deletions(-) diff --git a/config.cpp b/config.cpp index 342d973..2b37f91 100644 --- a/config.cpp +++ b/config.cpp @@ -45,6 +45,8 @@ void Config::readConfigfile(const std::filesystem::path& filename) m_threads = tree.get("webserver.threads"); + m_statistics_path = tree.get("webserver.statisticspath", "/var/lib/webserver/stats.db"); + // optional entries auto elements = tree.get_child_optional("webserver"); if (elements) { @@ -226,6 +228,11 @@ int Config::Threads() const return m_threads; } +fs::path Config::statistics_path() const +{ + return m_statistics_path; +} + const std::vector& Config::PluginDirectories() const { return m_plugin_directories; @@ -245,10 +252,12 @@ void Config::dump() const { std::cout << "=== Configuration ===========================" << std::endl; std::cout << "User: " << m_user << std::endl; - std::cout << "Group: " << m_user << std::endl; + std::cout << "Group: " << m_group << std::endl; std::cout << "Threads: " << m_threads << std::endl; + std::cout << "Statistics Path: " << statistics_path() << std::endl; + std::cout << "Plugin Directories:"; for (const auto& dir: m_plugin_directories) std::cout << " " << dir; diff --git a/config.h b/config.h index d0e78ba..42a979e 100644 --- a/config.h +++ b/config.h @@ -52,6 +52,7 @@ class Config std::string m_user; std::string m_group; int m_threads; + std::filesystem::path m_statistics_path; std::vector m_plugin_directories; std::unordered_map m_sites; std::vector m_sockets; @@ -65,6 +66,8 @@ class Config int Threads() const; + std::filesystem::path statistics_path() const; + const std::vector& PluginDirectories() const; const std::unordered_map& Sites() const; const std::vector& Sockets() const; diff --git a/debian/webserver.conf b/debian/webserver.conf index f40e793..4a9e3af 100644 --- a/debian/webserver.conf +++ b/debian/webserver.conf @@ -1,6 +1,7 @@ www-data www-data + /var/lib/webserver/stats.db 10 /usr/lib/webserver/plugins diff --git a/server.cpp b/server.cpp index 51e80ba..5d9d7ea 100644 --- a/server.cpp +++ b/server.cpp @@ -59,7 +59,7 @@ Server::~Server() int run_server(Config& config, plugins_container_type& plugins) { - Statistics stats; + Statistics stats(config.statistics_path()); auto const threads = std::max(1, config.Threads()); diff --git a/statistics.cpp b/statistics.cpp index 1d67bfd..fb7148b 100644 --- a/statistics.cpp +++ b/statistics.cpp @@ -8,14 +8,14 @@ namespace fs = std::filesystem; using namespace std::string_literals; namespace { - const fs::path statsfilepath{ "/var/lib/webserver/stats.db" }; + const fs::path default_statsfilepath{ "stats.db" }; } // anonymous namespace void Statistics::load() { std::lock_guard lock(mMutex); std::cout << "Loading statistics..." << std::endl; - std::ifstream file{statsfilepath, std::ios::in | std::ios::binary}; + std::ifstream file{mPath, std::ios::in | std::ios::binary}; if (file.is_open()) { Reichwein::Serialization::IArchive archive{file}; @@ -32,7 +32,7 @@ void Statistics::save() 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}; + std::ofstream file{mPath, std::ios::out | std::ios::binary | std::ios::trunc}; if (file.is_open()) { Reichwein::Serialization::OArchive archive{file}; @@ -45,7 +45,12 @@ void Statistics::save() } } -Statistics::Statistics() +Statistics::Statistics(): mPath{default_statsfilepath} +{ + load(); +} + +Statistics::Statistics(const fs::path& path): mPath{path.empty() ? default_statsfilepath : path} { load(); } diff --git a/statistics.h b/statistics.h index a8b4854..f2ebcd3 100644 --- a/statistics.h +++ b/statistics.h @@ -1,5 +1,7 @@ #pragma once +#include "config.h" + #include "libreichwein/archive.h" #include @@ -63,12 +65,14 @@ private: bool mChanged{}; std::deque mBins; std::mutex mMutex; + std::filesystem::path mPath; void load(); void limit(); public: Statistics(); + Statistics(const std::filesystem::path& path); ~Statistics(); void count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, bool https); diff --git a/tests/test-config.cpp b/tests/test-config.cpp index c16c519..fe482f8 100644 --- a/tests/test-config.cpp +++ b/tests/test-config.cpp @@ -41,6 +41,7 @@ BOOST_FIXTURE_TEST_CASE(config, ConfigFixture) user1 www-data 10 + stats.db