Skip to content

Commit

Permalink
Fixes score calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jan 29, 2019
1 parent 0812863 commit e739e7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
24 changes: 14 additions & 10 deletions vendor/bat-native-ledger/src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@ BatPublishers::BatPublishers(bat_ledger::LedgerImpl* ledger):
ledger_(ledger),
state_(new braveledger_bat_helper::PUBLISHER_STATE_ST),
server_list_(std::map<std::string, braveledger_bat_helper::SERVER_LIST>()) {
calcScoreConsts();
calcScoreConsts(state_->min_publisher_duration_);
}

BatPublishers::~BatPublishers() {
}

void BatPublishers::calcScoreConsts() {
uint64_t min_duration_ms = state_->min_publisher_duration_ * 1000;
//TODO: check Warning C4244 '=': conversion from 'double' to 'unsigned int', possible loss of data
a_ = 1.0 / (braveledger_ledger::_d * 2.0) - min_duration_ms;
a2_ = a_ * 2;
a4_ = a2_ * 2;
void BatPublishers::calcScoreConsts(const uint64_t& min_duration) {
uint64_t min_duration_ms = min_duration * 1000;
a_ = (1.0 / (braveledger_ledger::_d * 2.0)) - min_duration_ms;
a2_ = a_ * 2.0;
a4_ = a2_ * 2.0;
b_ = min_duration_ms - a_;
b2_ = b_ * b_;
}

// courtesy of @dimitry-xyz: https://github.com/brave/ledger/issues/2#issuecomment-221752002
double BatPublishers::concaveScore(const uint64_t& duration) {
return (std::sqrt(b2_ + a4_ * duration) - b_) / (double)a2_;
return (-b_ + std::sqrt(b2_ + (a4_ * duration))) / a2_;
}

std::string getProviderName(const std::string& publisher_id) {
Expand Down Expand Up @@ -262,7 +261,7 @@ void BatPublishers::saveVisitInternal(
verified_old) {
publisher_info->visits += 1;
publisher_info->duration += duration;
publisher_info->score += concaveScore(duration);
publisher_info->score += concaveScore(duration * 1000);
publisher_info->reconcile_stamp = ledger_->GetReconcileStamp();

panel_info = std::make_unique<ledger::PublisherInfo>(*publisher_info);
Expand Down Expand Up @@ -529,6 +528,11 @@ void BatPublishers::synopsisNormalizerInternal(
}
double totalScores = 0.0;
for (size_t i = 0; i < list.size(); i++) {
// Check which would test uint problem from this issue
// https://github.com/brave/brave-browser/issues/3134
if (list[i].score < -100000) {
list[i].score = concaveScore(list[i].duration);
}
totalScores += list[i].score;
}
std::vector<unsigned int> percents;
Expand Down Expand Up @@ -755,7 +759,7 @@ bool BatPublishers::loadState(const std::string& data) {
return false;

state_.reset(new braveledger_bat_helper::PUBLISHER_STATE_ST(state));
calcScoreConsts();
calcScoreConsts(state_->min_publisher_duration_);
return true;
}

Expand Down
14 changes: 7 additions & 7 deletions vendor/bat-native-ledger/src/bat_publishers.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ class BatPublishers : public ledger::LedgerCallbackHandler {

void OnRestorePublishersInternal(bool success);

void calcScoreConsts(const uint64_t& duration);

double concaveScore(const uint64_t& duration);

void saveState();

void calcScoreConsts();

void synopsisNormalizer();
void synopsisNormalizerInternal(ledger::PublisherInfoList* newList, bool saveData,
const ledger::PublisherInfoList& list, uint32_t /* next_record */);
Expand All @@ -207,15 +207,15 @@ class BatPublishers : public ledger::LedgerCallbackHandler {

std::map<std::string, braveledger_bat_helper::SERVER_LIST> server_list_;

unsigned int a_;
double a_;

unsigned int a2_;
double a2_;

unsigned int a4_;
double a4_;

unsigned int b_;
double b_;

unsigned int b2_;
double b2_;
};

} // namespace braveledger_bat_publishers
Expand Down

0 comments on commit e739e7d

Please sign in to comment.