diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/ad_rewards/ad_rewards.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/ad_rewards/ad_rewards.cc index 8378822e0982..113c26bba239 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/ad_rewards/ad_rewards.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/ad_rewards/ad_rewards.cc @@ -115,8 +115,36 @@ uint64_t AdRewards::GetAdsReceivedThisMonth() const { uint64_t AdRewards::GetAdsReceivedForMonth( const base::Time& time) const { - const PaymentInfo payment = payments_->GetForThisMonth(time); - return payment.transaction_count; + const TransactionList transactions = + ConfirmationsState::Get()->get_transactions(); + + uint64_t ads_received_this_month = 0; + + base::Time::Exploded exploded; + time.LocalExplode(&exploded); + + for (const auto& transaction : transactions) { + if (transaction.timestamp == 0) { + // Workaround for Windows crash when passing 0 to UTCExplode + continue; + } + + const base::Time transaction_time = + base::Time::FromDoubleT(transaction.timestamp); + + base::Time::Exploded transaction_time_exploded; + transaction_time.LocalExplode(&transaction_time_exploded); + + if (transaction_time_exploded.year == exploded.year && + transaction_time_exploded.month == exploded.month && + transaction.estimated_redemption_value > 0.0 && + ConfirmationType(transaction.confirmation_type) == + ConfirmationType::kViewed) { + ads_received_this_month++; + } + } + + return ads_received_this_month; } double AdRewards::GetEarningsForThisMonth() const { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.cc index 240540a193a5..0ead3fda9e52 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.cc @@ -5,7 +5,6 @@ #include "bat/ads/internal/account/statement/statement.h" -#include "base/time/time.h" #include "bat/ads/internal/account/ad_rewards/ad_rewards.h" #include "bat/ads/internal/account/transactions/transactions.h" #include "bat/ads/statement_info.h" @@ -32,7 +31,7 @@ StatementInfo Statement::Get( statement.next_payment_date = ad_rewards_->GetNextPaymentDate(); - statement.ads_received_this_month = ad_rewards_->GetAdsReceivedThisMonth(); + statement.ads_received_this_month = GetAdsReceivedThisMonth(); statement.earnings_this_month = GetEarningsForThisMonth(); @@ -46,6 +45,8 @@ StatementInfo Statement::Get( return statement; } +/////////////////////////////////////////////////////////////////////////////// + double Statement::GetEarningsForThisMonth() const { return ad_rewards_->GetEarningsForThisMonth(); } @@ -68,4 +69,9 @@ double Statement::GetEarningsForLastMonth() const { return ad_rewards_->GetEarningsForMonth(last_month); } +uint64_t Statement::GetAdsReceivedThisMonth() const { + const base::Time now = base::Time::Now(); + return transactions::GetCountForMonth(now); +} + } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.h b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.h index 1901620f15e9..6d2feca68fdc 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement.h @@ -8,6 +8,8 @@ #include +#include "base/time/time.h" + namespace ads { class AdRewards; @@ -28,6 +30,8 @@ class Statement { double GetEarningsForThisMonth() const; double GetEarningsForLastMonth() const; + uint64_t GetAdsReceivedThisMonth() const; + AdRewards* ad_rewards_; // NOT OWNED }; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement_unittest.cc index bf1a3670ee1a..f225221f97f4 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement_unittest.cc @@ -10,8 +10,8 @@ #include "net/http/http_status_code.h" #include "bat/ads/internal/account/ad_rewards/ad_rewards.h" #include "bat/ads/internal/account/ad_rewards/ad_rewards_delegate_mock.h" +#include "bat/ads/internal/account/transactions/transactions.h" #include "bat/ads/internal/account/wallet/wallet.h" -#include "bat/ads/internal/account/wallet/wallet_info.h" #include "bat/ads/internal/unittest_base.h" #include "bat/ads/internal/unittest_util.h" @@ -45,6 +45,16 @@ class BatAdsStatementTest : public UnitTestBase { return wallet.Get(); } + void AddTransactions( + const int count) { + for (int i = 0; i < count; i++) { + ConfirmationInfo confirmation; + confirmation.type = ConfirmationType::kViewed; + + transactions::Add(0.05, confirmation); + } + } + std::unique_ptr ad_rewards_; std::unique_ptr ad_rewards_delegate_mock_; std::unique_ptr statement_; @@ -94,7 +104,8 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -107,8 +118,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.0; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -140,7 +152,7 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("18 November 2020")); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -153,8 +165,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 0; expected_statement.earnings_this_month = 0.0; expected_statement.earnings_last_month = 0.0; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -186,7 +199,7 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("18 November 2020")); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -199,8 +212,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 0; expected_statement.earnings_this_month = 0.0; expected_statement.earnings_last_month = 0.0; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -249,7 +263,8 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -262,8 +277,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.0; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -312,7 +328,8 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -325,8 +342,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.0; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -380,7 +398,10 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("31 October 2020")); + AddTransactions(14); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -393,8 +414,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.7; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -448,7 +470,10 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); + AdvanceClock(TimeFromDateString("25 December 2019")); + AddTransactions(14); AdvanceClock(TimeFromDateString("1 January 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -461,8 +486,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.7; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -521,7 +547,12 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("12 September 2020")); + AddTransactions(9); + AdvanceClock(TimeFromDateString("31 October 2020")); + AddTransactions(14); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -534,8 +565,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.7; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -600,7 +632,12 @@ TEST_F(BatAdsStatementTest, const WalletInfo wallet = GetWallet(); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("12 September 2020")); + AddTransactions(9); + AdvanceClock(TimeFromDateString("31 October 2020")); + AddTransactions(14); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -613,8 +650,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 0.7; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -672,7 +710,10 @@ TEST_F(BatAdsStatementTest, ad_rewards_->MaybeReconcile(wallet); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("31 October 2020")); + AddTransactions(35); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -685,8 +726,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 1.75; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } @@ -766,7 +808,10 @@ TEST_F(BatAdsStatementTest, ad_rewards_->MaybeReconcile(wallet); ad_rewards_->MaybeReconcile(wallet); - AdvanceClock(TimeFromDateString("11 November 2020")); + AdvanceClock(TimeFromDateString("31 October 2020")); + AddTransactions(35); + AdvanceClock(TimeFromDateString("18 November 2020")); + AddTransactions(7); // Act const StatementInfo statement = statement_->Get(DistantPast(), Now()); @@ -779,8 +824,9 @@ TEST_F(BatAdsStatementTest, expected_statement.ads_received_this_month = 7; expected_statement.earnings_this_month = 0.35; expected_statement.earnings_last_month = 1.75; - expected_statement.transactions = {}; - expected_statement.uncleared_transactions = {}; + expected_statement.transactions = + transactions::GetCleared(DistantPast(), Now()); + expected_statement.uncleared_transactions = transactions::GetUncleared(); EXPECT_EQ(expected_statement, statement); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.cc index 0ca1b211cb2b..d4259dc1443b 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.cc @@ -7,7 +7,6 @@ #include -#include "base/time/time.h" #include "bat/ads/internal/confirmations/confirmation_info.h" #include "bat/ads/internal/confirmations/confirmations_state.h" #include "bat/ads/internal/logging.h" @@ -58,6 +57,40 @@ TransactionList GetUncleared() { return tail_transactions; } +uint64_t GetCountForMonth( + const base::Time& time) { + const TransactionList transactions = + ConfirmationsState::Get()->get_transactions(); + + uint64_t count = 0; + + base::Time::Exploded exploded; + time.LocalExplode(&exploded); + + for (const auto& transaction : transactions) { + if (transaction.timestamp == 0) { + // Workaround for Windows crash when passing 0 to UTCExplode + continue; + } + + const base::Time transaction_time = + base::Time::FromDoubleT(transaction.timestamp); + + base::Time::Exploded transaction_time_exploded; + transaction_time.LocalExplode(&transaction_time_exploded); + + if (transaction_time_exploded.year == exploded.year && + transaction_time_exploded.month == exploded.month && + transaction.estimated_redemption_value > 0.0 && + ConfirmationType(transaction.confirmation_type) == + ConfirmationType::kViewed) { + count++; + } + } + + return count; +} + void Add( const double estimated_redemption_value, const ConfirmationInfo& confirmation) { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.h b/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.h index 4ed6982b7986..1eeb7a57363a 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/transactions/transactions.h @@ -8,6 +8,7 @@ #include +#include "base/time/time.h" #include "bat/ads/transaction_info.h" namespace ads { @@ -22,6 +23,9 @@ TransactionList GetCleared( TransactionList GetUncleared(); +uint64_t GetCountForMonth( + const base::Time& time); + void Add( const double estimated_redemption_value, const ConfirmationInfo& confirmation);