GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/ClientStatisticsModel.cpp
Date: 2024-12-15 04:08:34
Exec Total Coverage
Lines: 0 86 0.0%
Branches: 0 90 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2009 - 2022 Bjarne von Horn <vh@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22 #include "ClientStatisticsModel.h"
23
24 #include "Process.h"
25 #include <QDateTime>
26
27 using QtPdCom::ClientStatisticsModel;
28 using QtPdCom::ClientStatisticsModelPrivate;
29
30 namespace QtPdCom {
31 struct ClientStatisticsModelPrivate
32 {
33 ClientStatisticsModelPrivate(ClientStatisticsModel *q_ptr): q_ptr(q_ptr)
34 {}
35
36 Q_DECLARE_PUBLIC(ClientStatisticsModel)
37 ClientStatisticsModel *q_ptr;
38 Process *process = nullptr;
39 std::vector<PdCom::ClientStatistics> statistics;
40 };
41
42 } // namespace QtPdCom
43
44 ClientStatisticsModel::ClientStatisticsModel(QObject *parent):
45 QAbstractTableModel(parent), d_ptr(new ClientStatisticsModelPrivate(this))
46 {}
47
48 ClientStatisticsModel::~ClientStatisticsModel() = default;
49
50 int ClientStatisticsModel::rowCount(const QModelIndex &) const
51 {
52 const Q_D(ClientStatisticsModel);
53 return d->statistics.size();
54 }
55
56 int ClientStatisticsModel::columnCount(const QModelIndex &) const
57 {
58 return 5;
59 }
60
61 QVariant ClientStatisticsModel::data(const QModelIndex &index, int role) const
62 {
63 const Q_D(ClientStatisticsModel);
64
65 if (index.row() < 0 or index.row() >= int(d->statistics.size()))
66 return QVariant();
67 if (index.column() < 0 or index.column() >= 5)
68 return QVariant();
69 if (role != Qt::DisplayRole)
70 return QVariant();
71
72 auto column = index.column();
73 if (NameRole <= role && role <= ConnectedTimeRole) {
74 column = role - NameRole;
75 }
76
77 switch (column) {
78 case 0:
79 return QString::fromStdString(d->statistics[index.row()].name_);
80 case 1:
81 return QString::fromStdString(
82 d->statistics[index.row()].application_name_);
83 case 2:
84 return static_cast<qulonglong>(
85 d->statistics[index.row()].received_bytes_);
86 case 3:
87 return static_cast<qulonglong>(
88 d->statistics[index.row()].sent_bytes_);
89 case 4: {
90 QDateTime dt;
91 dt.setSecsSinceEpoch(std::chrono::duration_cast<std::chrono::seconds>(
92 d->statistics[index.row()].connected_time_)
93 .count());
94 return dt.toString("yyyy-MM-dd hh:mm:ss");
95 }
96 default:
97 break;
98 }
99 return QVariant();
100 }
101
102 QVariant ClientStatisticsModel::headerData(
103 int section,
104 Qt::Orientation o,
105 int role) const
106 {
107 if (role == Qt::DisplayRole && o == Qt::Horizontal) {
108 switch (section) {
109 case 0:
110 return tr("Name");
111 case 1:
112 return tr("Application");
113 case 2:
114 return tr("Received bytes");
115 case 3:
116 return tr("Sent bytes");
117 case 4:
118 return tr("Connected time");
119 default:
120 break;
121 }
122 }
123 return QVariant();
124 }
125
126 void ClientStatisticsModel::poll()
127 {
128 Q_D(ClientStatisticsModel);
129
130 if (!d->process)
131 return;
132
133 d->process->getClientStatistics(
134 this,
135 [this, d](std::vector<PdCom::ClientStatistics> statistics) {
136 this->beginResetModel();
137 d->statistics = std::move(statistics);
138 this->endResetModel();
139 });
140 }
141
142 void QtPdCom::ClientStatisticsModel::clear()
143 {
144 Q_D(ClientStatisticsModel);
145
146 beginResetModel();
147 d->statistics.clear();
148 endResetModel();
149 }
150
151 void ClientStatisticsModel::setProcess(QtPdCom::Process *process)
152 {
153 Q_D(ClientStatisticsModel);
154
155 d->process = process;
156 if (process && process->isConnected())
157 poll();
158 }
159
160 QtPdCom::Process *ClientStatisticsModel::getProcess() const
161 {
162 const Q_D(ClientStatisticsModel);
163 return d->process;
164 }
165
166 QHash<int, QByteArray> ClientStatisticsModel::roleNames() const
167 {
168 auto ans = QAbstractTableModel::roleNames();
169
170 ans[NameRole] = "name";
171 ans[ApplicationNameRole] = "application_name";
172 ans[RxByteRole] = "received_bytes";
173 ans[TxByteRole] = "sent_bytes";
174 ans[ConnectedTimeRole] = "connected_time";
175 return ans;
176 }
177