summaryrefslogtreecommitdiffhomepage
path: root/statistics.cpp
blob: fb7148bf6093ae6a28314f67b8a8d70401d2374a (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "statistics.h"

#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;
using namespace std::string_literals;

namespace {
 const fs::path default_statsfilepath{ "stats.db" };
} // anonymous namespace

void Statistics::load()
{
 std::lock_guard<std::mutex> lock(mMutex);
 std::cout << "Loading statistics..." << std::endl;
 std::ifstream file{mPath, std::ios::in | std::ios::binary};
 if (file.is_open()) {
  Reichwein::Serialization::IArchive archive{file};

  archive >> mBins;
 } else {
  std::cerr << "Warning: Couldn't read statistics" << std::endl;
 }

 mChanged = false;
}

void Statistics::save()
{
 if (mChanged) {
  std::lock_guard<std::mutex> lock(mMutex);
  std::cout << "Saving statistics..." << std::endl;
  std::ofstream file{mPath, std::ios::out | std::ios::binary | std::ios::trunc};
  if (file.is_open()) {
   Reichwein::Serialization::OArchive archive{file};

   archive << mBins;
  } else {
   std::cerr << "Warning: Couldn't write statistics" << std::endl;
  }

  mChanged = false;
 }
}

Statistics::Statistics(): mPath{default_statsfilepath}
{
 load();
}

Statistics::Statistics(const fs::path& path): mPath{path.empty() ? default_statsfilepath : path}
{
 load();
}

Statistics::~Statistics()
{
 save();
}

bool Statistics::Bin::expired() const
{
 uint64_t now {static_cast<uint64_t>(time(nullptr))};

 if (now < start_time)
  std::runtime_error("Statistics time is in the future");

 return start_time + binsize < now;
}

void Statistics::limit()
{
 while (mBins.size() * sizeof(Bin) > maxSize)
  mBins.pop_front(); // discard oldest element
}

void Statistics::count(size_t bytes_in, size_t bytes_out, bool error, bool ipv6, bool https)
{
 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});
 }

 Bin& bin{mBins.back()};

 bin.requests++;
 if (error) bin.errors++;
 bin.bytes_in += bytes_in;
 bin.bytes_out += bytes_out;
 
 if (ipv6) {
  bin.requests_ipv6++;
  if (error) bin.errors_ipv6++;
  bin.bytes_in_ipv6 += bytes_in;
  bin.bytes_out_ipv6 += bytes_out;
 }
 
 if (https) {
  bin.requests_https++;
  if (error) bin.errors_https++;
  bin.bytes_in_https += bytes_in;
  bin.bytes_out_https += bytes_out;
 }

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