Skip to content

Commit

Permalink
Merge pull request #923 from datacite/igsn-main
Browse files Browse the repository at this point in the history
Support for igsnCatalog client_type and client-type filter for DOIs queries
  • Loading branch information
codycooperross authored Mar 3, 2023
2 parents 5d5e073 + 78c7897 commit b5c5d0b
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def index
end
client_types =
if total.positive?
facet_by_key(response.aggregations.client_types.buckets)
facet_by_client_type(response.aggregations.client_types.buckets)
end
certificates =
if total.positive?
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module Facetable

LOWER_BOUND_YEAR = 2_010

CLIENT_TYPES = {
"repository" => "Repository",
"periodical" => "Periodical",
"igsnCatalog" => "IGSN ID Catalog"
}.freeze

included do
def facet_by_key_as_string(arr)
arr.map do |hsh|
Expand Down Expand Up @@ -201,6 +207,16 @@ def facet_by_bool(arr)
end
end

def facet_by_client_type(arr)
arr.map do |hsh|
{
"id" => hsh["key"],
"title" => CLIENT_TYPES[hsh["key"]] || hsh["key"],
"count" => hsh["doc_count"],
}
end
end

def facet_by_software(arr)
arr.map do |hsh|
{
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def index
page: page,
sort: sort,
random: params[:random],
client_type: params[:client_type],
)
end

Expand Down Expand Up @@ -219,6 +220,7 @@ def index
resource_types = facet_by_combined_key(response.aggregations.resource_types.buckets)
published = facet_by_range(response.aggregations.published.buckets)
created = facet_by_key_as_string(response.aggregations.created.buckets)
created_by_month = response.aggregations.created_by_month ? facet_by_key_as_string(response.aggregations.created_by_month.buckets) : nil
registered = facet_by_key_as_string(response.aggregations.registered.buckets)
providers = facet_by_combined_key(response.aggregations.providers.buckets)
clients = facet_by_combined_key(response.aggregations.clients.buckets)
Expand Down Expand Up @@ -278,6 +280,7 @@ def index
states: states,
"resourceTypes" => resource_types,
created: created,
createdByMonth: created_by_month,
published: published,
registered: registered,
providers: providers,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def index
end
client_types =
if total.positive?
facet_by_key(response.aggregations.client_types.buckets)
facet_by_client_type(response.aggregations.client_types.buckets)
end
repository_types =
if total.positive?
Expand Down
2 changes: 1 addition & 1 deletion app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Client < ApplicationRecord
in: %w[ROLE_DATACENTRE],
message: "Role %s is not included in the list"
validates_inclusion_of :client_type,
in: %w[repository periodical],
in: %w[repository periodical igsnCatalog],
message: "Client type %s is not included in the list"
validates_associated :provider
validate :check_id, on: :create
Expand Down
11 changes: 11 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,13 @@ def self.export_sub_aggregations
}
end

def self.igsn_id_catalog_aggregations
{
created_by_month: { date_histogram: { field: "created", interval: "month", format: "yyyy-MM", order: { _key: "desc" }, min_doc_count: 1 },
aggs: { bucket_truncate: { bucket_sort: { size: 10 } } } }
}
end

def self.query_fields
["uid^50", "related_identifiers.relatedIdentifier^3", "titles.title^3", "creator_names^3", "creators.id^3", "publisher^3", "descriptions.description^3", "subjects.subject^3"]
end
Expand Down Expand Up @@ -1028,6 +1035,8 @@ def self.query(query, options = {})
client_export_aggregations
elsif options[:totals_agg] == "prefix"
prefix_aggregations
elsif options[:client_type] == "igsnCatalog"
query_aggregations(disable_facets: options[:disable_facets]).merge(self.igsn_id_catalog_aggregations)
else
query_aggregations(disable_facets: options[:disable_facets])
end
Expand Down Expand Up @@ -1133,6 +1142,8 @@ def self.query(query, options = {})
filter << { terms: { "client.certificate" => options[:certificate].split(",") } } if options[:certificate].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifier" => "https://orcid.org/#{orcid_from_url(options[:user_id])}" } } if options[:user_id].present?
filter << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ORCID" } } if options[:has_person].present?
filter << { term: { "client.client_type" => options[:client_type] } } if options[:client_type]
filter << { term: { "types.resourceTypeGeneral" => "PhysicalObject" } } if options[:client_type] == "igsnCatalog"

# match either one of has_affiliation, has_organization, or has_funder
if options[:has_organization].present?
Expand Down
6 changes: 6 additions & 0 deletions spec/models/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@
expect(client.errors.details).to be_empty
end

it "igsnCatalog" do
client.client_type = "igsnCatalog"
expect(client.save).to be true
expect(client.errors.details).to be_empty
end

it "unsupported" do
client.client_type = "conference"
expect(client.save).to be false
Expand Down
4 changes: 4 additions & 0 deletions spec/models/prefix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
let!(:prefixes) { create_list(:prefix, 10) }
let!(:prefix) { prefixes.first }

after(:each) do
Prefix.destroy_all
end

describe "Validations" do
it { should validate_presence_of(:uid) }
end
Expand Down
5 changes: 3 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@
end

config.before(:each) do |example|
prefix_pool_size = example.metadata[:prefix_pool_size].present? ? example.metadata[:prefix_pool_size] : ENV["PREFIX_POOL_SIZE"].to_i
if prefix_pool_size < 0
prefix_pool_size = example.metadata[:prefix_pool_size].present? ? example.metadata[:prefix_pool_size].to_i : ENV["PREFIX_POOL_SIZE"].to_i
if prefix_pool_size <= 0
@prefix_pool = []
else
@prefix_pool = create_list(:prefix, prefix_pool_size)
end
Prefix.import
end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/requests/clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@

describe "POST /clients" do
context "when the request is valid" do
let(:params_igsn_catalog) do
{
"data" => {
"type" => "clients",
"attributes" => {
"symbol" => provider.symbol + ".IGSN",
"name" => "Imperial College",
"contactEmail" => "[email protected]",
"clientType" => "igsnCatalog",
},
"relationships": {
"provider": {
"data": { "type": "providers", "id": provider.uid },
},
},
},
}
end

it "creates a client" do
post "/clients", params, headers

Expand All @@ -161,6 +180,24 @@
[{ "count" => 2, "id" => "repository", "title" => "Repository" }],
)
end

it "creates a client with igsnCatalog client_type" do
post "/clients", params_igsn_catalog, headers

expect(last_response.status).to eq(201)
attributes = json.dig("data", "attributes")
expect(attributes["clientType"]).to eq("igsnCatalog")

Client.import
sleep 2

get "/clients", nil, headers

expect(json["data"].size).to eq(2)
expect(json.dig("meta", "clientTypes").find { |clientTypeAgg| clientTypeAgg["id"] == "igsnCatalog" }).to eq(
{ "count" => 1, "id" => "igsnCatalog", "title" => "IGSN ID Catalog" },
)
end
end

context "when the request is invalid" do
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@
end
end

describe "GET /dois with filter", elasticsearch: true do
let!(:dois) { create_list(:doi, 10, client: client, aasm_state: "findable", version_info: "testtag") }
let(:client_igsn_id_catalog) { create(:client, provider: provider, client_type: "igsnCatalog") }
let!(:doi_igsn_id) { create(:doi, client: client_igsn_id_catalog, aasm_state: "findable", types: { "resourceTypeGeneral": "PhysicalObject" }) }
let!(:dois_other) { create_list(:doi, 5, client: client_igsn_id_catalog, aasm_state: "findable", types: { "resourceTypeGeneral": "Dataset" }) }

before do
DataciteDoi.import
sleep 2
end

it "filters by client_type when client-type is set", vcr: true do
get "/dois?client-type=repository", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(10)
end

it "returns additional createdByMonth meta attribute and only DOIs with resourceTypeGeneral=PhysicalObject and client_type=igsnCatalog when client-type is set to igsnCatalog", vcr: true do
get "/dois?client-type=igsnCatalog", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(1)
expect(json.dig("data", 0, "id")).to eq(doi_igsn_id.uid)
expect(json.dig("meta", "createdByMonth", 0, "title")).to eq(doi_igsn_id.created.to_time.strftime("%Y-%m"))
end
end

describe "GET /dois with query", elasticsearch: true do
let!(:doi) do
create(:doi, client: client, aasm_state: "findable", creators:
Expand Down
37 changes: 24 additions & 13 deletions spec/requests/provider_prefixes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,39 @@
require "rails_helper"

describe ProviderPrefixesController, type: :request, elasticsearch: true do
let(:consortium) { create(:provider, role_name: "ROLE_CONSORTIUM") }
let(:provider) do
let!(:consortium) { create(:provider, role_name: "ROLE_CONSORTIUM") }
let!(:provider) do
create(
:provider,
consortium: consortium,
role_name: "ROLE_CONSORTIUM_ORGANIZATION",
password_input: "12345",
)
end
let(:prefix) { create(:prefix) }
let!(:prefix) { create(:prefix) }
let!(:provider_prefixes) do
create_list(:provider_prefix, 3, provider: provider)
[
create(:provider_prefix, provider: provider, prefix: @prefix_pool[0]),
create(:provider_prefix, provider: provider, prefix: @prefix_pool[1]),
create(:provider_prefix, provider: provider, prefix: @prefix_pool[2])
]
end
let!(:provider_prefixes2) { create_list(:provider_prefix, 2) }
let(:provider_prefix) { create(:provider_prefix) }
let(:bearer) { User.generate_token(role_id: "staff_admin") }
let(:headers) do
let!(:provider_prefixes2) do
[
create(:provider_prefix, prefix: @prefix_pool[3]),
create(:provider_prefix, prefix: @prefix_pool[4])
]
end
let!(:provider_prefix) { create(:provider_prefix, prefix: @prefix_pool[5]) }
let!(:bearer) { User.generate_token(role_id: "staff_admin") }
let!(:headers) do
{
"HTTP_ACCEPT" => "application/vnd.api+json",
"HTTP_AUTHORIZATION" => "Bearer " + bearer,
}
end

before do
before(:each) do
ProviderPrefix.import
Prefix.import
Provider.import
Expand Down Expand Up @@ -102,7 +111,7 @@
get "/provider-prefixes?query=10.508", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(5)
expect(json["data"].size).to eq(6)
end
end

Expand All @@ -111,7 +120,7 @@
get "/provider-prefixes", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(5)
expect(json["data"].size).to eq(6)
end

it "returns correct paging links" do
Expand Down Expand Up @@ -191,19 +200,21 @@
get "/prefixes?state=unassigned", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("meta", "total")).to eq(@prefix_pool.length)
expect(json.dig("meta", "total")).to eq(@prefix_pool.length - 6)


delete "/provider-prefixes/#{provider_prefix.uid}", nil, headers

expect(last_response.status).to eq(204)

Prefix.import
ProviderPrefix.import
sleep 2

get "/prefixes?state=unassigned", nil, headers

expect(last_response.status).to eq(200)
expect(json.dig("meta", "total")).to eq(@prefix_pool.length + 1)
expect(json.dig("meta", "total")).to eq(@prefix_pool.length - 5)
end
end

Expand Down

0 comments on commit b5c5d0b

Please sign in to comment.