Skip to content

Commit

Permalink
Controls: Add Discovery Engine representation
Browse files Browse the repository at this point in the history
This adds `Control#to_discovery_engine_control` (as well as
`#to_discovery_engine_control_action` for `BoostAction` and
`FilterAction`) to convert a local record into a hash for use by the
Discovery Engine API client.

See https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-ControlService-Client
  • Loading branch information
csutter committed Feb 3, 2025
1 parent 8933f64 commit ef774ba
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/models/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,31 @@ class Control < ApplicationRecord
accepts_nested_attributes_for :action

validates :display_name, presence: true

# Returns a representation of this Control as a Discovery Engine control resource
def to_discovery_engine_control
{
name:,
display_name:,
**action.to_discovery_engine_control_action,
solution_type: Google::Cloud::DiscoveryEngine::V1::SolutionType::SOLUTION_TYPE_SEARCH,
# Trip hazard: despite the plural name, this expects _one_ use case in an array
use_cases: [Google::Cloud::DiscoveryEngine::V1::SearchUseCase::SEARCH_USE_CASE_SEARCH],
}
end

# The fully qualified name of the control on Discovery Engine (like a path)
def name
[parent, "controls", discovery_engine_id].join("/")
end

# The parent of the control on Discovery Engine (always the engine)
def parent
Rails.configuration.discovery_engine_engine
end

# The ID of the resource on Discovery Engine
def discovery_engine_id
"search-admin-#{id}"
end
end
11 changes: 11 additions & 0 deletions app/models/control/boost_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ class Control::BoostAction < ApplicationRecord

validates :filter_expression, presence: true
validates :boost_factor, numericality: { in: BOOST_FACTOR_RANGE, other_than: 0 }

# Returns a representation of this boost as part of a Discovery Engine control resource
def to_discovery_engine_control_action
{
boost_action: {
filter: filter_expression,
boost: boost_factor,
data_store: Rails.configuration.discovery_engine_datastore,
},
}
end
end
10 changes: 10 additions & 0 deletions app/models/control/filter_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ class Control::FilterAction < ApplicationRecord
include Control::Actionable

validates :filter_expression, presence: true

# Returns a representation of this filter as part of a Discovery Engine control resource
def to_discovery_engine_control_action
{
filter_action: {
filter: filter_expression,
data_store: Rails.configuration.discovery_engine_datastore,
},
}
end
end
16 changes: 16 additions & 0 deletions spec/models/control/boost_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@
end
end
end

describe "#to_discovery_engine_control_action" do
subject(:boost) do
build_stubbed(:control_boost_action, filter_expression: "foo = 1", boost_factor: 0.13)
end

it "returns a representation of the action for Discovery Engine" do
expect(boost.to_discovery_engine_control_action).to eq({
boost_action: {
filter: "foo = 1",
boost: 0.13,
data_store: "[datastore]",
},
})
end
end
end
13 changes: 13 additions & 0 deletions spec/models/control/filter_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@
end
end
end

describe "#to_discovery_engine_control_action" do
subject(:filter) { build_stubbed(:control_filter_action, filter_expression: "foo = 1") }

it "returns a representation of the action for Discovery Engine" do
expect(filter.to_discovery_engine_control_action).to eq({
filter_action: {
filter: "foo = 1",
data_store: "[datastore]",
},
})
end
end
end
38 changes: 38 additions & 0 deletions spec/models/control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,42 @@
end
end
end

describe "Discovery Engine representation" do
subject(:control) { build_stubbed(:control, id: 42, display_name: "My boost control", action:) }

let(:action) { build(:control_boost_action) }

describe "#discovery_engine_id" do
it "builds an ID from the control's database ID" do
expect(control.discovery_engine_id).to eq("search-admin-42")
end
end

describe "#parent" do
it "is the configured engine" do
expect(control.parent).to eq("[engine]")
end
end

describe "#name" do
it "returns the fully qualified name of the control" do
expect(control.name).to eq("[engine]/controls/search-admin-42")
end
end

describe "#to_discovery_engine_control" do
it "returns a representation of the control for Discovery Engine" do
expect(control.to_discovery_engine_control).to include(
name: "[engine]/controls/search-admin-42",
display_name: "My boost control",
# We don't care what's in the action (that's tested elsewhere), but we do care that the
# key is present
boost_action: hash_including,
solution_type: Google::Cloud::DiscoveryEngine::V1::SolutionType::SOLUTION_TYPE_SEARCH,
use_cases: [Google::Cloud::DiscoveryEngine::V1::SearchUseCase::SEARCH_USE_CASE_SEARCH],
)
end
end
end
end

0 comments on commit ef774ba

Please sign in to comment.