Skip to content

Commit

Permalink
Format timestamp for mediaflux (#533)
Browse files Browse the repository at this point in the history
* Update to new date format

Co-authored-by: Bess Sadler <[email protected]>

* Update time format

Co-authored-by: Bess Sadler <[email protected]>

* Progress towards standardizing timestamps

Co-authored-by: Bess Sadler <[email protected]>

* rubocop corrections

---------

Co-authored-by: Bess Sadler <[email protected]>
  • Loading branch information
leefaisonr and bess authored Feb 27, 2024
1 parent 1431cb9 commit 25ad9da
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ node_modules

# Do not check aterm.jar into the repo
aterm.jar

public
11 changes: 9 additions & 2 deletions app/models/project_mediaflux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def self.update(project:, session_id:)
Mediaflux::Http::UpdateAssetRequest.new(session_token: session_id, id: project.mediaflux_id, tigerdata_values: tigerdata_values).resolve
end

# This method is used for transforming iso8601 dates to dates that MediaFlux likes
# Take a string like "2024-02-26T10:33:11-05:00" and convert this string to "22-FEB-2024 13:57:19"
def self.format_date_for_mediaflux(iso8601_date)
return if iso8601_date.nil?
Time.parse(iso8601_date).strftime("%e-%b-%Y %H:%M:%S").upcase
end

# Translates database record into mediaflux meta document.
# This is where the XML payload is generated.
# rubocop:disable Metrics/MethodLength
Expand All @@ -47,9 +54,9 @@ def self.project_values(project:)
data_user_read_only: project.metadata[:data_user_read_only],
data_user_read_write: project.metadata[:data_user_read_write],
departments: project.metadata[:departments],
created_on: project.metadata[:created_on],
created_on: format_date_for_mediaflux(project.metadata[:created_on]),
created_by: project.metadata[:created_by],
updated_on: project.metadata[:updated_on],
updated_on: format_date_for_mediaflux(project.metadata[:updated_on]),
updated_by: project.metadata[:updated_by],
project_id: project.metadata[:project_id],
storage_capacity: project.metadata[:storage_capacity_requested],
Expand Down
4 changes: 2 additions & 2 deletions app/models/project_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def project_timestamps
timestamps = {}
if project.metadata[:created_by].nil?
timestamps[:created_by] = current_user.uid
timestamps[:created_on] = DateTime.now.strftime("%d-%b-%Y %H:%M:%S")
timestamps[:created_on] = Time.current.in_time_zone("America/New_York").iso8601

else
timestamps[:created_by] = project.metadata[:created_by]
timestamps[:created_on] = project.metadata[:created_on]
timestamps[:updated_by] = current_user.uid
timestamps[:updated_on] = DateTime.now.strftime("%d-%b-%Y %H:%M:%S")
timestamps[:updated_on] = Time.current.in_time_zone("America/New_York").iso8601
end
timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" <CreatedOn>#{project.metadata[:created_on]}</CreatedOn>\n" \
" <CreatedOn>#{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>\n" \
" <CreatedBy>#{project.metadata[:created_by]}</CreatedBy>\n" \
" <ProjectID>abc-123</ProjectID>\n" \
" <StorageCapacity>500 GB</StorageCapacity>\n" \
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
data_user_read_only { [] }
data_user_read_write { [] }
title { FFaker::Movie.title }
created_on { DateTime.now }
updated_on { DateTime.now }
created_on { Time.current.in_time_zone("America/New_York").iso8601 }
updated_on { Time.current.in_time_zone("America/New_York").iso8601 }
project_id { nil }
status { "pending" }
storage_capacity { "500 GB" }
Expand All @@ -28,9 +28,9 @@
directory: directory,
title: title,
description: "a random description",
created_on: created_on.strftime("%d-%b-%Y %H:%M:%S"),
created_on: created_on,
created_by: FactoryBot.create(:user).uid,
updated_on: created_on.strftime("%d-%b-%Y %H:%M:%S"),
updated_on: updated_on,
updated_by: FactoryBot.create(:user).uid,
project_id: project_id,
status: status,
Expand Down
8 changes: 4 additions & 4 deletions spec/models/mediaflux/http/create_asset_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
before do
stub_request(:post, mediflux_url).to_return(status: 200, body: create_response, headers: {})
end
it "send the metadata to the server" do
it "sends the metadata to the server" do
data_user_ro = FactoryBot.create :user
data_user_rw = FactoryBot.create :user
created_on = DateTime.now
created_on = Time.current.in_time_zone("America/New_York").iso8601
project = FactoryBot.create :project, data_user_read_only: [data_user_ro.uid], data_user_read_write: [data_user_rw.uid], created_on: created_on
tigerdata_values = ProjectMediaflux.project_values(project:)
create_request = described_class.new(session_token: "secretsecret/2/31", name: "testasset", collection: false, tigerdata_values: tigerdata_values)
Expand All @@ -46,7 +46,7 @@
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<DataManager>#{project.metadata[:data_manager]}</DataManager>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<Department>#{project.metadata[:departments].first}</Department>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<Department>#{project.metadata[:departments].last}</Department>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<CreatedOn>#{created_on.strftime("%d-%b-%Y %H:%M:%S")}</CreatedOn>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<CreatedOn>#{ProjectMediaflux.format_date_for_mediaflux(created_on)}</CreatedOn>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<CreatedBy>#{project.metadata[:created_by]}</CreatedBy>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<DataUser ReadOnly=\"true\">#{data_user_ro.uid}</DataUser>") }).to have_been_made
expect(a_request(:post, mediflux_url).with { |req| req.body.include?("<DataUser>#{data_user_rw.uid}</DataUser>") }).to have_been_made
Expand Down Expand Up @@ -116,7 +116,7 @@
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" <CreatedOn>#{project.metadata[:created_on]}</CreatedOn>\n" \
" <CreatedOn>#{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>\n" \
" <CreatedBy>#{project.metadata[:created_by]}</CreatedBy>\n" \
" <ProjectID>abc-123</ProjectID>\n" \
" <StorageCapacity>500 GB</StorageCapacity>\n" \
Expand Down
6 changes: 3 additions & 3 deletions spec/models/mediaflux/http/update_asset_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
it "sends the metadata to the server" do
data_user_ro = FactoryBot.create :user
data_user_rw = FactoryBot.create :user
updated_on = DateTime.now
updated_on = Time.current.in_time_zone("America/New_York").iso8601
project = FactoryBot.create :project, data_user_read_only: [data_user_ro.uid], data_user_read_write: [data_user_rw.uid], updated_on: updated_on
tigerdata_values = ProjectMediaflux.project_values(project:)
update_request = described_class.new(session_token: "secretsecret/2/31", id: "1234", tigerdata_values: tigerdata_values)
Expand All @@ -57,7 +57,7 @@
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<DataManager>#{project.metadata[:data_manager]}</DataManager>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<Department>#{project.metadata[:departments].first}</Department>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<Department>#{project.metadata[:departments].last}</Department>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<UpdatedOn>#{updated_on.strftime("%d-%b-%Y %H:%M:%S")}</UpdatedOn>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<UpdatedOn>#{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:updated_on])}</UpdatedOn>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<UpdatedBy>#{project.metadata[:updated_by]}</UpdatedBy>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<DataUser ReadOnly=\"true\">#{data_user_ro.uid}</DataUser>") }).to have_been_made
expect(a_request(:post, mediaflux_url).with { |req| req.body.include?("<DataUser>#{data_user_rw.uid}</DataUser>") }).to have_been_made
Expand All @@ -84,7 +84,7 @@
" <DataSponsor>#{project.metadata[:data_sponsor]}</DataSponsor>\n" \
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <UpdatedBy>#{project.metadata[:updated_by]}</UpdatedBy>\n" \
" <UpdatedOn>#{project.metadata[:updated_on]}</UpdatedOn>\n" \
" <UpdatedOn>#{ProjectMediaflux.format_date_for_mediaflux(project.metadata[:updated_on])}</UpdatedOn>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" </tigerdata:project>\n" \
Expand Down
12 changes: 12 additions & 0 deletions spec/models/project_mediaflux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
allow(create_asset_request).to receive(:id).and_return("123")
end

context "it formats dates correctly for MediaFlux" do
let(:project) { FactoryBot.build(:project, created_on: "2024-02-22T13:57:19-05:00", updated_on: "2024-02-26T13:57:19-05:00") }
it "formats created_on as expected" do
project_values = ProjectMediaflux.project_values(project: project)
expect(project_values[:created_on]).to eq "22-FEB-2024 13:57:19"
end
it "formats updated_on as expected" do
project_values = ProjectMediaflux.project_values(project: project)
expect(project_values[:updated_on]).to eq "26-FEB-2024 13:57:19"
end
end

it "creates a project namespace and collection and returns the mediaflux id" do
mediaflux_id = described_class.create!(project: project, session_id: "test-session-token")
expect(namespace_request).to have_received("error?")
Expand Down

0 comments on commit 25ad9da

Please sign in to comment.