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

Groups/samples_count #783

Merged
merged 41 commits into from
Oct 29, 2024
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
244af6e
Add Groups samples_count tests
malchua Sep 25, 2024
7d8b79c
Update group tests with proper assertions
malchua Sep 25, 2024
3c3e4cf
Add test for project deletions and transfers
malchua Sep 25, 2024
6b81aab
Add tests for sample deletions and transfers
malchua Sep 25, 2024
6d2f43a
Add samples_count column to Namespaces table
malchua Sep 25, 2024
da3cca1
Fix query in group_test.rb
malchua Sep 26, 2024
6f4ac83
Add samples_count column to namespaces table
malchua Sep 26, 2024
c002c61
Add samples_count to Groups row component
malchua Sep 26, 2024
9bee8d4
Create test for group samples_count after sample creation
malchua Sep 27, 2024
308993d
Update group samples_count after sample creation
malchua Sep 27, 2024
a048859
Fix samples destroy service test
malchua Sep 27, 2024
d39018b
Update groups' samples_count after sample deletions
malchua Sep 27, 2024
6d54723
Fix sample transfer service test
malchua Sep 27, 2024
b663280
Update samples_count for groups when samples are transferred
malchua Sep 27, 2024
d2799f6
Fix samples clone service test
malchua Sep 27, 2024
2b0926b
Update samples_count when samples are cloned
malchua Sep 27, 2024
e6e670c
Fix project transfer service tests
malchua Sep 30, 2024
ab84cfd
Update samples_count after a project transfer
malchua Sep 30, 2024
00c3303
Fix group tests
malchua Oct 1, 2024
87b833c
Update samples_count after project deletion
malchua Oct 1, 2024
f1b986d
Update samples_count after group transfer
malchua Oct 1, 2024
6929e41
Update samples_count after group deletion
malchua Oct 1, 2024
6945cdb
Fix errors in groups tests
malchua Oct 2, 2024
1b684aa
Address rubocop warnings
malchua Oct 3, 2024
980e774
Fix project destroy_service to update samples_count properly
malchua Oct 3, 2024
2e00f5d
Add UI testing for group deletions
malchua Oct 8, 2024
ab31762
Add more ui tests
malchua Oct 16, 2024
f5eb6fc
Fix bug with displaying project samples_count
malchua Oct 17, 2024
41696fa
Create migration to reset group samples counters
malchua Oct 17, 2024
90c23be
Update projects fixture to allow for samples counter reset migration
malchua Oct 17, 2024
f8f0ccd
Update schema by running reset samples counter migration file
malchua Oct 17, 2024
1ea3a65
Add UI tests for Groups: Subgroups and projects tab
malchua Oct 17, 2024
d66a76e
Clean up dashboard/groups_test.rb file
malchua Oct 17, 2024
458d56e
Add samples_counts to the groups fixture
malchua Oct 18, 2024
911ad80
Add test to ensure test projects' samples_counts are accurate
malchua Oct 18, 2024
bcb1723
Update test to ensure test groups' samples_counts are accurate
malchua Oct 18, 2024
6ef9571
Revert certain tests now that samples_count in the groups fixture has…
malchua Oct 18, 2024
5d09521
Simplify deleted_samples_count calculation
malchua Oct 18, 2024
c8421d2
Refactor service tests to reuse common variables
malchua Oct 18, 2024
59a7065
Add edge cases for updating samples_count after tranferring projects
malchua Oct 21, 2024
59587ac
Add edge cases for updating samples_count after tranferring samples
malchua Oct 21, 2024
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
Original file line number Diff line number Diff line change
@@ -58,6 +58,14 @@

<div class="flex flex-row-reverse gap-x-3 text-slate-500">
<% if @namespace.type == "Group" %>
<%= viral_tooltip(title: t(:'.stats.samples')) do %>
<span
id="<%= "#{dom_id(@namespace)}-samples-count" %>"
class="flex items-center text-sm samples-count"
>
<%= viral_icon(name: :beaker, color: :subdued, classes: "h-5 w-5") %><%= @namespace.samples_count %>
</span>
<% end %>
<%= viral_tooltip(title: t(:'.stats.projects')) do %>
<span class="flex items-center text-sm">
<%= viral_icon(name: :rectangle_stack, color: :subdued, classes: "h-5 w-5") %>
@@ -73,7 +81,7 @@
<% if @namespace.type == "Project" %>
<%= viral_tooltip(title: t(:'.stats.samples')) do %>
<span class="flex items-center text-sm">
<%= viral_icon(name: :beaker, color: :subdued, classes: "h-5 w-5") %><%= @sample_count %>
<%= viral_icon(name: :beaker, color: :subdued, classes: "h-5 w-5") %><%= @namespace.project.samples.size %>
</span>
<% end %>
<% end %>
5 changes: 1 addition & 4 deletions app/components/namespace_tree/row/row_contents_component.rb
Original file line number Diff line number Diff line change
@@ -4,16 +4,13 @@ module NamespaceTree
module Row
# Component for the contents of NamespaceTree row
class RowContentsComponent < Viral::Component
# rubocop: disable Metrics/ParameterLists
def initialize(namespace:, path: nil, path_args: {}, collapsed: false, sample_count: 0, icon_size: :small)
def initialize(namespace:, path: nil, path_args: {}, collapsed: false, icon_size: :small)
@namespace = namespace
@path = path
@path_args = path_args
@collapsed = collapsed
@sample_count = sample_count
@icon_size = icon_size
end
# rubocop: enable Metrics/ParameterLists
end
end
end
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
namespace: @namespace,
path: @path,
path_args: @path_args,
sample_count: @sample_count,
icon_size: @icon_size,
) %>
</div>
42 changes: 41 additions & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Namespace for Groups
class Group < Namespace
class Group < Namespace # rubocop:disable Metrics/ClassLength
include History

has_many :group_members, foreign_key: :namespace_id, inverse_of: :group,
@@ -107,4 +107,44 @@ def retrieve_group_activity
)
)
end

def add_to_samples_count(namespaces, addition_amount)
namespaces.each do |namespace|
namespace.samples_count += addition_amount
namespace.save
end
end

def subtract_from_samples_count(namespaces, subtraction_amount)
namespaces.each do |namespace|
namespace.samples_count -= subtraction_amount
namespace.save
end
end

def update_samples_count_by_addition_services(added_samples_count = 1)
namespaces_to_update = self_and_ancestors.where(type: Group.sti_name)
add_to_samples_count(namespaces_to_update, added_samples_count)
end

def update_samples_count_by_destroy_service(deleted_samples_count)
namespaces_to_update = self_and_ancestors.where(type: Group.sti_name)
subtract_from_samples_count(namespaces_to_update, deleted_samples_count)
end

def update_samples_count_by_transfer_service(destination, transferred_samples_count, destination_type = 'Project')
namespaces_to_update = self_and_ancestors.where(type: Group.sti_name)
subtract_from_samples_count(namespaces_to_update, transferred_samples_count)

case destination_type
when 'Project'
namespaces_to_update = destination.parent.self_and_ancestors.where(type: Group.sti_name)
when 'Group'
namespaces_to_update = destination.self_and_ancestors.where(type: Group.sti_name)
else
return
end

add_to_samples_count(namespaces_to_update, transferred_samples_count)
end
end
7 changes: 7 additions & 0 deletions app/services/groups/destroy_service.rb
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ class DestroyService < BaseService
def initialize(group, user = nil, params = {})
super(user, params.except(:group, :group_id))
@group = group
@deleted_samples_count = @group.samples_count
end

def execute
@@ -26,9 +27,15 @@ def execute
removed_group_puid: @group.puid,
action: 'group_subgroup_destroy'
}

update_samples_count
end

group.update_metadata_summary_by_namespace_deletion
end

def update_samples_count
@group.update_samples_count_by_destroy_service(@deleted_samples_count)
end
end
end
13 changes: 13 additions & 0 deletions app/services/groups/transfer_service.rb
Original file line number Diff line number Diff line change
@@ -31,6 +31,8 @@ def execute(new_namespace) # rubocop:disable Metrics/AbcSize, Metrics/MethodLeng

UpdateMembershipsJob.perform_later(new_namespace_member_ids)

update_samples_count(old_namespace, new_namespace)

new_namespace.update_metadata_summary_by_namespace_transfer(@group, old_namespace)

true
@@ -103,5 +105,16 @@ def create_activities(old_namespace, new_namespace) # rubocop:disable Metrics/Me
}
end
end

def update_samples_count(old_namespace, new_namespace)
transferred_samples_count = Project.joins(:namespace).where(namespace: { parent_id: @group.self_and_descendants })
.select(:samples_count).pluck(:samples_count).sum
if old_namespace
old_namespace.update_samples_count_by_transfer_service(new_namespace, transferred_samples_count,
new_namespace.type)
elsif new_namespace.type == 'Group'
new_namespace.update_samples_count_by_addition_services(transferred_samples_count)
end
end
end
end
10 changes: 9 additions & 1 deletion app/services/projects/destroy_service.rb
Original file line number Diff line number Diff line change
@@ -3,15 +3,19 @@
module Projects
# Service used to Delete Projects
class DestroyService < BaseProjectService
def execute
def execute # rubocop:disable Metrics/AbcSize
authorize! project, to: :destroy?

deleted_samples_count = @project.samples.size

project.namespace.destroy!

create_activities if project.namespace.deleted?

return unless project.namespace.deleted? && project.namespace.type != Namespaces::UserNamespace.sti_name

update_samples_count(deleted_samples_count) if @project.parent.type == 'Group'

project.namespace.update_metadata_summary_by_namespace_deletion
end

@@ -28,5 +32,9 @@ def create_activities
action: 'group_project_destroy'
}
end

def update_samples_count(deleted_samples_count)
@project.parent.update_samples_count_by_destroy_service(deleted_samples_count)
end
end
end
14 changes: 13 additions & 1 deletion app/services/projects/transfer_service.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ class TransferService < BaseProjectService
TransferError = Class.new(StandardError)
attr_reader :new_namespace, :old_namespace

def execute(new_namespace) # rubocop:disable Metrics/AbcSize
def execute(new_namespace) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@new_namespace = new_namespace
@old_namespace = @project.parent

@@ -27,6 +27,8 @@ def execute(new_namespace) # rubocop:disable Metrics/AbcSize

@new_namespace.update_metadata_summary_by_namespace_transfer(@project.namespace, @old_namespace)

update_samples_count

true
rescue Projects::TransferService::TransferError => e
project.errors.add(:new_namespace, e.message)
@@ -84,5 +86,15 @@ def create_activities(project) # rubocop:disable Metrics/MethodLength
action: 'project_namespace_transfer'
}
end

def update_samples_count
transferred_samples_count = @project.samples.size
if @old_namespace.type == 'Group'
@old_namespace.update_samples_count_by_transfer_service(@new_namespace, transferred_samples_count,
@new_namespace.type)
elsif @new_namespace.type == 'Group'
@new_namespace.update_samples_count_by_addition_services(transferred_samples_count)
end
end
end
end
12 changes: 10 additions & 2 deletions app/services/samples/clone_service.rb
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ def validate(new_project_id, sample_ids)
raise CloneError, I18n.t('services.samples.clone.same_project')
end

def clone_samples(sample_ids)
def clone_samples(sample_ids) # rubocop:disable Metrics/AbcSize
cloned_sample_ids = {}
cloned_sample_puids = {}

@@ -42,7 +42,11 @@ def clone_samples(sample_ids)
cloned_sample_puids[sample.puid] = cloned_sample.puid unless cloned_sample.nil?
end

create_activities(cloned_sample_ids, cloned_sample_puids) if cloned_sample_ids.count.positive?
if cloned_sample_ids.count.positive?
update_samples_count(cloned_sample_ids.count) if @new_project.parent.type == 'Group'

create_activities(cloned_sample_ids, cloned_sample_puids)
end

cloned_sample_ids
end
@@ -69,6 +73,10 @@ def clone_attachments(sample, clone)
Attachments::CreateService.new(current_user, clone, { files:, include_activity: false }).execute
end

def update_samples_count(cloned_samples_count)
@new_project.parent.update_samples_count_by_addition_services(cloned_samples_count)
end

def create_activities(cloned_sample_ids, cloned_sample_puids) # rubocop:disable Metrics/MethodLength
@project.namespace.create_activity key: 'namespaces_project_namespace.samples.clone',
owner: current_user,
6 changes: 6 additions & 0 deletions app/services/samples/create_service.rb
Original file line number Diff line number Diff line change
@@ -25,7 +25,13 @@ def execute
}
end

update_samples_count if @project.parent.type == 'Group'

sample
end

def update_samples_count
@project.parent.update_samples_count_by_addition_services
end
end
end
8 changes: 8 additions & 0 deletions app/services/samples/destroy_service.rb
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ def destroy_single
}
end

update_samples_count if @project.parent.type == 'Group'

update_metadata_summary(sample)
end

@@ -48,6 +50,8 @@ def destroy_multiple # rubocop:disable Metrics/MethodLength
samples_deleted_puids << sample.puid
end

update_samples_count(samples_to_delete_count) if @project.parent.type == 'Group'

@project.namespace.create_activity key: 'namespaces_project_namespace.samples.destroy_multiple',
owner: current_user,
parameters:
@@ -63,5 +67,9 @@ def destroy_multiple # rubocop:disable Metrics/MethodLength
def update_metadata_summary(sample)
sample.project.namespace.update_metadata_summary_by_sample_deletion(sample) if sample.deleted?
end

def update_samples_count(deleted_samples_count = 1)
@project.parent.update_samples_count_by_destroy_service(deleted_samples_count)
end
end
end
9 changes: 9 additions & 0 deletions app/services/samples/transfer_service.rb
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ def transfer(new_project_id, sample_ids) # rubocop:disable Metrics/MethodLength,

@project.namespace.update_metadata_summary_by_sample_transfer(transferred_samples_ids,
new_project_id)
update_samples_count(transferred_samples_ids.count)
end

transferred_samples_ids
@@ -103,5 +104,13 @@ def create_activities(transferred_samples_ids, transferred_samples_puids) # rubo
action: 'sample_transfer'
}
end

def update_samples_count(transferred_samples_count)
if @project.parent.type == 'Group'
@project.parent.update_samples_count_by_transfer_service(@new_project, transferred_samples_count)
elsif @new_project.parent.type == 'Group'
@new_project.parent.update_samples_count_by_addition_services(transferred_samples_count)
end
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20240925230422_add_samples_count_to_namespaces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# migration to add samples count to namespaces table
class AddSamplesCountToNamespaces < ActiveRecord::Migration[7.2]
def change
add_column :namespaces, :samples_count, :integer, default: 0
end
end
ChrisHuynh333 marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions db/migrate/20241004162923_reset_all_group_samples_counters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# migration to reset the samples_count to the total number of samples found in all
# subgroups and projects, for each group
class ResetAllGroupSamplesCounters < ActiveRecord::Migration[7.2]
def up
Group.all.each do |group|
group.samples_count = Project.joins(:namespace).where(namespace: { parent_id: group.self_and_descendants })
.select(:samples_count).pluck(:samples_count).sum
group.save!
end
end
end
Loading