Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project's total_asset_size includes git_repository size. #1630

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def self.can_create?(user = User.current_user)
end

def total_asset_size
assets.sum do |asset|
blobs = assets.sum do |asset|
if asset.respond_to?(:content_blob)
asset.content_blob.try(:file_size) || 0
elsif asset.respond_to?(:content_blobs)
Expand All @@ -233,6 +233,25 @@ def total_asset_size
0
end
end
local_paths = []
git_repos = assets.sum do |asset|
if asset.is_git_versioned?
asset.git_versions.sum do |ver|
lp = ver.git_repository.local_path
dir_size = 0
unless lp.in?(local_paths)
Dir.glob(File.join(lp, '**', '*'), File::FNM_DOTMATCH) do |file|
dir_size += File.size(file)
end
local_paths << lp
end
dir_size
end
else
0
end
end
blobs + git_repos
end

# whether the user is able to request membership of this project
Expand Down
21 changes: 20 additions & 1 deletion test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -946,5 +946,24 @@ def test_update_first_letter
project.topic_annotations = 'Chemistry'
assert project.controlled_vocab_annotations?
end


test 'total asset size includes blobs and git repos without duplication' do
project = FactoryBot.create(:project)
df = FactoryBot.create(:data_file, projects: [project])
workflow1 = FactoryBot.create(:local_git_workflow, projects: [project])
workflow2 = FactoryBot.create(:local_git_workflow, projects: [project])
repo_size = 116994 # repo_size = `du -bs #{workflow1.local_git_repository.local_path}`.split("\t").first.to_i
df_blob_size = 8827 # df_blob_size = df.content_blob.file_size

assert_equal 1, workflow2.git_versions.length
one_version_size = project.total_asset_size

disable_authorization_checks { workflow2.save_as_new_git_version }
assert_equal 2, workflow2.git_versions.length
assert_equal workflow2.git_versions.first.git_repository.local_path, workflow2.git_versions.last.git_repository.local_path

assert_equal one_version_size, project.total_asset_size, "total_asset_size is the same before and after adding a new version"
assert_equal df_blob_size + 2 * repo_size, project.total_asset_size, "total_asset_size includes each workflow and df"
end

end
Loading