Skip to content

Commit

Permalink
Fixes ac table for 1min setting
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Feb 12, 2019
1 parent d735b7f commit c83662f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
12 changes: 6 additions & 6 deletions vendor/bat-native-ledger/src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,18 @@ BatPublishers::~BatPublishers() {
}

void BatPublishers::calcScoreConsts(const uint64_t& min_duration) {
uint64_t min_duration_ms = min_duration *
braveledger_ledger::_milliseconds_second;
a_ = (1.0 / (braveledger_ledger::_d * 2.0)) - min_duration_ms;
uint64_t min_duration_big = min_duration * 100;
a_ = (1.0 / (braveledger_ledger::_d * 2.0)) - min_duration_big;
a2_ = a_ * 2.0;
a4_ = a2_ * 2.0;
b_ = min_duration_ms - a_;
b_ = min_duration_big - a_;
b2_ = b_ * b_;
}

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

std::string getProviderName(const std::string& publisher_id) {
Expand Down Expand Up @@ -431,6 +430,7 @@ void BatPublishers::OnRestorePublishersInternal(bool success) {

void BatPublishers::setPublisherMinVisitTime(const uint64_t& duration) { // In seconds
state_->min_publisher_duration_ = duration;
calcScoreConsts(duration);
SynopsisNormalizer();
saveState();
}
Expand Down
10 changes: 6 additions & 4 deletions vendor/bat-native-ledger/src/bat_publishers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ struct PUBLISHER_STATE_ST;

namespace braveledger_bat_publishers {

// FORWARD_DECLARE_TEST(BatPublishersTest, calcScoreConsts);

class BatPublishers : public ledger::LedgerCallbackHandler {
public:

Expand Down Expand Up @@ -135,8 +133,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler {
bool isVerified(const std::string& publisher_id);

private:
friend class BatPublishersTest;
FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, calcScoreConsts);

void onPublisherActivitySave(uint64_t windowId,
const ledger::VisitData& visit_data,
ledger::Result result,
Expand Down Expand Up @@ -225,6 +222,11 @@ class BatPublishers : public ledger::LedgerCallbackHandler {
double b_;

double b2_;

// For testing purposes
friend class BatPublishersTest;
FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, calcScoreConsts);
FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, concaveScore);
};

} // namespace braveledger_bat_publishers
Expand Down
80 changes: 74 additions & 6 deletions vendor/bat-native-ledger/src/bat_publishers_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,82 @@ TEST_F(BatPublishersTest, calcScoreConsts) {
braveledger_bat_publishers::BatPublishers* publishers =
new braveledger_bat_publishers::BatPublishers(nullptr);

// Test 8 seconds
/*
* Test 5 seconds
*/
publishers->calcScoreConsts(5);

EXPECT_EQ(publishers->a_, 14500);
EXPECT_EQ(publishers->a2_, 29000);
EXPECT_EQ(publishers->a4_, 58000);
EXPECT_EQ(publishers->b_, -14000);
EXPECT_EQ(publishers->b2_, 196000000);

/*
* Test 8 seconds
*/
publishers->calcScoreConsts(8);

EXPECT_EQ(publishers->a_, 14200);
EXPECT_EQ(publishers->a2_, 28400);
EXPECT_EQ(publishers->a4_, 56800);
EXPECT_EQ(publishers->b_, -13400);
EXPECT_EQ(publishers->b2_, 179560000);

/*
* Test 1min (60 seconds)
*/
publishers->calcScoreConsts(60);

EXPECT_EQ(publishers->a_, 9000);
EXPECT_EQ(publishers->a2_, 18000);
EXPECT_EQ(publishers->a4_, 36000);
EXPECT_EQ(publishers->b_, -3000);
EXPECT_EQ(publishers->b2_, 9000000);
}

TEST_F(BatPublishersTest, concaveScore) {
braveledger_bat_publishers::BatPublishers* publishers =
new braveledger_bat_publishers::BatPublishers(nullptr);

/*
* min duration: 5 seconds
* duration: 5, 15, 60, 1000, 10000, 150000, 500000
*/
publishers->calcScoreConsts(5);
EXPECT_NEAR(publishers->concaveScore(5), 1, 0.001f);
EXPECT_NEAR(publishers->concaveScore(15), 1.06285, 0.001f);
EXPECT_NEAR(publishers->concaveScore(60), 1.28703, 0.001f);
EXPECT_NEAR(publishers->concaveScore(1000), 3.15289, 0.001f);
EXPECT_NEAR(publishers->concaveScore(10000), 8.80133, 0.001f);
EXPECT_NEAR(publishers->concaveScore(150000), 32.6498, 0.001f);
EXPECT_NEAR(publishers->concaveScore(500000), 59.2068, 0.001f);

/*
* min duration: 8 seconds
* duration: 5, 15, 60, 1000, 10000, 150000, 500000
*/
publishers->calcScoreConsts(8);
EXPECT_NEAR(publishers->concaveScore(5), 0.979606, 0.001f);
EXPECT_NEAR(publishers->concaveScore(15), 1.04477, 0.001f);
EXPECT_NEAR(publishers->concaveScore(60), 1.27505, 0.001f);
EXPECT_NEAR(publishers->concaveScore(1000), 3.16717, 0.001f);
EXPECT_NEAR(publishers->concaveScore(10000), 8.8769, 0.001f);
EXPECT_NEAR(publishers->concaveScore(150000), 32.9766, 0.001f);
EXPECT_NEAR(publishers->concaveScore(500000), 59.8128, 0.001f);

ASSERT_EQ(publishers->a_, 7000);
ASSERT_EQ(publishers->a2_, 14000);
ASSERT_EQ(publishers->a4_, 28000);
ASSERT_EQ(publishers->b_, 1000);
ASSERT_EQ(publishers->b2_, 1000000);
/*
* min duration: 60 seconds
* duration: 5, 15, 60, 1000, 10000, 150000, 500000
*/
publishers->calcScoreConsts(60);
EXPECT_NEAR(publishers->concaveScore(5), 0.455342, 0.001f);
EXPECT_NEAR(publishers->concaveScore(15), 0.607625, 0.001f);
EXPECT_NEAR(publishers->concaveScore(60), 1, 0.001f);
EXPECT_NEAR(publishers->concaveScore(1000), 3.50416, 0.001f);
EXPECT_NEAR(publishers->concaveScore(10000), 10.7089, 0.001f);
EXPECT_NEAR(publishers->concaveScore(150000), 40.9918, 0.001f);
EXPECT_NEAR(publishers->concaveScore(500000), 74.7025, 0.001f);
}

}

0 comments on commit c83662f

Please sign in to comment.