-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics.cpp
More file actions
168 lines (138 loc) · 4.84 KB
/
Statistics.cpp
File metadata and controls
168 lines (138 loc) · 4.84 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#define _CRT_SECURE_NO_WARNINGS
#include "Statistics.h"
#include <algorithm>
#include <ctime>
using namespace std;
void Statistics::recordTransaction(const std::string& sender, const std::string& receiver, double amount) {
// Update transaction counts
userTransactionCount[sender]++;
userTransactionCount[receiver]++;
// Update sorted transaction counts
for (const auto& user : { sender, receiver }) {
int count = userTransactionCount[user];
// Remove from old count if exists
for (auto& [c, users] : transactionCountToUsers) {
users.erase(user);
if (users.empty()) {
transactionCountToUsers.erase(c);
}
}
// Add to new count
transactionCountToUsers[count].insert(user);
}
// Record large transactions
std::string transactionInfo = sender + " -> " + receiver;
topTransactions.push({ amount, transactionInfo });
// Record user interaction
userInteractions[sender][receiver]++;
userInteractions[receiver][sender]++;
// Record hourly activity
std::time_t now = std::time(nullptr);
struct tm timeinfo;
localtime_s(&timeinfo, &now);
hourlyTransactionCount[timeinfo.tm_hour]++;
}
void Statistics::recordLogin(const std::string& username) {
std::time_t now = std::time(nullptr);
// Add to login history
loginHistory.emplace_back(now, username);
// Update user's login times
auto& userQueue = userLoginTimes[username];
userQueue.push(now);
// Keep only last 10 logins
if (userQueue.size() > 10) {
userQueue.pop();
}
}
void Statistics::recordFailedOperation(const std::string& username, const std::string& error) {
failedOperations[username].push_back(error);
}
std::vector<std::string> Statistics::getMostActiveUsers(int count) const {
std::vector<std::string> result;
// Iterate through transaction counts in reverse (highest first)
for (auto it = transactionCountToUsers.rbegin();
it != transactionCountToUsers.rend() && result.size() < count;
++it) {
for (const auto& user : it->second) {
if (result.size() < count) {
result.push_back(user);
}
else {
break;
}
}
}
return result;
}
std::vector<std::pair<double, std::string>> Statistics::getLargestTransactions(int count) const {
std::vector<std::pair<double, std::string>> result;
auto tempQueue = topTransactions;
while (!tempQueue.empty() && result.size() < count) {
result.push_back(tempQueue.top());
tempQueue.pop();
}
return result;
}
std::vector<std::pair<std::string, std::string>> Statistics::getMostFrequentInteractions() const {
std::vector<std::pair<std::string, std::string>> result;
std::vector<std::tuple<int, std::string, std::string>> interactions;
// Collect all interactions
for (const auto& [user1, userMap] : userInteractions) {
for (const auto& [user2, count] : userMap) {
if (user1 < user2) { // Avoid duplicates
interactions.emplace_back(count, user1, user2);
}
}
}
// Sort by count
std::sort(interactions.rbegin(), interactions.rend());
// Take top 5 interactions
for (const auto& [count, user1, user2] : interactions) {
if (result.size() >= 5) break;
result.emplace_back(user1, user2);
}
return result;
}
int Statistics::getPeakActivityHour() const {
if (hourlyTransactionCount.empty()) return -1;
return std::max_element(
hourlyTransactionCount.begin(),
hourlyTransactionCount.end(),
[](const auto& p1, const auto& p2) {
return p1.second < p2.second;
}
)->first;
}
std::vector<std::string> Statistics::getUserErrors(const std::string& username) const {
auto it = failedOperations.find(username);
if (it != failedOperations.end()) {
return it->second;
}
return {};
}
void Statistics::clearOldHistory(std::time_t before) {
// Clear old login history
loginHistory.erase(
std::remove_if(
loginHistory.begin(),
loginHistory.end(),
[before](const auto& entry) {
return entry.first < before;
}
),
loginHistory.end()
);
// Clear old login times from user queues
for (auto& [username, times] : userLoginTimes) {
while (!times.empty() && times.front() < before) {
times.pop();
}
}
}
void Statistics::pruneLoginHistory(int maxPerUser) {
for (auto& [username, times] : userLoginTimes) {
while (times.size() > maxPerUser) {
times.pop();
}
}
}