added a display of turtle search/tunnel requests over time, to help spotting potential spammers

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4216 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2011-05-23 21:45:25 +00:00
parent 0429baf807
commit 3f21835114
7 changed files with 270 additions and 52 deletions

View file

@ -1,10 +1,104 @@
#include <iostream>
#include <QTimer>
#include <QObject>
#include <retroshare/rsturtle.h>
#include <retroshare/rspeers.h>
#include "TurtleRouterDialog.h"
#include <QPainter>
#include <QStylePainter>
static const int MAX_TUNNEL_REQUESTS_DISPLAY = 10 ;
class TRHistogram
{
public:
TRHistogram(const std::vector<TurtleRequestDisplayInfo >& info) :_infos(info) {}
QColor colorScale(float f)
{
return QColor::fromHsv((int)((1.0-f)*280),200,255) ;
}
virtual void draw(QPainter *painter,int& ox,int& oy,const QString& title)
{
static const int MaxTime = 61 ;
static const int cellx = 6 ;
static const int celly = 10 ;
int save_ox = ox ;
painter->setPen(QColor::fromRgb(0,0,0)) ;
painter->drawText(2+ox,celly+oy,title) ;
oy+=2+2*celly ;
if(_infos.empty())
return ;
ox += 10 ;
std::map<std::string,std::vector<int> > hits ;
std::map<std::string,std::vector<int> >::iterator it ;
int max_hits = 1;
for(uint32_t i=0;i<_infos.size();++i)
{
std::vector<int>& h(hits[_infos[i].source_peer_id]) ;
if(h.size() <= _infos[i].age)
h.resize(MaxTime,0) ;
if(_infos[i].age < h.size())
{
h[_infos[i].age]++ ;
if(h[_infos[i].age] > max_hits)
max_hits = h[_infos[i].age] ;
}
}
int p=0 ;
for(it=hits.begin();it!=hits.end();++it,++p)
{
int total = 0 ;
for(int i=0;i<MaxTime;++i)
{
painter->fillRect(ox+i*cellx,oy+p*celly,cellx,celly,colorScale(it->second[i]/(float)max_hits)) ;
total += it->second[i] ;
}
painter->setPen(QColor::fromRgb(0,0,0)) ;
painter->drawText(ox+(MaxTime+1)*cellx,oy+(p+1)*celly,TurtleRouterDialog::getPeerName(it->first)) ;
painter->drawText(ox+(MaxTime+1)*cellx+80,oy+(p+1)*celly,"("+QString::number(total)+")") ;
}
painter->drawRect(ox,oy,MaxTime*cellx,p*celly) ;
for(int i=0;i<MaxTime;i+=5)
painter->drawText(ox+i*cellx,oy+(p+1)*celly+4,QString::number(i)) ;
oy += (p+1)*celly+4 ;
painter->drawText(ox,oy+celly,QObject::tr("(Age in seconds)"));
oy += 3*celly ;
// now, draw a scale
for(int i=0;i<=10;++i)
{
painter->fillRect(ox+i*(cellx+40),oy,cellx,celly,colorScale(i/10.0f)) ;
painter->setPen(QColor::fromRgb(0,0,0)) ;
painter->drawRect(ox+i*(cellx+40),oy,cellx,celly) ;
painter->drawText(ox+i*(cellx+40)+cellx+4,oy+celly,QString::number((int)(max_hits*i/10.0))) ;
}
oy += celly*2 ;
ox = save_ox ;
}
private:
const std::vector<TurtleRequestDisplayInfo>& _infos ;
};
TurtleRouterDialog::TurtleRouterDialog(QWidget *parent)
: RsAutoUpdatePage(2000,parent)
{
@ -26,17 +120,47 @@ TurtleRouterDialog::TurtleRouterDialog(QWidget *parent)
_f2f_TW->insertTopLevelItem(n++,top_level_t_requests) ;
top_level_hashes.clear() ;
QVBoxLayout v(_tunnel_statistics_F) ;
v.addWidget( _tst_CW = new TurtleRouterStatisticsWidget() ) ;
}
void TurtleRouterDialog::updateDisplay()
{
std::vector<std::vector<std::string> > hashes_info ;
std::vector<std::vector<std::string> > tunnels_info ;
std::vector<std::vector<std::string> > search_reqs_info ;
std::vector<std::vector<std::string> > tunnel_reqs_info ;
std::vector<TurtleRequestDisplayInfo > search_reqs_info ;
std::vector<TurtleRequestDisplayInfo > tunnel_reqs_info ;
rsTurtle->getInfo(hashes_info,tunnels_info,search_reqs_info,tunnel_reqs_info) ;
updateTunnelRequests(hashes_info,tunnels_info,search_reqs_info,tunnel_reqs_info) ;
_tst_CW->updateTunnelStatistics(hashes_info,tunnels_info,search_reqs_info,tunnel_reqs_info) ;
}
QString TurtleRouterDialog::getPeerName(const std::string& peer_id)
{
static std::map<std::string, QString> names ;
std::map<std::string,QString>::const_iterator it = names.find(peer_id) ;
if( it != names.end())
return it->second ;
else
{
RsPeerDetails detail ;
if(!rsPeers->getPeerDetails(peer_id,detail))
return "unknown peer";
return (names[peer_id] = QString::fromStdString(detail.name)) ;
}
}
void TurtleRouterDialog::updateTunnelRequests( const std::vector<std::vector<std::string> >& hashes_info,
const std::vector<std::vector<std::string> >& tunnels_info,
const std::vector<TurtleRequestDisplayInfo >& search_reqs_info,
const std::vector<TurtleRequestDisplayInfo >& tunnel_reqs_info)
{
// now display this in the QTableWidgets
QStringList stl ;
@ -73,7 +197,7 @@ void TurtleRouterDialog::updateDisplay()
for(uint i=0;i<search_reqs_info.size();++i)
{
QString str = QString::fromStdString( "Request id: " + search_reqs_info[i][0] + "\t from [" + search_reqs_info[i][1] + "]\t " + search_reqs_info[i][2]) ;
QString str = "Request id: " + QString::number(search_reqs_info[i].request_id,16) + "\t from [" + getPeerName(search_reqs_info[i].source_peer_id) + "]\t " + QString::number(search_reqs_info[i].age)+" secs ago" ;
stl.clear() ;
stl.push_back(str) ;
@ -82,12 +206,10 @@ void TurtleRouterDialog::updateDisplay()
}
top_level_s_requests->setText(0, tr("Search requests") + "(" + QString::number(search_reqs_info.size()) + ")" ) ;
int reqs_size = tunnel_reqs_info.size() ;
for(uint i=0;i<reqs_size;++i)
if(i+MAX_TUNNEL_REQUESTS_DISPLAY >= reqs_size || i < MAX_TUNNEL_REQUESTS_DISPLAY)
for(uint i=0;i<tunnel_reqs_info.size();++i)
if(i+MAX_TUNNEL_REQUESTS_DISPLAY >= tunnel_reqs_info.size() || i < MAX_TUNNEL_REQUESTS_DISPLAY)
{
QString str = QString::fromStdString( "Request id: " + tunnel_reqs_info[i][0] + "\t from [" + tunnel_reqs_info[i][1] + "]\t " + tunnel_reqs_info[i][2]) ;
QString str = "Request id: " + QString::number(tunnel_reqs_info[i].request_id,16) + "\t from [" + getPeerName(tunnel_reqs_info[i].source_peer_id) + "]\t " + QString::number(tunnel_reqs_info[i].age)+" secs ago" ;
stl.clear() ;
stl.push_back(str) ;
@ -148,4 +270,47 @@ QTreeWidgetItem *TurtleRouterDialog::findParentHashItem(const std::string& hash)
return items.front() ;
}
TurtleRouterStatisticsWidget::TurtleRouterStatisticsWidget(QWidget *parent)
: QWidget(parent)
{
maxWidth = 200 ;
maxHeight = 100 ;
}
void TurtleRouterStatisticsWidget::updateTunnelStatistics(const std::vector<std::vector<std::string> >& hashes_info,
const std::vector<std::vector<std::string> >& tunnels_info,
const std::vector<TurtleRequestDisplayInfo >& search_reqs_info,
const std::vector<TurtleRequestDisplayInfo >& tunnel_reqs_info)
{
QPixmap tmppixmap(maxWidth, maxHeight);
tmppixmap.fill(this, 0, 0);
QPainter painter(&tmppixmap);
painter.initFrom(this);
// std::cerr << "Drawing into pixmap of size " << maxWidth << "x" << maxHeight << std::endl;
// draw...
int ox=5,oy=5 ;
TRHistogram(search_reqs_info).draw(&painter,ox,oy,QObject::tr("Evolution of search requests:")) ;
TRHistogram(tunnel_reqs_info).draw(&painter,ox,oy,QObject::tr("Evolution of tunnel requests:")) ;
// update the pixmap
pixmap = tmppixmap;
}
void TurtleRouterStatisticsWidget::paintEvent(QPaintEvent *event)
{
QStylePainter(this).drawPixmap(0, 0, pixmap);
}
void TurtleRouterStatisticsWidget::resizeEvent(QResizeEvent *event)
{
QRect TaskGraphRect = geometry();
maxWidth = TaskGraphRect.width();
maxHeight = TaskGraphRect.height();
QWidget::resizeEvent(event);
}