]> git.stg.codes - stg.git/blob - include/stg/user_stat.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / include / stg / user_stat.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "user_traff.h"
24
25 #include <ctime>
26 #include <cstdint>
27 #include <map>
28 #include <utility>
29 #include <string>
30 #include <optional>
31
32 namespace STG
33 {
34 //-----------------------------------------------------------------------------
35 struct IPDirPair
36 {
37     #ifdef TRAFF_STAT_WITH_PORTS
38     IPDirPair(uint32_t _ip, int _dir, uint16_t _port) noexcept
39         : ip(_ip),
40           dir(_dir),
41           port(_port)
42     {}
43     #else
44     IPDirPair(uint32_t _ip, int _dir) noexcept
45         : ip(_ip),
46           dir(_dir)
47     {}
48     #endif
49     //------------------------
50     bool operator<(const IPDirPair& rhs) const noexcept
51     {
52         if (ip < rhs.ip)
53             return true;
54
55         if (ip > rhs.ip)
56             return false;
57
58         #ifdef TRAFF_STAT_WITH_PORTS
59         if (port < rhs.port)
60             return true;
61
62         if (port > rhs.port)
63             return false;
64         #endif
65
66         if (dir < rhs.dir)
67             return true;
68
69         return false;
70     }
71     //------------------------
72     bool operator==(const IPDirPair& rhs) const noexcept
73     {
74         #ifdef TRAFF_STAT_WITH_PORTS
75         return ip == rhs.ip && port == rhs.port && dir == rhs.dir;
76         #else
77         return ip == rhs.ip && dir == rhs.dir;
78         #endif
79     }
80     bool operator!=(const IPDirPair& rhs) const noexcept
81     {
82         return !operator==(rhs);
83     }
84
85     IPDirPair(const IPDirPair&) = default;
86     IPDirPair& operator=(const IPDirPair&) = default;
87     IPDirPair(IPDirPair&&) = default;
88     IPDirPair& operator=(IPDirPair&&) = default;
89     //------------------------
90     uint32_t ip;
91     int      dir;
92     #ifdef TRAFF_STAT_WITH_PORTS
93     uint16_t port;
94     #endif
95 };
96 //-----------------------------------------------------------------------------
97 struct StatNode
98 {
99     StatNode(uint64_t _up, uint64_t _down, double _cash) noexcept
100         : up(_up),
101           down(_down),
102           cash(_cash)
103     {}
104
105     StatNode(const StatNode&) = default;
106     StatNode& operator=(const StatNode&) = default;
107     StatNode(StatNode&&) = default;
108     StatNode& operator=(StatNode&&) = default;
109
110     uint64_t up;
111     uint64_t down;
112     double cash;
113 };
114 //-----------------------------------------------------------------------------
115 struct UserStat
116 {
117     UserStat() noexcept
118         : cash(0),
119           freeMb(0),
120           lastCashAdd(0),
121           lastCashAddTime(0),
122           passiveTime(0),
123           lastActivityTime(0)
124     {}
125
126     UserStat(const UserStat&) = default;
127     UserStat& operator=(const UserStat&) = default;
128     UserStat(UserStat&&) = default;
129     UserStat& operator=(UserStat&&) = default;
130
131     DirTraff sessionUp;
132     DirTraff sessionDown;
133     DirTraff monthUp;
134     DirTraff monthDown;
135     double   cash;
136     double   freeMb;
137     double   lastCashAdd;
138     time_t   lastCashAddTime;
139     time_t   passiveTime;
140     time_t   lastActivityTime;
141 };
142 //-----------------------------------------------------------------------------
143 using TraffStat = std::map<IPDirPair, StatNode>;
144 //-----------------------------------------------------------------------------
145 using CashInfo = std::pair<double, std::string>;
146 //-----------------------------------------------------------------------------
147 struct UserStatOpt
148 {
149     UserStatOpt() = default;
150
151     UserStatOpt(const UserStat& data) noexcept
152         : cash(data.cash),
153           freeMb(data.freeMb),
154           lastCashAdd(data.lastCashAdd),
155           lastCashAddTime(data.lastCashAddTime),
156           passiveTime(data.passiveTime),
157           lastActivityTime(data.lastActivityTime),
158           sessionUp(data.sessionUp),
159           sessionDown(data.sessionDown),
160           monthUp(data.monthUp),
161           monthDown(data.monthDown)
162     {}
163     UserStatOpt& operator=(const UserStat& us)
164     {
165         cash             = us.cash;
166         freeMb           = us.freeMb;
167         lastCashAdd      = us.lastCashAdd;
168         lastCashAddTime  = us.lastCashAddTime;
169         passiveTime      = us.passiveTime;
170         lastActivityTime = us.lastActivityTime;
171         sessionUp        = us.sessionUp;
172         sessionDown      = us.sessionDown;
173         monthUp          = us.monthUp;
174         monthDown        = us.monthDown;
175         return *this;
176     }
177
178     UserStatOpt(const UserStatOpt&) = default;
179     UserStatOpt& operator=(const UserStatOpt&) = default;
180     UserStatOpt(UserStatOpt&&) = default;
181     UserStatOpt& operator=(UserStatOpt&&) = default;
182
183     std::optional<double>    cash;
184     std::optional<CashInfo>  cashAdd;
185     std::optional<CashInfo>  cashSet;
186     std::optional<double>    freeMb;
187     std::optional<double>    lastCashAdd;
188     std::optional<time_t>    lastCashAddTime;
189     std::optional<time_t>    passiveTime;
190     std::optional<time_t>    lastActivityTime;
191     DirTraffOpt         sessionUp;
192     DirTraffOpt         sessionDown;
193     DirTraffOpt         monthUp;
194     DirTraffOpt         monthDown;
195 };
196 //-----------------------------------------------------------------------------
197 }