From 86b2eea383d90c48562a26873704e8323861ac90 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 17 Jun 2020 23:21:56 +0200 Subject: [PATCH 1/2] Add repository snapshots to export all [SCI-4738] --- app/helpers/reports_helper.rb | 15 ++------------- app/models/project.rb | 6 ++++++ app/models/report.rb | 16 ++++++---------- app/models/repository.rb | 6 ++++++ app/models/repository_snapshot.rb | 9 +++++++++ app/models/team_zip_export.rb | 19 ++++++------------- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 9d0ae9387..38424c98b 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -138,19 +138,8 @@ module ReportsHelper end def assigned_repositories_in_project_list(project) - live_repositories = Repository.accessible_by_teams(project.team) - .joins(repository_rows: - { my_module_repository_rows: { my_module: { experiment: :project } } }) - .where(repository_rows: - { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) - .select(:id, :name) - - snapshots = RepositorySnapshot.joins(my_module: { experiment: :project }) - .where('experiments.project_id = ?', project.id) - .left_outer_joins(:original_repository) - .where(original_repositories_repositories: { id: nil }) - .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') - .order(:parent_id, updated_at: :desc) + live_repositories = Repository.assigned_in_project(project) + snapshots = RepositorySnapshot.assigned_in_project(project) snapshots.each { |snapshot| snapshot.name = "#{snapshot.name} #{t('projects.reports.index.deleted')}" } (live_repositories + snapshots).sort_by { |r| r.name.downcase } diff --git a/app/models/project.rb b/app/models/project.rb index 7b42f9d71..d88817b62 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -210,6 +210,12 @@ class Project < ApplicationRecord }) end + def assigned_repositories_and_snapshots + live_repositories = Repository.assigned_in_project(self) + snapshots = RepositorySnapshot.assigned_in_project(self) + (live_repositories + snapshots).sort_by { |r| r.name.downcase } + end + def my_modules_ids ids = active_experiments.map do |exp| exp.my_modules.pluck(:id) if exp.my_modules diff --git a/app/models/report.rb b/app/models/report.rb index f9f63db38..507914e01 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -131,17 +131,13 @@ class Report < ApplicationRecord result_children) end - module_children += - gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - module_children += - gen_element_content(my_module, - my_module.repository_rows.select(:repository_id) - .distinct.map(&:repository), - 'my_module_repository', true, 'asc') + module_children += gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - modules += - gen_element_content(my_module, nil, 'my_module', true, nil, - module_children) + repositories = project.assigned_repositories_and_snapshots + + module_children += gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') + + modules += gen_element_content(my_module, nil, 'my_module', true, nil, module_children) end report_contents += diff --git a/app/models/repository.rb b/app/models/repository.rb index e5e9bb33e..b27be9fa4 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -83,6 +83,12 @@ class Repository < RepositoryBase end end + def self.assigned_in_project(project) + accessible_by_teams(project.team) + .joins(repository_rows: { my_module_repository_rows: { my_module: { experiment: :project } } }) + .where(repository_rows: { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) + end + def default_columns_count Constants::REPOSITORY_TABLE_DEFAULT_STATE['length'] end diff --git a/app/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index bd5020feb..297584860 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -15,6 +15,15 @@ class RepositorySnapshot < RepositoryBase validates :status, presence: true validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? :selected } + def self.assigned_in_project(project) + joins(my_module: { experiment: :project }) + .where('experiments.project_id = ?', project.id) + .left_outer_joins(:original_repository) + .where(original_repositories_repositories: { id: nil }) + .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') + .order(:parent_id, updated_at: :desc) + end + def default_columns_count Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length'] end diff --git a/app/models/team_zip_export.rb b/app/models/team_zip_export.rb index c31f59b8f..e3e07c169 100644 --- a/app/models/team_zip_export.rb +++ b/app/models/team_zip_export.rb @@ -59,17 +59,11 @@ class TeamZipExport < ZipExport inventories = "#{project_path}/Inventories" FileUtils.mkdir_p(inventories) - # Find all assigned inventories through all tasks in the project - task_ids = p.project_my_modules - repo_rows = RepositoryRow.joins(:my_modules) - .where(my_modules: { id: task_ids }) - .distinct + repositories = p.assigned_repositories_and_snapshots # Iterate through every inventory repo and save it to CSV - repo_rows.map(&:repository).uniq.each_with_index do |repo, repo_idx| - curr_repo_rows = repo_rows.select { |x| x.repository_id == repo.id } - obj_filenames[:my_module_repository][repo.id] = - save_inventories_to_csv(inventories, repo, curr_repo_rows, repo_idx) + repositories.each_with_index do |repo, repo_idx| + obj_filenames[:my_module_repository][repo.id] = save_inventories_to_csv(inventories, repo, repo_idx) end # Include all experiments @@ -233,7 +227,7 @@ class TeamZipExport < ZipExport end # Helper method for saving inventories to CSV - def save_inventories_to_csv(path, repo, repo_rows, idx) + def save_inventories_to_csv(path, repo, idx) repo_name = "#{to_filesystem_name(repo.name)} (#{idx})" # Attachment folder @@ -263,14 +257,13 @@ class TeamZipExport < ZipExport end # Generate CSV - csv_data = RepositoryZipExport.to_csv(repo_rows, col_ids, @user, @team, - handle_name_func) + csv_data = RepositoryZipExport.to_csv(repo.repository_rows, col_ids, @user, @team, handle_name_func) File.open(csv_file, 'wb') { |f| f.write(csv_data) } # Save all attachments (it doesn't work directly in callback function assets.each do |asset, asset_path| asset.file.open do |file| - FileUtils.cp(file.path, asset_path) + FileUtils.cp(file.path, asset_path) end end From 166a0ddd3464c9e2f1936eaef599658e7b2d1260 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 29 Jun 2020 11:43:23 +0200 Subject: [PATCH 2/2] Improve code styling [SCI-4738] --- app/helpers/reports_helper.rb | 4 ++-- app/models/project.rb | 4 ++-- app/models/report.rb | 4 +--- app/models/repository.rb | 12 ++++++------ app/models/repository_snapshot.rb | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 38424c98b..5f9106c99 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -138,8 +138,8 @@ module ReportsHelper end def assigned_repositories_in_project_list(project) - live_repositories = Repository.assigned_in_project(project) - snapshots = RepositorySnapshot.assigned_in_project(project) + live_repositories = Repository.assigned_to_project(project) + snapshots = RepositorySnapshot.assigned_to_project(project) snapshots.each { |snapshot| snapshot.name = "#{snapshot.name} #{t('projects.reports.index.deleted')}" } (live_repositories + snapshots).sort_by { |r| r.name.downcase } diff --git a/app/models/project.rb b/app/models/project.rb index d88817b62..501927bd3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -211,8 +211,8 @@ class Project < ApplicationRecord end def assigned_repositories_and_snapshots - live_repositories = Repository.assigned_in_project(self) - snapshots = RepositorySnapshot.assigned_in_project(self) + live_repositories = Repository.assigned_to_project(self) + snapshots = RepositorySnapshot.assigned_to_project(self) (live_repositories + snapshots).sort_by { |r| r.name.downcase } end diff --git a/app/models/report.rb b/app/models/report.rb index 507914e01..87d49b5e4 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -131,12 +131,10 @@ class Report < ApplicationRecord result_children) end - module_children += gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - repositories = project.assigned_repositories_and_snapshots + module_children += gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') module_children += gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') - modules += gen_element_content(my_module, nil, 'my_module', true, nil, module_children) end diff --git a/app/models/repository.rb b/app/models/repository.rb index b27be9fa4..6dec83b53 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -36,6 +36,12 @@ class Repository < RepositoryBase .distinct } + scope :assigned_to_project, lambda { |project| + accessible_by_teams(project.team) + .joins(repository_rows: { my_module_repository_rows: { my_module: { experiment: :project } } }) + .where(repository_rows: { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) + } + def self.within_global_limits? return true unless Rails.configuration.x.global_repositories_limit.positive? @@ -83,12 +89,6 @@ class Repository < RepositoryBase end end - def self.assigned_in_project(project) - accessible_by_teams(project.team) - .joins(repository_rows: { my_module_repository_rows: { my_module: { experiment: :project } } }) - .where(repository_rows: { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) - end - def default_columns_count Constants::REPOSITORY_TABLE_DEFAULT_STATE['length'] end diff --git a/app/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index 297584860..57cc77d55 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -15,14 +15,14 @@ class RepositorySnapshot < RepositoryBase validates :status, presence: true validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? :selected } - def self.assigned_in_project(project) + scope :assigned_to_project, lambda { |project| joins(my_module: { experiment: :project }) .where('experiments.project_id = ?', project.id) .left_outer_joins(:original_repository) .where(original_repositories_repositories: { id: nil }) .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') .order(:parent_id, updated_at: :desc) - end + } def default_columns_count Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length']