Add group restrictions to Geo node status

parent d58b9b3b
...@@ -15,7 +15,12 @@ class GeoNodeStatus ...@@ -15,7 +15,12 @@ class GeoNodeStatus
end end
def repositories_count def repositories_count
@repositories_count ||= Project.count @repositories_count ||=
if restricted_project_ids
Project.where(id: restricted_project_ids).count
else
Project.count
end
end end
def repositories_count=(value) def repositories_count=(value)
...@@ -23,7 +28,12 @@ class GeoNodeStatus ...@@ -23,7 +28,12 @@ class GeoNodeStatus
end end
def repositories_synced_count def repositories_synced_count
@repositories_synced_count ||= Geo::ProjectRegistry.synced.count @repositories_synced_count ||=
if restricted_project_ids
Geo::ProjectRegistry.synced.where(project_id: restricted_project_ids).count
else
Geo::ProjectRegistry.synced.count
end
end end
def repositories_synced_count=(value) def repositories_synced_count=(value)
...@@ -35,7 +45,12 @@ class GeoNodeStatus ...@@ -35,7 +45,12 @@ class GeoNodeStatus
end end
def repositories_failed_count def repositories_failed_count
@repositories_failed_count ||= Geo::ProjectRegistry.failed.count @repositories_failed_count ||=
if restricted_project_ids
Geo::ProjectRegistry.failed.where(project_id: restricted_project_ids).count
else
Geo::ProjectRegistry.failed.count
end
end end
def repositories_failed_count=(value) def repositories_failed_count=(value)
...@@ -43,7 +58,7 @@ class GeoNodeStatus ...@@ -43,7 +58,7 @@ class GeoNodeStatus
end end
def lfs_objects_count def lfs_objects_count
@lfs_objects_count ||= LfsObject.count @lfs_objects_count ||= lfs_objects.count
end end
def lfs_objects_count=(value) def lfs_objects_count=(value)
...@@ -51,7 +66,15 @@ class GeoNodeStatus ...@@ -51,7 +66,15 @@ class GeoNodeStatus
end end
def lfs_objects_synced_count def lfs_objects_synced_count
@lfs_objects_synced_count ||= Geo::FileRegistry.where(file_type: :lfs).count @lfs_objects_synced_count ||= begin
relation = Geo::FileRegistry.where(file_type: :lfs)
if restricted_project_ids
relation = relation.where(file_id: lfs_objects.pluck(:id))
end
relation.count
end
end end
def lfs_objects_synced_count=(value) def lfs_objects_synced_count=(value)
...@@ -63,7 +86,7 @@ class GeoNodeStatus ...@@ -63,7 +86,7 @@ class GeoNodeStatus
end end
def attachments_count def attachments_count
@attachments_count ||= Upload.count @attachments_count ||= attachments.count
end end
def attachments_count=(value) def attachments_count=(value)
...@@ -72,7 +95,7 @@ class GeoNodeStatus ...@@ -72,7 +95,7 @@ class GeoNodeStatus
def attachments_synced_count def attachments_synced_count
@attachments_synced_count ||= begin @attachments_synced_count ||= begin
upload_ids = Upload.pluck(:id) upload_ids = attachments.pluck(:id)
synced_ids = Geo::FileRegistry.where(file_type: [:attachment, :avatar, :file]).pluck(:file_id) synced_ids = Geo::FileRegistry.where(file_type: [:attachment, :avatar, :file]).pluck(:file_id)
(synced_ids & upload_ids).length (synced_ids & upload_ids).length
...@@ -94,4 +117,33 @@ class GeoNodeStatus ...@@ -94,4 +117,33 @@ class GeoNodeStatus
(synced.to_f / total.to_f) * 100.0 (synced.to_f / total.to_f) * 100.0
end end
def attachments
@attachments ||=
if restricted_project_ids
uploads_table = Upload.arel_table
group_uploads = uploads_table[:model_type].eq('Namespace').and(uploads_table[:model_id].in(Gitlab::Geo.current_node.group_ids))
project_uploads = uploads_table[:model_type].eq('Project').and(uploads_table[:model_id].in(restricted_project_ids))
other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project])
Upload.where(group_uploads.or(project_uploads).or(other_uploads))
else
Upload.all
end
end
def lfs_objects
@lfs_objects ||=
if restricted_project_ids
LfsObject.joins(:projects).where(projects: { id: restricted_project_ids })
else
LfsObject.all
end
end
def restricted_project_ids
return @restricted_project_ids if defined?(@restricted_project_ids)
@restricted_project_ids = Gitlab::Geo.current_node.project_ids
end
end end
...@@ -27,7 +27,6 @@ module Geo ...@@ -27,7 +27,6 @@ module Geo
other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project]) other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project])
Upload.where(group_uploads.or(project_uploads).or(other_uploads)) Upload.where(group_uploads.or(project_uploads).or(other_uploads))
else else
Upload.all Upload.all
end end
......
...@@ -8,5 +8,9 @@ FactoryGirl.define do ...@@ -8,5 +8,9 @@ FactoryGirl.define do
trait :avatar do trait :avatar do
file_type :avatar file_type :avatar
end end
trait :lfs do
file_type :lfs
end
end end
end end
require 'spec_helper' require 'spec_helper'
describe GeoNodeStatus do describe GeoNodeStatus do
let!(:geo_node) { create(:geo_node, :current) }
let(:group) { create(:group) }
let!(:project_1) { create(:empty_project, group: group) }
let!(:project_2) { create(:empty_project, group: group) }
let!(:project_3) { create(:empty_project) }
let!(:project_4) { create(:empty_project) }
subject { described_class.new } subject { described_class.new }
describe '#healthy?' do describe '#healthy?' do
...@@ -40,7 +47,7 @@ describe GeoNodeStatus do ...@@ -40,7 +47,7 @@ describe GeoNodeStatus do
expect(subject.attachments_synced_count).to eq(0) expect(subject.attachments_synced_count).to eq(0)
upload = Upload.find_by(model: user, uploader: 'AvatarUploader') upload = Upload.find_by(model: user, uploader: 'AvatarUploader')
Geo::FileRegistry.create(file_type: :avatar, file_id: upload.id) create(:geo_file_registry, :avatar, file_id: upload.id)
subject = described_class.new subject = described_class.new
expect(subject.attachments_count).to eq(1) expect(subject.attachments_count).to eq(1)
...@@ -53,7 +60,7 @@ describe GeoNodeStatus do ...@@ -53,7 +60,7 @@ describe GeoNodeStatus do
expect(subject.attachments_synced_count).to eq(0) expect(subject.attachments_synced_count).to eq(0)
upload = Upload.find_by(model: user, uploader: 'AvatarUploader') upload = Upload.find_by(model: user, uploader: 'AvatarUploader')
Geo::FileRegistry.create(file_type: :avatar, file_id: upload.id) create(:geo_file_registry, :avatar, file_id: upload.id)
subject = described_class.new subject = described_class.new
expect(subject.attachments_count).to eq(1) expect(subject.attachments_count).to eq(1)
...@@ -62,51 +69,80 @@ describe GeoNodeStatus do ...@@ -62,51 +69,80 @@ describe GeoNodeStatus do
end end
describe '#attachments_synced_in_percentage' do describe '#attachments_synced_in_percentage' do
it 'returns 0 when no objects are available' do let(:avatar) { fixture_file_upload(Rails.root.join('spec/fixtures/dk.png')) }
subject.attachments_count = 0 let(:upload_1) { create(:upload, model: group, path: avatar) }
subject.attachments_synced_count = 0 let(:upload_2) { create(:upload, model: project_1, path: avatar) }
before do
create(:upload, model: create(:group), path: avatar)
create(:upload, model: project_3, path: avatar)
end
it 'returns 0 when no objects are available' do
expect(subject.attachments_synced_in_percentage).to eq(0) expect(subject.attachments_synced_in_percentage).to eq(0)
end end
it 'returns the right percentage' do it 'returns the right percentage with no group restrictions' do
subject.attachments_count = 4 create(:geo_file_registry, :avatar, file_id: upload_1.id)
subject.attachments_synced_count = 1 create(:geo_file_registry, :avatar, file_id: upload_2.id)
expect(subject.attachments_synced_in_percentage).to be_within(0.0001).of(25) expect(subject.attachments_synced_in_percentage).to be_within(0.0001).of(50)
end
it 'returns the right percentage with group restrictions' do
geo_node.update_attribute(:groups, [group])
create(:geo_file_registry, :avatar, file_id: upload_1.id)
create(:geo_file_registry, :avatar, file_id: upload_2.id)
expect(subject.attachments_synced_in_percentage).to be_within(0.0001).of(100)
end end
end end
describe '#lfs_objects_synced_in_percentage' do describe '#lfs_objects_synced_in_percentage' do
it 'returns 0 when no objects are available' do let(:lfs_object_project) { create(:lfs_objects_project, project: project_1) }
subject.lfs_objects_count = 0
subject.lfs_objects_synced_count = 0
before do
allow(ProjectCacheWorker).to receive(:perform_async).and_return(true)
create(:lfs_objects_project, project: project_1)
create_list(:lfs_objects_project, 2, project: project_3)
end
it 'returns 0 when no objects are available' do
expect(subject.lfs_objects_synced_in_percentage).to eq(0) expect(subject.lfs_objects_synced_in_percentage).to eq(0)
end end
it 'returns the right percentage' do it 'returns the right percentage with no group restrictions' do
subject.lfs_objects_count = 4 create(:geo_file_registry, :lfs, file_id: lfs_object_project.lfs_object_id)
subject.lfs_objects_synced_count = 1
expect(subject.lfs_objects_synced_in_percentage).to be_within(0.0001).of(25) expect(subject.lfs_objects_synced_in_percentage).to be_within(0.0001).of(25)
end end
it 'returns the right percentage with group restrictions' do
geo_node.update_attribute(:groups, [group])
create(:geo_file_registry, :lfs, file_id: lfs_object_project.lfs_object_id)
expect(subject.lfs_objects_synced_in_percentage).to be_within(0.0001).of(50)
end
end end
describe '#repositories_synced_in_percentage' do describe '#repositories_synced_in_percentage' do
it 'returns 0 when no objects are available' do it 'returns 0 when no projects are available' do
subject.repositories_count = 0
subject.repositories_synced_count = 0
expect(subject.repositories_synced_in_percentage).to eq(0) expect(subject.repositories_synced_in_percentage).to eq(0)
end end
it 'returns the right percentage' do it 'returns the right percentage with no group restrictions' do
subject.repositories_count = 4 create(:geo_project_registry, :synced, project: project_1)
subject.repositories_synced_count = 1
expect(subject.repositories_synced_in_percentage).to be_within(0.0001).of(25) expect(subject.repositories_synced_in_percentage).to be_within(0.0001).of(25)
end end
it 'returns the right percentage with group restrictions' do
geo_node.update_attribute(:groups, [group])
create(:geo_project_registry, :synced, project: project_1)
expect(subject.repositories_synced_in_percentage).to be_within(0.0001).of(50)
end
end end
context 'when no values are available' do context 'when no values are available' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment