swift
Loading...
Searching...
No Matches
urlloglist.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CUrlLog, CUrlLogList)
7
8namespace swift::misc::network
9{
11
12 int CUrlLogList::addPendingUrl(const CUrl &url, int maxNumber)
13 {
14 if (maxNumber > 0) this->truncate(maxNumber - 1);
15 const CUrlLog rl(url);
16 this->push_front(rl);
17 return rl.getId();
18 }
19
20 int CUrlLogList::addPendingUrl(const CUrl &url, QNetworkReply *nwReply, int maxNumber)
21 {
22 const int id = this->addPendingUrl(url, maxNumber);
23 if (nwReply)
24 {
26 }
27 return id;
28 }
29
31
33 {
34 if (this->isEmpty())
35 {
36 return {};
37 }
38 return this->findPending().findBeforeNowMinusOffset(outdatedOffsetMs);
39 }
40
42 {
43 return this->findBy(&CUrlLog::isPending, false, &CUrlLog::isSuccess, false);
44 }
45
47 {
48 if (this->isEmpty()) return 0;
49 return this->findPending().size();
50 }
51
53 {
54 // faster as using sizePending()
55 return this->contains(&CUrlLog::isPending, true);
56 }
57
59 {
60 // faster as using sizePending()
61 return this->contains(&CUrlLog::isPending, false);
62 }
63
65 {
66 if (this->isEmpty()) return 0;
67 return this->findErrors().size();
68 }
69
71
72 bool CUrlLogList::markAsReceived(int id, bool success)
73 {
74 for (CUrlLog &rl : *this)
75 {
76 if (rl.getId() == id)
77 {
79 rl.setSuccess(success);
80 return true;
81 }
82 }
83 return false;
84 }
85
86 bool CUrlLogList::markAsReceived(const QNetworkReply *nwReply, bool success)
87 {
88 Q_ASSERT_X(nwReply, Q_FUNC_INFO, "missing reply");
89 bool ok {};
90 const int id = nwReply->property(CUrlLog::propertyNameId()).toInt(&ok);
91 return (ok && id >= 0) ? this->markAsReceived(id, success) : false;
92 }
93
94 bool CUrlLogList::containsId(int id) const { return this->contains(&CUrlLog::getId, id); }
95
97 {
98 qint64 max = 0;
99 for (const CUrlLog &rl : *this)
100 {
101 if (rl.isPending())
102 {
103 continue;
104 }
105 max = std::max(rl.getResponseTimeMs(), max);
106 }
107 return max;
108 }
109
111 {
112 if (!this->hasCompleted())
113 {
114 return 0;
115 }
116 qint64 min = std::numeric_limits<qint64>::max();
117 for (const CUrlLog &rl : *this)
118 {
119 if (rl.isPending())
120 {
121 continue;
122 }
123 min = std::min(rl.getResponseTimeMs(), min);
124 }
125 return min;
126 }
127
129 {
130 qint64 sum = 0;
131 int c = 0;
132 for (const CUrlLog &rl : *this)
133 {
134 if (rl.isPending())
135 {
136 continue;
137 }
138 sum += rl.getResponseTimeMs();
139 c++;
140 }
141 if (c == 0) return 0;
142 return sum / c;
143 }
144
146 {
147 static const QString s("Entries: %1, pending: %2, errors: %3, min: %4ms avg: %5ms max: %6ms");
148 if (this->isEmpty()) return QStringLiteral("No data");
149 return s.arg(this->size())
150 .arg(this->sizePending())
151 .arg(this->sizeErrors())
152 .arg(this->getMinResponseTime())
154 .arg(this->getMaxResponseTime());
155 }
156} // namespace swift::misc::network
auto findFirstByOrDefault(Predicate p, const Value &def) const
Definition range.h:70
bool contains(const T &object) const
Definition range.h:109
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
CSequence findBy(Predicate p) const
Definition sequence.h:407
void truncate(size_type maxSize)
Definition sequence.h:291
void push_front(const CUrlLog &value)
Definition sequence.h:311
CONTAINER findBeforeNowMinusOffset(qint64 msOffset) const
List of objects before now - offset.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition url.h:27
Information about accessing one URL over the network.
Definition urllog.h:24
static const char * propertyNameId()
Property name used for request.
Definition urllog.cpp:87
qint64 getResponseTimeMs() const
Response time.
Definition urllog.h:55
void setSuccess(bool s)
Set success.
Definition urllog.h:64
bool isSuccess() const
Success?
Definition urllog.h:61
bool isPending() const
Pending.
Definition urllog.cpp:27
void setResponseTimestampToNow()
Set response time and response timestamp.
Definition urllog.cpp:21
int getId() const
Unique id.
Definition urllog.h:40
int sizePending() const
Pending calls.
bool markAsReceived(int id, bool success)
Mark as received.
CUrlLogList findPending() const
Find pending log entries.
bool hasCompleted() const
Any completed calls.
CSequence()=default
Default constructor.
bool containsId(int id) const
Contains the id?
int sizeErrors() const
Erroneous calls.
CUrlLogList findOutdatedPending(int outdatedOffsetMs) const
Find outdated pending log entries.
bool hasPending() const
Any pending calls.
QString getSummary() const
Summary.
int addPendingUrl(const CUrl &url, int maxNumber=10)
Add a pending URL.
qint64 getMaxResponseTime() const
Maximum response time.
qint64 getAverageResponseTime() const
Average response time.
CUrlLogList()=default
Default constructor.
CUrlLogList findErrors() const
Find log entries with errors (not pending).
CUrlLog findByIdOrDefault(int id) const
Find by id.
qint64 getMinResponseTime() const
Minimum response time.
QVariant property(const char *name) const const
bool setProperty(const char *name, QVariant &&value)
QString arg(Args &&... args) const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition sequence.h:63