cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
stats.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2015-2025 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "stats.h"
6 
7 #include "application.h"
8 #include "common.h"
9 #include "dispatchtype.h"
10 #include "enginerequest.h"
11 #include "stats_p.h"
12 #include "utils.h"
13 
14 #include <QtCore/QStringList>
15 
16 using namespace Cutelyst;
17 using namespace Qt::StringLiterals;
18 
19 Stats::Stats(EngineRequest *request)
20  : d_ptr(new StatsPrivate)
21 {
22  Q_D(Stats);
23  d->engineRequest = request;
24 }
25 
26 Stats::~Stats()
27 {
28  delete d_ptr;
29 }
30 
31 void Stats::profileStart(const QString &action)
32 {
33  Q_D(Stats);
34  StatsAction stat;
35  stat.action = action;
36  stat.begin = std::chrono::steady_clock::now();
37  d->actions.push_back(stat);
38 }
39 
40 void Stats::profileEnd(const QString &action)
41 {
42  Q_D(Stats);
43  auto it = std::ranges::find_if(d->actions,
44  [action](const auto &stat) { return stat.action == action; });
45 
46  if (it != d->actions.end()) {
47  it->end = std::chrono::steady_clock::now();
48  }
49 }
50 
51 QByteArray Stats::report()
52 {
53  Q_D(const Stats);
54 
55  QByteArray ret;
56  if (d->actions.empty()) {
57  return ret;
58  }
59 
61  for (const auto &stat : d->actions) {
62  const std::chrono::duration<double> duration =
63  (stat.end.has_value() ? stat.end.value() : std::chrono::steady_clock::now()) -
64  stat.begin;
65  table.append({stat.action, QString::number(duration.count(), 'f') + u's'});
66  }
67 
68  ret = Utils::buildTable(table,
69  {
70  u"Action"_s,
71  u"Time"_s,
72  });
73  return ret;
74 }
The Cutelyst namespace holds all public Cutelyst API.
void append(QList< T > &&value)
iterator begin()
iterator begin()