From b33b992bab57375fee83f7b9c559202cf80330fd Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Wed, 19 Aug 2026 06:23:58 +0100 Subject: [PATCH 1/2] Sidebar, exhibition, and PSS updates Removing sidebar buttons for pages that no longer work (website and API timelines; user locations; API item views; and the API search terms tab) Grey out Wikimedia readiness button for institutions outside the upload pipeline. Grey out exhibition and source set view buttons for institutions with no items in that content. Each row in the exhibition and source set tables now names the exhibition or set holding the item, coinciding with updated API that provides this data. --- README.md | 7 +- app/assets/stylesheets/dashboard.css | 15 +++ app/helpers/curated_content_helper.rb | 43 ++++++++ app/helpers/data_menu_helper.rb | 64 ++++------- app/helpers/tooltips_helper.rb | 30 ++++- app/lib/dpla_api_response_builder.rb | 83 ++++++++++++++ app/lib/ga_response_presenter.rb | 7 ++ app/lib/website_events_presenter.rb | 26 +++++ app/views/search_terms/show.html.erb | 43 +++----- app/views/shared/_data_menu.html.erb | 12 -- app/views/shared/_events_table.html.erb | 12 +- app/views/shared/_wikimedia_overview.html.erb | 6 +- spec/lib/dpla_api_response_builder_spec.rb | 104 ++++++++++++++++++ spec/lib/website_events_presenter_spec.rb | 33 ++++++ spec/rails_helper.rb | 8 ++ 15 files changed, 407 insertions(+), 86 deletions(-) create mode 100644 app/helpers/curated_content_helper.rb diff --git a/README.md b/README.md index ef600a87..62e8244c 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,12 @@ The GA4 integration lives in `app/lib/ga_response_builder.rb`. Each metric secti ### DPLA API -The DPLA API (`api.dp.la/v2/`) is used only to resolve contributor names for item IDs not yet in the S3 cache (`ItemDataProviders`). Hub and contributor item counts, and the contributor lists used throughout the dashboard, come from `hub_stats.json` in S3 (see below). +The DPLA API (`api.dp.la/v2/`) is used for two things: resolving contributor names for item IDs not yet in the S3 cache (`ItemDataProviders`), and live lookups of which DPLA exhibitions and primary source sets hold an institution's items. The index stamps `exhibitions` and `primarySourceSets` slugs onto items (added to the ingestion pipeline in August 2026); the dashboard reads them two ways: + +- `DplaApiResponseBuilder#curated_breakdown` facets per institution (`facets=exhibitions&provider.name="..."&page_size=0`). The data menu uses it to disable "Exhibition views" and "Primary source set views" when nothing is there. One memoized call per page render, 3-second timeout, no retries (to avoid excessive API calls). +- `DplaApiResponseBuilder#curated_memberships_for_items` resolves the items on each page of the exhibition and source set views tables (`id=a OR b ...&fields=id,exhibitions`). Each row shows which exhibition or set holds the item, linked by slug. The API caps each field parameter at 200 characters, so the OR list holds five IDs per request (ten requests for a full 50-row table page). + +Hub and contributor item counts, and the contributor lists used throughout the dashboard, come from `hub_stats.json` in S3 (see below). The source of truth for which hubs and contributors exist, and their Wikidata IDs, is a separate JSON file maintained by the ingestion team: [`institutions_v2.json`](https://raw.githubusercontent.com/dpla/ingestion3/main/src/main/resources/wiki/institutions_v2.json). This file is used by the Wikimedia cache builder but not directly by the API-based item count queries. diff --git a/app/assets/stylesheets/dashboard.css b/app/assets/stylesheets/dashboard.css index d8acbbf8..0514e2df 100644 --- a/app/assets/stylesheets/dashboard.css +++ b/app/assets/stylesheets/dashboard.css @@ -186,6 +186,21 @@ nav .divider { background-color: #d7ebf4 } +.data-menu span.disabled { + color: #a3a5a6; + border-bottom: 1px solid #e9dec8; + display: block; + padding: 10px; + cursor: default; +} + +.item-table .curated-membership { + font-size: 0.85em; + color: #767879; + margin-top: 2px; + padding-left: 4px; +} + .alert-banner { clear: both; background-color: #fef9ec; diff --git a/app/helpers/curated_content_helper.rb b/app/helpers/curated_content_helper.rb new file mode 100644 index 00000000..0e98b62a --- /dev/null +++ b/app/helpers/curated_content_helper.rb @@ -0,0 +1,43 @@ +module CuratedContentHelper + + CURATED_PAGE_URLS = { + exhibitions: "https://dp.la/exhibitions", + primary_source_sets: "https://dp.la/primary-source-sets", + }.freeze + + ## + # Which exhibitions or primary source sets hold the institution's items, + # as { slug => item count } + # nil when the API call failed + # + # @param target [Hub|Contributor] + # @param kind [Symbol] :exhibitions or :primary_source_sets + # + def curated_breakdown_for(target, kind) + hub, contributor = target.is_a?(Hub) ? + [target.name, nil] : [target.hub.name, target.name] + + @curated_breakdowns ||= {} + key = [kind, hub, contributor] + @curated_breakdowns.fetch(key) do + @curated_breakdowns[key] = + DplaApiResponseBuilder.new.curated_breakdown(kind, hub, contributor) + end + end + + # Fails open: an API failure counts as participation, so links never + # wrongly disable. + def curated_participant?(target, kind) + breakdown = curated_breakdown_for(target, kind) + breakdown.nil? || breakdown.any? + end + + def curated_page_url(kind, slug) + "#{CURATED_PAGE_URLS.fetch(kind)}/#{slug}" + end + + def curated_noun(kind, count) + noun = kind == :exhibitions ? "exhibition" : "primary source set" + count == 1 ? noun : noun.pluralize + end +end diff --git a/app/helpers/data_menu_helper.rb b/app/helpers/data_menu_helper.rb index f3c6f921..11188286 100644 --- a/app/helpers/data_menu_helper.rb +++ b/app/helpers/data_menu_helper.rb @@ -8,30 +8,6 @@ def render_overview_link(target) link_to("Overview", path, html_opts(path)) end - def render_website_timelines_link(target) - path = target.is_a?(Hub) ? - hub_timeline_path(route_id(target.name), 'website', date_opts) : - hub_contributor_timeline_path(route_id(target.hub.name), route_id(target.name), 'website', date_opts) - - link_to("DPLA website use timelines", path, html_opts(path)) - end - - def render_api_timelines_link(target) - path = target.is_a?(Hub) ? - hub_timeline_path(route_id(target.name), 'api', date_opts) : - hub_contributor_timeline_path(route_id(target.hub.name), route_id(target.name), 'api', date_opts) - - link_to("API use timelines", path, html_opts(path)) - end - - def render_locations_link(target) - path = target.is_a?(Hub) ? - hub_locations_path(route_id(target.name), date_opts) : - hub_contributor_locations_path(route_id(target.hub.name), route_id(target.name), date_opts) - - link_to("DPLA website user locations", path, html_opts(path)) - end - def render_view_item_link(target) path = target.is_a?(Hub) ? hub_event_path(route_id(target.name), 'view_item', date_opts) : @@ -41,6 +17,10 @@ def render_view_item_link(target) end def render_view_exhibit_link(target) + unless curated_participant?(target, :exhibitions) + return disabled_menu_item("Exhibition views", "No items in DPLA exhibitions") + end + path = target.is_a?(Hub) ? hub_event_path(route_id(target.name), 'view_exhibit', date_opts) : hub_contributor_event_path(route_id(target.hub.name), route_id(target.name), 'view_exhibit', date_opts) @@ -49,6 +29,10 @@ def render_view_exhibit_link(target) end def render_view_pss_link(target) + unless curated_participant?(target, :primary_source_sets) + return disabled_menu_item("Primary source set views", "No items in DPLA primary source sets") + end + path = target.is_a?(Hub) ? hub_event_path(route_id(target.name), 'view_pss', date_opts) : hub_contributor_event_path(route_id(target.hub.name), route_id(target.name), 'view_pss', date_opts) @@ -64,25 +48,11 @@ def render_click_through_link(target) link_to("DPLA website click throughs", path, html_opts(path)) end - def render_view_api_link(target) - path = target.is_a?(Hub) ? - hub_event_path(route_id(target.name), 'view_api', date_opts) : - hub_contributor_event_path(route_id(target.hub.name), route_id(target.name), 'view_api', date_opts) - - link_to("API item views", path, html_opts(path)) - end - - def render_website_terms_link - path = search_term_path('website', date_opts) - link_to("Website", path, html_opts(path)) - end - - def render_api_terms_link - path = search_term_path('api', date_opts) - link_to("API", path, html_opts(path)) - end - def render_wikimedia_readiness_link(target) + unless wikimedia_participant?(target) + return disabled_menu_item("Wikimedia readiness", "Not a Wikimedia pipeline participant") + end + path = target.is_a?(Hub) ? hub_wikimedia_preparations_path(route_id(target.name), date_opts) : hub_contributor_wikimedia_preparations_path(route_id(target.hub.name), route_id(target.name), date_opts) @@ -90,6 +60,16 @@ def render_wikimedia_readiness_link(target) link_to("Wikimedia readiness", path, html_opts(path)) end + def wikimedia_participant?(target) + target.is_a?(Hub) ? + WikimediaParticipant.hub_participant?(target.name) : + WikimediaParticipant.participant?(target.hub.name, target.name) + end + + def disabled_menu_item(label, title) + content_tag(:span, label, class: "disabled", title: title) + end + ## # Set HTML class to selected if the given path matches the current request # path. Parameters (e.g. start_date and end_date) are ignored. diff --git a/app/helpers/tooltips_helper.rb b/app/helpers/tooltips_helper.rb index 0a8a3b03..c6806b70 100644 --- a/app/helpers/tooltips_helper.rb +++ b/app/helpers/tooltips_helper.rb @@ -19,12 +19,12 @@ def view_metadata_record_tooltip end def view_exhibition_tooltip - "One of your items was viewed within a curated DPLA exhibition. + "A user viewed one of your items in a DPLA exhibition. Comparable to \"pageview\" in Google Analytics." end def view_primary_source_set_tooltip - "One of your items was viewed within a curated DPLA primary source set. + "A user viewed one of your items in a DPLA primary source set. Comparable to \"pageview\" in Google Analytics." end @@ -139,6 +139,32 @@ def wikimedia_views_total_tooltip "All-time total page views of Wikimedia pages featuring files uploaded from your collection." end + # Fuller copy for the table pages; the short forms above stay as tooltips. + + def view_exhibition_description + "Views of your items on DPLA exhibition pages. Each row is one of your + items; the count is how many times visitors viewed it in an exhibition. Comparable to \"pageview\" in + Google Analytics." + end + + def view_primary_source_set_description + "Views of your items on DPLA primary source set pages. Each source in a + set has its own page featuring one item; the count is how many times + visitors viewed your item's page. + Comparable to \"pageview\" in Google Analytics." + end + + def find_event_page_description(key) + case key + when "view_exhibit" + view_exhibition_description + when "view_pss" + view_primary_source_set_description + else + find_tooltip(key) + end + end + def find_tooltip(key) case key when "view_item" diff --git a/app/lib/dpla_api_response_builder.rb b/app/lib/dpla_api_response_builder.rb index 2470265b..91c333e8 100644 --- a/app/lib/dpla_api_response_builder.rb +++ b/app/lib/dpla_api_response_builder.rb @@ -32,6 +32,89 @@ def data_providers_for_items(ids) result end + # Fields the DPLA index stamps onto items in curated content. + CURATED_FIELDS = { + exhibitions: 'exhibitions', + primary_source_sets: 'primarySourceSets', + }.freeze + + # Covers all 32 exhibitions and 142 source sets. + CURATED_FACET_SIZE = 200 + + # The API caps each parameter at 200 characters, + # which fits five 32-character IDs. + CURATED_ID_BATCH = 5 + + # Runs during page render, so one short attempt and no retries. + CURATED_TIMEOUT_SECONDS = 3 + + ## + # Which exhibitions or primary source sets hold an institution's items. + # + # @param kind [Symbol] :exhibitions or :primary_source_sets + # @param hub [String] provider name + # @param contributor [String, nil] dataProvider name + # @return [Hash, nil] { slug => item count }; {} when + # there are none, nil when the request failed so callers can fail open. + # + def curated_breakdown(kind, hub, contributor = nil) + field = CURATED_FIELDS.fetch(kind) + query = { + 'facets' => field, + 'facet_size' => CURATED_FACET_SIZE, + 'provider.name' => %("#{hub.delete('"')}"), + 'page_size' => 0, + 'api_key' => api_key, + } + query['dataProvider.name'] = %("#{contributor.delete('"')}") if contributor + + res = self.class.get('/items', query: query, timeout: CURATED_TIMEOUT_SECONDS) + return nil unless res.code == 200 + + terms = JSON.parse(res.body).dig('facets', field, 'terms') || [] + terms.to_h { |term| [term['term'], term['count'].to_i] } + rescue StandardError => e + Rails.logger.warn("DplaApiResponseBuilder#curated_breakdown: #{e.class}: #{e.message}") + nil + end + + ## + # Which curated content holds each of the given items. Uses the search + # endpoint rather than the multi-ID path endpoint, which errors on some + # mixes of IDs no longer in the index. + # + # @param kind [Symbol] :exhibitions or :primary_source_sets + # @param ids [Array] DPLA item hex IDs + # @return [Hash>] { item_id => [slug, ...] }; items + # with no membership are omitted, and {} on failure — the annotation is + # optional, so callers carry on without it. + # + def curated_memberships_for_items(kind, ids) + result = {} + field = CURATED_FIELDS.fetch(kind) + + ids.filter_map { |id| id&.strip.presence }.uniq.each_slice(CURATED_ID_BATCH) do |batch| + query = { + 'id' => batch.join(' OR '), + 'fields' => "id,#{field}", + 'page_size' => batch.size, + 'api_key' => api_key, + } + res = self.class.get('/items', query: query, timeout: CURATED_TIMEOUT_SECONDS) + next unless res.code == 200 + + (JSON.parse(res.body)['docs'] || []).each do |doc| + # `fields` returns a lone membership as a bare string, not an array. + slugs = Array(doc[field]).map(&:to_s).reject(&:empty?) + result[doc['id']] = slugs if doc['id'] && slugs.any? + end + end + result + rescue StandardError => e + Rails.logger.warn("DplaApiResponseBuilder#curated_memberships_for_items: #{e.class}: #{e.message}") + result + end + private def api_key diff --git a/app/lib/ga_response_presenter.rb b/app/lib/ga_response_presenter.rb index 199c17b2..18435dee 100644 --- a/app/lib/ga_response_presenter.rb +++ b/app/lib/ga_response_presenter.rb @@ -42,4 +42,11 @@ def id(row) def title(row) event_label(row)&.split(" : ", 2)&.last&.strip end + + # Overridden where a table shows curated content. + def membership_kind; end + + def memberships(_row) + [] + end end diff --git a/app/lib/website_events_presenter.rb b/app/lib/website_events_presenter.rb index 1588bca3..77a66940 100644 --- a/app/lib/website_events_presenter.rb +++ b/app/lib/website_events_presenter.rb @@ -2,6 +2,11 @@ class WebsiteEventsPresenter < GaResponsePresenter + MEMBERSHIP_KINDS = { + "View Exhibition Item" => :exhibitions, + "View Primary Source" => :primary_source_sets, + }.freeze + def label dict = { "View Item" => "Digital library catalog views", @@ -26,6 +31,21 @@ def count(row) row[columns.index("ga:totalEvents")] end + # nil unless this table shows curated content. + def membership_kind + MEMBERSHIP_KINDS[@ga_response.event_name] + end + + ## + # Curated-content slugs for the row's item, e.g. ["erie-canal"]. + # Empty for other tables, which never hit the API. + # + def memberships(row) + return [] unless membership_kind + + membership_lookup[id(row)] || [] + end + ## # Generate CSV of all events # @return [CSV] @@ -72,4 +92,10 @@ def data_providers def item_contributor_lookup @item_contributor_lookup ||= contributor_lookup(rows) end + + # One lookup for the whole displayed page. + def membership_lookup + @membership_lookup ||= DplaApiResponseBuilder.new.curated_memberships_for_items( + membership_kind, rows.filter_map { |row| id(row) }) + end end diff --git a/app/views/search_terms/show.html.erb b/app/views/search_terms/show.html.erb index 466b55d3..516aa691 100644 --- a/app/views/search_terms/show.html.erb +++ b/app/views/search_terms/show.html.erb @@ -6,37 +6,28 @@
-
-
    -
  • <%= render_website_terms_link %>
  • -
  • <%= render_api_terms_link %>
  • -
-
- -
-
+
-

<%= scope_label %>

+

<%= scope_label %>

-
- Top search terms across the DPLA <%= scope_label %> for all hubs and contributors. - Equivalent to "search terms" in Google Analytics. -
+
+ Top search terms across the DPLA <%= scope_label %> for all hubs and contributors. + Equivalent to "search terms" in Google Analytics. +
- <% if params[:id] == "website" %> + <% if params[:id] == "website" %> - <%= render_async_section website_search_terms_path(date_opts.merge(page_opts)) do %> - <%= render "shared/loading", message: "Loading search terms…" %> - <% end %> + <%= render_async_section website_search_terms_path(date_opts.merge(page_opts)) do %> + <%= render "shared/loading", message: "Loading search terms…" %> + <% end %> - <% elsif params[:id] == "api" %> - <%= render_async_section api_search_terms_path(date_opts) do %> - <%= render "shared/loading", message: "Loading search terms…" %> - <% end %> + <% elsif params[:id] == "api" %> + <%= render_async_section api_search_terms_path(date_opts) do %> + <%= render "shared/loading", message: "Loading search terms…" %> + <% end %> - <% else %> - The ID in the URI, "<%= params[:id] %>", is not recognized. - <% end %> -
+ <% else %> + The ID in the URI, "<%= params[:id] %>", is not recognized. + <% end %>
diff --git a/app/views/shared/_data_menu.html.erb b/app/views/shared/_data_menu.html.erb index 19b63cda..640ec40a 100644 --- a/app/views/shared/_data_menu.html.erb +++ b/app/views/shared/_data_menu.html.erb @@ -5,15 +5,6 @@
  • <%= render_overview_link(target) %>
  • -
  • - <%= render_website_timelines_link(target) %> -
  • -
  • - <%= render_api_timelines_link(target) %> -
  • -
  • - <%= render_locations_link(target) %> -
  • <%= render_view_item_link(target) %>
  • @@ -26,9 +17,6 @@
  • <%= render_click_through_link(target) %>
  • -
  • - <%= render_view_api_link(target) %> -
  • <%= render_wikimedia_readiness_link(target) %>
  • diff --git a/app/views/shared/_events_table.html.erb b/app/views/shared/_events_table.html.erb index 3c712571..73488078 100644 --- a/app/views/shared/_events_table.html.erb +++ b/app/views/shared/_events_table.html.erb @@ -2,7 +2,7 @@

    <%= @events.label %>

    - <%= find_tooltip(params[:event_id]) %> + <%= find_event_page_description(params[:event_id]) %>
    <% if @events.total_results.to_i > 0 %> @@ -36,6 +36,16 @@ "> <%= @events.title(row) %> + <% slugs = @events.memberships(row) %> + <% if slugs.any? %> +
    + In <%= curated_noun(@events.membership_kind, slugs.size) %> + <%= safe_join(slugs.map { |slug| + link_to(slug, curated_page_url(@events.membership_kind, slug), + target: "_blank", rel: "noopener") + }, ", ") %> +
    + <% end %> <% unless @contributor %> <%= @events.contributor(row) %> diff --git a/app/views/shared/_wikimedia_overview.html.erb b/app/views/shared/_wikimedia_overview.html.erb index 899366e0..b43e27ed 100644 --- a/app/views/shared/_wikimedia_overview.html.erb +++ b/app/views/shared/_wikimedia_overview.html.erb @@ -71,6 +71,8 @@
    Wikimedia readiness data not available
    <% end %> -
    +<% if @wikimedia_participant %> +
    -

    More data about <%= render_wikimedia_readiness_link(@target) %>

    +

    More data about <%= render_wikimedia_readiness_link(@target) %>

    +<% end %> diff --git a/spec/lib/dpla_api_response_builder_spec.rb b/spec/lib/dpla_api_response_builder_spec.rb index af5ec556..713f5e1e 100644 --- a/spec/lib/dpla_api_response_builder_spec.rb +++ b/spec/lib/dpla_api_response_builder_spec.rb @@ -32,4 +32,108 @@ expect(builder.data_providers_for_items(%w[abc123 def456])) .to eq('abc123' => 'Some Institution') end + + describe '#curated_breakdown' do + def stub_facets(field, terms) + body = { 'facets' => { field => { 'terms' => terms } } } + allow(response).to receive(:code).and_return(200) + allow(response).to receive(:body).and_return(body.to_json) + end + + it 'maps facet terms to slug => item count' do + stub_facets('exhibitions', + [{ 'term' => 'erie-canal', 'count' => 7 }, + { 'term' => 'race-to-the-moon', 'count' => 3 }]) + expect(builder.curated_breakdown(:exhibitions, 'HathiTrust')) + .to eq('erie-canal' => 7, 'race-to-the-moon' => 3) + end + + it 'is empty when the institution has no items in the curated content' do + stub_facets('exhibitions', []) + expect(builder.curated_breakdown(:exhibitions, 'Texas Digital Library')).to eq({}) + end + + it 'facets the field for the given kind, scoped to the institution' do + stub_facets('primarySourceSets', []) + expect(described_class).to receive(:get).with( + '/items', + hash_including( + query: hash_including( + 'facets' => 'primarySourceSets', + 'provider.name' => '"HathiTrust"', + 'dataProvider.name' => '"University of Michigan"', + 'page_size' => 0 + ) + ) + ).and_return(response) + builder.curated_breakdown(:primary_source_sets, 'HathiTrust', 'University of Michigan') + end + + it 'is nil on a non-200 response so callers can fail open' do + allow(response).to receive(:code).and_return(500) + expect(builder.curated_breakdown(:exhibitions, 'HathiTrust')).to be_nil + end + + it 'is nil when the request raises' do + allow(described_class).to receive(:get).and_raise(Net::OpenTimeout) + expect(builder.curated_breakdown(:exhibitions, 'HathiTrust')).to be_nil + end + + it 'makes a single attempt with no retries' do + allow(response).to receive(:code).and_return(500) + expect(described_class).to receive(:get).exactly(1).times.and_return(response) + builder.curated_breakdown(:exhibitions, 'HathiTrust') + end + end + + describe '#curated_memberships_for_items' do + def stub_docs(docs) + allow(response).to receive(:code).and_return(200) + allow(response).to receive(:body).and_return({ 'docs' => docs }.to_json) + end + + it 'maps item ids to slugs, handling both array and bare-string fields' do + stub_docs([{ 'id' => 'aaa', 'exhibitions' => %w[erie-canal activism] }, + { 'id' => 'bbb', 'exhibitions' => 'race-to-the-moon' }]) + expect(builder.curated_memberships_for_items(:exhibitions, %w[aaa bbb])) + .to eq('aaa' => %w[erie-canal activism], 'bbb' => %w[race-to-the-moon]) + end + + it 'omits items with no membership' do + stub_docs([{ 'id' => 'aaa' }]) + expect(builder.curated_memberships_for_items(:exhibitions, %w[aaa ded])) + .to eq({}) + end + + it 'queries the search endpoint with an OR list and slim fields' do + stub_docs([]) + expect(described_class).to receive(:get).with( + '/items', + hash_including( + query: hash_including( + 'id' => 'aaa OR bbb', + 'fields' => 'id,primarySourceSets' + ) + ) + ).and_return(response) + builder.curated_memberships_for_items(:primary_source_sets, %w[aaa bbb]) + end + + it 'is empty on a non-200 response' do + allow(response).to receive(:code).and_return(500) + expect(builder.curated_memberships_for_items(:exhibitions, %w[aaa])).to eq({}) + end + + it 'splits ids into batches that fit the 200-character parameter cap' do + stub_docs([]) + ids = Array.new(12) { |i| format('%032x', i) } + expect(described_class).to receive(:get).exactly(3).times.and_return(response) + builder.curated_memberships_for_items(:exhibitions, ids) + end + + it 'is empty when the request raises' do + allow(described_class).to receive(:get).and_raise(Net::OpenTimeout) + expect(builder.curated_memberships_for_items(:exhibitions, %w[aaa])).to eq({}) + end + end end diff --git a/spec/lib/website_events_presenter_spec.rb b/spec/lib/website_events_presenter_spec.rb index 3b97d837..eccf6fb6 100644 --- a/spec/lib/website_events_presenter_spec.rb +++ b/spec/lib/website_events_presenter_spec.rb @@ -86,4 +86,37 @@ end end end + + describe 'curated content membership' do + let(:column_header) { [double(name: 'ga:eventLabel')] } + let(:row) { ['abc123 : Some Title'] } + let(:ga_data) { double(column_headers: column_header, rows: [row], total_results: 1) } + let(:ga_response) do + double(response: ga_data, multi_page_response: [], event_name: event_name) + end + + context 'on an exhibition views table' do + let(:event_name) { 'View Exhibition Item' } + + it 'resolves slugs for the row through one batched lookup' do + api = instance_double(DplaApiResponseBuilder) + allow(DplaApiResponseBuilder).to receive(:new).and_return(api) + allow(api).to receive(:curated_memberships_for_items) + .with(:exhibitions, ['abc123']) + .and_return('abc123' => ['erie-canal']) + expect(presenter.membership_kind).to eq :exhibitions + expect(presenter.memberships(row)).to eq ['erie-canal'] + end + end + + context 'on a table that is not curated content' do + let(:event_name) { 'View Item' } + + it 'is empty and never calls the API' do + expect(DplaApiResponseBuilder).not_to receive(:new) + expect(presenter.membership_kind).to be_nil + expect(presenter.memberships(row)).to eq [] + end + end + end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index e91d9472..ab1e48c8 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -56,4 +56,12 @@ # config.filter_gems_from_backtrace("gem name") config.include Devise::Test::IntegrationHelpers, type: :request + + # The data menu and event tables query the live DPLA API + config.before(:each, type: :request) do + allow_any_instance_of(DplaApiResponseBuilder) + .to receive(:curated_breakdown).and_return("stub-exhibition" => 1) + allow_any_instance_of(DplaApiResponseBuilder) + .to receive(:curated_memberships_for_items).and_return({}) + end end From 377ea41c1a93fd022461165cd4f7e73bd7abe8de Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Wed, 19 Aug 2026 06:37:43 +0100 Subject: [PATCH 2/2] README fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 62e8244c..c2f336a3 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ The GA4 integration lives in `app/lib/ga_response_builder.rb`. Each metric secti The DPLA API (`api.dp.la/v2/`) is used for two things: resolving contributor names for item IDs not yet in the S3 cache (`ItemDataProviders`), and live lookups of which DPLA exhibitions and primary source sets hold an institution's items. The index stamps `exhibitions` and `primarySourceSets` slugs onto items (added to the ingestion pipeline in August 2026); the dashboard reads them two ways: -- `DplaApiResponseBuilder#curated_breakdown` facets per institution (`facets=exhibitions&provider.name="..."&page_size=0`). The data menu uses it to disable "Exhibition views" and "Primary source set views" when nothing is there. One memoized call per page render, 3-second timeout, no retries (to avoid excessive API calls). +- `DplaApiResponseBuilder#curated_breakdown` facets per institution (`facets=exhibitions&provider.name="..."&page_size=0`). The data menu uses it to disable "Exhibition views" and "Primary source set views" when nothing is there. Results are memoized per kind, hub, and contributor, so the menu's two checks cost one call each. 3-second timeout, no retries (to avoid excessive API calls). - `DplaApiResponseBuilder#curated_memberships_for_items` resolves the items on each page of the exhibition and source set views tables (`id=a OR b ...&fields=id,exhibitions`). Each row shows which exhibition or set holds the item, linked by slug. The API caps each field parameter at 200 characters, so the OR list holds five IDs per request (ten requests for a full 50-row table page). Hub and contributor item counts, and the contributor lists used throughout the dashboard, come from `hub_stats.json` in S3 (see below).