summaryrefslogtreecommitdiffhomepage
path: root/statistics.h
blob: c38ef666a483c8398e04cb53323b738f77bb409c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once

#include "libreichwein/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:
 bool mChanged{};
 std::deque<Bin> mBins;
 std::mutex mMutex;

 void load();
 void limit();

public:
 Statistics();
 ~Statistics();

 void count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, bool https);
 void save();

 std::string getValues();
};

// 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;
}

}