]> git.stg.codes - stg.git/blob - include/stg/user_stat.h
Fixed merge issues.
[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  /*
22  $Revision: 1.15 $
23  $Date: 2010/03/11 14:42:05 $
24  $Author: faust $
25  */
26
27 #ifndef USER_STAT_H
28 #define USER_STAT_H
29
30 #include <ctime>
31 #include <map>
32 #include <utility>
33 #include <string>
34
35 #include "os_int.h"
36 #include "resetable.h"
37 #include "user_traff.h"
38 //-----------------------------------------------------------------------------
39 struct IP_DIR_PAIR
40 {
41     #ifdef TRAFF_STAT_WITH_PORTS
42     IP_DIR_PAIR(uint32_t _ip,
43                 int _dir,
44                 uint16_t _port)
45         : ip(_ip),
46           dir(_dir),
47           port(_port)
48     {}
49     #else
50     IP_DIR_PAIR(uint32_t _ip,
51                 int _dir)
52         : ip(_ip),
53           dir(_dir)
54     {}
55     #endif
56     //------------------------
57     bool operator<(const IP_DIR_PAIR & idp) const
58         {
59         if (ip < idp.ip)
60             return true;
61
62         if (ip > idp.ip)
63             return false;
64
65         #ifdef TRAFF_STAT_WITH_PORTS
66         if (port < idp.port)
67             return true;
68
69         if (port > idp.port)
70             return false;
71         #endif
72
73         if (dir < idp.dir)
74             return true;
75
76         return false;
77         }
78     //------------------------
79     bool operator!=(const IP_DIR_PAIR & rvalue) const
80         {
81         if (ip != rvalue.ip)
82             return true;
83
84         #ifdef TRAFF_STAT_WITH_PORTS
85         if (port != rvalue.port)
86             return true;
87         #endif
88
89         if (dir != rvalue.dir)
90             return true;
91
92         return false;
93         }
94     //------------------------
95     uint32_t        ip;
96     int             dir;
97     #ifdef TRAFF_STAT_WITH_PORTS
98     uint16_t        port;
99     #endif
100 };
101 //-----------------------------------------------------------------------------
102 struct STAT_NODE
103 {
104     STAT_NODE(uint64_t _up,
105               uint64_t _down,
106               double   _cash)
107         : up(_up),
108           down(_down),
109           cash(_cash)
110     {}
111     uint64_t        up;
112     uint64_t        down;
113     double          cash;
114 };
115 //-----------------------------------------------------------------------------
116 struct USER_STAT
117 {
118     USER_STAT()
119         : sessionUp(),
120           sessionDown(),
121           monthUp(),
122           monthDown(),
123           cash(0),
124           freeMb(0),
125           lastCashAdd(0),
126           lastCashAddTime(0),
127           passiveTime(0),
128           lastActivityTime(0)
129     {}
130
131     DIR_TRAFF   sessionUp;
132     DIR_TRAFF   sessionDown;
133     DIR_TRAFF   monthUp;
134     DIR_TRAFF   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 typedef std::map<IP_DIR_PAIR, STAT_NODE> TRAFF_STAT;
144 //-----------------------------------------------------------------------------
145 typedef std::pair<double, std::string> CASH_INFO;
146 //-----------------------------------------------------------------------------
147 struct USER_STAT_RES
148 {
149     USER_STAT_RES()
150         : cash(),
151           freeMb(),
152           lastCashAdd(),
153           lastCashAddTime(),
154           passiveTime(),
155           lastActivityTime(),
156           sessionUp(),
157           sessionDown(),
158           monthUp(),
159           monthDown()
160     {}
161
162     USER_STAT_RES & operator= (const USER_STAT & us)
163     {
164         cash             = us.cash;
165         freeMb           = us.freeMb;
166         lastCashAdd      = us.lastCashAdd;
167         lastCashAddTime  = us.lastCashAddTime;
168         passiveTime      = us.passiveTime;
169         lastActivityTime = us.lastActivityTime;
170         sessionUp        = us.sessionUp;
171         sessionDown      = us.sessionDown;
172         monthUp          = us.monthUp;
173         monthDown        = us.monthDown;
174         return *this;
175     }
176     USER_STAT GetData() const
177     {
178         USER_STAT us;
179         us.cash             = cash.data();
180         us.freeMb           = freeMb.data();
181         us.lastCashAdd      = lastCashAdd.data();
182         us.lastCashAddTime  = lastCashAddTime.data();
183         us.passiveTime      = passiveTime.data();
184         us.lastActivityTime = lastActivityTime.data();
185         us.sessionUp        = sessionUp.GetData();
186         us.sessionDown      = sessionDown.GetData();
187         us.monthUp          = monthUp.GetData();
188         us.monthDown        = monthDown.GetData();
189         return us;
190     }
191
192     RESETABLE<double>      cash;
193     RESETABLE<CASH_INFO>   cashAdd;
194     RESETABLE<CASH_INFO>   cashSet;
195     RESETABLE<double>      freeMb;
196     RESETABLE<double>      lastCashAdd;
197     RESETABLE<time_t>      lastCashAddTime;
198     RESETABLE<time_t>      passiveTime;
199     RESETABLE<time_t>      lastActivityTime;
200     DIR_TRAFF_RES          sessionUp;
201     DIR_TRAFF_RES          sessionDown;
202     DIR_TRAFF_RES          monthUp;
203     DIR_TRAFF_RES          monthDown;
204 };
205 //-----------------------------------------------------------------------------
206 #endif