Skip to content

Commit 32b3822

Browse files
committed
Adds support for configuring specific TIMDEX index
Why are these changes being introduced: * Being able to choose an index to search is required to be able to provide an RDI specific instance of this app Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/RDI-205 How does this address that need: * Pulls in the latest schema from GraphQL which enables optional setting of the index * Provdes `TIMDEX_INDEX` value in GraphQL query if it is set in ENV, if it is not set, the TIMDEX endpoint will default to whatever default value it is configured for * Updates cassettes since the schema changed Side effect: - Analyzer#hits private method was created to allow for stubbing hits in tests to avoid having to use cassettes to test the Analyzer. This removes a fragile test. - Added mocha gem to facilitate the stubbing noted above - Some cassettes were shared between controller and model tests for Record and they now need their own cassettes because the GraphQL is slightly different (we also could have updated the model tests but this seemed easier and less fragile down the road to not couple unrelated tests to the same cassette)
1 parent 3ece046 commit 32b3822

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+685
-431
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838
group :test do
3939
gem 'capybara'
4040
gem 'climate_control'
41+
gem 'mocha'
4142
gem 'selenium-webdriver'
4243
gem 'simplecov'
4344
gem 'simplecov-lcov'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ GEM
158158
mitlibraries-theme (0.7.0)
159159
rails (>= 5, < 8)
160160
sassc (~> 2)
161+
mocha (1.14.0)
161162
msgpack (1.5.2)
162163
net-imap (0.2.3)
163164
digest
@@ -317,6 +318,7 @@ DEPENDENCIES
317318
importmap-rails
318319
jbuilder
319320
mitlibraries-theme (~> 0.7.0)
321+
mocha
320322
pg
321323
puma (~> 5.0)
322324
rails (~> 7.0.2, >= 7.0.2.3)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ change as part of the work.
5151

5252
- `GLOBAL_ALERT`: The main functionality for this comes from our theme gem, but when set the value will be rendered as
5353
safe html above the main header of the site.
54+
- `TIMDEX_INDEX`: Name of the index, or alias, to provide to the GraphQL endpoint. Defaults to `nil` which will let TIMDEX determine the best index to use. Wildcard values can be set, for example `rdi*` would search any indexes that begin with `rdi` in the underlying OpenSearch instance behind TIMDEX.
5455
- `TIMDEX_SOURCES`: Comma-separated list of sources to display in the advanced-search source selection element. This
5556
overrides the default which is set in ApplicationHelper.
5657

app/controllers/record_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class RecordController < ApplicationController
55

66
def view
77
id = params[:id]
8+
index = ENV.fetch('TIMDEX_INDEX', nil)
89

9-
response = TimdexBase::Client.query(TimdexRecord::Query, variables: { id: })
10+
response = TimdexBase::Client.query(TimdexRecord::Query, variables: { id:, index: })
1011

1112
# Detection of unexpected response from the API would go here...
1213

app/models/analyzer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ class Analyzer
55

66
def initialize(enhanced_query, response)
77
@pagination = {}
8-
@pagination[:hits] = response&.data&.search&.to_h&.dig('hits')
8+
@pagination[:hits] = hits(response)
99
@pagination[:page] = enhanced_query[:page]
1010
@pagination[:prev] = enhanced_query[:page] - 1 if enhanced_query[:page] > 1
1111
@pagination[:next] = next_page(enhanced_query[:page], @pagination[:hits])
1212
end
1313

1414
private
1515

16+
def hits(response)
17+
response&.data&.search&.to_h&.dig('hits')
18+
end
19+
1620
def next_page(page, hits)
1721
page + 1 if page * RESULTS_PER_PAGE < hits
1822
end

app/models/query_builder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize(enhanced_query)
1010
@query['from'] = calculate_from(enhanced_query[:page])
1111
extract_query(enhanced_query)
1212
extract_filters(enhanced_query)
13+
@query['index'] = ENV.fetch('TIMDEX_INDEX', nil)
1314
@query.compact!
1415
end
1516

app/models/timdex_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
class TimdexRecord < TimdexBase
55
Query = TimdexBase::Client.parse <<-'GRAPHQL'
6-
query($id: String!) {
7-
recordId(id: $id) {
6+
query($id: String!, $index: String) {
7+
recordId(id: $id, index: $index) {
88
alternateTitles {
99
kind
1010
value

app/models/timdex_search.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TimdexSearch < TimdexBase
1313
$subjects: String
1414
$title: String
1515
$sourceFacet: [String!]
16+
$index: String
1617
$from: String
1718
$contentType: String
1819
) {
@@ -26,6 +27,7 @@ class TimdexSearch < TimdexBase
2627
subjects: $subjects
2728
title: $title
2829
sourceFacet: $sourceFacet
30+
index: $index
2931
from: $from
3032
contentTypeFacet: $contentType
3133
) {

config/schema/schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,16 @@
10871087
}
10881088
},
10891089
"defaultValue": null
1090+
},
1091+
{
1092+
"name": "index",
1093+
"description": "It is not recommended to provide an index value unless we have provided you with one for your specific use case",
1094+
"type": {
1095+
"kind": "SCALAR",
1096+
"name": "String",
1097+
"ofType": null
1098+
},
1099+
"defaultValue": "null"
10901100
}
10911101
],
10921102
"type": {
@@ -1195,6 +1205,16 @@
11951205
},
11961206
"defaultValue": "\"0\""
11971207
},
1208+
{
1209+
"name": "index",
1210+
"description": "It is not recommended to provide an index value unless we have provided you with one for your specific use case",
1211+
"type": {
1212+
"kind": "SCALAR",
1213+
"name": "String",
1214+
"ofType": null
1215+
},
1216+
"defaultValue": "null"
1217+
},
11981218
{
11991219
"name": "collectionFacet",
12001220
"description": null,

test/controllers/record_controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class RecordControllerTest < ActionDispatch::IntegrationTest
88
end
99

1010
test 'full record path with an id does not return an error' do
11-
VCR.use_cassette('timdex record sample',
11+
VCR.use_cassette('timdex controller record sample',
1212
allow_playback_repeats: true,
1313
match_requests_on: %i[method uri body]) do
1414
get '/record/jpal:doi:10.7910-DVN-MNIBOL'
@@ -18,7 +18,7 @@ class RecordControllerTest < ActionDispatch::IntegrationTest
1818

1919
test 'record ids can include multiple periods' do
2020
needle_id = 'there.is.no.record'
21-
VCR.use_cassette('timdex record no record',
21+
VCR.use_cassette('timdex controller record no record',
2222
allow_playback_repeats: true,
2323
match_requests_on: %i[method uri body]) do
2424
get "/record/#{needle_id}"
@@ -28,7 +28,7 @@ class RecordControllerTest < ActionDispatch::IntegrationTest
2828

2929
test 'full record display where no record exists displays an error' do
3030
needle_id = 'there.is.no.record'
31-
VCR.use_cassette('timdex record no record',
31+
VCR.use_cassette('timdex controller record no record',
3232
allow_playback_repeats: true,
3333
match_requests_on: %i[method uri body]) do
3434
get "/record/#{needle_id}"

0 commit comments

Comments
 (0)