Skip to content

Commit afb07ca

Browse files
committed
Added connector metadata to DownloadFile
1 parent 9d0fa83 commit afb07ca

16 files changed

Lines changed: 156 additions & 96 deletions

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**What this PR does / why we need it**:
2+
3+
**Which issue(s) this PR closes**:
4+
5+
Closes #
6+
7+
**Special notes for your reviewer**:
8+
9+
**Suggestions on how to test this**:
10+
11+
**Does this PR introduce a user interface change? If mockups are available, please link/include them here**:
12+
13+
**Is there a release notes update needed for this change?**:
14+
15+
**Additional documentation**:

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: test
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches: [ main ]
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
build-and-test-app:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout ${{ github.sha }}
17+
uses: actions/checkout@v4
18+
- name: test app
19+
working-directory: .
20+
run: make tests
21+

app/lib/logging_common.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ def log_info(message, data = {})
44
Rails.logger.info(format_log("INFO", message, data))
55
end
66

7-
def log_error(message, data = {})
8-
Rails.logger.error(format_log("ERROR", message, data))
7+
def log_error(message, data = {}, exception = nil)
8+
log_message = format_log("ERROR", message, data)
9+
10+
if exception
11+
# First 5 lines as a stack trace
12+
log_message += "\n[STACK] " + exception.message
13+
log_message += "\n[STACK] " + exception.backtrace&.first(5)&.join("\n[STACK] ")
14+
end
15+
16+
Rails.logger.error(log_message)
917
end
1018

1119
private

app/models/dataverse/connector_download_processor.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/models/dataverse/connector_file_metadata.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module Dataverse
4+
class ConnectorMetadata
5+
def initialize(download_file)
6+
@metadata = download_file.metadata.to_h.deep_symbolize_keys
7+
@metadata.each_key do |key|
8+
define_singleton_method("#{key.to_s}="){ |value| @metadata[key] = value }
9+
define_singleton_method(key){ @metadata[key] }
10+
end
11+
end
12+
13+
# To avoid errors when expected fields are removed from the list of configured attributes
14+
def method_missing(method_name, *arguments, &block)
15+
nil
16+
end
17+
18+
def to_h
19+
@metadata.deep_stringify_keys
20+
end
21+
end
22+
end

app/models/dataverse/connector_status.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class ConnectorStatus
77

88
def initialize(file)
99
@file = file
10-
@connector_metadata = Dataverse::FileMetadata.new(file.connector_metadata)
10+
@connector_metadata = file.connector_metadata
1111
end
1212

1313
def download_progress
14-
download_location = connector_metadata[:temp_location]
15-
file_size = connector_metadata[:size]
14+
download_location = connector_metadata.temp_location
15+
file_size = file.size
1616
return 0 unless File.exist?(download_location) && file_size.to_i.positive?
1717

1818
downloaded_size = File.size(download_location)

app/models/download_collection.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
class DownloadCollection < ApplicationDiskRecord
44
include ActiveModel::Model
55

6-
ATTRIBUTES = %w[id type metadata_id name download_dir].freeze
7-
TYPES = %w[dataverse]
6+
ATTRIBUTES = %w[id name download_dir].freeze
87

98
attr_accessor *ATTRIBUTES
109

1110
validates_presence_of *ATTRIBUTES
12-
validates :type, inclusion: { in: TYPES }
1311

1412
def self.all
1513
Dir.glob(File.join(metadata_directory, '*'))
@@ -27,8 +25,10 @@ def self.find(collection_id)
2725
load_from_file(filename)
2826
end
2927

30-
def initialize(id: nil, type: nil, name: nil, download_dir: nil, connector_metadata: {})
31-
self.download_dir = download_dir || File.join(Configuration.download_root, id.to_s)
28+
def initialize(id: nil, name: nil, download_dir: nil)
29+
self.id = id || DownloadCollection.generate_id
30+
self.name = name || self.id
31+
self.download_dir = download_dir || File.join(Configuration.download_root, self.id.to_s)
3232
end
3333

3434
def files

app/models/download_file.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class DownloadFile < ApplicationDiskRecord
44
include ActiveModel::Model
55
include LoggingCommon
66

7-
ATTRIBUTES = %w[id collection_id type metadata_id external_id filename status size checksum content_type connector_metadata].freeze
7+
ATTRIBUTES = %w[id collection_id type filename status size metadata].freeze
88
TYPES = %w[dataverse].freeze
99
STATUS = %w[ready downloading success error].freeze
1010

@@ -55,6 +55,10 @@ def connector_status
5555
ConnectorClassDispatcher.file_connector_status(self)
5656
end
5757

58+
def connector_metadata
59+
ConnectorClassDispatcher.connector_metadata(self)
60+
end
61+
5862
private
5963

6064
#TODO: This needs to be taken from the DownloadCollection object

app/services/connector_class_dispatcher.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ def self.file_connector_status(download_file)
88
self.load(download_file.type, 'ConnectorStatus', download_file)
99
end
1010

11+
def self.connector_metadata(download_file)
12+
self.load(download_file.type, 'ConnectorMetadata', download_file)
13+
end
14+
1115
def self.download_processor(download_file)
1216
self.load(download_file.type, 'ConnectorDownloadProcessor', download_file)
1317
end

0 commit comments

Comments
 (0)