From 07a458367bbde5eada5bcb7e3e665f8026f0bba1 Mon Sep 17 00:00:00 2001 From: drbob Date: Sun, 3 Feb 2008 16:43:03 +0000 Subject: [PATCH] Addition of sorting algorithms to the LinksDialog. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@330 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/services/p3ranking.cc | 203 +++++++++++++++++++++--- retroshare-gui/src/gui/LinksDialog.cpp | 15 +- 2 files changed, 192 insertions(+), 26 deletions(-) diff --git a/libretroshare/src/services/p3ranking.cc b/libretroshare/src/services/p3ranking.cc index 7ba59e8a3..ac05fdd2b 100644 --- a/libretroshare/src/services/p3ranking.cc +++ b/libretroshare/src/services/p3ranking.cc @@ -38,6 +38,7 @@ std::string generateRandomLinkId(); * */ +#define RANK_DEBUG 1 p3Ranking::p3Ranking(uint16_t subtype, CacheTransfer *cft, std::string sourcedir, std::string storedir, @@ -47,6 +48,9 @@ p3Ranking::p3Ranking(uint16_t subtype, CacheTransfer *cft, mStorePeriod(storePeriod) { mOwnId = getAuthMgr()->OwnId(); + mViewPeriod = 60 * 60 * 24 * 30; /* one Month */ + mSortType = RS_RANK_ALG; + createDummyData(); return; } @@ -173,53 +177,176 @@ void p3Ranking::addRankMsg(RsRankMsg *msg) bool p3Ranking::setSortPeriod(uint32_t period) { + bool reSort = (mViewPeriod != period); mViewPeriod = period; + + if (reSort) + { + sortAllMsgs(); + } + return true; } bool p3Ranking::setSortMethod(uint32_t type) { + bool reSort = (mSortType != type); mSortType = type; + + if (reSort) + { + sortAllMsgs(); + } + return true; } bool p3Ranking::clearPeerFilter() { + bool reSort = (mPeerFilter.size() > 0); + mPeerFilter.clear(); + + if (reSort) + { + sortAllMsgs(); + } + return true; } bool p3Ranking::setPeerFilter(std::list peers) { mPeerFilter = peers; + + sortAllMsgs(); + return true; } -float p3Ranking::locked_calcRank(RankGroup &grp) /* returns 0->100 */ +float p3Ranking::locked_calcRank(RankGroup &grp) { + /* Ranking Calculations ..... + */ -#if 0 - /* where all the work is done */ - bool doScore = (mSortType == - bool doTime = (mSortType == + time_t now = time(NULL); + time_t minTime = now-mViewPeriod; bool doFilter = (mPeerFilter.size() > 0); - float rank = 0; + bool doScore = (mSortType & RS_RANK_SCORE); + bool doTime = (mSortType & RS_RANK_TIME); - for(it = grp.comments.begin(); it != grp.comments.end(); it++) - { - - if (doFilter) - { - /* do first so we can discard */ + uint32_t count = 0; + float algScore = 0; - - if (doScore) - if (mSortType +#ifdef RANK_DEBUG + std::string normlink(grp.link.begin(), grp.link.end()); + std::cerr << "p3Ranking::locked_calcRank() for: " << normlink; + std::cerr << std::endl; + std::cerr << "Period: " << mViewPeriod; + std::cerr << " doFilter: " << doFilter; + std::cerr << " doScore: " << doScore; + std::cerr << " doTime: " << doTime; + std::cerr << std::endl; #endif - return 100; + std::map::iterator it; + for(it = grp.comments.begin(); it != grp.comments.end(); it++) + { +#ifdef RANK_DEBUG + std::cerr << "Comment by:" << it->first << " age: " << now - it->second->timestamp; + std::cerr << std::endl; +#endif + if (doFilter) + { + if (mPeerFilter.end() == + std::find(mPeerFilter.begin(), mPeerFilter.end(), it->first)) + { + continue; /* skip it */ +#ifdef RANK_DEBUG + std::cerr << "\tFiltered Out"; + std::cerr << std::endl; +#endif + + } + } + + /* if Scoring is involved... drop old ones */ + if ((doScore) && (it->second->timestamp < minTime)) + { +#ifdef RANK_DEBUG + std::cerr << "\tToo Old"; + std::cerr << std::endl; +#endif + continue; + } + + time_t deltaT; + if (it->second->timestamp > now) + { + deltaT = it->second->timestamp - now; + } + else + { + deltaT = now - it->second->timestamp; + } + float timeScore = ((float) mViewPeriod - deltaT) / (mViewPeriod + 0.01); + +#ifdef RANK_DEBUG + std::cerr << "\tTimeScore: " << timeScore; + std::cerr << std::endl; +#endif + + /* algScore is sum of (filtered) timeScores */ + /* timeScore is average of (all) timeScores */ + /* popScore is just count of valid scores */ + + algScore += timeScore; + count++; + } + +#ifdef RANK_DEBUG + std::cerr << "p3Ranking::locked_calcRank() algScore: " << algScore; + std::cerr << " Count: " << count; + std::cerr << std::endl; +#endif + + if ((count < 0) || (algScore < 0)) + { +#ifdef RANK_DEBUG + std::cerr << "Final score: 0"; + std::cerr << std::endl; +#endif + return 0; + } + + if ((doScore) && (doTime)) + { +#ifdef RANK_DEBUG + std::cerr << "Final (alg) score:" << algScore; + std::cerr << std::endl; +#endif + return algScore; + } + else if (doScore) + { +#ifdef RANK_DEBUG + std::cerr << "Final (pop) score:" << count; + std::cerr << std::endl; +#endif + return count; + } + else if (doTime) + { +#ifdef RANK_DEBUG + std::cerr << "Final (time) score:" << algScore / count; + std::cerr << std::endl; +#endif + return algScore / count; + } + return 0; } + void p3Ranking::reSortGroup(RankGroup &grp) { std::string rid = grp.rid; @@ -281,11 +408,11 @@ float p3Ranking::getMaxRank() bool p3Ranking::getRankings(uint32_t first, uint32_t count, std::list &rids) { uint32_t i = 0; - std::multimap::iterator rit; - for(rit = mRankings.begin(); (i < first) && (rit != mRankings.end()); rit++); + std::multimap::reverse_iterator rit; + for(rit = mRankings.rbegin(); (i < first) && (rit != mRankings.rend()); rit++); i = 0; - for(; (i < count) && (rit != mRankings.end()); rit++) + for(; (i < count) && (rit != mRankings.rend()); rit++) { rids.push_back(rit->second); } @@ -404,7 +531,7 @@ void p3Ranking::createDummyData() msg->PeerId(mOwnId); msg->rid = "0001"; msg->title = L"Original Awesome Site!"; - msg->timestamp = now - 12345; + msg->timestamp = now - 60 * 60 * 24 * 15; msg->link = L"http://www.retroshare.org"; msg->comment = L"Retroshares Website"; @@ -424,12 +551,46 @@ void p3Ranking::createDummyData() msg->PeerId("ALTID"); msg->rid = "0002"; msg->title = L"Awesome Site!"; - msg->timestamp = now - 12345; + msg->timestamp = now - 60 * 60 * 24 * 29; msg->link = L"http://www.lunamutt.org"; msg->comment = L"Lunamutt's Website (TWO) How Long can this comment be!\n"; msg->comment += L"What happens to the second line?\n"; msg->comment += L"And a 3rd!"; addRankMsg(msg); + + msg = new RsRankMsg(); + msg->PeerId("ALTID2"); + msg->rid = "0002"; + msg->title = L"Awesome Site!"; + msg->timestamp = now - 60 * 60 * 7; + msg->link = L"http://www.lunamutt.org"; + msg->comment += L"A Short Comment"; + + addRankMsg(msg); + + + /***** Third one ****/ + + msg = new RsRankMsg(); + msg->PeerId(mOwnId); + msg->rid = "0003"; + msg->title = L"Weird Site!"; + msg->timestamp = now - 60 * 60; + msg->link = L"http://www.lunamutt.com"; + msg->comment = L""; + + addRankMsg(msg); + + msg = new RsRankMsg(); + msg->PeerId("ALTID"); + msg->rid = "0003"; + msg->title = L"Weird Site!"; + msg->timestamp = now - 60 * 60 * 24 * 2; + msg->link = L"http://www.lunamutt.com"; + msg->comment = L""; + + addRankMsg(msg); + } diff --git a/retroshare-gui/src/gui/LinksDialog.cpp b/retroshare-gui/src/gui/LinksDialog.cpp index c6e15731f..136a7a6f6 100644 --- a/retroshare-gui/src/gui/LinksDialog.cpp +++ b/retroshare-gui/src/gui/LinksDialog.cpp @@ -344,7 +344,14 @@ void LinksDialog::updateLinks() QTreeWidgetItem *child = new QTreeWidgetItem((QTreeWidget*)0); /* (0) Comment */ - child -> setText(0, QString::fromStdWString(cit->comment)); + if (cit->comment != L"") + { + child -> setText(0, QString::fromStdWString(cit->comment)); + } + else + { + child -> setText(0, "No Comment"); + } /* (2) Peer / Date */ { @@ -412,14 +419,12 @@ void LinksDialog::openLink ( QTreeWidgetItem * item, int column ) return; } - std::cerr << "LinksDialog::openLink() " << (item->text(0)).toStdString() << std::endl; + std::cerr << "LinksDialog::openLink() " << (item->text(2)).toStdString() << std::endl; /* open a browser */ - QUrl url(item->text(0)); + QUrl url(item->text(2)); QDesktopServices::openUrl ( url ); /* close expansion */ - //ui.linkTreeWidget->collapseItem(item); - bool state = item->isExpanded(); item->setExpanded(!state); }