Bitcoin Core  21.99.0
P2P Digital Currency
peertablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/peertablemodel.h>
6 
7 #include <qt/guiconstants.h>
8 #include <qt/guiutil.h>
9 
10 #include <interfaces/node.h>
11 
12 #include <utility>
13 
14 #include <QDebug>
15 #include <QList>
16 #include <QTimer>
17 
18 bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombinedStats &right) const
19 {
20  const CNodeStats *pLeft = &(left.nodeStats);
21  const CNodeStats *pRight = &(right.nodeStats);
22 
23  if (order == Qt::DescendingOrder)
24  std::swap(pLeft, pRight);
25 
26  switch(column)
27  {
29  return pLeft->nodeid < pRight->nodeid;
31  return pLeft->addrName.compare(pRight->addrName) < 0;
33  return pLeft->m_network < pRight->m_network;
35  return pLeft->m_min_ping_usec < pRight->m_min_ping_usec;
37  return pLeft->nSendBytes < pRight->nSendBytes;
39  return pLeft->nRecvBytes < pRight->nRecvBytes;
41  return pLeft->cleanSubVer.compare(pRight->cleanSubVer) < 0;
42  }
43 
44  return false;
45 }
46 
47 // private implementation
49 {
50 public:
52  QList<CNodeCombinedStats> cachedNodeStats;
54  int sortColumn{-1};
56  Qt::SortOrder sortOrder;
58  std::map<NodeId, int> mapNodeRows;
59 
62  {
63  {
64  cachedNodeStats.clear();
65 
66  interfaces::Node::NodesStats nodes_stats;
67  node.getNodesStats(nodes_stats);
68  cachedNodeStats.reserve(nodes_stats.size());
69  for (const auto& node_stats : nodes_stats)
70  {
71  CNodeCombinedStats stats;
72  stats.nodeStats = std::get<0>(node_stats);
73  stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
74  stats.nodeStateStats = std::get<2>(node_stats);
75  cachedNodeStats.append(stats);
76  }
77  }
78 
79  if (sortColumn >= 0)
80  // sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily)
81  std::stable_sort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder));
82 
83  // build index map
84  mapNodeRows.clear();
85  int row = 0;
86  for (const CNodeCombinedStats& stats : cachedNodeStats)
87  mapNodeRows.insert(std::pair<NodeId, int>(stats.nodeStats.nodeid, row++));
88  }
89 
90  int size() const
91  {
92  return cachedNodeStats.size();
93  }
94 
96  {
97  if (idx >= 0 && idx < cachedNodeStats.size())
98  return &cachedNodeStats[idx];
99 
100  return nullptr;
101  }
102 };
103 
105  QAbstractTableModel(parent),
106  m_node(node),
107  timer(nullptr)
108 {
109  priv.reset(new PeerTablePriv());
110 
111  // set up timer for auto refresh
112  timer = new QTimer(this);
113  connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
114  timer->setInterval(MODEL_UPDATE_DELAY);
115 
116  // load initial data
117  refresh();
118 }
119 
121 {
122  // Intentionally left empty
123 }
124 
126 {
127  timer->start();
128 }
129 
131 {
132  timer->stop();
133 }
134 
135 int PeerTableModel::rowCount(const QModelIndex &parent) const
136 {
137  if (parent.isValid()) {
138  return 0;
139  }
140  return priv->size();
141 }
142 
143 int PeerTableModel::columnCount(const QModelIndex &parent) const
144 {
145  if (parent.isValid()) {
146  return 0;
147  }
148  return columns.length();
149 }
150 
151 QVariant PeerTableModel::data(const QModelIndex &index, int role) const
152 {
153  if(!index.isValid())
154  return QVariant();
155 
156  CNodeCombinedStats *rec = static_cast<CNodeCombinedStats*>(index.internalPointer());
157 
158  if (role == Qt::DisplayRole) {
159  switch(index.column())
160  {
161  case NetNodeId:
162  return (qint64)rec->nodeStats.nodeid;
163  case Address:
164  // prepend to peer address down-arrow symbol for inbound connection and up-arrow for outbound connection
165  return QString(rec->nodeStats.fInbound ? "↓ " : "↑ ") + QString::fromStdString(rec->nodeStats.addrName);
166  case Network:
168  case Ping:
170  case Sent:
172  case Received:
174  case Subversion:
175  return QString::fromStdString(rec->nodeStats.cleanSubVer);
176  }
177  } else if (role == Qt::TextAlignmentRole) {
178  switch (index.column()) {
179  case Network:
180  return QVariant(Qt::AlignCenter);
181  case Ping:
182  case Sent:
183  case Received:
184  return QVariant(Qt::AlignRight | Qt::AlignVCenter);
185  default:
186  return QVariant();
187  }
188  } else if (role == StatsRole) {
189  switch (index.column()) {
190  case NetNodeId: return QVariant::fromValue(rec);
191  default: return QVariant();
192  }
193  }
194 
195  return QVariant();
196 }
197 
198 QVariant PeerTableModel::headerData(int section, Qt::Orientation orientation, int role) const
199 {
200  if(orientation == Qt::Horizontal)
201  {
202  if(role == Qt::DisplayRole && section < columns.size())
203  {
204  return columns[section];
205  }
206  }
207  return QVariant();
208 }
209 
210 Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
211 {
212  if (!index.isValid()) return Qt::NoItemFlags;
213 
214  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
215  return retval;
216 }
217 
218 QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent) const
219 {
220  Q_UNUSED(parent);
221  CNodeCombinedStats *data = priv->index(row);
222 
223  if (data)
224  return createIndex(row, column, data);
225  return QModelIndex();
226 }
227 
229 {
230  Q_EMIT layoutAboutToBeChanged();
231  priv->refreshPeers(m_node);
232  Q_EMIT layoutChanged();
233 }
234 
236 {
237  std::map<NodeId, int>::iterator it = priv->mapNodeRows.find(nodeid);
238  if (it == priv->mapNodeRows.end())
239  return -1;
240 
241  return it->second;
242 }
243 
244 void PeerTableModel::sort(int column, Qt::SortOrder order)
245 {
246  priv->sortColumn = column;
247  priv->sortOrder = order;
248  refresh();
249 }
void refreshPeers(interfaces::Node &node)
Pull a full list of peers from vNodes into our cache.
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:94
int getRowByNodeId(NodeId nodeid)
std::deque< CInv >::iterator it
bool operator()(const CNodeCombinedStats &left, const CNodeCombinedStats &right) const
CNodeStateStats nodeStateStats
NodeContext & m_node
Definition: interfaces.cpp:660
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent) const override
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:861
CNodeCombinedStats * index(int idx)
std::string cleanSubVer
Definition: net.h:246
QModelIndex index(int row, int column, const QModelIndex &parent) const override
NodeLessThan(int nColumn, Qt::SortOrder fOrder)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Qt::SortOrder order
Qt::ItemFlags flags(const QModelIndex &index) const override
CNodeStats nodeStats
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:752
interfaces::Node & m_node
QString formatPingTime(int64_t ping_usec)
Format a CNodeStats.m_ping_usec into a user-readable string or display N/A, if 0. ...
Definition: guiutil.cpp:814
QList< CNodeCombinedStats > cachedNodeStats
Local cache of peer information.
Network m_network
Definition: net.h:267
bool fInbound
Definition: net.h:247
std::unique_ptr< PeerTablePriv > priv
int size() const
uint64_t nRecvBytes
Definition: net.h:253
std::string addrName
Definition: net.h:244
int64_t NodeId
Definition: net.h:83
uint64_t nSendBytes
Definition: net.h:251
const QStringList columns
static const int MODEL_UPDATE_DELAY
Definition: guiconstants.h:11
void sort(int column, Qt::SortOrder order) override
std::map< NodeId, int > mapNodeRows
Index of rows by node ID.
virtual bool getNodesStats(NodesStats &stats)=0
PeerTableModel(interfaces::Node &node, QObject *parent)
int columnCount(const QModelIndex &parent) const override
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:52
int64_t m_min_ping_usec
Definition: net.h:258
NodeId nodeid
Definition: net.h:235