Rabbit Remote Control 0.1.0-bate10
Loading...
Searching...
No Matches
Stats.cpp
1// Author: Kang Lin <kl222@126.com>
2#include "Stats.h"
3
4Q_DECLARE_METATYPE(CSecurityLevel::Levels)
5
6CStats::CStats(CParameterOperate *parent, const QString &szPrefix)
7 : CParameterOperate{parent}
8 , m_tmInterval(1)
9 , m_lastTime(QDateTime::currentDateTime())
10 , m_dbSendRate(0)
11 , m_dbReceiveRate(0)
12{
13 SetInterval();
14}
15
16QString CStats::Convertbytes(quint64 bytes)
17{
18 QString szBytes;
19 if((1 << 10) >= bytes)
20 szBytes = QString::number(bytes) + " " + tr("B");
21 else if((1 << 20) >= bytes)
22 szBytes = QString::number((qreal)bytes / (1 << 10), 'f', 2) + " " + tr("KB");
23 else if((1 << 30) >= bytes)
24 szBytes = QString::number((qreal)bytes / (1 << 20), 'f', 2) + " " + tr("MB");
25 else
26 szBytes = QString::number((qreal)bytes / (1 << 30), 'f', 2) + " " + tr("GB");
27 return szBytes;
28}
29
30QString CStats::TotalSends()
31{
32 return Convertbytes(GetTotalSends());
33}
34
35QString CStats::TotalReceives()
36{
37 return Convertbytes(GetTotalReceives());
38}
39
40quint64 CStats::GetTotalSends()
41{
42 return m_TotalSends;
43}
44
45quint64 CStats::GetTotalReceives()
46{
47 return m_TotalReceives;
48}
49
50void CStats::AddSends(quint64 size)
51{
52 m_TotalSends += size;
53 if(GetSendRate() <= 0)
54 emit sigDataChanged();
55}
56
57void CStats::AddReceives(quint64 size)
58{
59 m_TotalReceives += size;
60 if(GetReceiveRate() <= 0)
61 emit sigDataChanged();
62}
63
64QString CStats::SendRate()
65{
66 return Convertbytes(GetSendRate()) + "/" + tr("S");
67}
68
69QString CStats::ReceiveRate()
70{
71 return Convertbytes(GetReceiveRate()) + "/" + tr("S");
72}
73
75{
76 return m_dbSendRate;
77}
78
79double CStats::GetReceiveRate()
80{
81 return m_dbReceiveRate;
82}
83
85{
86 return m_tmInterval;
87}
88
89int CStats::SetInterval(int interval)
90{
91 if(m_tmInterval == interval)
92 return m_tmInterval;
93 int old = interval;
94 m_tmInterval = interval;
95 SetModified(true);
96 return old;
97}
98
100{
101 qint64 interval = m_lastTime.msecsTo(QDateTime::currentDateTime());
102
103 if(interval > 0) {
104 m_dbSendRate = (double)(m_TotalSends - m_lastSends) * 1000 / interval;
105 m_dbReceiveRate = (double)(m_TotalReceives - m_lastReceives) * 1000 / interval;
106 }
107
108 if(interval >= GetInterval() * 1000) {
109 m_lastSends = m_TotalSends;
110 m_lastReceives = m_TotalReceives;
111 m_lastTime = QDateTime::currentDateTime();
112 }
113}
114
115int CStats::OnLoad(QSettings &set)
116{
117 return 0;
118}
119
120int CStats::OnSave(QSettings &set)
121{
122 return 0;
123}
124
125CStatsSever::CStatsSever(CParameterOperate *parent, const QString &szPrefix)
126 : CStats(parent, szPrefix)
127{
128}
129
130QString CStatsSever::ToString() const
131{
132 return tr("Connect count: Current: %1; Disconnect: %2; Total: %3")
133 .arg(GetTotalConnects() - GetDisconnects())
134 .arg(GetDisconnects()).arg(GetTotalConnects());
135}
136
137quint64 CStatsSever::GetTotalConnects() const
138{
139 return m_TotalConnects;
140}
141
142quint64 CStatsSever::GetConnects() const
143{
144 return m_TotalConnects - m_Disconnects;
145}
146
147quint64 CStatsSever::GetDisconnects() const
148{
149 return m_Disconnects;
150}
151
152void CStatsSever::AddConnects(quint64 c)
153{
154 m_TotalConnects += c;
155}
156
157void CStatsSever::Disconnects(quint64 c)
158{
159 m_Disconnects += c;
160}
161
162CSecurityLevel::CSecurityLevel(Levels level, QObject* parent)
163 : QObject(parent)
164 , m_Level(level)
165{
166}
167
168CSecurityLevel::~CSecurityLevel()
169{}
170
171CSecurityLevel::Levels CSecurityLevel::GetLevel() const
172{
173 return m_Level;
174}
175
176QString CSecurityLevel::GetString() const
177{
178 return GetString(GetLevel());
179}
180
181QColor CSecurityLevel::GetColor() const
182{
183 return GetColor(GetLevel());
184}
185
186QString CSecurityLevel::GetUnicodeIcon() const
187{
188 return GetUnicodeIcon(GetLevel());
189}
190
191QIcon CSecurityLevel::GetIcon() const
192{
193 return GetIcon(GetLevel());
194}
195
196QString CSecurityLevel::GetString(const Levels &level)
197{
198 QString szSecurity;
199 if(Level::SecureChannel & level) {
200 if(!szSecurity.isEmpty())
201 szSecurity += " + ";
202 szSecurity += tr("Secure channel");
203 }
204 if(Level::Authentication & level) {
205 if(!szSecurity.isEmpty())
206 szSecurity += " + ";
207 szSecurity += tr("Authentication");
208 }
209 QString szNormal;
210 if(Level::Proxy & level) {
211 if(!szNormal.isEmpty())
212 szNormal += " + ";
213 szNormal += tr("Proxy");
214 }
215 if(Level::Gateway & level) {
216 if(!szNormal.isEmpty())
217 szNormal += " + ";
218 szNormal += tr("Gateway");
219 }
220 if(Level::Redirect & level) {
221 if(!szNormal.isEmpty())
222 szNormal += " + ";
223 szNormal += tr("Redirect");
224 }
225
226 if(level & Level::No)
227 return QString();
228
229 if(level == Level::SecureMask)
230 return tr("Secure") + ": " + szSecurity;
231 if(Level::SecureMask & level) {
232 QString szMsg = tr("Normal") + ": " + szSecurity;
233 if(!szNormal.isEmpty()) {
234 if(!szSecurity.isEmpty())
235 szMsg += " + ";
236 szMsg += szNormal;
237 }
238 return szMsg;
239 }
240 return tr("Risk");
241}
242
260QString CSecurityLevel::GetUnicodeIcon(const Levels &level)
261{
262 if(Level::No & level)
263 return QString();
264 if(level == Level::SecureMask)
265 return "🟢🛡🔐";
266 if(Level::SecureMask & level) {
267 QString s = "🟡";
268 if(Level::SecureChannel & level)
269 s += "🛡";
270 else if(Level::Authentication & level)
271 s+= "🔐";
272 return s;
273 }
274 return "🔴";
275}
276
277QColor CSecurityLevel::GetColor(const Levels &level)
278{
279 if(Level::No & level)
280 return QColor();
281 if(level == Level::SecureMask)
282 return Qt::GlobalColor::green;
283 if(Level::SecureMask & level)
284 return Qt::GlobalColor::yellow;
285 return Qt::GlobalColor::red;
286}
287
288QIcon CSecurityLevel::GetIcon(const Levels &level)
289{
290 if(Level::No & level)
291 return QIcon();
292 if(level == Level::SecureMask)
293 return QIcon::fromTheme("lock");
294 if(Level::SecureMask & level)
295 return QIcon::fromTheme("dialog-warning");
296 return QIcon::fromTheme("unlock");
297}
298
Operational parameter interface.
int SetModified(bool bModified=true)
When setting parameters, if there is a modification, it is called.
Definition Stats.h:11
virtual void slotCalculating()
Calculating.
Definition Stats.cpp:99
int SetInterval(int interval=1)
Set interval.
Definition Stats.cpp:89
void sigDataChanged()
Triggered when data changes, used to notify users to call slotCalculating(true) in real time for calc...
virtual double GetSendRate()
Send rate.
Definition Stats.cpp:74
int GetInterval()
Get interval.
Definition Stats.cpp:84