From 7f4fe692cd148a7abae09e2b22f6eef1c99ba0a7 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 28 Sep 2020 08:43:42 +0200 Subject: [PATCH] fix activities api. #533 --- app/controllers/activities_controller.rb | 2 +- app/models/activity.rb | 4 ++ app/models/concerns/indexable.rb | 8 +++ spec/requests/activities_spec.rb | 87 +++++++++++++++++++++++- 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 0e050a065..e4955f50a 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -19,7 +19,7 @@ def index response = Activity.find_by_id(params[:ids], page: page, sort: sort) else response = Activity.query(params[:query], - uid: params[:doi_id] || params[:provider_id] || params[:client_id] || params[:repository_id], + uid: params[:datacite_doi_id] || params[:provider_id] || params[:client_id] || params[:repository_id], page: page, sort: sort, scroll_id: params[:scroll_id]) diff --git a/app/models/activity.rb b/app/models/activity.rb index cd61b10c6..4c4a0edd9 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -54,6 +54,10 @@ def as_indexed_json(options={}) } end + def self.query_fields + ['uid^10', 'username^5', 'action', 'changes', 'was_derived_from', 'was_attributed_to', 'was_generated_by'] + end + def self.query_aggregations {} end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 7911bf638..1e353b354 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -309,6 +309,14 @@ def query(query, options={}) filter << { range: { created_at: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present? filter << { terms: { client_id: options[:client_id].split(",") }} if options[:client_id].present? filter << { term: { prefix_id: options[:prefix_id] }} if options[:prefix_id].present? + elsif self.name == "Activity" + if query.present? + must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }] + else + must = [{ match_all: {} }] + end + + filter << { terms: { uid: options[:uid].to_s.split(",") }} if options[:uid].present? end # ES query can be optionally defined in different ways diff --git a/spec/requests/activities_spec.rb b/spec/requests/activities_spec.rb index 34484cd15..f7f021247 100644 --- a/spec/requests/activities_spec.rb +++ b/spec/requests/activities_spec.rb @@ -9,6 +9,7 @@ describe "activities for doi", elasticsearch: true do let!(:doi) { create(:doi, client: client) } + let!(:other_doi) { create(:doi, client: client) } before do DataciteDoi.import @@ -18,9 +19,93 @@ context "without username" do it "returns the activities" do - get "/activities", nil, headers + get "/dois/#{doi.doi.downcase}/activities", nil, headers expect(last_response.status).to eq(200) + expect(json.dig("data").length).to eq(1) + expect(json.dig("meta", "total")).to eq(1) + expect(json.dig("data", 0, "attributes", "action")).to eq("create") + expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft") + + expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil + expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present + expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present + expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present + end + end + end + + describe "activities for repository", elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + let!(:other_doi) { create(:doi, client: client) } + + before do + DataciteDoi.import + Activity.import + sleep 2 + end + + context "repository" do + it "returns the activities" do + get "/repositories/#{client.symbol.downcase}/activities", nil, headers + + expect(last_response.status).to eq(200) + expect(json.dig("data").length).to eq(1) + expect(json.dig("meta", "total")).to eq(1) + expect(json.dig("data", 0, "attributes", "action")).to eq("create") + + expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil + expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present + expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present + expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present + end + end + end + + describe "activities for provider", elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + let!(:other_doi) { create(:doi, client: client) } + + before do + DataciteDoi.import + Activity.import + sleep 2 + end + + context "provider" do + it "returns the activities" do + get "/providers/#{provider.symbol.downcase}/activities", nil, headers + + expect(last_response.status).to eq(200) + expect(json.dig("data").length).to eq(1) + expect(json.dig("meta", "total")).to eq(1) + expect(json.dig("data", 0, "attributes", "action")).to eq("create") + + expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil + expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present + expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present + expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present + end + end + end + + describe "query activities", elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + let!(:other_doi) { create(:doi, client: client) } + + before do + DataciteDoi.import + Activity.import + sleep 2 + end + + context "query" do + it "returns the activities" do + get "/activities?query=#{doi.doi.downcase}", nil, headers + + expect(last_response.status).to eq(200) + expect(json.dig("data").length).to eq(1) + expect(json.dig("meta", "total")).to eq(1) expect(json.dig("data", 0, "attributes", "action")).to eq("create") expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft")