Skip to content

Commit

Permalink
Implement interestGroupNames namespace
Browse files Browse the repository at this point in the history
Bug: b/349858448
Change-Id: I5dc076feec81feced97a9ff3b414d3470e5745a9
GitOrigin-RevId: 77cb48a75c9b11d8729fcfffd36ee2997e0da21c
  • Loading branch information
lusayaa authored and Privacy Sandbox Team committed Jul 2, 2024
1 parent 4f82d2a commit c43bf11
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
9 changes: 9 additions & 0 deletions components/data_server/request_handler/get_values_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using google::protobuf::util::JsonStringToMessage;

constexpr char kKeysTag[] = "keys";
constexpr char kRenderUrlsTag[] = "renderUrls";
constexpr char kInterestGroupNamesTag[] = "interestGroupNames";
constexpr char kAdComponentRenderUrlsTag[] = "adComponentRenderUrls";
constexpr char kKvInternalTag[] = "kvInternal";
constexpr char kCustomTag[] = "custom";
Expand Down Expand Up @@ -71,6 +72,10 @@ v2::GetValuesRequest BuildV2Request(const v1::GetValuesRequest& v1_request) {
if (v1_request.keys_size() > 0) {
*partition->add_arguments() = BuildArgument(v1_request.keys(), kKeysTag);
}
if (v1_request.interest_group_names_size() > 0) {
*partition->add_arguments() = BuildArgument(
v1_request.interest_group_names(), kInterestGroupNamesTag);
}
if (v1_request.render_urls_size() > 0) {
*partition->add_arguments() =
BuildArgument(v1_request.render_urls(), kRenderUrlsTag);
Expand Down Expand Up @@ -146,6 +151,10 @@ void ProcessKeyGroupOutput(application_pa::KeyGroupOutput key_group_output,
if (tag_namespace_status_or.value() == kKeysTag) {
ProcessKeyValues(std::move(key_group_output), *v1_response.mutable_keys());
}
if (tag_namespace_status_or.value() == kInterestGroupNamesTag) {
ProcessKeyValues(std::move(key_group_output),
*v1_response.mutable_per_interest_group_data());
}
if (tag_namespace_status_or.value() == kRenderUrlsTag) {
ProcessKeyValues(std::move(key_group_output),
*v1_response.mutable_render_urls());
Expand Down
93 changes: 93 additions & 0 deletions components/data_server/request_handler/get_values_adapter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,98 @@ TEST_F(GetValuesAdapterTest, ValueWithStatusSuccess) {
EXPECT_THAT(v1_response, EqualsProto(v1_expected));
}

TEST_F(GetValuesAdapterTest, V1RequestWithInterestGroupNamesReturnsOk) {
UDFExecutionMetadata udf_metadata;
TextFormat::ParseFromString(kEmptyMetadata, &udf_metadata);
UDFArgument arg;
TextFormat::ParseFromString(R"(
tags {
values {
string_value: "custom"
}
values {
string_value: "interestGroupNames"
}
}
data {
list_value {
values {
string_value: "interestGroup1"
}
values {
string_value: "interestGroup2"
}
}
})",
&arg);
application_pa::KeyGroupOutputs key_group_outputs;
TextFormat::ParseFromString(R"(
key_group_outputs: {
tags: "custom"
tags: "interestGroupNames"
key_values: {
key: "interestGroup1"
value: {
value: {
string_value: "value1"
}
}
}
key_values: {
key: "interestGroup2"
value: {
value: {
string_value: "{\"priorityVector\":{\"signal1\":1}}"
}
}
}
}
)",
&key_group_outputs);
EXPECT_CALL(mock_udf_client_,
ExecuteCode(testing::_, EqualsProto(udf_metadata),
testing::ElementsAre(EqualsProto(arg)), testing::_))
.WillOnce(Return(
application_pa::KeyGroupOutputsToJson(key_group_outputs).value()));

v1::GetValuesRequest v1_request;
v1_request.add_interest_group_names("interestGroup1");
v1_request.add_interest_group_names("interestGroup2");
v1::GetValuesResponse v1_response;
auto status = get_values_adapter_->CallV2Handler(*request_context_factory_,
v1_request, v1_response);
EXPECT_TRUE(status.ok());
v1::GetValuesResponse v1_expected;
TextFormat::ParseFromString(
R"pb(
per_interest_group_data {
key: "interestGroup1"
value { value { string_value: "value1" } }
}
per_interest_group_data {
key: "interestGroup2"
value {
value {
struct_value {
fields {
key: "priorityVector"
value {
struct_value {
fields {
key: "signal1"
value { number_value: 1 }
}
}
}
}
}
}
}
})pb",
&v1_expected);
EXPECT_THAT(v1_response, EqualsProto(v1_expected));
}

} // namespace
} // namespace kv_server
7 changes: 7 additions & 0 deletions components/data_server/request_handler/get_values_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ grpc::Status GetValuesHandler::GetValues(
ProcessKeys(request_context, request.keys(), cache_,
*response->mutable_keys(), add_missing_keys_v1_);
}
if (!request.interest_group_names().empty()) {
PS_VLOG(5, request_context.GetPSLogContext())
<< "Processing interest_group_names for " << request.DebugString();
ProcessKeys(request_context, request.interest_group_names(), cache_,
*response->mutable_per_interest_group_data(),
add_missing_keys_v1_);
}
if (!request.render_urls().empty()) {
PS_VLOG(5, request_context.GetPSLogContext())
<< "Processing render_urls for " << request.DebugString();
Expand Down
29 changes: 29 additions & 0 deletions components/data_server/request_handler/get_values_handler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,34 @@ TEST_F(GetValuesHandlerTest, CallsV2Adapter) {
EXPECT_THAT(response, EqualsProto(adapter_response));
}

TEST_F(GetValuesHandlerTest, ReturnsPerInterestGroupData) {
EXPECT_CALL(mock_cache_, GetKeyValuePairs(_, UnorderedElementsAre("my_key")))
.Times(2)
.WillRepeatedly(Return(absl::flat_hash_map<std::string, std::string>{
{"my_key", "my_value"}}));
GetValuesRequest request;
request.add_interest_group_names("my_key");
GetValuesResponse response;
GetValuesHandler handler(mock_cache_, mock_get_values_adapter_,
/*use_v2=*/false);
const auto result =
handler.GetValues(GetRequestContextFactory(), request, &response);
ASSERT_TRUE(result.ok()) << "code: " << result.error_code()
<< ", msg: " << result.error_message();

GetValuesResponse expected;
TextFormat::ParseFromString(
R"pb(per_interest_group_data {
key: "my_key"
value { value { string_value: "my_value" } }
})pb",
&expected);
EXPECT_THAT(response, EqualsProto(expected));

ASSERT_TRUE(
handler.GetValues(GetRequestContextFactory(), request, &response).ok());
EXPECT_THAT(response, EqualsProto(expected));
}

} // namespace
} // namespace kv_server

0 comments on commit c43bf11

Please sign in to comment.