2016-11-24 10:14:38 -06:00
|
|
|
/*
|
|
|
|
Copyright (C) 2016 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "streamstatsmodel.h"
|
|
|
|
|
|
|
|
#include "protocol.pb.h"
|
|
|
|
|
2022-02-11 00:07:03 -06:00
|
|
|
#include <QApplication>
|
2017-01-01 00:42:10 -06:00
|
|
|
#include <QBrush>
|
2022-02-11 00:07:03 -06:00
|
|
|
#include <QFont>
|
|
|
|
#include <QPalette>
|
2017-01-01 00:42:10 -06:00
|
|
|
|
2016-11-26 02:07:36 -06:00
|
|
|
// XXX: Keep the enum in sync with it's string
|
|
|
|
enum {
|
|
|
|
kTxPkts,
|
2016-12-09 06:42:06 -06:00
|
|
|
kRxPkts,
|
2016-11-26 02:07:36 -06:00
|
|
|
kTxBytes,
|
2016-12-09 06:42:06 -06:00
|
|
|
kRxBytes,
|
2016-11-26 02:07:36 -06:00
|
|
|
kMaxStreamStats
|
|
|
|
};
|
|
|
|
static QStringList statTitles = QStringList()
|
|
|
|
<< "Tx Pkts"
|
2016-12-09 06:42:06 -06:00
|
|
|
<< "Rx Pkts"
|
|
|
|
<< "Tx Bytes"
|
|
|
|
<< "Rx Bytes";
|
2016-11-26 02:07:36 -06:00
|
|
|
|
2016-12-27 08:50:36 -06:00
|
|
|
// XXX: Keep the enum in sync with it's string
|
|
|
|
enum {
|
|
|
|
kAggrTxPkts,
|
|
|
|
kAggrRxPkts,
|
|
|
|
kAggrPktLoss,
|
|
|
|
kMaxAggrStreamStats
|
|
|
|
};
|
|
|
|
static QStringList aggrStatTitles = QStringList()
|
2017-01-23 06:56:35 -06:00
|
|
|
<< "Total\nTx Pkts"
|
|
|
|
<< "Total\nRx Pkts"
|
|
|
|
<< "Total\nPkt Loss";
|
2016-12-27 08:50:36 -06:00
|
|
|
|
2017-01-10 10:13:58 -06:00
|
|
|
static const uint kAggrGuid = 0xffffffff;
|
2016-12-27 08:50:36 -06:00
|
|
|
|
2016-11-24 10:14:38 -06:00
|
|
|
StreamStatsModel::StreamStatsModel(QObject *parent)
|
2016-11-26 02:07:36 -06:00
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
{
|
2016-12-27 08:50:36 -06:00
|
|
|
clearStats();
|
2016-11-26 02:07:36 -06:00
|
|
|
}
|
|
|
|
|
2018-04-20 10:20:51 -05:00
|
|
|
int StreamStatsModel::rowCount(const QModelIndex &/*parent*/) const
|
2016-11-26 02:07:36 -06:00
|
|
|
{
|
|
|
|
return guidList_.size();
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:20:51 -05:00
|
|
|
int StreamStatsModel::columnCount(const QModelIndex &/*parent*/) const
|
2016-11-26 02:07:36 -06:00
|
|
|
{
|
2016-12-27 08:50:36 -06:00
|
|
|
if (!portList_.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return kMaxAggrStreamStats + portList_.size() * kMaxStreamStats;
|
2016-11-26 02:07:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant StreamStatsModel::headerData(
|
|
|
|
int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case Qt::Horizontal: // Column Header
|
2016-12-27 08:50:36 -06:00
|
|
|
if (section < kMaxAggrStreamStats)
|
|
|
|
return aggrStatTitles.at(section % kMaxAggrStreamStats);
|
|
|
|
|
|
|
|
section -= kMaxAggrStreamStats;
|
2016-11-26 02:07:36 -06:00
|
|
|
return QString("Port %1-%2\n%3")
|
|
|
|
.arg(portList_.at(section/kMaxStreamStats).first)
|
|
|
|
.arg(portList_.at(section/kMaxStreamStats).second)
|
|
|
|
.arg(statTitles.at(section % kMaxStreamStats));
|
|
|
|
case Qt::Vertical: // Row Header
|
2017-01-10 10:13:58 -06:00
|
|
|
if (section == (guidList_.size() - 1))
|
2017-01-23 06:56:35 -06:00
|
|
|
return QString("GUID Total");
|
2016-11-26 02:07:36 -06:00
|
|
|
return QString("Stream GUID %1")
|
|
|
|
.arg(guidList_.at(section));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant StreamStatsModel::data(const QModelIndex &index, int role) const
|
2016-11-24 10:14:38 -06:00
|
|
|
{
|
2016-11-27 00:26:18 -06:00
|
|
|
if (role == Qt::TextAlignmentRole)
|
|
|
|
return Qt::AlignRight;
|
|
|
|
|
2017-01-01 00:42:10 -06:00
|
|
|
int portColumn = index.column() - kMaxAggrStreamStats;
|
2022-02-11 00:07:03 -06:00
|
|
|
|
|
|
|
// Stylesheets typically don't use or set palette colors, so if
|
|
|
|
// using one, don't use palette colors
|
|
|
|
if ((role == Qt::BackgroundRole) && qApp->styleSheet().isEmpty()) {
|
|
|
|
QPalette palette = QApplication::palette();
|
|
|
|
if (index.row() == (guidList_.size() - 1)) // Aggregate Row
|
|
|
|
return palette.dark();
|
|
|
|
if (portColumn < 0) // Aggregate Column
|
|
|
|
return palette.alternateBase();
|
|
|
|
if ((portColumn/kMaxStreamStats) & 1) // Color alternate Ports
|
|
|
|
return palette.alternateBase();
|
2017-01-01 00:42:10 -06:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:43:14 -06:00
|
|
|
Guid guid = guidList_.at(index.row());
|
2022-02-11 00:07:03 -06:00
|
|
|
if ((role == Qt::ForegroundRole && qApp->styleSheet().isEmpty())) {
|
|
|
|
QPalette palette = QApplication::palette();
|
2017-01-10 06:43:14 -06:00
|
|
|
if ((index.column() == kAggrPktLoss)
|
2017-01-10 07:20:22 -06:00
|
|
|
&& aggrGuidStats_.value(guid).pktLoss)
|
2022-02-11 00:07:03 -06:00
|
|
|
return palette.link();
|
|
|
|
if (index.row() == (guidList_.size() - 1)) // Aggregate Row
|
|
|
|
return palette.brightText();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == Qt::FontRole ) {
|
|
|
|
if (index.row() == (guidList_.size() - 1)) { // Aggregate Row
|
|
|
|
QFont font;
|
|
|
|
font.setBold(true);
|
|
|
|
return font;
|
|
|
|
}
|
2017-01-10 06:43:14 -06:00
|
|
|
}
|
|
|
|
|
2016-11-26 02:07:36 -06:00
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
2016-12-27 08:50:36 -06:00
|
|
|
if (index.column() < kMaxAggrStreamStats) {
|
|
|
|
int stat = index.column() % kMaxAggrStreamStats;
|
|
|
|
switch (stat) {
|
|
|
|
case kAggrRxPkts:
|
2017-01-10 07:20:22 -06:00
|
|
|
return QString("%L1").arg(aggrGuidStats_.value(guid).rxPkts);
|
2016-12-27 08:50:36 -06:00
|
|
|
case kAggrTxPkts:
|
2017-01-10 07:20:22 -06:00
|
|
|
return QString("%L1").arg(aggrGuidStats_.value(guid).txPkts);
|
2016-12-27 08:50:36 -06:00
|
|
|
case kAggrPktLoss:
|
2017-01-10 07:20:22 -06:00
|
|
|
return QString("%L1").arg(aggrGuidStats_.value(guid).pktLoss);
|
2016-12-27 08:50:36 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
PortGroupPort pgp = portList_.at(portColumn/kMaxStreamStats);
|
|
|
|
int stat = portColumn % kMaxStreamStats;
|
2016-11-26 02:07:36 -06:00
|
|
|
|
|
|
|
switch (stat) {
|
|
|
|
case kRxPkts:
|
2016-11-27 00:26:18 -06:00
|
|
|
return QString("%L1").arg(streamStats_.value(guid).value(pgp).rxPkts);
|
2016-11-26 02:07:36 -06:00
|
|
|
case kTxPkts:
|
2016-11-27 00:26:18 -06:00
|
|
|
return QString("%L1").arg(streamStats_.value(guid).value(pgp).txPkts);
|
2016-11-26 02:07:36 -06:00
|
|
|
case kRxBytes:
|
2016-11-27 00:26:18 -06:00
|
|
|
return QString("%L1").arg(streamStats_.value(guid).value(pgp).rxBytes);
|
2016-11-26 02:07:36 -06:00
|
|
|
case kTxBytes:
|
2016-11-27 00:26:18 -06:00
|
|
|
return QString("%L1").arg(streamStats_.value(guid).value(pgp).txBytes);
|
2016-11-26 02:07:36 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
2016-11-24 10:14:38 -06:00
|
|
|
}
|
|
|
|
|
2020-03-17 08:19:12 -05:00
|
|
|
Qt::DropActions StreamStatsModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::IgnoreAction; // read-only model, doesn't accept any data
|
|
|
|
}
|
|
|
|
|
2016-11-26 02:07:36 -06:00
|
|
|
// --------------------------------------------- //
|
|
|
|
// Slots
|
|
|
|
// --------------------------------------------- //
|
2016-11-24 10:14:38 -06:00
|
|
|
void StreamStatsModel::clearStats()
|
|
|
|
{
|
2016-11-26 02:07:36 -06:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
beginResetModel();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
guidList_.clear();
|
|
|
|
portList_.clear();
|
|
|
|
streamStats_.clear();
|
2017-01-10 07:20:22 -06:00
|
|
|
aggrGuidStats_.clear();
|
2016-11-26 02:07:36 -06:00
|
|
|
|
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
endResetModel();
|
|
|
|
#else
|
|
|
|
reset();
|
|
|
|
#endif
|
2016-11-24 10:14:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamStatsModel::appendStreamStatsList(
|
2016-11-26 02:07:36 -06:00
|
|
|
quint32 portGroupId,
|
2016-11-24 10:14:38 -06:00
|
|
|
const OstProto::StreamStatsList *stats)
|
|
|
|
{
|
2016-11-26 02:07:36 -06:00
|
|
|
int n = stats->stream_stats_size();
|
|
|
|
|
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
beginResetModel();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
const OstProto::StreamStats &s = stats->stream_stats(i);
|
|
|
|
PortGroupPort pgp = PortGroupPort(portGroupId, s.port_id().id());
|
|
|
|
Guid guid = s.stream_guid().id();
|
|
|
|
StreamStats &ss = streamStats_[guid][pgp];
|
2017-01-10 10:13:58 -06:00
|
|
|
StreamStats &aggrPort = streamStats_[kAggrGuid][pgp];
|
2017-01-10 07:20:22 -06:00
|
|
|
AggrGuidStats &aggrGuid = aggrGuidStats_[guid];
|
2017-01-10 10:13:58 -06:00
|
|
|
AggrGuidStats &aggrAggr = aggrGuidStats_[kAggrGuid];
|
2016-11-26 02:07:36 -06:00
|
|
|
|
|
|
|
ss.rxPkts = s.rx_pkts();
|
|
|
|
ss.txPkts = s.tx_pkts();
|
|
|
|
ss.rxBytes = s.rx_bytes();
|
|
|
|
ss.txBytes = s.tx_bytes();
|
|
|
|
|
2017-01-10 10:13:58 -06:00
|
|
|
aggrPort.rxPkts += ss.rxPkts;
|
|
|
|
aggrPort.txPkts += ss.txPkts;
|
|
|
|
aggrPort.rxBytes += ss.rxBytes;
|
|
|
|
aggrPort.txBytes += ss.txBytes;
|
|
|
|
|
2017-01-10 07:20:22 -06:00
|
|
|
aggrGuid.rxPkts += ss.rxPkts;
|
|
|
|
aggrGuid.txPkts += ss.txPkts;
|
|
|
|
aggrGuid.pktLoss += ss.txPkts - ss.rxPkts;
|
2016-12-27 08:50:36 -06:00
|
|
|
|
2017-01-10 10:13:58 -06:00
|
|
|
aggrAggr.rxPkts += ss.rxPkts;
|
|
|
|
aggrAggr.txPkts += ss.txPkts;
|
|
|
|
aggrAggr.pktLoss += ss.txPkts - ss.rxPkts;
|
|
|
|
|
2016-11-26 02:07:36 -06:00
|
|
|
if (!portList_.contains(pgp))
|
|
|
|
portList_.append(pgp);
|
|
|
|
if (!guidList_.contains(guid))
|
|
|
|
guidList_.append(guid);
|
|
|
|
}
|
|
|
|
|
2020-10-23 02:12:57 -05:00
|
|
|
if (guidList_.size() && !guidList_.contains(kAggrGuid))
|
2017-01-10 10:13:58 -06:00
|
|
|
guidList_.append(kAggrGuid);
|
|
|
|
|
2016-11-26 02:07:36 -06:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
endResetModel();
|
|
|
|
#else
|
|
|
|
reset();
|
|
|
|
#endif
|
2016-11-24 10:14:38 -06:00
|
|
|
|
|
|
|
// Prevent receiving any future updates from this sender
|
|
|
|
disconnect(sender(), 0, this, 0);
|
|
|
|
}
|