Skip to content

Commit

Permalink
Show files in S3 on dataset show page
Browse files Browse the repository at this point in the history
Also: Add configuration so we do not update ARK url unless we explicitly
say we're ready.
  • Loading branch information
bess committed Apr 28, 2022
1 parent 727c8ca commit 757d9a7
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ To create a tagged release use the [steps in the RDSS handbook](https://github.c
An early stages Entity-Relationship Diagram (ERD) is available in [this Google Doc](https://docs.google.com/drawings/d/1q2sfj8rrcNVgqQPK5uT_t79A9SYqncinh3HbnCSGMyQ/edit).

### Sample Data
Sample data available here: https://docs.google.com/document/d/18ZkBldqWxIIR1UA6qMY87RnGFTKU9HG3EJzodzzFf2A/edit#heading=h.cj3zec9ihjhc
Sample data available here: https://docs.google.com/document/d/18ZkBldqWxIIR1UA6qMY87RnGFTKU9HG3EJzodzzFf2A/edit
14 changes: 9 additions & 5 deletions app/models/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ class Dataset < ApplicationRecord
end

after_save do |ds|
if ds.ark.present?
# Ensure that the ARK metadata is updated for the new URL
if ark_object.target != ds.url
ark_object.target = ds.url
ark_object.save!
# We only want to update the ark url under certain conditions.
# Set this value in config/update_ark_url.yml
if Rails.configuration.update_ark_url
if ds.ark.present?
# Ensure that the ARK metadata is updated for the new URL
if ark_object.target != ds.url
ark_object.target = ds.url
ark_object.save!
end
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/s3_file.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true
class S3File
attr_accessor :filename
attr_accessor :filename, :last_modified, :size

def initialize(filename:)
def initialize(filename:, last_modified:, size:)
@filename = filename
@last_modified = last_modified
@size = size
end
end
2 changes: 1 addition & 1 deletion app/services/s3_query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def data_profile
objects = []
resp = client.list_objects_v2({ bucket: bucket_name, max_keys: 1000, prefix: prefix })
resp.to_h[:contents].each do |object|
s3_file = S3File.new(filename: object[:key])
s3_file = S3File.new(filename: object[:key], last_modified: object[:last_modified], size: object[:size])
objects << s3_file
end
objects
Expand Down
63 changes: 52 additions & 11 deletions app/views/datasets/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
<h1><%= @dataset.title %></h1>

<div>
<p>DOI: <%= link_to(@dataset.doi, @dataset.doi) %></p>
<p>ARK: <%= link_to(@dataset.ark, @dataset.ark_url) %></p>
<p>Created by: <%= @dataset.created_by_user.uid %></p>
<p>Collection: <%= @dataset.collection_title %></p>
</div>

<!-- TODO: Move this logic to a helper -->
<div>
<h3>Files in S3</h3>
<% if @files %>
<% @files.each do |file| %>
<%= file.filename %>
<% end %>
<% else %>
<p>No files in S3</p>
<% end %>
</div>

<section class="files-section">
<div class="lux">
<div class="card">
<div class="files card-body">

<!-- Only render file download table if there are files in DataSpace -->

<% if @files && @files.empty? %>
<p>No files in S3</p>
<% else %>
<table id="files-table" class="table">
<thead>
<tr>
<th scope="col" nowrap="nowrap"><span>#</span></th>
<th scope="col" nowrap="nowrap"><span>Filename</span></th>
<th scope="col"><span>Last Modified</span></th>
<th scope="col" nowrap="nowrap"><span>Filesize</span></th>
</tr>
</thead>
<tbody>
<% @files&.each_with_index do |file, ix| %>
<tr class="files">
<th scope="row">
<span><span><%= ix + 1 %></span></span>
</th>
<td>
<span>
<i class="bi bi-file-arrow-down"></i>
<a href="<%= file.filename %>" class="documents-file-link" target="_blank" title="<%= file.filename %>"><%= truncate(file.filename, length: 80) %></a>
</span>
</td>
<td>
<span><%= file.last_modified %></span>
</td>
<td>
<span><span><%= number_to_human_size(file.size) %></span></span>
</td>
</tr>
<% end %>
</tbody>
<tfoot></tfoot>
</table>
<% end %>

<!-- End of file download table -->

</div>
</div>
</div>
</section>

<div>
<%= link_to("Edit", edit_dataset_path(@dataset), class: "btn btn-primary") %>
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/load_update_ark_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
module PdcDescribe
class Application < Rails::Application
config.update_ark_url = config_for(:update_ark_url)
end
end
9 changes: 9 additions & 0 deletions config/update_ark_url.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
production:
false
staging:
false
development:
false
test:
false
2 changes: 1 addition & 1 deletion spec/factories/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
factory :dataset do
factory :shakespeare_and_company_dataset do
doi { "https://doi.org/10.34770/pe9w-x904" }
ark { "http://arks.princeton.edu/ark:/88435/dsp01zc77st047" }
ark { "ark:/88435/dsp01zc77st047" }
work { FactoryBot.create(:shakespeare_and_company_work) }
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/models/dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
subject(:data_set) { described_class.create_skeleton("test title", user.id, collection.id) }

context "and when the ARK is valid" do
around do |example|
Rails.configuration.update_ark_url = true
example.run
Rails.configuration.update_ark_url = false
end

before do
# stub_request(:get, "https://ezid.cdlib.org/id/#{ezid}").to_return(status: 200, body: response_body)
end
Expand Down
10 changes: 7 additions & 3 deletions spec/models/s3_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
require "rails_helper"

RSpec.describe S3File, type: :model do
let(:subject) { described_class.new(filename: filename) }
let(:filename) { "research_data.csv" }
let(:subject) { described_class.new(filename: filename, last_modified: last_modified, size: size) }
let(:filename) { "10-34770/pe9w-x904/SCoData_combined_v1_2020-07_README.txt" }
let(:last_modified) { Time.parse("2022-04-21T18:29:40.000Z") }
let(:size) { 10_759 }

it "can take a filename as an initial argument" do
it "can take S3 file data at creation time" do
expect(subject.filename).to eq filename
expect(subject.last_modified).to eq last_modified
expect(subject.size).to eq size
end
end
2 changes: 2 additions & 0 deletions spec/services/s3_query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@
expect(data_profile.count).to eq 2
expect(data_profile.first).to be_instance_of(S3File)
expect(data_profile.first.filename).to match(/README/)
expect(data_profile.first.last_modified).to eq Time.parse("2022-04-21T18:29:40.000Z")
expect(data_profile.first.size).to eq 10_759
end
end
22 changes: 20 additions & 2 deletions spec/system/view_data_in_s3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@
let(:user) { FactoryBot.create :user }
let(:dataset) { FactoryBot.create :shakespeare_and_company_dataset }
let(:s3_query_service_double) { instance_double(S3QueryService) }
let(:file1) { S3File.new(filename: "SCoData_combined_v1_2020-07_README.txt") }
let(:file2) { S3File.new(filename: "SCoData_combined_v1_2020-07_datapackage.json") }
let(:file1) do
S3File.new(
filename: "SCoData_combined_v1_2020-07_README.txt",
last_modified: Time.parse("2022-04-21T18:29:40.000Z"),
size: 10_759
)
end
let(:file2) do
S3File.new(
filename: "SCoData_combined_v1_2020-07_datapackage.json",
last_modified: Time.parse("2022-04-21T18:30:07.000Z"),
size: 12_739
)
end
let(:s3_data) { [file1, file2] }

before do
Expand All @@ -48,8 +60,14 @@
it "shows data from S3", js: true do
visit dataset_path(dataset)
expect(page).to have_content dataset.title

expect(page).to have_content file1.filename
expect(page).to have_content file1.last_modified
expect(page).to have_content "10.5 KB"

expect(page).to have_content file2.filename
expect(page).to have_content file2.last_modified
expect(page).to have_content "12.4 KB"
end
end
end

0 comments on commit 757d9a7

Please sign in to comment.