-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-model.cpp
295 lines (251 loc) · 8.42 KB
/
object-model.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
/*
* 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 "object-model.h"
#include <iostream>
using std::cout;
using std::endl;
ObjectListModel::ObjectListModel(QObject* parent, std::string unique, const QStringList& columnList) :
QAbstractTableModel(parent), uniqueProperty(unique),
sampleProperties(),
invalid()
{
sampleLife = 600;
sampleProperties = columnList;
}
void ObjectListModel::addObject(const qmf::Data& object, uint correlator)
{
if (!object.isValid())
return;
// get the uniqueu property for this object
const qpid::types::Variant& name = object.getProperty(uniqueProperty);
// create a new sample
addSample(object, name);
// see if the object exists in the list
for (int idx=0; idx<dataList.size(); idx++) {
qmf::Data existing = dataList.at(idx);
if (name.isEqualTo(existing.getProperty(uniqueProperty))) {
qpid::types::Variant::Map map = qpid::types::Variant::Map(object.getProperties());
map["correlator"] = correlator;
existing.overwriteProperties(map);
return;
}
}
qmf::Data o = qmf::Data(object);
qpid::types::Variant corr = qpid::types::Variant(correlator);
o.setProperty("correlator", corr);
// this is a new queue
int last = dataList.size();
beginInsertRows(QModelIndex(), last, last);
dataList.append(o);
endInsertRows();
//dataHash[name.asString()] = o;
}
void ObjectListModel::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) {
// clear out the old samples
QString name(dataList.at(idx).getProperty(uniqueProperty).asString().c_str());
if (samplesData.contains(name)) {
samplesData.remove(name);
}
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 ObjectListModel::connectionChanged(bool isConnected)
{
if (!isConnected)
clear();
}
void ObjectListModel::clear()
{
beginRemoveRows(QModelIndex(), 0, dataList.count() - 1);
dataList.clear();
endRemoveRows();
}
int ObjectListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return (int) dataList.size();
}
int ObjectListModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return sampleProperties.size() + 1;
}
void ObjectListModel::setKey(const std::string &altKey)
{
dataKey = altKey;
}
const std::string &ObjectListModel::unique(bool useKey)
{
if (useKey) {
if (dataKey != "") {
return dataKey;
}
}
return uniqueProperty;
}
const qmf::Data& ObjectListModel::find(const qmf::Data& existing)
{
const qpid::types::Variant& name = existing.getProperty(uniqueProperty);
for (int idx=0; idx<dataList.size(); idx++) {
if (name.isEqualTo(dataList.at(idx).getProperty(uniqueProperty))) {
return dataList.at(idx);
}
}
return invalid;
}
QVariant ObjectListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
// we want the entire data object at this row
if (role == Qt::UserRole) {
return QVariant(dataList.at(index.row()));
}
if (role != Qt::DisplayRole && role != Qt::ToolTipRole)
return QVariant();
const qmf::Data& object(dataList[index.row()]);
//const qmf::Data& object = dataList.at(index.row());
const qpid::types::Variant::Map& props(object.getProperties());
qpid::types::Variant::Map::const_iterator iter;
// for the "name" column, we will show either the key (prefered) or the unique field
if (index.column() == 0) {
iter = props.find(dataKey);
if (iter == props.end()) {
iter = props.find(uniqueProperty);
}
if (iter != props.end())
return QString((*iter).second.asString().c_str());
return QString();
}
// for the value columns (shown in the related table) return the numeric value
int col = index.column() - 1;
std::string prop = sampleProperties.at(col).toStdString();
iter = props.find(prop);
if (iter != props.end()) {
if (role == Qt::DisplayRole)
return QVariant((qreal)(*iter).second.asInt64());
else {
return QString("%1 %2").arg(prop.c_str()).arg((*iter).second.asInt64());
}
}
return QString();
}
std::string ObjectListModel::fieldValue(int row, const std::string& field)
{
const qmf::Data& object= dataList.at(row);
qpid::types::Variant value = object.getProperty(field);
if ((field == "queueRef") ||
(field == "exchangeRef") ||
(field == "sessionRef") ||
(field == "connectionRef") ||
(field == "vhostRef")) {
return value.asMap()["_object_name"].asString();
}
return value.asString();
}
const qmf::Data& ObjectListModel::qmfData(int row)
{
return dataList.at(row);
}
QVariant ObjectListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) {
if (section == 0) {
if (dataKey == "")
return QString(uniqueProperty.c_str());
else
return QString(dataKey.c_str());
}
}
}
return QVariant();
}
// SLOT
void ObjectListModel::selected(const QModelIndex &index)
{
if (index.isValid()) {
const qmf::Data& object = dataList.at(index.row());
// show the details for this object in the dialog's object table
emit objectSelected(object);
}
}
const qmf::Data& ObjectListModel::getSelected(const QModelIndex &index)
{
return dataList.at(index.row());
}
std::ostream& operator<<(std::ostream& out, const qmf::Data& object)
{
if (object.isValid()) {
qpid::types::Variant::Map::const_iterator iter;
const qpid::types::Variant::Map& attrs(object.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;
}
void ObjectListModel::addSample(const qmf::Data& object, const qpid::types::Variant& name)
{
QString key = QString(name.asString().c_str());
samplesData[key].append(Sample(object, sampleProperties));
}
void ObjectListModel::expireSamples()
{
QDateTime tnow(QDateTime::currentDateTime());
int secs;
QStringList keys = samplesData.keys();
QStringList::const_iterator iter = keys.constBegin();
while (iter != keys.constEnd()) {
SampleList &sampleList(samplesData[*iter]);
iterSampleList iterList = sampleList.begin();
while (iterList != sampleList.end()) {
secs = (*iterList).dateTime().secsTo(tnow);
if (secs > sampleLife) {
// erase increments iterList
iterList = sampleList.erase(iterList);
} else
break;
}
++iter;
}
}
void ObjectListModel::clearSamples()
{
samplesData.clear();
}