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

Spike into popular tasks powered by Search #3659

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
22 changes: 20 additions & 2 deletions app/models/mainstream_browse_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.find(base_path)

def initialize(content_item)
@content_item = content_item
@documents_service = PopularBrowseSearchDocuments
end

def top_level_browse_pages
Expand Down Expand Up @@ -49,7 +50,24 @@ def lists
@lists ||= ListSet.new("section", @content_item.content_id, details["groups"])
end

def slug
base_path.sub(%r{\A/browse/}, "")
def slug(path = base_path)
path.sub(%r{\A/browse/}, "")
end

def display_popular_tasks?
%w[benefits business].include?(slug)
end

def popular_tasks
return if second_level_browse_pages.blank?

slugs = second_level_browse_pages.map { |page| slug(page.base_path) }
service_pages_tagged_to_browse_subtopics(slugs)
end

def service_pages_tagged_to_browse_subtopics(subtopic_slugs)
@documents_service.new(
subtopic_slugs, "filter_any_mainstream_browse_pages"
).fetch_related_documents_with_format({ filter_format: "transaction" })
end
end
16 changes: 10 additions & 6 deletions app/models/topical_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TopicalEvent

def initialize(content_item)
@content_item = content_item
@documents_service = SearchDocuments.new(slug, "filter_topical_events")
@documents_service = TopicalEventSearchDocuments
end

def self.find!(base_path)
Expand Down Expand Up @@ -92,24 +92,28 @@ def ordered_featured_documents
end
end

def documents_service
@documents_service.new([slug], "filter_topical_events")
end

def latest
@latest ||= @documents_service.fetch_related_documents_with_format
@latest ||= documents_service.fetch_related_documents_with_format
end

def publications
@publications ||= @documents_service.fetch_related_documents_with_format({ filter_format: "publication" })
@publications ||= documents_service.fetch_related_documents_with_format({ filter_format: "publication" })
end

def consultations
@consultations ||= @documents_service.fetch_related_documents_with_format({ filter_format: "consultation" })
@consultations ||= documents_service.fetch_related_documents_with_format({ filter_format: "consultation" })
end

def announcements
@announcements ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications", reject_content_purpose_subgroup: %w[decisions updates_and_alerts] })
@announcements ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications", reject_content_purpose_subgroup: %w[decisions updates_and_alerts] })
end

def guidance_and_regulation
@guidance_and_regulation ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "guidance_and_regulation" })
@guidance_and_regulation ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "guidance_and_regulation" })
end

def organisations
Expand Down
14 changes: 9 additions & 5 deletions app/models/world_location_news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class WorldLocationNews

def initialize(content_item)
@content_item = content_item
@documents_service = SearchDocuments.new(slug, "filter_world_locations")
@documents_service = TopicalEventSearchDocuments
end

def self.find!(base_path)
Expand Down Expand Up @@ -70,19 +70,23 @@ def latest
@latest ||= [*announcements, *publications, *statistics]
.select { |entry| entry.dig(:metadata, :public_updated_at).present? }
.sort { |entry1, entry2| - (entry1.dig(:metadata, :public_updated_at) <=> entry2.dig(:metadata, :public_updated_at)) }
.first(SearchDocuments::DEFAULT_COUNT)
.first(@documents_service::DEFAULT_COUNT)
end

def documents_service
@documents_service.new([slug], "filter_world_locations")
end

def announcements
@announcements ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications" })
@announcements ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: "news_and_communications" })
end

def publications
@publications ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: %w[guidance_and_regulation policy_and_engagement transparency] })
@publications ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_supergroup: %w[guidance_and_regulation policy_and_engagement transparency] })
end

def statistics
@statistics ||= @documents_service.fetch_related_documents_with_format({ filter_content_purpose_subgroup: "statistics" })
@statistics ||= documents_service.fetch_related_documents_with_format({ filter_content_purpose_subgroup: "statistics" })
end

def type
Expand Down
9 changes: 9 additions & 0 deletions app/services/popular_browse_search_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PopularBrowseSearchDocuments < SearchDocuments
def fields
TOPICAL_EVENTS_SEARCH_FIELDS
end

def order
"-popularity"
end
end
20 changes: 14 additions & 6 deletions app/services/search_documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ class SearchDocuments
include SearchApiFields
DEFAULT_COUNT = 3

def initialize(slug, filter_field)
@slug = slug
def initialize(slug_array, filter_field)
@slug_array = slug_array
@filter_field = filter_field
end

def fetch_related_documents_with_format(filter_format = {})
search_response = Services.search_api.search(default_search_options.merge(filter_format))
search_response = Services.cached_search(default_search_options.merge(filter_format))
format_results(search_response)
end

def fields
Raise NotImplementedError
end

def order
Raise NotImplementedError
end

private

def default_search_options
{ @filter_field.to_sym => [@slug],
{ @filter_field.to_sym => @slug_array,
count: DEFAULT_COUNT,
order: "-public_timestamp",
fields: TOPICAL_EVENTS_SEARCH_FIELDS }
order:,
fields: }
end

def display_type(document)
Expand Down
9 changes: 9 additions & 0 deletions app/services/topical_event_search_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TopicalEventSearchDocuments < SearchDocuments
def fields
TOPICAL_EVENTS_SEARCH_FIELDS
end

def order
"-public_timestamp"
end
end
22 changes: 19 additions & 3 deletions app/views/browse/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
} %>
<% end %>

<% if display_action_links_for_slug?(page.slug) %>
<% if page.display_popular_tasks? %>
<div class="govuk-width-container">
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
Expand All @@ -42,9 +42,25 @@
font_size: "m"
} %>
<ul class="govuk-list govuk-!-margin-bottom-7">
<% action_link_data(page.slug).each do |link| %>
<% page.popular_tasks.each_with_index do |task, index| %>
<li>
<%= render "shared/browse_action_link", locals: link %>
<%= render "govuk_publishing_components/components/action_link", {
text: task[:link][:text],
href: task[:link][:path],
dark_large_icon: true,
margin_bottom: 3,
data_attributes: {
module: "ga4-link-tracker",
ga4_track_links_only: "",
ga4_link: {
event_name: "navigation",
type: "action",
index_link: index + 1 ,
index_total: page.popular_tasks.length,
text: "something in here"
}
}
} %>
</li>
<% end %>
</ul>
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/browse_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
RSpec.describe BrowseController do
include GovukAbTesting::RspecHelpers
include SearchApiHelpers
render_views
describe "GET index" do
before do
Expand Down Expand Up @@ -30,6 +31,11 @@
second_level_browse_pages:,
},
)
stub_search(body: {
"results" => [],
"start" => 0,
"total" => 0,
})
end

it "sets correct expiry headers" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/mainstream_browsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

schemas.each do |content_item|
stub_content_store_has_item(content_item["base_path"], content_item)

search_api_has_documents_for_browse_page(
content_item["content_id"],
%w[
Expand All @@ -22,6 +21,7 @@
],
page_size: SearchApiSearch::PAGE_SIZE_TO_GET_EVERYTHING,
)
search_api_has_popular_documents_for_browse(content_item)

visit content_item["base_path"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
RSpec.describe SearchDocuments do
RSpec.describe TopicalEventSearchDocuments do
include SearchApiHelpers

context "for topical events" do
describe "#fetch_related_documents_with_format" do
before do
@slug = "/some/slug"
@documents_service = SearchDocuments.new(@slug, "filter_topical_events")
@documents_service = TopicalEventSearchDocuments.new([@slug], "filter_topical_events")
end

it "makes the correct call to search api" do
Expand Down Expand Up @@ -70,7 +70,7 @@
describe "#fetch_related_documents_with_format" do
before do
@slug = "mock-country"
@documents_service = SearchDocuments.new(@slug, "filter_world_locations")
@documents_service = TopicalEventSearchDocuments.new([@slug], "filter_world_locations")
end

it "makes the correct call to search api" do
Expand Down
25 changes: 25 additions & 0 deletions spec/support/search_api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ def search_api_has_documents_for_browse_page(browse_page_content_id, document_sl
end
end

def search_api_has_popular_documents_for_browse(browse_content_item)
return if browse_content_item["links"]["second_level_browse_pages"].blank?

level_two_browse_slugs =
browse_content_item["links"]["second_level_browse_pages"].map do |link|
link["base_path"].sub(%r{\A/browse/}, "")
end

fields = SearchApiFields::TOPICAL_EVENTS_SEARCH_FIELDS

params = {
count: "3",
filter_any_mainstream_browse_pages: webmock_match_array(level_two_browse_slugs),
order: "-popularity",
fields: webmock_match_array(fields),
}

results = %w[slug-a slug-2].map do |slug|
search_api_document_for_slug(slug, 1.hour.ago, "guide")
end

body = { results: }
stub_search(params:, body:)
end

def section_tagged_content_list(doc_type, count = 1)
content_list = []

Expand Down