summaryrefslogtreecommitdiffhomepage
path: root/statistics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'statistics.cpp')
-rw-r--r--statistics.cpp69
1 files changed, 58 insertions, 11 deletions
diff --git a/statistics.cpp b/statistics.cpp
index 19d0258..3fb99a3 100644
--- a/statistics.cpp
+++ b/statistics.cpp
@@ -5,13 +5,15 @@
#include <iostream>
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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(mMutex);
+ mChanged = true;
+
if (mBins.empty() || mBins.back().expired()) {
mBins.emplace_back(Bin{static_cast<uint64_t>((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<std::mutex> 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;
+}