Skip to content

Commit

Permalink
Add accumulators when we create a mediaflux project (#713)
Browse files Browse the repository at this point in the history
* Create accumulators when we make a mediaflux project

* Remove accumulator creation from test project generator

* Only save the project once when we persist to mediaflux
  • Loading branch information
bess authored May 10, 2024
1 parent 31be243 commit 29d447e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
4 changes: 4 additions & 0 deletions app/models/mediaflux/http/asset_metadata_request.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true
module Mediaflux
module Http
# Get metadata about an asset in mediaflux
# @example
# metadata_request = Mediaflux::Http::AssetMetadataRequest.new(
# session_token: current_user.mediaflux_session, id: mediaflux_id).metadata
class AssetMetadataRequest < Request
attr_reader :id

Expand Down
7 changes: 5 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ def self.data_user_projects(user)
Project.where("(metadata_json @> ? :: jsonb) OR (metadata_json @> ? :: jsonb)", query_ro, query_rw)
end

# If the project hasn't yet been created in mediaflux, create it.
# If it already exists, update it.
# @return [String] the mediaflux id of the project
def save_in_mediaflux(session_id:)
if mediaflux_id.nil?
self.mediaflux_id = ProjectMediaflux.create!(project: self, session_id: session_id)
save!
ProjectMediaflux.create!(project: self, session_id: session_id)
self.reload
Rails.logger.debug "Project #{id} has been created in MediaFlux (asset id #{mediaflux_id})"
else
ProjectMediaflux.update(project: self, session_id: session_id)
Expand Down
24 changes: 24 additions & 0 deletions app/models/project_mediaflux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,33 @@ def self.create!(project:, session_id:, xml_namespace: nil)
raise(StandardError,"An error has occured during project creation, not related to namespace creation or collection creation")
end
end
project.mediaflux_id = id
project.save!
self.create_accumulators(mediaflux_project_id: id, session_id: session_id)
id
end

# Create accumulators for all newly created mediaflux projects
#
# @param mediaflux_project_id [] the id of the project that needs accumulators
# @param session_id [] the session id for the user who is currently authenticated to MediaFlux
def self.create_accumulators(mediaflux_project_id:, session_id:)
accum_count = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(
session_token: session_id,
name: "accum-count",
collection: mediaflux_project_id,
type: "collection.asset.count"
)
accum_count.resolve
accum_size = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(
session_token: session_id,
name: "accum-size",
collection: mediaflux_project_id,
type: "content.all.size"
)
accum_size.resolve
end

def self.update(project:, session_id:)
tigerdata_values = project_values(project: project)
Mediaflux::Http::AssetUpdateRequest.new(session_token: session_id, id: project.mediaflux_id, tigerdata_values: tigerdata_values).resolve
Expand Down
6 changes: 1 addition & 5 deletions app/services/test_project_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ def initialize(user:, number:, project_prefix:)
def generate
project = create_project
session_id = user.mediaflux_session
id = project.save_in_mediaflux(session_id:)
accum_count = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(session_token: session_id, name: "accum-count", collection: id, type: "collection.asset.count")
accum_count.resolve
accum_size = Mediaflux::Http::CreateCollectionAccumulatorRequest.new(session_token: session_id, name: "accum-size", collection: id, type: "content.all.size")
accum_size.resolve
project.save_in_mediaflux(session_id:)
project.save!
project
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/mediaflux/http/asset_metadata_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
expect(metadata[:collection]).to be_truthy
expect(metadata[:path].include?(valid_project.metadata_json["project_directory"])).to be_truthy
expect(metadata[:type]).to eq("application/arc-asset-collection")
expect(metadata[:size]).to eq("") # accumulators are not added to project when created
expect(metadata[:total_file_count]).to eq("") # accumulators are not added to project when created
expect(metadata[:size]).to eq("0 bytes")
expect(metadata[:total_file_count]).to eq("0")
expect(metadata[:quota_allocation]).to eq("") # quotas are not added to project when created
expect(metadata[:project_id]).to eq("10.34770/tbd")
expect(metadata[:project_directory]).to eq(valid_project.project_directory)
Expand Down
14 changes: 14 additions & 0 deletions spec/models/project_mediaflux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
expect(namespace_metadata[:description]).to match(/Namespace for/)
end

describe "accumulators", connect_to_mediaflux: true do
it "adds accumulators when it creates a project in mediaflux" do
project = FactoryBot.create(:project_with_doi)
described_class.create!(project: project, session_id: current_user.mediaflux_session)
# project.save_in_mediaflux(session_id: current_user.mediaflux_session)
metadata = Mediaflux::Http::AssetMetadataRequest.new(
session_token: current_user.mediaflux_session,
id: project.mediaflux_id
).metadata
expect(metadata[:accumulators]).not_to be_empty
expect(metadata[:accumulators].size).to eq 2
end
end

context "when the name is already taken" do
it "raises an error" do
# Make the project once
Expand Down
13 changes: 6 additions & 7 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,13 @@
end
end

describe "#save_in_mediaflux" do
let(:project) { FactoryBot.create(:project) }
describe "#save_in_mediaflux", connect_to_mediaflux: true do
let(:user) { FactoryBot.create(:user) }
let(:project) { FactoryBot.create(:project_with_doi) }
it "calls ProjectMediaflux to create the project and save the id" do
allow(ProjectMediaflux).to receive(:create!).with(project: project, session_id: "111222333").and_return(27)
project.save_in_mediaflux(session_id: "111222333")
expect(ProjectMediaflux).to have_received(:create!)
expect(project.mediaflux_id).to eq(27)
expect(project).not_to be_changed # the method saves the id to the database
expect(project.mediaflux_id).to be nil
project.save_in_mediaflux(session_id: user.mediaflux_session)
expect(project.mediaflux_id).not_to be nil
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/system/project_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
click_on("Review Contents")
expect(page).to have_content("Project Contents")
expect(page).to have_content("File Count")
expect(find(:css, "#file_count").text).to eq "16"
expect(find(:css, "#file_count").text).to eq "1616"

# Be able to return to the dashboard
expect(page).to have_selector(:link_or_button, "Return to Dashboard")
Expand Down

0 comments on commit 29d447e

Please sign in to comment.