diff --git a/app/models/project.rb b/app/models/project.rb index 6f04b3246b..006a0c9474 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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) @@ -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 diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index a64415828a..da3348a9d6 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -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