Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes panel query for the publisher #1642

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,17 @@ PublisherInfoDatabase::GetPanelPublisher(
}

sql::Statement info_sql(db_.GetUniqueStatement(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, "
"pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi "
"LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id "
"WHERE pi.publisher_id=? AND ((ai.month = ? "
"AND ai.year = ? AND ai.reconcile_stamp = ?) OR "
"ai.percent IS NULL) LIMIT 1"));
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't the original PR; but I would recommend looking at situations where using a view makes sense (instead putting the select statement here). With a view, you can select from it like a table and there's a possibility you can re-use the view in other methods. Kind of nice because you abstract away the joins and basically just have the business logic in here

"pi.provider, pi.verified, pi.excluded, "
"("
"SELECT IFNULL(percent, 0) FROM activity_info WHERE "
"publisher_id = ? AND reconcile_stamp = ? "
") as percent "
"FROM publisher_info AS pi WHERE pi.publisher_id = ? LIMIT 1"));

info_sql.BindString(0, filter.id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of hard-coding the number, you might initialize a variable (unsigned char, unsigned short, etc) to 0 and just ++ it on each field

unsigned short field_index = 0;
info_sql.BindString(field_index++, filter.id);
info_sql.BindInt64(field_index++, filter.reconcile_stamp);

info_sql.BindInt(1, filter.month);
info_sql.BindInt(2, filter.year);
info_sql.BindInt64(3, filter.reconcile_stamp);
info_sql.BindInt64(1, filter.reconcile_stamp);
info_sql.BindString(2, filter.id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID is being returned twice here; field 0 and 2. Is that correct?

Copy link
Contributor Author

@NejcZdovc NejcZdovc Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we bind id in two places


if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,65 @@ TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {

}

TEST_F(PublisherInfoDatabaseTest, GetPanelPublisher) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateTempDatabase(&temp_dir, &db_file);

/**
* Publisher ID is missing
*/
ledger::ActivityInfoFilter filter_1;
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr);

/**
* Empty table
*/
ledger::ActivityInfoFilter filter_2;
filter_2.id = "test";
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr);

/**
* Ignore month and year filter
*/
ledger::PublisherInfo info_1;
info_1.id = "brave.com";
info_1.url = "https://brave.com";
info_1.percent = 11;
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
info_1.year = 2019;
info_1.reconcile_stamp = 10;

bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
EXPECT_TRUE(success);

ledger::ActivityInfoFilter filter_3;
filter_3.id = "brave.com";
filter_3.month = ledger::ACTIVITY_MONTH::ANY;
filter_3.year = -1;
filter_3.reconcile_stamp = 10;
std::unique_ptr<ledger::PublisherInfo> result =
publisher_info_database_->GetPanelPublisher(filter_3);
EXPECT_TRUE(result);
EXPECT_EQ(result->id, "brave.com");

/**
* Still get data if reconcile stamp is not found
*/
info_1.id = "page.com";
info_1.url = "https://page.com";
info_1.reconcile_stamp = 9;

success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
EXPECT_TRUE(success);

ledger::ActivityInfoFilter filter_4;
filter_4.id = "page.com";
filter_4.reconcile_stamp = 10;
result = publisher_info_database_->GetPanelPublisher(filter_4);
EXPECT_TRUE(result);
EXPECT_EQ(result->id, "page.com");
EXPECT_EQ(result->percent, 0u);
}

} // namespace brave_rewards