Commit e6cee6a1 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix tracking of issue weight change events

We cannot use an after_save hook here because the records are inserted
using bulk_insert
parent 3fd38e1e
......@@ -4,12 +4,4 @@ class ResourceWeightEvent < ResourceEvent
include IssueResourceEvent
validates :issue, presence: true
after_save :usage_metrics
private
def usage_metrics
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_weight_changed_action(author: user)
end
end
......@@ -42,7 +42,7 @@ module EE
def handle_weight_change
return unless issuable.previous_changes.include?('weight')
::ResourceEvents::ChangeWeightService.new([issuable], current_user, Time.current).execute
::ResourceEvents::ChangeWeightService.new(issuable, current_user, Time.current).execute
end
def handle_health_status_change
......
# frozen_string_literal: true
module EE
module ResourceEvents
class ChangeWeightService
attr_reader :resources, :user, :event_created_at
def initialize(resources, user, created_at)
@resources = resources
@user = user
@event_created_at = created_at
end
def execute
unless resource_weight_changes.empty?
::Gitlab::Database.bulk_insert(ResourceWeightEvent.table_name, resource_weight_changes) # rubocop:disable Gitlab/BulkInsert
resources.each(&:expire_note_etag_cache)
end
end
private
def resource_weight_changes
@weight_changes ||= resources.map do |resource|
changes = []
base_data = { user_id: user.id, issue_id: resource.id }
changes << base_data.merge({ weight: resource.previous_weight, created_at: resource.previous_updated_at }) if resource.first_weight_event?
changes << base_data.merge({ weight: resource.weight, created_at: event_created_at })
end.flatten
end
end
end
end
......@@ -2,31 +2,31 @@
module ResourceEvents
class ChangeWeightService
attr_reader :resources, :user, :event_created_at
attr_reader :resource, :user, :event_created_at
def initialize(resources, user, created_at)
@resources = resources
def initialize(resource, user, created_at)
@resource = resource
@user = user
@event_created_at = created_at
end
def execute
unless resource_weight_changes.empty?
::Gitlab::Database.bulk_insert(ResourceWeightEvent.table_name, resource_weight_changes) # rubocop:disable Gitlab/BulkInsert
resources.each(&:expire_note_etag_cache)
end
::Gitlab::Database.bulk_insert(ResourceWeightEvent.table_name, resource_weight_changes) # rubocop:disable Gitlab/BulkInsert
resource.expire_note_etag_cache
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_weight_changed_action(author: user)
end
private
def resource_weight_changes
@weight_changes ||= resources.map do |resource|
changes = []
base_data = { user_id: user.id, issue_id: resource.id }
changes = []
base_data = { user_id: user.id, issue_id: resource.id }
changes << base_data.merge({ weight: resource.previous_weight, created_at: resource.previous_updated_at }) if resource.first_weight_event?
changes << base_data.merge({ weight: resource.weight, created_at: event_created_at })
changes << base_data.merge({ weight: resource.previous_weight, created_at: resource.previous_updated_at }) if resource.first_weight_event?
changes << base_data.merge({ weight: resource.weight, created_at: event_created_at })
end.flatten
changes
end
end
end
......@@ -73,14 +73,4 @@ RSpec.describe ResourceWeightEvent, type: :model do
expect(event.discussion_id).to eq('73d167c478')
end
end
context 'callbacks' do
describe '#usage_metrics' do
it 'tracks changed weights' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_weight_changed_action).with(author: user1)
create(:resource_weight_event, issue: issue1, user: user1)
end
end
end
end
......@@ -8,7 +8,7 @@ RSpec.describe ResourceEvents::ChangeWeightService do
let(:issue) { create(:issue, weight: 3) }
let(:created_at_time) { Time.utc(2019, 1, 1, 12, 30, 48, '123.123'.to_r) }
subject { described_class.new([issue], user, created_at_time).execute }
subject { described_class.new(issue, user, created_at_time).execute }
before do
ResourceWeightEvent.new(issue: issue, user: user).save!
......@@ -51,31 +51,16 @@ RSpec.describe ResourceEvents::ChangeWeightService do
end
end
it 'tracks issue usage data counters' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_weight_changed_action).with(author: user)
subject
end
def expect_event_record(record, weight:, created_at:)
expect(record.issue).to eq(issue)
expect(record.user).to eq(user)
expect(record.weight).to eq(weight)
expect(record.created_at).to be_like_time(created_at)
end
describe 'bulk issue weight updates' do
let(:issues) { create_list(:issue, 3, weight: 1) }
before do
issues.each { |issue| issue.update!(weight: 3) }
end
it 'bulk insert weight changes' do
expect do
described_class.new(issues, user, created_at_time).execute
end.to change { ResourceWeightEvent.count }.by(6)
end
it 'calls first_weight_event? once per resource' do
service = described_class.new(issues, user, created_at_time)
allow(service).to receive(:first_weight_event?).exactly(3).times
service.execute
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