added mechanism to allow services to document the names of their items, and improved bandwidth graph to show names for GXS services. Other services still need to supply their own names

This commit is contained in:
csoler 2017-04-20 20:54:51 +02:00
parent f406b81238
commit 5f8bf03dfe
15 changed files with 280 additions and 81 deletions

View file

@ -99,6 +99,11 @@ QString RSGraphSource::displayValue(float v) const
return QString::number(v,'f',_digits) + " " + unitName() ;
}
void RSGraphSource::getCumulatedValues(std::vector<float>& vals) const
{
for(std::map<std::string,float>::const_iterator it = _totals.begin();it!=_totals.end();++it)
vals.push_back(it->second) ;
}
void RSGraphSource::getCurrentValues(std::vector<QPointF>& vals) const
{
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
@ -108,9 +113,9 @@ void RSGraphSource::getCurrentValues(std::vector<QPointF>& vals) const
vals.push_back(QPointF( (now - it->second.back().first)/1000.0f,it->second.back().second)) ;
}
QString RSGraphSource::legend(int i,float v) const
QString RSGraphSource::legend(int i,float v,bool show_value) const
{
return displayName(i) + " (" + displayValue(v) + " )";
return displayName(i) + (show_value?(" (" + displayValue(v) + ")"):"");
}
void RSGraphSource::getDataPoints(int index,std::vector<QPointF>& pts,float filter_factor) const
@ -209,11 +214,30 @@ void RSGraphSource::update()
}
else
++it ;
updateTotals();
}
void RSGraphSource::updateTotals()
{
// now compute totals
_totals.clear();
for(std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it(_points.begin());it!=_points.end();++it)
{
float& f = _totals[it->first] ;
f = 0.0f ;
for(std::list<std::pair<qint64,float> >::const_iterator it2=it->second.begin();it2!=it->second.end();++it2)
f += (*it2).second ;
}
}
void RSGraphSource::reset()
{
_points.clear() ;
_points.clear();
_totals.clear();
}
void RSGraphSource::setCollectionTimeLimit(qint64 s) { _time_limit_msecs = s ; }
@ -630,8 +654,19 @@ void RSGraphWidget::paintLegend()
{
//int bottom = _rec.height();
std::vector<QPointF> vals ;
_source->getCurrentValues(vals) ;
std::vector<float> vals ;
if(_flags & RSGRAPH_FLAGS_LEGEND_CUMULATED)
_source->getCumulatedValues(vals) ;
else
{
std::vector<QPointF> cvals ;
_source->getCurrentValues(cvals) ;
for(uint32_t i=0;i<cvals.size();++i)
vals.push_back(cvals[i].y()) ;
}
int j=0;
float FS = QFontMetricsF(font()).height();
@ -640,12 +675,13 @@ void RSGraphWidget::paintLegend()
for(uint i=0;i<vals.size();++i)
if( _masked_entries.find(_source->displayName(i).toStdString()) == _masked_entries.end() )
{
if( _rec.width() - (vals[i].x()-0)*_time_scale < SCALE_WIDTH*fact )
continue ;
// if( _rec.width() - (vals[i].x()-0)*_time_scale < SCALE_WIDTH*fact )
// continue ;
qreal paintStep = 4*fact+FS;
qreal pos = 15*fact+j*paintStep;
QString text = _source->legend(i,vals[i].y()) ;
QString text = _source->legend(i,vals[i]) ;
QPen oldPen = _painter->pen();
_painter->setPen(QPen(getColor(i), Qt::SolidLine));

View file

@ -69,8 +69,11 @@ public:
// return the vector of last values up to date
virtual void getCurrentValues(std::vector<QPointF>& vals) const ;
// return the vector of cumulated values up to date
virtual void getCumulatedValues(std::vector<float>& vals) const;
// returns what to display in the legend. Derive this to show additional info.
virtual QString legend(int i,float v) const ;
virtual QString legend(int i, float v, bool show_value=true) const ;
// Returns the n^th interpolated value at the given time in floating point seconds backward.
virtual void getDataPoints(int index, std::vector<QPointF>& pts, float filter_factor=0.0f) const ;
@ -95,11 +98,13 @@ protected slots:
protected:
virtual void getValues(std::map<std::string,float>& values) const = 0 ;// overload this in your own class to fill in the values you want to display.
void updateTotals();
qint64 getTime() const ; // returns time in ms since RS has started
// Storage of collected events. The string is any string used to represent the collected data.
std::map<std::string, std::list<std::pair<qint64,float> > > _points ;
std::map<std::string, float> _totals ;
QTimer *_timer ;
@ -118,8 +123,9 @@ public:
static const uint32_t RSGRAPH_FLAGS_LOG_SCALE_Y = 0x0002 ;// log scale in Y
static const uint32_t RSGRAPH_FLAGS_ALWAYS_COLLECT = 0x0004 ;// keep collecting while not displayed
static const uint32_t RSGRAPH_FLAGS_PAINT_STYLE_PLAIN = 0x0008 ;// use plain / line drawing style
static const uint32_t RSGRAPH_FLAGS_SHOW_LEGEND = 0x0010 ;// show legend in the graph
static const uint32_t RSGRAPH_FLAGS_PAINT_STYLE_FLAT = 0x0020 ;// do not interpolate, and draw flat colored boxes
static const uint32_t RSGRAPH_FLAGS_SHOW_LEGEND = 0x0010 ;// show legend in the graph
static const uint32_t RSGRAPH_FLAGS_PAINT_STYLE_FLAT = 0x0020 ;// do not interpolate, and draw flat colored boxes
static const uint32_t RSGRAPH_FLAGS_LEGEND_CUMULATED = 0x0040 ;// show the total in the legend rather than current values
/** Bandwidth graph style. */
enum GraphStyle

View file

@ -107,16 +107,25 @@ void BWGraphSource::update()
// remove empty lists
float duration = 0.0f;
for(std::map<std::string,std::list<std::pair<qint64,float> > >::iterator it=_points.begin();it!=_points.end();)
if(it->second.empty())
{
std::map<std::string,std::list<std::pair<qint64,float> > >::iterator tmp(it) ;
++tmp;
_points.erase(it) ;
it=tmp ;
}
{
std::map<std::string,std::list<std::pair<qint64,float> > >::iterator tmp(it) ;
++tmp;
_points.erase(it) ;
it=tmp ;
}
else
{
float d = it->second.back().first - it->second.front().first;
if(duration < d)
duration = d ;
++it ;
}
// also clears history
@ -138,11 +147,36 @@ void BWGraphSource::update()
break ;
}
// now update the totals, and possibly convert into an average if the unit requires it.
updateTotals();
if(_current_unit == UNIT_KILOBYTES)
for(std::map<std::string,float>::iterator it(_totals.begin());it!=_totals.end();++it)
it->second /= (duration/1000.0) ;
#ifdef BWGRAPH_DEBUG
std::cerr << "Traffic history has size " << mTrafficHistory.size() << std::endl;
#endif
}
std::string BWGraphSource::makeSubItemName(uint16_t service_id,uint8_t sub_item_type) const
{
RsServiceInfoWithNames& s(mServiceInfoMap[service_id]) ;
if(s.item_names.empty())
return "item #"+QString("%1").arg(sub_item_type,2,16,QChar('0')).toStdString() ;
else
{
std::map<uint8_t,std::string>::const_iterator it = s.item_names.find(sub_item_type) ;
if(it == s.item_names.end())
return "item #"+QString("%1").arg(sub_item_type,2,16,QChar('0')).toStdString() + " (undocumented)";
return QString("%1").arg(sub_item_type,2,16,QChar('0')).toStdString()+": " + it->second ;
}
}
void BWGraphSource::convertTrafficClueToValues(const std::list<RSTrafficClue>& lst,std::map<std::string,float>& vals) const
{
vals.clear() ;
@ -162,7 +196,7 @@ void BWGraphSource::convertTrafficClueToValues(const std::list<RSTrafficClue>& l
for(uint32_t i=0;i<256;++i)
if(clue_per_sub_id[i].count > 0)
vals["item #"+QString::number(i,16).toStdString()] = (_current_unit == UNIT_KILOBYTES)?(clue_per_sub_id[i].size):(clue_per_sub_id[i].count) ;
vals[makeSubItemName(clue_per_sub_id[i].service_id,i)] = (_current_unit == UNIT_KILOBYTES)?(clue_per_sub_id[i].size):(clue_per_sub_id[i].count) ;
}
break ;
@ -233,11 +267,14 @@ void BWGraphSource::convertTrafficClueToValues(const std::list<RSTrafficClue>& l
for(std::list<RSTrafficClue>::const_iterator it(lst.begin());it!=lst.end();++it)
if(it->service_id == _current_selected_service)
{
clue_per_sub_id[it->service_sub_id] += *it ;
clue_per_sub_id[it->service_sub_id].service_id = it->service_id ;
}
for(uint32_t i=0;i<256;++i)
if(clue_per_sub_id[i].count > 0)
vals["item #"+QString::number(i,16).toStdString()] = (_current_unit == UNIT_KILOBYTES)?(clue_per_sub_id[i].size):(clue_per_sub_id[i].count) ;
vals[makeSubItemName(clue_per_sub_id[i].service_id,i)] = (_current_unit == UNIT_KILOBYTES)?(clue_per_sub_id[i].size):(clue_per_sub_id[i].count) ;
}
break ;
@ -297,7 +334,11 @@ BWGraphSource::BWGraphSource()
rsServiceControl->getOwnServices(rspsi) ;
for(std::map<uint32_t,RsServiceInfo>::const_iterator it(rspsi.mServiceList.begin());it!=rspsi.mServiceList.end();++it)
{
mServiceInfoMap[ (it->first >> 8) & 0xffff ] = it->second ;
rsServiceControl->getServiceItemNames(it->first,mServiceInfoMap[(it->first >> 8) & 0xffff].item_names) ;
}
}
void BWGraphSource::getValues(std::map<std::string,float>& values) const
@ -359,9 +400,9 @@ QString BWGraphSource::displayValue(float v) const
return QString() ;
}
QString BWGraphSource::legend(int i,float v) const
QString BWGraphSource::legend(int i,float v,bool show_value) const
{
return RSGraphSource::legend(i,v) ;//+ " Total: " + niceNumber(_total_recv) ;
return RSGraphSource::legend(i,v,show_value) ;
}
QString BWGraphSource::niceNumber(float v) const
{
@ -446,6 +487,7 @@ void BWGraphSource::setUnit(int unit)
recomputeCurrentCurves() ;
}
void BWGraphSource::setDirection(int dir)
{
if(dir == _current_direction)

View file

@ -13,6 +13,14 @@ public:
std::list<RSTrafficClue> out_rstcl ;
std::list<RSTrafficClue> in_rstcl ;
};
class RsServiceInfoWithNames: public RsServiceInfo
{
public:
RsServiceInfoWithNames(const RsServiceInfo& s) : RsServiceInfo(s) {}
RsServiceInfoWithNames(){}
std::map<uint8_t,std::string> item_names ;
};
BWGraphSource() ;
@ -25,7 +33,7 @@ public:
virtual void getValues(std::map<std::string,float>& values) const;
virtual QString displayValue(float v) const;
virtual QString legend(int i,float v) const;
virtual QString legend(int i,float v,bool show_value=true) const;
virtual void update();
QString unitName() const ;
@ -45,6 +53,7 @@ public:
protected:
void convertTrafficClueToValues(const std::list<RSTrafficClue> &lst, std::map<std::string, float> &vals) const;
std::string makeSubItemName(uint16_t service_id,uint8_t sub_item_type) const;
void recomputeCurrentCurves() ;
std::string visibleFriendName(const RsPeerId &pid) const ;
@ -67,7 +76,7 @@ private:
std::map<RsPeerId,std::string> mVisibleFriends ;
std::set<uint16_t> mVisibleServices ;
mutable std::map<uint16_t,RsServiceInfo> mServiceInfoMap ;
mutable std::map<uint16_t,RsServiceInfoWithNames> mServiceInfoMap ;
};
class BWGraph: public RSGraphWidget

View file

@ -28,13 +28,18 @@ BandwidthStatsWidget::BandwidthStatsWidget(QWidget *parent)
ui.bwgraph_BW->setSelector(BWGraphSource::SELECTOR_TYPE_SERVICE,BWGraphSource::GRAPH_TYPE_SUM) ;
ui.bwgraph_BW->setUnit(BWGraphSource::UNIT_KILOBYTES) ;
ui.bwgraph_BW->resetFlags(RSGraphWidget::RSGRAPH_FLAGS_LEGEND_CUMULATED) ;
updateUnitSelection(0);
// Setup connections
QObject::connect(ui.friend_CB ,SIGNAL(currentIndexChanged(int)),this, SLOT( updateFriendSelection(int))) ;
QObject::connect(ui.updn_CB ,SIGNAL(currentIndexChanged(int)),this, SLOT( updateUpDownSelection(int))) ;
QObject::connect(ui.unit_CB ,SIGNAL(currentIndexChanged(int)),this, SLOT( updateUnitSelection(int))) ;
QObject::connect(ui.service_CB,SIGNAL(currentIndexChanged(int)),this, SLOT(updateServiceSelection(int))) ;
QObject::connect(ui.logScale_CB,SIGNAL(toggled(bool)),this, SLOT(toggleLogScale(bool))) ;
QObject::connect(ui.friend_CB ,SIGNAL(currentIndexChanged(int )),this, SLOT( updateFriendSelection(int ))) ;
QObject::connect(ui.updn_CB ,SIGNAL(currentIndexChanged(int )),this, SLOT( updateUpDownSelection(int ))) ;
QObject::connect(ui.unit_CB ,SIGNAL(currentIndexChanged(int )),this, SLOT( updateUnitSelection(int ))) ;
QObject::connect(ui.service_CB ,SIGNAL(currentIndexChanged(int )),this, SLOT(updateServiceSelection(int ))) ;
QObject::connect(ui.legend_CB ,SIGNAL(currentIndexChanged(int )),this, SLOT( updateLegendType(int ))) ;
QObject::connect(ui.logScale_CB,SIGNAL( toggled(bool)),this, SLOT( toggleLogScale(bool))) ;
// setup one timer for auto-update
@ -156,6 +161,13 @@ void BandwidthStatsWidget::updateFriendSelection(int n)
ui.bwgraph_BW->setSelector(BWGraphSource::SELECTOR_TYPE_FRIEND,BWGraphSource::GRAPH_TYPE_SINGLE,ui.friend_CB->itemData(ci,Qt::UserRole).toString().toStdString()) ;
}
}
void BandwidthStatsWidget::updateLegendType(int n)
{
if(n==0)
ui.bwgraph_BW->resetFlags(RSGraphWidget::RSGRAPH_FLAGS_LEGEND_CUMULATED) ;
else
ui.bwgraph_BW->setFlags(RSGraphWidget::RSGRAPH_FLAGS_LEGEND_CUMULATED) ;
}
void BandwidthStatsWidget::updateServiceSelection(int n)
{
if(n == 0)
@ -187,7 +199,13 @@ void BandwidthStatsWidget::updateUpDownSelection(int n)
void BandwidthStatsWidget::updateUnitSelection(int n)
{
if(n==0)
{
ui.bwgraph_BW->setUnit(BWGraphSource::UNIT_KILOBYTES) ;
ui.legend_CB->setItemText(1,tr("Average"));
}
else
{
ui.bwgraph_BW->setUnit(BWGraphSource::UNIT_COUNT) ;
ui.legend_CB->setItemText(1,tr("Total"));
}
}

View file

@ -15,7 +15,8 @@ protected slots:
void updateUpDownSelection(int n);
void updateUnitSelection(int n);
void toggleLogScale(bool b);
void updateLegendType(int n);
private:
Ui::BwStatsWidget ui;

View file

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1128</width>
<width>1148</width>
<height>385</height>
</rect>
</property>
@ -14,7 +14,16 @@
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@ -103,6 +112,27 @@
<item>
<widget class="QComboBox" name="unit_CB"/>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Legend:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="legend_CB">
<item>
<property name="text">
<string>Current</string>
</property>
</item>
<item>
<property name="text">
<string>Total</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QCheckBox" name="logScale_CB">
<property name="text">