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 
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) { nwReply->setProperty(CUrlLog::propertyNameId(), QVariant::fromValue(id)); }
24  return id;
25  }
26 
28 
29  CUrlLogList CUrlLogList::findOutdatedPending(int outdatedOffsetMs) const
30  {
31  if (this->isEmpty()) { return {}; }
32  return this->findPending().findBeforeNowMinusOffset(outdatedOffsetMs);
33  }
34 
36  {
37  return this->findBy(&CUrlLog::isPending, false, &CUrlLog::isSuccess, false);
38  }
39 
41  {
42  if (this->isEmpty()) return 0;
43  return this->findPending().size();
44  }
45 
47  {
48  // faster as using sizePending()
49  return this->contains(&CUrlLog::isPending, true);
50  }
51 
53  {
54  // faster as using sizePending()
55  return this->contains(&CUrlLog::isPending, false);
56  }
57 
59  {
60  if (this->isEmpty()) return 0;
61  return this->findErrors().size();
62  }
63 
65 
66  bool CUrlLogList::markAsReceived(int id, bool success)
67  {
68  for (CUrlLog &rl : *this)
69  {
70  if (rl.getId() == id)
71  {
73  rl.setSuccess(success);
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  bool CUrlLogList::markAsReceived(const QNetworkReply *nwReply, bool success)
81  {
82  Q_ASSERT_X(nwReply, Q_FUNC_INFO, "missing reply");
83  bool ok {};
84  const int id = nwReply->property(CUrlLog::propertyNameId()).toInt(&ok);
85  return (ok && id >= 0) ? this->markAsReceived(id, success) : false;
86  }
87 
88  bool CUrlLogList::containsId(int id) const { return this->contains(&CUrlLog::getId, id); }
89 
91  {
92  qint64 max = 0;
93  for (const CUrlLog &rl : *this)
94  {
95  if (rl.isPending()) { continue; }
96  max = std::max(rl.getResponseTimeMs(), max);
97  }
98  return max;
99  }
100 
102  {
103  if (!this->hasCompleted()) { return 0; }
104  qint64 min = std::numeric_limits<qint64>::max();
105  for (const CUrlLog &rl : *this)
106  {
107  if (rl.isPending()) { continue; }
108  min = std::min(rl.getResponseTimeMs(), min);
109  }
110  return min;
111  }
112 
114  {
115  qint64 sum = 0;
116  int c = 0;
117  for (const CUrlLog &rl : *this)
118  {
119  if (rl.isPending()) { continue; }
120  sum += rl.getResponseTimeMs();
121  c++;
122  }
123  if (c == 0) return 0;
124  return sum / c;
125  }
126 
128  {
129  static const QString s("Entries: %1, pending: %2, errors: %3, min: %4ms avg: %5ms max: %6ms");
130  if (this->isEmpty()) return QStringLiteral("No data");
131  return s.arg(this->size())
132  .arg(this->sizePending())
133  .arg(this->sizeErrors())
134  .arg(this->getMinResponseTime())
135  .arg(this->getAverageResponseTime())
136  .arg(this->getMaxResponseTime());
137  }
138 } // 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:81
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
Value object encapsulating a list of voice rooms.
Definition: urlloglist.h:26
int sizePending() const
Pending calls.
Definition: urlloglist.cpp:40
bool markAsReceived(int id, bool success)
Mark as received.
Definition: urlloglist.cpp:66
CUrlLogList findPending() const
Find pending log entries.
Definition: urlloglist.cpp:27
bool hasCompleted() const
Any completed calls.
Definition: urlloglist.cpp:52
bool containsId(int id) const
Contains the id?
Definition: urlloglist.cpp:88
int sizeErrors() const
Erroneous calls.
Definition: urlloglist.cpp:58
CUrlLogList findOutdatedPending(int outdatedOffsetMs) const
Find outdated pending log entries.
Definition: urlloglist.cpp:29
bool hasPending() const
Any pending calls.
Definition: urlloglist.cpp:46
QString getSummary() const
Summary.
Definition: urlloglist.cpp:127
int addPendingUrl(const CUrl &url, int maxNumber=10)
Add a pending URL.
Definition: urlloglist.cpp:12
qint64 getMaxResponseTime() const
Maximum response time.
Definition: urlloglist.cpp:90
qint64 getAverageResponseTime() const
Average response time.
Definition: urlloglist.cpp:113
CUrlLogList()=default
Default constructor.
CUrlLogList findErrors() const
Find log entries with errors (not pending)
Definition: urlloglist.cpp:35
CUrlLog findByIdOrDefault(int id) const
Find by id.
Definition: urlloglist.cpp:64
qint64 getMinResponseTime() const
Minimum response time.
Definition: urlloglist.cpp:101
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