Skip to content

Commit

Permalink
Merge pull request #1630 from seek4science/include-git-repo-size
Browse files Browse the repository at this point in the history
Project's total_asset_size includes git_repository size.
  • Loading branch information
fherreazcue authored Oct 26, 2023
2 parents b478a8b + 1c87590 commit 5b349f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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

0 comments on commit 5b349f2

Please sign in to comment.