improved RSGraph class. Used it for Turtle statistics and bandwidth statistics as well

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7612 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-10-15 22:00:49 +00:00
parent bd96859704
commit 8c1bf3cf8d
16 changed files with 267 additions and 145 deletions

View File

@ -65,6 +65,32 @@ void RSGraphSource::start()
int RSGraphSource::n_values() const { return _points.size() ; }
QString RSGraphSource::displayName(int i) const
{
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
int n=0;
for(it = _points.begin();it!=_points.end() && n<i;++it,++n) ;
if(n != i)
return QString("[error]");
return QString::fromStdString(it->first) ;
}
QString RSGraphSource::displayValue(float v) const
{
return QString::number(v,'g',2) + " " + unitName() ;
}
void RSGraphSource::getCurrentValues(std::vector<float>& vals) const
{
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
for(it = _points.begin();it!=_points.end();++it)
vals.push_back(it->second.back().second) ;
}
void RSGraphSource::getDataPoints(int index,std::vector<QPointF>& pts) const
{
pts.clear() ;
@ -222,6 +248,9 @@ void RSGraphWidget::paintEvent(QPaintEvent *)
/* Paint the rsDHT/allDHT totals */
paintTotals();
if(_flags & RSGRAPH_FLAGS_SHOW_LEGEND)
paintLegend() ;
/* Stop the painter */
_painter->end();
}
@ -229,7 +258,7 @@ void RSGraphWidget::paintEvent(QPaintEvent *)
QColor RSGraphWidget::getColor(int i)
{
// shuffle the colors a little bit
int h = (i*44497)%359 ;
int h = (i*86243)%359 ;
return QColor::fromHsv(h,128+127*(i&1),255) ;
}
@ -430,8 +459,10 @@ void RSGraphWidget::paintScale()
scale = pixelsToValue(i * paintStep);
QString text = _source->displayValue(scale) ;
_painter->setPen(SCALE_COLOR);
_painter->drawText(QPointF(5, pos+0.5*FONT_SIZE), tr("%1 %2").arg(scale, 0, 'f', _precision_digits).arg(unit_name));
_painter->drawText(QPointF(5, pos+0.5*FONT_SIZE), text);
_painter->setPen(GRID_COLOR);
_painter->drawLine(QPointF(SCALE_WIDTH, pos), QPointF(_rec.width(), pos));
}
@ -440,3 +471,26 @@ void RSGraphWidget::paintScale()
_painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
}
void RSGraphWidget::paintLegend()
{
int bottom = _rec.height();
std::vector<float> vals ;
_source->getCurrentValues(vals) ;
for(uint i=0;i<vals.size();++i)
{
qreal paintStep = 4+FONT_SIZE;
qreal pos = 20+i*paintStep;
QString text = _source->displayName(i) + " (" + _source->displayValue(vals[i]) + " )";
QPen oldPen = _painter->pen();
_painter->setPen(QPen(getColor(i), Qt::SolidLine));
_painter->drawLine(QPointF(SCALE_WIDTH+10.0, pos), QPointF(SCALE_WIDTH+30.0, pos));
_painter->setPen(oldPen);
_painter->setPen(SCALE_COLOR);
_painter->drawText(QPointF(SCALE_WIDTH + 40,pos + 0.5*FONT_SIZE), text) ;
}
}

View File

@ -59,19 +59,29 @@ public:
void stop() ;
void clear() ;
virtual int n_values() const ;
virtual QString unitName() const { return "" ; }// overload to give your own unit name (KB/s, Users, etc)
int n_values() const ;
// Might be overloaded in order to show a fancy digit number with adaptive units.
// The default is to return v + unitName()
virtual QString displayValue(float v) const ;
// return the vector of last values up to date
virtual void getCurrentValues(std::vector<float>& vals) 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) const ;
// returns the name to give to the nth entry in the graph
virtual QString displayName(int index) const ;
// Sets the maximum time for keeping values. Units=seconds.
void setCollectionTimeLimit(qint64 msecs) ;
// Sets the time period for collecting new values. Units=milliseconds.
void setCollectionTimePeriod(qint64 msecs) ;
virtual QString unitName() const =0;// overload to give your own unit name (KB/s, Users, etc)
protected slots:
// Calls the internal source for a new data points; called by the timer. You might want to overload this
// if the collection system needs it. Otherwise, the default method will call getValues()
@ -102,6 +112,7 @@ class RSGraphWidget: public QFrame
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
/** Bandwidth graph style. */
enum GraphStyle
@ -149,6 +160,8 @@ class RSGraphWidget: public QFrame
/** Paints the rsdht/alldht totals. */
void paintTotals();
/** Paints the scale in the graph. */
void paintLegend();
/** Paints the scale in the graph. */
void paintScale();
QColor getColor(int i) ;

View File

@ -1,7 +1,7 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* This program is free software; you can redistribute it and/or

View File

@ -20,6 +20,7 @@
****************************************************************/
#include "BwCtrlWindow.h"
#include "gui/common/RSGraphWidget.h"
#include "ui_BwCtrlWindow.h"
#include <QTimer>
#include <QDateTime>
@ -37,14 +38,21 @@
#include <QPainter>
#include <limits>
class BWListDelegate: public QAbstractItemDelegate
{
public:
BWListDelegate(QObject *parent=0);
virtual ~BWListDelegate();
void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const;
};
BWListDelegate::BWListDelegate(QObject *parent) : QAbstractItemDelegate(parent)
{
;
}
BWListDelegate::~BWListDelegate(void)
{
;
}
void BWListDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
@ -163,31 +171,25 @@ QSize BWListDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QM
return QSize(50,17);
}
BwCtrlWindow::BwCtrlWindow(QWidget *parent)
: RsAutoUpdatePage(1000,parent)
{
setupUi(this);
BWDelegate = new BWListDelegate();
bwTreeWidget->setItemDelegate(BWDelegate);
/* Set header resize modes and initial section sizes Peer TreeView*/
/* Set header resize modes and initial section sizes Peer TreeView*/
QHeaderView * _header = bwTreeWidget->header () ;
_header->resizeSection ( COLUMN_RSNAME, 170 );
}
BwCtrlWindow::~BwCtrlWindow()
{
}
void BwCtrlWindow::updateDisplay()
{
/* do nothing if locked, or not visible */
if (RsAutoUpdatePage::eventsLocked() == true)
{
@ -205,12 +207,7 @@ void BwCtrlWindow::updateDisplay()
return;
}
RsAutoUpdatePage::lockAllEvents();
//std::cerr << "BwCtrlWindow::update()" << std::endl;
updateBandwidth();
RsAutoUpdatePage::unlockAllEvents() ;
}
void BwCtrlWindow::updateBandwidth()
@ -223,7 +220,7 @@ void BwCtrlWindow::updateBandwidth()
std::map<RsPeerId, RsConfigDataRates> rateMap;
std::map<RsPeerId, RsConfigDataRates>::iterator it;
rsConfig->getTotalBandwidthRates(totalRates);
rsConfig->getTotalBandwidthRates(totalRates);
rsConfig->getAllBandwidthRates(rateMap);
/* insert */
@ -231,8 +228,6 @@ void BwCtrlWindow::updateBandwidth()
peerTreeWidget->addTopLevelItem(item);
peerTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
updateGraph(totalRates.mRateIn,totalRates.mRateOut);
/* do Totals */
item -> setData(COLUMN_PEERID, Qt::DisplayRole, tr("TOTALS"));
item -> setData(COLUMN_RSNAME, Qt::DisplayRole, tr("Totals"));
@ -369,10 +364,4 @@ void BwCtrlWindow::updateBandwidth()
}
}
/** Adds new data to the graph. */
void BwCtrlWindow::updateGraph(qreal bytesRead, qreal bytesWritten)
{
/* Graph only cares about kilobytes */
frmGraph->addPoints(bytesRead/*/1024.0*/, bytesWritten/*/1024.0*/);
}

View File

@ -1,6 +1,3 @@
#ifndef RSBWCTRL_WINDOW_H
#define RSBWCTRL_WINDOW_H
/****************************************************************
* RetroShare is distributed under the following license:
*
@ -22,11 +19,14 @@
* Boston, MA 02110-1301, USA.
****************************************************************/
#pragma once
#include <QMainWindow>
#include <QAbstractItemDelegate>
#include "RsAutoUpdatePage.h"
#include "gui/common/RSGraphWidget.h"
#include "ui_BwCtrlWindow.h"
// Defines for download list list columns
@ -48,47 +48,22 @@
class QModelIndex;
class QPainter;
class BWListDelegate ;
class BWListDelegate: public QAbstractItemDelegate {
Q_OBJECT
public:
BWListDelegate(QObject *parent=0);
~BWListDelegate();
void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const;
private:
public slots:
signals:
};
class BwCtrlWindow : public RsAutoUpdatePage, public Ui::BwCtrlWindow {
class BwCtrlWindow : public RsAutoUpdatePage, public Ui::BwCtrlWindow
{
Q_OBJECT
public:
BwCtrlWindow(QWidget *parent = 0);
~BwCtrlWindow();
void updateBandwidth();
void updateBandwidth();
public slots:
virtual void updateDisplay() ;
virtual void updateDisplay() ;
/** Adds new data to the graph */
void updateGraph(qreal bytesRead, qreal bytesWritten);
protected:
private:
BWListDelegate *BWDelegate;
BWListDelegate *BWDelegate;
};
#endif // RSBWCTRL_WINDOW_H

View File

@ -84,7 +84,7 @@
</property>
</column>
</widget>
<widget class="GraphFrame" name="frmGraph">
<widget class="BWGraph" name="frmGraph" native="true">
<property name="minimumSize">
<size>
<width>120</width>
@ -99,12 +99,6 @@
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
</widget>
</widget>
</item>
@ -112,9 +106,9 @@
</widget>
<customwidgets>
<customwidget>
<class>GraphFrame</class>
<extends>QFrame</extends>
<header>gui/graphframe.h</header>
<class>BWGraph</class>
<extends>QWidget</extends>
<header>gui/statistics/bwgraph.h</header>
<container>1</container>
</customwidget>
</customwidgets>

View File

@ -695,10 +695,3 @@ void DhtWindow::getDHTStatus()
// }
// }
}
/** Adds new data to the graph. */
void DhtWindow::updateGraph(qreal rsDHT, qreal allDHT)
{
// ui.frmGraph->addPoints(rsDHT, allDHT);
}

View File

@ -42,8 +42,6 @@ public:
public slots:
virtual void updateDisplay() ;
void updateGraph(qreal rsDHT, qreal allDHT);
protected:
//void changeEvent(QEvent *e);

View File

@ -22,6 +22,8 @@
* Boston, MA 02110-1301, USA.
****************************************************************/
#pragma once
#include <QMainWindow>
namespace Ui {

View File

@ -242,7 +242,6 @@ void TurtleRouterStatistics::updateDisplay()
//updateTunnelRequests(hashes_info,tunnels_info,search_reqs_info,tunnel_reqs_info) ;
_tst_CW->updateTunnelStatistics(hashes_info,tunnels_info,search_reqs_info,tunnel_reqs_info) ;
_tst_CW->update();
updateTunnelGraph();
}
QString TurtleRouterStatistics::getPeerName(const RsPeerId &peer_id)
@ -355,17 +354,3 @@ void TurtleRouterStatisticsWidget::resizeEvent(QResizeEvent *event)
QWidget::resizeEvent(event);
update();
}
void TurtleRouterStatistics::updateTunnelGraph()
{
TurtleTrafficStatisticsInfo info ;
rsTurtle->getTrafficStatistics(info) ;
updateGraph(info.tr_up_Bps,info.tr_dn_Bps,info.data_dn_Bps,info.data_up_Bps,info.unknown_updn_Bps);
}
void TurtleRouterStatistics::updateGraph(qreal bytesTrup, qreal bytesTrdown, qreal bytesDatadown, qreal bytesDataup, qreal bytesunknownupdn)
{
/* Graph only cares about kilobytes */
frmGraph->addPoints(bytesTrup/1024.0, bytesTrdown/1024.0, bytesDatadown/1024.0, bytesDataup/1024.0, bytesunknownupdn/1024.0 );
}

View File

@ -31,28 +31,23 @@ class TurtleRouterStatisticsWidget ;
class TurtleRouterStatistics: public RsAutoUpdatePage, public Ui::TurtleRouterStatistics
{
Q_OBJECT
Q_OBJECT
public:
TurtleRouterStatistics(QWidget *parent = NULL) ;
~TurtleRouterStatistics();
// Cache for peer names.
static QString getPeerName(const RsPeerId& peer_id) ;
public slots:
void updateTunnelGraph();
void updateGraph(qreal bytesTrup, qreal bytesTrdown, qreal bytesDatadown, qreal bytesDataup, qreal bytesunknownupdn);
public:
TurtleRouterStatistics(QWidget *parent = NULL) ;
~TurtleRouterStatistics();
// Cache for peer names.
static QString getPeerName(const RsPeerId& peer_id) ;
private:
void processSettings(bool bLoad);
bool m_bProcessSettings;
private:
virtual void updateDisplay() ;
void processSettings(bool bLoad);
bool m_bProcessSettings;
TurtleRouterStatisticsWidget *_tst_CW ;
virtual void updateDisplay() ;
TurtleRouterStatisticsWidget *_tst_CW ;
} ;
class TurtleRouterStatisticsWidget: public QWidget

View File

@ -47,8 +47,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>638</width>
<height>255</height>
<width>640</width>
<height>248</height>
</rect>
</property>
<property name="autoFillBackground">
@ -59,7 +59,7 @@
</widget>
</item>
<item row="1" column="0">
<widget class="GraphFrameTunnel" name="frmGraph">
<widget class="TurtleGraph" name="frmGraph" native="true">
<property name="minimumSize">
<size>
<width>120</width>
@ -74,12 +74,6 @@
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
</widget>
</item>
</layout>
@ -90,9 +84,9 @@
</widget>
<customwidgets>
<customwidget>
<class>GraphFrameTunnel</class>
<extends>QFrame</extends>
<header>gui/graphframetunnel.h</header>
<class>TurtleGraph</class>
<extends>QWidget</extends>
<header>gui/statistics/turtlegraph.h</header>
<container>1</container>
</customwidget>
</customwidgets>

View File

@ -0,0 +1,45 @@
#pragma once
#include "retroshare/rsconfig.h"
#include <gui/common/RSGraphWidget.h>
class BWGraphSource: public RSGraphSource
{
public:
virtual void getValues(std::map<std::string,float>& values) const
{
RsConfigDataRates totalRates;
rsConfig->getTotalBandwidthRates(totalRates);
values.insert(std::make_pair(std::string("Bytes in"),(float)totalRates.mRateIn)) ;
values.insert(std::make_pair(std::string("Bytes out"),(float)totalRates.mRateOut)) ;
}
virtual QString unitName() const { return tr("KB/s"); }
};
class BWGraph: public RSGraphWidget
{
public:
BWGraph(QWidget *parent)
: RSGraphWidget(parent)
{
BWGraphSource *src = new BWGraphSource() ;
src->setCollectionTimeLimit(30*60*1000) ; // 30 mins
src->setCollectionTimePeriod(1000) ; // collect every second
src->start() ;
addSource(src) ;
setTimeScale(1.0f) ; // 1 pixels per second of time.
setScaleParams(2) ;
resetFlags(RSGRAPH_FLAGS_LOG_SCALE_Y) ;
resetFlags(RSGRAPH_FLAGS_PAINT_STYLE_PLAIN) ;
setFlags(RSGRAPH_FLAGS_SHOW_LEGEND) ;
}
};

View File

@ -26,26 +26,56 @@
#include <QApplication>
#include <gui/common/RSGraphWidget.h>
#define HOR_SPC 2 /** Space between data points */
#define SCALE_WIDTH 75 /** Width of the scale */
#define MINUSER_SCALE 2000 /** 2000 users is the minimum scale */
#define SCROLL_STEP 4 /** Horizontal change on graph update */
#include <retroshare/rsdht.h>
#include <retroshare/rsconfig.h>
#include "dhtgraph.h"
#define BACK_COLOR Qt::white
#define SCALE_COLOR Qt::black
#define GRID_COLOR Qt::black
#define RSDHT_COLOR Qt::magenta
#define ALLDHT_COLOR Qt::yellow
class DHTGraphSource: public RSGraphSource
{
public:
virtual int n_values() const
{
return 1 ;
}
virtual void getValues(std::map<std::string,float>& values) const
{
RsConfigNetStatus config;
rsConfig->getConfigNetStatus(config);
if (config.DHTActive && config.netDhtOk)
{
values.insert(std::make_pair(std::string("RS Net size"),(float)config.netDhtRsNetSize)) ;
//values.insert(std::make_pair(std::string("GLobal Net size"),(float)config.netDhtNetSize)) ;
}
else
{
values.insert(std::make_pair(std::string("RS Net size"),0.0f)) ;
//values.insert(std::make_pair(std::string("GLobal Net size"),0.0f)) ;
}
}
virtual QString unitName() const { return tr("users"); }
};
#define FONT_SIZE 11
class DhtGraph : public RSGraphWidget
{
public:
DhtGraph(QWidget *parent = 0);
public:
DhtGraph(QWidget *parent = 0)
: RSGraphWidget(parent)
{
DHTGraphSource *src = new DHTGraphSource() ;
/** Show the respective lines and counters. */
bool _showRSDHT;
bool _showALLDHT;
src->setCollectionTimeLimit(30*60*1000) ; // 30 mins
src->setCollectionTimePeriod(1000) ; // collect every second
src->start() ;
addSource(src) ;
setTimeScale(1.0f) ; // 1 pixels per second of time.
setScaleParams(0) ;
resetFlags(RSGRAPH_FLAGS_LOG_SCALE_Y) ;
setFlags(RSGRAPH_FLAGS_PAINT_STYLE_PLAIN) ;
}
};

View File

@ -0,0 +1,54 @@
#pragma once
#include "retroshare/rsturtle.h"
#include <gui/common/RSGraphWidget.h>
class TurtleGraphSource: public RSGraphSource
{
public:
virtual void getValues(std::map<std::string,float>& values) const
{
TurtleTrafficStatisticsInfo info ;
rsTurtle->getTrafficStatistics(info) ;
values.insert(std::make_pair(QObject::tr("TR up").toStdString(),(float)info.tr_up_Bps)) ;
values.insert(std::make_pair(QObject::tr("TR dn").toStdString(),(float)info.tr_dn_Bps)) ;
values.insert(std::make_pair(QObject::tr("Data up").toStdString(),(float)info.data_up_Bps)) ;
values.insert(std::make_pair(QObject::tr("Data dn").toStdString(),(float)info.data_dn_Bps)) ;
values.insert(std::make_pair(QObject::tr("Data forward").toStdString(),(float)info.unknown_updn_Bps)) ;
}
virtual QString displayValue(float v) const
{
if(v < 1000)
return QString::number(v,'g',2) + " B/s" ;
else if(v < 1000*1024)
return QString::number(v/1024.0,'g',2) + " KB/s" ;
else
return QString::number(v/(1024.0*1024),'g',2) + " MB/s" ;
}
};
class TurtleGraph: public RSGraphWidget
{
public:
TurtleGraph(QWidget *parent)
: RSGraphWidget(parent)
{
TurtleGraphSource *src = new TurtleGraphSource() ;
src->setCollectionTimeLimit(30*60*1000) ; // 30 mins
src->setCollectionTimePeriod(1000) ; // collect every second
src->start() ;
addSource(src) ;
setTimeScale(1.0f) ; // 1 pixels per second of time.
setScaleParams(2) ;
resetFlags(RSGRAPH_FLAGS_LOG_SCALE_Y) ;
resetFlags(RSGRAPH_FLAGS_PAINT_STYLE_PLAIN) ;
}
};

View File

@ -344,6 +344,8 @@ HEADERS += rshare.h \
gui/statistics/TurtleRouterDialog.h \
gui/statistics/TurtleRouterStatistics.h \
gui/statistics/dhtgraph.h \
gui/statistics/bwgraph.h \
gui/statistics/turtlegraph.h \
gui/FileTransfer/TransferUserNotify.h \
gui/plugins/PluginInterface.h \
gui/im_history/ImHistoryBrowser.h \
@ -684,7 +686,6 @@ SOURCES += main.cpp \
gui/FileTransfer/xprogressbar.cpp \
gui/statistics/TurtleRouterDialog.cpp \
gui/statistics/TurtleRouterStatistics.cpp \
gui/statistics/dhtgraph.cpp \
gui/statistics/GlobalRouterStatistics.cpp \
gui/FileTransfer/DetailsDialog.cpp \
gui/FileTransfer/TransferUserNotify.cpp \