summaryrefslogtreecommitdiffhomepage
path: root/statistics.h
diff options
context:
space:
mode:
Diffstat (limited to 'statistics.h')
-rw-r--r--statistics.h110
1 files changed, 110 insertions, 0 deletions
diff --git a/statistics.h b/statistics.h
new file mode 100644
index 0000000..c4fce93
--- /dev/null
+++ b/statistics.h
@@ -0,0 +1,110 @@
+#pragma once
+
+#include "archive.h"
+
+#include <cstdint>
+#include <ctime>
+#include <deque>
+#include <iostream>
+#include <mutex>
+
+class Statistics
+{
+
+ static const int32_t binsize = 3600; // in seconds: i.e. 1 hour
+ static const size_t maxSize = 30000000; // maximum of statistics data in bytes
+
+public:
+ struct Bin
+ {
+ uint64_t start_time{};
+
+ uint64_t requests{};
+ uint64_t errors{};
+ uint64_t bytes_in{};
+ uint64_t bytes_out{};
+
+ uint64_t requests_ipv6{};
+ uint64_t errors_ipv6{};
+ uint64_t bytes_in_ipv6{};
+ uint64_t bytes_out_ipv6{};
+
+ uint64_t requests_https{};
+ uint64_t errors_https{};
+ uint64_t bytes_in_https{};
+ uint64_t bytes_out_https{};
+
+ template <class Archive>
+ void serialize (Archive& ar)
+ {
+ ar & start_time;
+
+ ar & requests;
+ ar & errors;
+ ar & bytes_in;
+ ar & bytes_out;
+
+ ar & requests_ipv6;
+ ar & errors_ipv6;
+ ar & bytes_in_ipv6;
+ ar & bytes_out_ipv6;
+
+ ar & requests_https;
+ ar & errors_https;
+ ar & bytes_in_https;
+ ar & bytes_out_https;
+ }
+
+ bool expired() const;
+
+ };
+
+private:
+ std::deque<Bin> mBins;
+ std::mutex mMutex;
+
+ void limit();
+
+public:
+ Statistics();
+ ~Statistics();
+
+ void count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, bool https);
+};
+
+// Serialization and Deserialization as free functions
+namespace Serialization {
+
+template <class T>
+Serialization::OArchive& operator& (Serialization::OArchive& ar, std::deque<T>& deque)
+{
+ uint64_t size { deque.size() };
+
+ ar & size;
+
+ for (auto element: deque) {
+ ar & element;
+ }
+
+ return ar;
+}
+
+template <class T>
+Serialization::IArchive& operator& (Serialization::IArchive& ar, std::deque<T>& deque)
+{
+ uint64_t size {};
+
+ ar & size;
+
+ deque.clear();
+
+ for (size_t i = 0; i < size; i++) {
+ T element;
+ ar & element;
+ deque.push_back(element);
+ }
+
+ return ar;
+}
+
+}