Skip to content

Commit 9bc12ba

Browse files
authored
Merge pull request #384 from alphagov/models
Refactor Discovery Engine resource handling
2 parents 2da041c + 4d6e809 commit 9bc12ba

File tree

24 files changed

+185
-33
lines changed

24 files changed

+185
-33
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ jobs:
4545
RAILS_ENV: test
4646
# All Google client library calls are mocked, but the application needs this set to boot
4747
GOOGLE_CLOUD_PROJECT_ID: not-used
48-
DISCOVERY_ENGINE_SERVING_CONFIG: not-used
49-
DISCOVERY_ENGINE_DATASTORE: not-used
50-
DISCOVERY_ENGINE_DATASTORE_BRANCH: not-used
48+
DISCOVERY_ENGINE_DEFAULT_COLLECTION_NAME: not-used
5149
# Redis running through govuk-infrastructure action
5250
REDIS_URL: redis://localhost:6379
5351
steps:

app/models/branch.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Represents a branch on a Discovery Engine data store.
2+
#
3+
# Currently, every data store on Discovery Engine has exactly *one* branch (the default branch), and
4+
# we are not able to make any changes to that resource. However, we still need to model it here
5+
# because documents are children of the branch, not the datastore itself.
6+
#
7+
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DocumentService-Client
8+
# (there is no documentation specific to branches)
9+
Branch = Data.define(:remote_resource_id) do
10+
include DiscoveryEngineNameable
11+
12+
# The default branch automatically available on a data store
13+
def self.default
14+
new("default_branch")
15+
end
16+
17+
def parent
18+
# We only use a single data store in our architecture, so we can hardcode it here.
19+
DataStore.default
20+
end
21+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Enhances models with a `#name` method returning their fully qualified Google Cloud Platform
2+
# resource name (like a path).
3+
#
4+
# For example, for a `Control`, this would be:
5+
# projects/{project}/locations/{location}/collections/{collection_id}/engines/
6+
# {engine_id}/controls/{control_id}
7+
#
8+
# Requires the including class to implement `#remote_resource_id`, and optionally `#parent` if the
9+
# parent resource is not the default collection.
10+
module DiscoveryEngineNameable
11+
# The name (fully qualified path) of this Discovery Engine resource on GCP
12+
def name
13+
[parent_name, resource_path_fragment, remote_resource_id].join("/")
14+
end
15+
16+
private
17+
18+
def resource_path_fragment
19+
# For example: `DataStore` -> `dataStores`
20+
self.class.name.downcase_first.pluralize
21+
end
22+
23+
def parent_name
24+
if respond_to?(:parent)
25+
parent.name
26+
else
27+
# The default collection is the parent of all root-level resources
28+
Rails.configuration.discovery_engine_default_collection_name
29+
end
30+
end
31+
end

app/models/data_store.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Represents a data store on Discovery Engine.
2+
#
3+
# A data store contains the indexed documents that can be searched through an engine.
4+
#
5+
# Our architecture currently only has a single data store, so we do not need the ability to manage
6+
# data stores through Search Admin.
7+
#
8+
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-DataStore
9+
DataStore = Data.define(:remote_resource_id) do
10+
include DiscoveryEngineNameable
11+
12+
def self.default
13+
new("govuk_content")
14+
end
15+
end

app/models/engine.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Represents an engine on Discovery Engine.
2+
#
3+
# An engine (called "app" in the Google Cloud UI) is an abstraction over the data stores that
4+
# contain our searchable documents, and is used for querying. It is the parent resource of several
5+
# other resources such as controls and serving configs.
6+
#
7+
# Our architecture currently only has a single engine, so we do not need the ability to manage
8+
# engines through Search Admin.
9+
#
10+
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1/latest/Google-Cloud-DiscoveryEngine-V1-Engine
11+
Engine = Data.define(:remote_resource_id) do
12+
include DiscoveryEngineNameable
13+
14+
# The default engine created through Terraform in `govuk-infrastructure`
15+
def self.default
16+
new("govuk")
17+
end
18+
end

app/models/serving_config.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Represents a serving config on Discovery Engine.
2+
#
3+
# A serving config is an endpoint on an engine that can be used for
4+
# querying. Each serving config can have different configuration (in particular, different sets of
5+
# active controls), which allows us to test out new configuration changes outside of the default
6+
# serving config.
7+
#
8+
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1beta/latest/Google-Cloud-DiscoveryEngine-V1beta-ServingConfig
9+
ServingConfig = Data.define(:remote_resource_id) do
10+
include DiscoveryEngineNameable
11+
12+
# The default serving config automatically available on an engine
13+
def self.default
14+
new("default_search")
15+
end
16+
17+
def parent
18+
# We only use a single engine in our architecture, so we can hardcode it here.
19+
Engine.default
20+
end
21+
end

app/services/discovery_engine/autocomplete/complete.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ def suggestions
3232

3333
def complete_query_request
3434
{
35-
data_store:,
35+
data_store: DataStore.default.name,
3636
query:,
3737
query_model: QUERY_MODEL,
3838
}
3939
end
40-
41-
def data_store
42-
Rails.configuration.discovery_engine_datastore
43-
end
4440
end
4541
end

app/services/discovery_engine/autocomplete/update_denylist.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def bucket_name
5454
end
5555

5656
def parent
57-
Rails.configuration.discovery_engine_datastore
57+
DataStore.default.name
5858
end
5959
end
6060
end

app/services/discovery_engine/query/search.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def query
5656
end
5757

5858
def serving_config
59-
Rails.configuration.discovery_engine_serving_config
59+
ServingConfig.default.name
6060
end
6161

6262
def page_size

app/services/discovery_engine/sync/operation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def version_cache
6060
end
6161

6262
def document_name
63-
"#{Rails.configuration.discovery_engine_datastore_branch}/documents/#{content_id}"
63+
[Branch.default.name, "documents", content_id].join("/")
6464
end
6565

6666
def log(level, message)

0 commit comments

Comments
 (0)