• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_discovery / 4cbb2ea8-3be7-4b42-a711-90e6c79f57d9

14 Sep 2023 04:48PM UTC coverage: 96.684% (+0.09%) from 96.593%
4cbb2ea8-3be7-4b42-a711-90e6c79f57d9

push

circleci

carolyncole
Ensuring that Solr requests are retransmitted should the Blacklight::Exceptions::ECONNREFUSED error be raised

37 of 37 new or added lines in 2 files covered. (100.0%)

2391 of 2473 relevant lines covered (96.68%)

179.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.58
/app/controllers/catalog_controller.rb
1
# frozen_string_literal: true
2

3
class CatalogController < ApplicationController
1✔
4
  include Blacklight::Catalog
1✔
5
  include BlacklightRangeLimit::ControllerOverride
1✔
6

7
  include Blacklight::Marc::Catalog
1✔
8

9
  around_action :retry_on_exception
1✔
10

11
  rescue_from Blacklight::Exceptions::RecordNotFound do
1✔
12
    error_page = Rails.env.production? || Rails.env.staging? ? '/discovery/errors/not_found' : '/errors/not_found'
1✔
13
    redirect_to error_page
1✔
14
  end
15

16
  def retry_on_exception
1✔
17
    yield
62✔
18
  rescue Blacklight::Exceptions::ECONNREFUSED
19
    # If the Solr service is available, retry the HTTP request
20
    if search_service.repository.ping
1✔
21
      retry
×
22
    else
23
      error_page = Rails.env.production? || Rails.env.staging? ? '/discovery/errors/network_error' : '/errors/network_error'
1✔
24
      redirect_to error_page
1✔
25
    end
26
  end
27

28
  configure_blacklight do |config|
1✔
29
    ## Class for sending and receiving requests from a search index
30
    # config.repository_class = Blacklight::Solr::Repository
31
    #
32
    ## Class for converting Blacklight's url parameters to into request parameters for the search index
33
    # config.search_builder_class = ::SearchBuilder
34
    #
35
    ## Model that maps search index responses to the blacklight response model
36
    # config.response_model = Blacklight::Solr::Response
37
    #
38
    ## Should the raw solr document endpoint (e.g. /catalog/:id/raw) be enabled
39
    config.raw_endpoint.enabled = true
1✔
40

41
    ## Default parameters to send to solr for all search-like requests. See also SearchBuilder#processed_parameters
42
    config.default_solr_params = {
1✔
43
      rows: 10
44
    }
45

46
    # solr path which will be added to solr base url before the other solr params.
47
    # config.solr_path = 'select'
48
    # config.document_solr_path = 'get'
49

50
    # items to show per page, each number in the array represent another option to choose from.
51
    # config.per_page = [10,20,50,100]
52

53
    # solr field configuration for search results/index views
54
    config.index.title_field = 'title_tesim'
1✔
55
    # config.index.display_type_field = 'format'
56
    # config.index.thumbnail_field = 'thumbnail_path_ss'
57

58
    config.add_results_document_tool(:bookmark, partial: 'bookmark_control', if: :render_bookmarks_control?)
1✔
59

60
    config.add_results_collection_tool(:sort_widget)
1✔
61
    config.add_results_collection_tool(:per_page_widget)
1✔
62
    config.add_results_collection_tool(:view_type_group)
1✔
63

64
    config.add_show_tools_partial(:bookmark, partial: 'bookmark_control', if: :render_bookmarks_control?)
1✔
65
    config.add_show_tools_partial(:email, callback: :email_action, validator: :validate_email_params)
1✔
66
    config.add_show_tools_partial(:sms, if: :render_sms_action?, callback: :sms_action, validator: :validate_sms_params)
1✔
67
    config.add_show_tools_partial(:citation)
1✔
68

69
    config.add_nav_action(:bookmark, partial: 'blacklight/nav/bookmark', if: :render_bookmarks_control?)
1✔
70
    config.add_nav_action(:search_history, partial: 'blacklight/nav/search_history')
1✔
71

72
    # solr field configuration for document/show views
73
    # config.show.display_type_field = 'format'
74
    # config.show.thumbnail_field = 'thumbnail_path_ss'
75

76
    # solr fields that will be treated as facets by the blacklight application
77
    #   The ordering of the field names is the order of the display
78
    #
79
    # Setting a limit will trigger Blacklight's 'more' facet values link.
80
    # * If left unset, then all facet values returned by solr will be displayed.
81
    # * If set to an integer, then "f.somefield.facet.limit" will be added to
82
    # solr request, with actual solr request being +1 your configured limit --
83
    # you configure the number of items you actually want _displayed_ in a page.
84
    # * If set to 'true', then no additional parameters will be sent to solr,
85
    # but any 'sniffed' request limit parameters will be used for paging, with
86
    # paging at requested limit -1. Can sniff from facet.limit or
87
    # f.specific_field.facet.limit solr request params. This 'true' config
88
    # can be used if you set limits in :default_solr_params, or as defaults
89
    # on the solr side in the request handler itself. Request handler defaults
90
    # sniffing requires solr requests to be made with "echoParams=all", for
91
    # app code to actually have it echo'd back to see it.
92
    #
93
    # :show may be set to false if you don't want the facet to be drawn in the
94
    # facet bar
95
    #
96
    # set :index_range to true if you want the facet pagination view to have facet prefix-based navigation
97
    #  (useful when user clicks "more" on a large facet and wants to navigate alphabetically across a large set of results)
98
    # :index_range can be an array or range of prefixes that will be used to create the navigation (note: It is case sensitive when searching values)
99

100
    # config.add_facet_field 'example_pivot_field', label: 'Pivot Field', pivot: %w[format language_ssim], collapsing: true
101

102
    # config.add_facet_field 'example_query_facet_field', label: 'Publish Date', query: {
103
    #   years_5: { label: 'within 5 Years', fq: "pub_date_ssim:[#{Time.zone.now.year - 5} TO *]" },
104
    #   years_10: { label: 'within 10 Years', fq: "pub_date_ssim:[#{Time.zone.now.year - 10} TO *]" },
105
    #   years_25: { label: 'within 25 Years', fq: "pub_date_ssim:[#{Time.zone.now.year - 25} TO *]" }
106
    # }
107

108
    config.add_facet_field 'domain_ssim', label: 'Domain'
1✔
109
    config.add_facet_field 'communities_ssim', label: 'Community'
1✔
110
    config.add_facet_field 'subcommunities_ssim', label: 'Subcommunity'
1✔
111

112
    config.add_facet_field 'collection_tag_ssim', label: 'Collection Tags'
1✔
113
    config.add_facet_field 'authors_affiliation_ssim', label: 'Affiliation'
1✔
114

115
    config.add_facet_field 'genre_ssim', label: 'Type'
1✔
116
    config.add_facet_field 'year_available_itsi', label: 'Year Published', range: true
1✔
117

118
    # Notice that is facet is not shown. Yet facet searches by this field do work
119
    # and we use them when users click on the "Keywords" links in the Show page.
120
    config.add_facet_field 'subject_all_ssim', label: 'Keywords', show: false
1✔
121

122
    # An extra facet to filter DataSpace vs PDC Describe records at will
123
    # (this is handy during the migration)
124
    config.add_facet_field 'data_source_ssi', label: 'Source'
1✔
125

126
    # Have BL send all facet field names to Solr, which has been the default
127
    # previously. Simply remove these lines if you'd rather use Solr request
128
    # handler defaults, or have no facets.
129
    config.add_facet_fields_to_solr_request!
1✔
130

131
    # solr fields to be displayed in the index (search results) view
132
    #   The ordering of the field names is the order of the display
133

134
    # Notice that for the author field we key of the `author_tesim` field but in reality
135
    # we render a different value (see the helper). We use `author_tesim` in here because
136
    # that is a common field between all our records, the ones coming from DataSpace
137
    # and the ones coming from PDC Describe.
138
    config.add_index_field 'author_tesim', label: 'Author(s)', helper_method: :authors_search_results_helper
1✔
139

140
    config.add_index_field 'format', label: 'Format'
1✔
141
    config.add_index_field 'abstract_tsim', label: 'Abstract'
1✔
142
    config.add_index_field 'published_ssim', label: 'Published'
1✔
143
    config.add_index_field 'published_vern_ssim', label: 'Published'
1✔
144
    config.add_index_field 'genre_ssim', label: 'Type'
1✔
145
    config.add_index_field 'issue_date_ssim', label: 'Issue Date'
1✔
146

147
    # solr fields to be displayed in the show (single result) view
148
    #   The ordering of the field names is the order of the display
149
    config.add_show_field 'author_tesim', label: 'Author'
1✔
150
    config.add_show_field 'format', label: 'Format'
1✔
151
    config.add_show_field 'url_fulltext_ssim', label: 'URL'
1✔
152
    config.add_show_field 'url_suppl_ssim', label: 'More Information'
1✔
153
    config.add_show_field 'language_ssim', label: 'Language'
1✔
154
    config.add_show_field 'published_ssim', label: 'Published'
1✔
155
    config.add_show_field 'published_vern_ssim', label: 'Published'
1✔
156
    config.add_show_field 'lc_callnum_ssim', label: 'Call number'
1✔
157
    config.add_show_field 'isbn_ssim', label: 'ISBN'
1✔
158
    config.add_show_field 'handle_ssim', label: 'Handle'
1✔
159

160
    config.add_show_field 'abstract_tsim', label: 'Abstract'
1✔
161
    config.add_show_field 'contributor_tsim', label: 'Author'
1✔
162
    config.add_show_field 'description_tsim', label: 'Description'
1✔
163
    config.add_show_field 'issue_date_ssim', label: 'Issued Date'
1✔
164
    config.add_show_field 'methods_tsim', label: 'Methods'
1✔
165

166
    # "fielded" search configuration. Used by pulldown among other places.
167
    # For supported keys in hash, see rdoc for Blacklight::SearchFields
168
    #
169
    # Search fields will inherit the :qt solr request handler from
170
    # config[:default_solr_parameters], OR can specify a different one
171
    # with a :qt key/value. Below examples inherit, except for subject
172
    # that specifies the same :qt as default for our own internal
173
    # testing purposes.
174
    #
175
    # The :key is what will be used to identify this BL search field internally,
176
    # as well as in URLs -- so changing it after deployment may break bookmarked
177
    # urls.  A display label will be automatically calculated from the :key,
178
    # or can be specified manually to be different.
179

180
    # This one uses all the defaults set by the solr request handler. Which
181
    # solr request handler? The one set in config[:default_solr_parameters][:qt],
182
    # since we aren't specifying it otherwise.
183

184
    config.add_search_field 'all_fields', label: 'All Fields'
1✔
185

186
    # Now we see how to over-ride Solr request handler defaults, in this
187
    # case for a BL "search field", which is really a dismax aggregate
188
    # of Solr search fields.
189

190
    config.add_search_field('title') do |field|
1✔
191
      # solr_parameters hash are sent to Solr as ordinary url query params.
192
      field.solr_parameters = {
1✔
193
        'spellcheck.dictionary': 'title',
194
        qf: '${title_qf}',
195
        pf: '${title_pf}'
196
      }
197
    end
198

199
    config.add_search_field('author') do |field|
1✔
200
      field.solr_parameters = {
1✔
201
        'spellcheck.dictionary': 'author',
202
        qf: '${author_qf}',
203
        pf: '${author_pf}'
204
      }
205
    end
206

207
    config.add_search_field('orcid') do |field|
1✔
208
      field.label = "ORCID"
1✔
209
      field.solr_parameters = {
1✔
210
        qf: 'authors_orcid_ssim',
211
        pf: 'authors_orcid_ssim'
212
      }
213
    end
214

215
    # Specifying a :qt only to show it's possible, and so our internal automated
216
    # tests can test it. In this case it's the same as
217
    # config[:default_solr_parameters][:qt], so isn't actually neccesary.
218
    config.add_search_field('subject') do |field|
1✔
219
      field.qt = 'search'
1✔
220
      field.solr_parameters = {
1✔
221
        'spellcheck.dictionary': 'subject',
222
        qf: '${subject_qf}',
223
        pf: '${subject_pf}'
224
      }
225
    end
226

227
    # "sort results by" select (pulldown)
228
    # label in pulldown is followed by the name of the Solr field to sort by and
229
    # whether the sort is ascending or descending (it must be asc or desc
230
    # except in the relevancy case). Add the sort: option to configure a
231
    # custom Blacklight url parameter value separate from the Solr sort fields.
232
    config.add_sort_field 'relevance', sort: 'score desc, year_available_itsi desc, title_si asc', label: 'relevance'
1✔
233
    config.add_sort_field 'year', sort: 'year_available_itsi desc, title_si asc', label: 'year'
1✔
234
    config.add_sort_field 'author', sort: 'author_si asc, title_si asc', label: 'author'
1✔
235
    config.add_sort_field 'title', sort: 'title_si asc, year_available_itsi desc', label: 'title'
1✔
236

237
    # If there are more than this many search results, no spelling ("did you
238
    # mean") suggestion is offered.
239
    config.spell_max = 5
1✔
240

241
    # # Configuration for autocomplete suggester
242
    # config.autocomplete_enabled = true
243
    # config.autocomplete_path = 'suggest'
244
    # # if the name of the solr.SuggestComponent provided in your solrconfig.xml is not the
245
    # # default 'mySuggester', uncomment and provide it below
246
    # # config.autocomplete_suggester = 'mySuggester'
247
    config.search_state_fields = config.search_state_fields + [
1✔
248
      :doi, :ark, :id,
249
      :a # this is in the search parameters becuase the search bar is shown on the error page
250
    ]
251
  end
252

253
  # Returns the raw BibTex citation information
254
  def bibtex
1✔
255
    _unused, @document = search_service.fetch(params[:id])
×
256
    citation = @document.cite("BibTeX")
×
257
    send_data citation, filename: "#{@document.bibtex_id}.bibtex", type: 'text/plain', disposition: 'attachment'
×
258
  end
259

260
  def resolve_doi
1✔
261
    raise Blacklight::Exceptions::RecordNotFound unless params.key?(:doi)
3✔
262

263
    doi_query = params[:doi]
3✔
264
    query = { q: "uri_ssim:*\"#{doi_query}\"" }
3✔
265

266
    solr_response = search_service.repository.search(**query)
3✔
267
    documents = solr_response.documents
3✔
268

269
    raise Blacklight::Exceptions::RecordNotFound if documents.empty?
3✔
270
    preferred = documents.select { |d| d.data_source == 'pdc_discovery' }
6✔
271
    document = if preferred.empty?
3✔
272
                 documents.first
3✔
273
               else
274
                 preferred.first
×
275
               end
276

277
    redirect_to(solr_document_path(id: document.id))
3✔
278
  end
279

280
  def resolve_ark
1✔
281
    raise Blacklight::Exceptions::RecordNotFound unless params.key?(:ark)
2✔
282

283
    ark = params[:ark]
2✔
284
    ark_query = "uri_ssim:*\"#{ark}\""
2✔
285
    query = { q: ark_query }
2✔
286

287
    solr_response = search_service.repository.search(**query)
2✔
288
    documents = solr_response.documents
2✔
289

290
    raise Blacklight::Exceptions::RecordNotFound if documents.empty?
2✔
291
    document = documents.first
2✔
292

293
    redirect_to(solr_document_path(id: document.id))
2✔
294
  end
295

296
  # Create an endpoint for PPPL / OSTI harvesting that provides full datacite records
297
  def pppl_reporting_feed
1✔
298
    # Limit to items from PPPL
299
    pppl_query = 'data_source_ssi:pdc_describe group_code_ssi:"PPPL"'
3✔
300
    page = params["page"] || "1"
3✔
301
    per_page = params["per_page"] || "10"
3✔
302
    start = per_page.to_i * (page.to_i - 1)
3✔
303

304
    query = { q: pppl_query, fl: 'pdc_describe_json_ss', format: 'json', sort: 'timestamp desc', rows: per_page, start: start }
3✔
305

306
    solr_response = search_service.repository.search(**query)
3✔
307

308
    @documents = solr_response.documents
3✔
309
    respond_to do |format|
3✔
310
      format.json { render json: @documents }
6✔
311
    end
312
  end
313
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc