swift
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 
6 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc::network, CUrlLog, CUrlLogList)
7 
8 namespace swift::misc::network
9 {
11 
13 
14  int CUrlLogList::addPendingUrl(const CUrl &url, int maxNumber)
15  {
16  if (maxNumber > 0) this->truncate(maxNumber - 1);
17  const CUrlLog rl(url);
18  this->push_front(rl);
19  return rl.getId();
20  }
21 
22  int CUrlLogList::addPendingUrl(const CUrl &url, QNetworkReply *nwReply, int maxNumber)
23  {
24  const int id = this->addPendingUrl(url, maxNumber);
25  if (nwReply) { nwReply->setProperty(CUrlLog::propertyNameId(), QVariant::fromValue(id)); }
26  return id;
27  }
28 
30 
31  CUrlLogList CUrlLogList::findOutdatedPending(int outdatedOffsetMs) const
32  {
33  if (this->isEmpty()) { return CUrlLogList(); }
34  return this->findPending().findBeforeNowMinusOffset(outdatedOffsetMs);
35  }
36 
38  {
39  return this->findBy(&CUrlLog::isPending, false, &CUrlLog::isSuccess, false);
40  }
41 
43  {
44  if (this->isEmpty()) return 0;
45  return this->findPending().size();
46  }
47 
49  {
50  // faster as using sizePending()
51  return this->contains(&CUrlLog::isPending, true);
52  }
53 
55  {
56  // faster as using sizePending()
57  return this->contains(&CUrlLog::isPending, false);
58  }
59 
61  {
62  if (this->isEmpty()) return 0;
63  return this->findErrors().size();
64  }
65 
67 
68  bool CUrlLogList::markAsReceived(int id, bool success)
69  {
70  for (CUrlLog &rl : *this)
71  {
72  if (rl.getId() == id)
73  {
75  rl.setSuccess(success);
76  return true;
77  }
78  }
79  return false;
80  }
81 
82  bool CUrlLogList::markAsReceived(const QNetworkReply *nwReply, bool success)
83  {
84  Q_ASSERT_X(nwReply, Q_FUNC_INFO, "missing reply");
85  bool ok;
86  const int id = nwReply->property(CUrlLog::propertyNameId()).toInt(&ok);
87  return (ok && id >= 0) ? this->markAsReceived(id, success) : false;
88  }
89 
90  bool CUrlLogList::containsId(int id) const { return this->contains(&CUrlLog::getId, id); }
91 
93  {
94  qint64 max = 0;
95  for (const CUrlLog &rl : *this)
96  {
97  if (rl.isPending()) { continue; }
98  if (rl.getResponseTimeMs() > max) { max = rl.getResponseTimeMs(); }
99  }
100  return max;
101  }
102 
104  {
105  if (!this->hasCompleted()) { return 0; }
106  qint64 min = std::numeric_limits<qint64>::max();
107  for (const CUrlLog &rl : *this)
108  {
109  if (rl.isPending()) { continue; }
110  if (rl.getResponseTimeMs() < min) { min = rl.getResponseTimeMs(); }
111  }
112  return min;
113  }
114 
116  {
117  qint64 sum = 0;
118  int c = 0;
119  for (const CUrlLog &rl : *this)
120  {
121  if (rl.isPending()) { continue; }
122  sum += rl.getResponseTimeMs();
123  c++;
124  }
125  if (c == 0) return 0;
126  return sum / c;
127  }
128 
129  QString CUrlLogList::getSummary() const
130  {
131  static const QString s("Entries: %1, pending: %2, errors: %3, min: %4ms avg: %5ms max: %6ms");
132  if (this->isEmpty()) return QStringLiteral("No data");
133  return s.arg(this->size())
134  .arg(this->sizePending())
135  .arg(this->sizeErrors())
136  .arg(this->getMinResponseTime())
137  .arg(this->getAverageResponseTime())
138  .arg(this->getMaxResponseTime());
139  }
140 } // namespace swift::misc::network
auto findFirstByOrDefault(Predicate p, const Value &def) const
Return a copy of the first element for which a given predicate returns true, or a default value if th...
Definition: range.h:70
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
Generic sequential container with value semantics.
Definition: sequence.h:86
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
CSequence findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition: sequence.h:398
void truncate(size_type maxSize)
Changes the size of the sequence, if it is bigger than the given size.
Definition: sequence.h:291
void push_front(const CUrlLog &value)
Insert as first element.
Definition: sequence.h:308
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
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:84
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:30
void setResponseTimestampToNow()
Set response time and response timestamp.
Definition: urllog.cpp:24
int getId() const
Unique id.
Definition: urllog.h:40
Value object encapsulating a list of voice rooms.
Definition: urlloglist.h:26
int sizePending() const
Pending calls.
Definition: urlloglist.cpp:42
bool markAsReceived(int id, bool success)
Mark as received.
Definition: urlloglist.cpp:68
CUrlLogList findPending() const
Find pending log entries.
Definition: urlloglist.cpp:29
bool hasCompleted() const
Any completed calls.
Definition: urlloglist.cpp:54
bool containsId(int id) const
Contains the id?
Definition: urlloglist.cpp:90
int sizeErrors() const
Erroneous calls.
Definition: urlloglist.cpp:60
CUrlLogList findOutdatedPending(int outdatedOffsetMs) const
Find outdated pending log entries.
Definition: urlloglist.cpp:31
bool hasPending() const
Any pending calls.
Definition: urlloglist.cpp:48
CUrlLogList()
Default constructor.
Definition: urlloglist.cpp:10
QString getSummary() const
Summary.
Definition: urlloglist.cpp:129
int addPendingUrl(const CUrl &url, int maxNumber=10)
Add a pending URL.
Definition: urlloglist.cpp:14
qint64 getMaxResponseTime() const
Maximum response time.
Definition: urlloglist.cpp:92
qint64 getAverageResponseTime() const
Average response time.
Definition: urlloglist.cpp:115
CUrlLogList findErrors() const
Find log entries with errors (not pending)
Definition: urlloglist.cpp:37
CUrlLog findByIdOrDefault(int id) const
Find by id.
Definition: urlloglist.cpp:66
qint64 getMinResponseTime() const
Minimum response time.
Definition: urlloglist.cpp:103
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63