Commit e5f50b70 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '230465-fj-add-service-and-worker-to-update-namespace-statistics' into 'master'

Update NamespaceStatistics after wiki update

See merge request gitlab-org/gitlab!54969
parents b6e25d27 b886191b
......@@ -166,6 +166,8 @@
- 1
- - groups_update_repository_storage
- 1
- - groups_update_statistics
- 1
- - hashed_storage
- 1
- - import_issues_csv
......
......@@ -45,8 +45,8 @@ class GroupWiki < Wiki
override :after_post_receive
def after_post_receive
# TODO: Update group wiki storage
# https://gitlab.com/gitlab-org/gitlab/-/issues/230465
# Update group wiki storage statistics
Groups::UpdateStatisticsWorker.perform_async(group.id, [:wiki_size])
end
override :git_garbage_collect_worker_klass
......
# frozen_string_literal: true
module Groups
class UpdateStatisticsService
attr_reader :group, :statistics
def initialize(group, statistics: [])
@group = group
@statistics = statistics
end
def execute
unless group
return ServiceResponse.error(message: 'Invalid group', http_status: 400)
end
namespace_statistics.refresh!(only: statistics.map(&:to_sym))
ServiceResponse.success(message: 'Group statistics successfully updated.')
end
private
def namespace_statistics
@namespace_statistics ||= group.namespace_statistics || group.build_namespace_statistics
end
end
end
......@@ -797,6 +797,14 @@
:weight: 1
:idempotent: true
:tags: []
- :name: groups_update_statistics
:feature_category: :source_code_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: incident_management_apply_incident_sla_exceeded_label
:feature_category: :incident_management
:has_external_dependencies:
......
# frozen_string_literal: true
# Worker for updating group statistics.
module Groups
class UpdateStatisticsWorker
include ApplicationWorker
feature_category :source_code_management
idempotent!
loggable_arguments 0, 1
# group_id - The ID of the group for which to flush the cache.
# statistics - An Array containing columns from NamespaceStatistics to
# refresh, if empty all columns will be refreshed
def perform(group_id, statistics = [])
group = Group.find_by_id(group_id)
return unless group
Groups::UpdateStatisticsService.new(group, statistics: statistics).execute
end
end
end
---
title: Update NamespaceStatistics after wiki update
merge_request: 54969
author:
type: added
......@@ -91,6 +91,14 @@ RSpec.describe GroupWiki do
expect(subject.disk_path).to eq("#{subject.storage.disk_path}.wiki")
end
end
describe '#after_post_receive' do
it 'updates group statistics' do
expect(Groups::UpdateStatisticsWorker).to receive(:perform_async).with(wiki.container.id, [:wiki_size])
subject.send(:after_post_receive)
end
end
end
it_behaves_like 'EE wiki model' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Groups::UpdateStatisticsService do
let_it_be(:group, reload: true) { create(:group) }
let(:statistics) { %w(wiki_size) }
subject(:service) { described_class.new(group, statistics: statistics)}
describe '#execute', :aggregate_failures do
context 'when group is nil' do
let(:group) { nil }
it 'does nothing' do
expect(NamespaceStatistics).not_to receive(:new)
result = service.execute
expect(result).to be_error
end
end
context 'with an existing group' do
context 'when namespace statistics exists for the group' do
it 'uses the existing statistics and refreshes them' do
namespace_statistics = create(:namespace_statistics, namespace: group)
expect(namespace_statistics).to receive(:refresh!).with(only: statistics.map(&:to_sym)).and_call_original
result = service.execute
expect(result).to be_success
end
end
context 'when namespace statistics does not exist for the group' do
it 'creates the statistics and refreshes them' do
expect_next_instance_of(NamespaceStatistics) do |instance|
expect(instance).to receive(:refresh!).with(only: statistics.map(&:to_sym)).and_call_original
end
result = nil
expect do
result = service.execute
end.to change { NamespaceStatistics.count }.by(1)
expect(result).to be_success
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Groups::UpdateStatisticsWorker do
let_it_be(:group) { create(:group) }
let(:statistics) { %w(wiki_size) }
subject(:worker) { described_class.new }
describe '#perform' do
it 'updates the group statistics' do
expect(Groups::UpdateStatisticsService).to receive(:new)
.with(group, statistics: statistics)
.and_call_original
worker.perform(group.id, statistics)
end
context 'when group id does not exist' do
it 'ends gracefully' do
expect(Groups::UpdateStatisticsService).not_to receive(:new)
expect { worker.perform(non_existing_record_id, statistics) }.not_to raise_error
end
end
end
end
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