-
Notifications
You must be signed in to change notification settings - Fork 0
/
model-queue.cpp
322 lines (271 loc) · 9.64 KB
/
model-queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "model-queue.h"
#include <iostream>
using std::cout;
using std::endl;
QueueTableModel::QueueTableModel(QObject* parent) : QAbstractTableModel(parent)
{
// Initialize the default columns
queueColumns.append(Column("name", "Name", Qt::AlignLeft, ""));
queueColumns.append(Column("autoDelete", "Auto Delete", Qt::AlignLeft, ""));
queueColumns.append(Column("msgDepth", "Messages", Qt::AlignRight, "N"));
queueColumns.append(Column("byteDepth", "Bytes", Qt::AlignRight, "N"));
queueColumns.append(Column("byteTotalEnqueues", "In Bytes", Qt::AlignRight, "N"));
queueColumns.append(Column("byteTotalDequeues", "Out Bytes", Qt::AlignRight, "N"));
managementQueues.append("amq.direct");
managementQueues.append("qmf.default.topic");
managementQueues.append("qmfa-");
managementQueues.append("qmfagent-");
managementQueues.append("qmfc-v2-");
managementQueues.append("reply-");
managementQueues.append("topic-");
}
bool QueueTableModel::isSystemQueue(const qmf::Data& queue)
{
QString name = QString(queue.getProperty("name").asString().c_str());
QStringList::const_iterator citer = managementQueues.constBegin();
while (citer != managementQueues.constEnd()) {
if (name.startsWith(*citer))
return true;
++citer;
}
return false;
return queue.getProperty("exclusive").asBool();
// when management queues are defined by an agrument, modify and enable the following:
/*
const qpid::types::Variant::Map& map(queue.getProperties());
qpid::types::Variant::Map::const_iterator iter;
iter = map.find("arguments");
if (iter != map.end()) {
const qpid::types::Variant::Map& args(iter->second.asMap());
iter = args.find("management");
if (iter == args.end()) {
return false;
}
return true;
}
*/
}
void QueueTableModel::addQueue(const qmf::Data& queue, uint correlator)
{
if (!queue.isValid())
return;
// filter out system queues if required
if (hideSystemQueues) {
if (isSystemQueue(queue))
return;
}
// see if the object already exists in the list
const qpid::types::Variant& name = queue.getProperty("name");
for (int idx=0; idx<dataList.size(); idx++) {
qmf::Data existing = dataList.at(idx);
if (name.isEqualTo(existing.getProperty("name"))) {
//const qpid::types::Variant::Map& map(queue.getProperties());
qpid::types::Variant::Map map = qpid::types::Variant::Map(queue.getProperties());
map["correlator"] = correlator;
existing.overwriteProperties(map);
return;
}
}
qmf::Data q = qmf::Data(queue);
qpid::types::Variant corr = qpid::types::Variant(correlator);
q.setProperty("correlator", corr);
// this is a new queue
int last = dataList.size();
beginInsertRows(QModelIndex(), last, last);
dataList.append(q);
endInsertRows();
}
void QueueTableModel::connectionChanged(bool isConnected)
{
if (!isConnected)
clear();
}
void QueueTableModel::clear()
{
beginRemoveRows(QModelIndex(), 0, dataList.count() - 1);
dataList.clear();
endRemoveRows();
}
int QueueTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return (int) dataList.size();
}
int QueueTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return queueColumns.size();
}
QVariant QueueTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return queueColumns[index.column()].alignment;
}
if (role != Qt::DisplayRole)
return QVariant();
const qmf::Data& queue = dataList.at(index.row());
const qpid::types::Variant::Map& attrs(queue.getProperties());
qpid::types::Variant::Map::const_iterator iter;
iter = attrs.find(queueColumns[index.column()].name);
if (iter == attrs.end()) {
return QString("--");
}
//return QString(iter->second.asString().c_str());
switch (iter->second.getType()) {
default:
if (queueColumns[index.column()].format == "B")
return fmtBytes(iter->second);
else if (queueColumns[index.column()].format == "N")
return QVariant((qulonglong)iter->second.asUint64());
// else display as string
case qpid::types::VAR_STRING:
case qpid::types::VAR_BOOL:
case qpid::types::VAR_UUID:
case qpid::types::VAR_VOID:
return QString(iter->second.asString().c_str());
case qpid::types::VAR_MAP:
return QString("<map>");
case qpid::types::VAR_LIST:
return QString("<list>");
}
return QVariant();
}
QString QueueTableModel::fmtBytes(const qpid::types::Variant& v) const
{
const char *sizes = " KMGTPY";
uint k = 1024;
uint which_size = 0;
qlonglong b = v.asUint64();
while (b >= k) {
b /= k;
++which_size;
}
if (which_size < strlen(sizes)) {
QChar c = QChar(sizes[which_size]);
return QString("%1").arg(b) + QString(&c, 1);
}
return QString().setNum(b);
}
QVariant QueueTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if ((section >=0) && (section < queueColumns.size())) {
if (role == Qt::TextAlignmentRole) {
return queueColumns[section].alignment;
}
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
return queueColumns[section].header;
}
}
return QVariant();
}
const qmf::Data& QueueTableModel::selectedQueue(const QModelIndex& selectedIndex)
{
if (selectedIndex.isValid()) {
return dataList.at(selectedIndex.row());
}
return qmf::Data();
}
const qmf::Agent& QueueTableModel::selectedQueueAgent(const QModelIndex& selectedIndex)
{
return dataList.at(selectedIndex.row()).getAgent();
}
const qmf::DataAddr& QueueTableModel::selectedQueueDataAddr(const QModelIndex& selectedIndex)
{
return dataList.at(selectedIndex.row()).getAddr();
}
QString QueueTableModel::selectedQueueName(const QModelIndex& selectedIndex)
{
if (selectedIndex.isValid()) {
qpid::types::Variant::Map::const_iterator iter;
const qmf::Data& queue = dataList.at(selectedIndex.row());
const qpid::types::Variant::Map& attrs(queue.getProperties());
iter = attrs.find("name");
if (iter != attrs.end())
return QString(iter->second.asString().c_str());
}
return QString("unknown");
}
QVariant QueueTableModel::selectedQueueDepth(const QModelIndex& selectedIndex)
{
if (selectedIndex.isValid()) {
qpid::types::Variant::Map::const_iterator iter;
const qmf::Data& queue = dataList.at(selectedIndex.row());
const qpid::types::Variant::Map& attrs(queue.getProperties());
iter = attrs.find("msgDepth");
if (iter != attrs.end())
return QVariant(iter->second.asUint32());
}
return QVariant(0);
}
void QueueTableModel::toggleSystemQueues(bool show)
{
this->hideSystemQueues = !show;
if (!show) {
// remove the system queues
for (int idx=0; idx<dataList.size(); idx++) {
if (isSystemQueue(dataList.at(idx))) {
beginRemoveRows( QModelIndex(), idx, idx );
dataList.removeAt(idx--);
endRemoveRows();
}
}
// force a refresh of the display
QModelIndex topLeft = index(0, 0);
QModelIndex bottomRight = index(dataList.size() - 1, 2);
emit dataChanged ( topLeft, bottomRight );
}
}
void QueueTableModel::refresh(uint correlator)
{
// remove any old queues that were not added/updated with this correlator
for (int idx=0; idx<dataList.size(); idx++) {
uint corr = dataList.at(idx).getProperty("correlator").asUint32();
if (corr != correlator) {
beginRemoveRows( QModelIndex(), idx, idx );
dataList.removeAt(idx--);
endRemoveRows();
}
}
// force a refresh of the display
QModelIndex topLeft = index(0, 0);
QModelIndex bottomRight = index(dataList.size() - 1, 2);
emit dataChanged ( topLeft, bottomRight );
}
std::ostream& operator<<(std::ostream& out, const qmf::Data& queue)
{
if (queue.isValid()) {
qpid::types::Variant::Map::const_iterator iter;
const qpid::types::Variant::Map& attrs(queue.getProperties());
out << " <properties>\n";
for (iter = attrs.begin(); iter != attrs.end(); iter++) {
if (iter->first != "name") {
out << " <property>\n";
out << " <name>" << iter->first << "</name>\n";
out << " <value>" << iter->second << "</value>\n";
out << " </property>\n";
}
}
out << " </properties>\n";
}
return out;
}