Commit 0ac6daed authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Track issue time tracking events in usage ping

This tracks changes to due date, time estimate, and time spent
parent 7d523a37
......@@ -16,6 +16,8 @@ module SystemNotes
def change_due_date(due_date)
body = due_date ? "changed due date to #{due_date.to_s(:long)}" : 'removed due date'
issue_activity_counter.track_issue_due_date_changed_action(author: author) if noteable.is_a?(Issue)
create_note(NoteSummary.new(noteable, project, author, body, action: 'due_date'))
end
......@@ -38,6 +40,8 @@ module SystemNotes
"changed time estimate to #{parsed_time}"
end
issue_activity_counter.track_issue_time_estimate_changed_action(author: author) if noteable.is_a?(Issue)
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
end
......@@ -67,7 +71,15 @@ module SystemNotes
body = text_parts.join(' ')
end
issue_activity_counter.track_issue_time_spent_changed_action(author: author) if noteable.is_a?(Issue)
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
end
private
def issue_activity_counter
Gitlab::UsageDataCounters::IssueActivityUniqueCounter
end
end
end
---
title: Track issue time tracking events in usage ping
merge_request: 44404
author:
type: other
......@@ -30,6 +30,9 @@ module Gitlab
ISSUE_DESIGNS_ADDED = 'g_project_management_issue_designs_added'
ISSUE_DESIGNS_MODIFIED = 'g_project_management_issue_designs_modified'
ISSUE_DESIGNS_REMOVED = 'g_project_management_issue_designs_removed'
ISSUE_DUE_DATE_CHANGED = 'g_project_management_issue_due_date_changed'
ISSUE_TIME_ESTIMATE_CHANGED = 'g_project_management_issue_time_estimate_changed'
ISSUE_TIME_SPENT_CHANGED = 'g_project_management_issue_time_spent_changed'
class << self
def track_issue_created_action(author:, time: Time.zone.now)
......@@ -132,6 +135,18 @@ module Gitlab
track_unique_action(ISSUE_DESIGNS_REMOVED, author, time)
end
def track_issue_due_date_changed_action(author:, time: Time.zone.now)
track_unique_action(ISSUE_DUE_DATE_CHANGED, author, time)
end
def track_issue_time_estimate_changed_action(author:, time: Time.zone.now)
track_unique_action(ISSUE_TIME_ESTIMATE_CHANGED, author, time)
end
def track_issue_time_spent_changed_action(author:, time: Time.zone.now)
track_unique_action(ISSUE_TIME_SPENT_CHANGED, author, time)
end
private
def track_unique_action(action, author, time)
......
......@@ -291,3 +291,15 @@
category: issues_edit
redis_slot: project_management
aggregation: daily
- name: g_project_management_issue_due_date_changed
category: issues_edit
redis_slot: project_management
aggregation: daily
- name: g_project_management_issue_time_estimate_changed
category: issues_edit
redis_slot: project_management
aggregation: daily
- name: g_project_management_issue_time_spent_changed
category: issues_edit
redis_slot: project_management
aggregation: daily
......@@ -262,6 +262,36 @@ RSpec.describe Gitlab::UsageDataCounters::IssueActivityUniqueCounter, :clean_git
end
end
context 'for Issue due date changed actions' do
it_behaves_like 'tracks and counts action' do
let(:action) { described_class::ISSUE_DUE_DATE_CHANGED }
def track_action(params)
described_class.track_issue_due_date_changed_action(**params)
end
end
end
context 'for Issue time estimate changed actions' do
it_behaves_like 'tracks and counts action' do
let(:action) { described_class::ISSUE_TIME_ESTIMATE_CHANGED }
def track_action(params)
described_class.track_issue_time_estimate_changed_action(**params)
end
end
end
context 'for Issue time spent changed actions' do
it_behaves_like 'tracks and counts action' do
let(:action) { described_class::ISSUE_TIME_SPENT_CHANGED }
def track_action(params)
described_class.track_issue_time_spent_changed_action(**params)
end
end
end
it 'can return the count of actions per user deduplicated', :aggregate_failures do
described_class.track_issue_title_changed_action(author: user1)
described_class.track_issue_description_changed_action(author: user1)
......
......@@ -6,124 +6,181 @@ RSpec.describe ::SystemNotes::TimeTrackingService do
let_it_be(:author) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:noteable) { create(:issue, project: project) }
describe '#change_due_date' do
subject { described_class.new(noteable: noteable, project: project, author: author).change_due_date(due_date) }
let(:due_date) { Date.today }
it_behaves_like 'a note with overridable created_at'
context 'when noteable is an issue' do
let_it_be(:noteable) { create(:issue, project: project) }
it_behaves_like 'a system note' do
let(:action) { 'due_date' }
end
it_behaves_like 'a note with overridable created_at'
it_behaves_like 'a system note' do
let(:action) { 'due_date' }
end
context 'when due date added' do
it 'sets the note text' do
expect(subject.note).to eq "changed due date to #{due_date.to_s(:long)}"
context 'when due date added' do
it 'sets the note text' do
expect(subject.note).to eq "changed due date to #{due_date.to_s(:long)}"
end
end
context 'when due date removed' do
let(:due_date) { nil }
it 'sets the note text' do
expect(subject.note).to eq 'removed due date'
end
end
it 'tracks the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_due_date_changed_action).with(author: author)
subject
end
end
context 'when due date removed' do
let(:due_date) { nil }
context 'when noteable is a merge request' do
let_it_be(:noteable) { create(:merge_request, source_project: project) }
it 'does not track the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_due_date_changed_action).with(author: author)
it 'sets the note text' do
expect(subject.note).to eq 'removed due date'
subject
end
end
end
describe '.change_time_estimate' do
describe '#change_time_estimate' do
subject { described_class.new(noteable: noteable, project: project, author: author).change_time_estimate }
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
end
context 'with a time estimate' do
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
context 'when noteable is an issue' do
let_it_be(:noteable, reload: true) { create(:issue, project: project) }
expect(subject.note).to eq "changed time estimate to 1w 4d 5h"
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
end
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
end
context 'with a time estimate' do
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
expect(subject.note).to eq "changed time estimate to 77h"
expect(subject.note).to eq "changed time estimate to 1w 4d 5h"
end
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
end
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
expect(subject.note).to eq "changed time estimate to 77h"
end
end
end
end
context 'without a time estimate' do
it 'sets the note text' do
expect(subject.note).to eq "removed time estimate"
context 'without a time estimate' do
it 'sets the note text' do
expect(subject.note).to eq "removed time estimate"
end
end
end
end
describe '.change_time_spent' do
# We need a custom noteable in order to the shared examples to be green.
let(:noteable) do
mr = create(:merge_request, source_project: project)
mr.spend_time(duration: 360000, user_id: author.id)
mr.save!
mr
end
it 'tracks the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_time_estimate_changed_action).with(author: author)
subject do
described_class.new(noteable: noteable, project: project, author: author).change_time_spent
subject
end
end
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
end
context 'when noteable is a merge request' do
let_it_be(:noteable) { create(:merge_request, source_project: project) }
context 'when time was added' do
it 'sets the note text' do
spend_time!(277200)
it 'does not track the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_time_estimate_changed_action).with(author: author)
expect(subject.note).to eq "added 1w 4d 5h of time spent"
subject
end
end
end
context 'when time was subtracted' do
it 'sets the note text' do
spend_time!(-277200)
describe '#change_time_spent' do
subject { described_class.new(noteable: noteable, project: project, author: author).change_time_spent }
expect(subject.note).to eq "subtracted 1w 4d 5h of time spent"
end
end
context 'when noteable is an issue' do
let_it_be(:noteable, reload: true) { create(:issue, project: project) }
context 'when time was removed' do
it 'sets the note text' do
spend_time!(:reset)
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
expect(subject.note).to eq "removed time spent"
before do
spend_time!(277200)
end
end
end
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
context 'when time was added' do
it 'sets the note text' do
spend_time!(277200)
expect(subject.note).to eq "added 1w 4d 5h of time spent"
end
context 'when time was subtracted' do
it 'sets the note text' do
spend_time!(360000)
spend_time!(-277200)
expect(subject.note).to eq "subtracted 1w 4d 5h of time spent"
end
end
context 'when time was removed' do
it 'sets the note text' do
spend_time!(:reset)
expect(subject.note).to eq "removed time spent"
end
end
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
end
it 'sets the note text' do
spend_time!(277200)
expect(subject.note).to eq "added 77h of time spent"
end
end
it 'tracks the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_time_spent_changed_action).with(author: author)
spend_time!(277200)
subject
end
end
it 'sets the note text' do
spend_time!(277200)
context 'when noteable is a merge request' do
let_it_be(:noteable) { create(:merge_request, source_project: project) }
it 'does not track the issue event in usage ping' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_time_estimate_changed_action).with(author: author)
spend_time!(277200)
expect(subject.note).to eq "added 77h of time spent"
subject
end
end
end
def spend_time!(seconds)
noteable.spend_time(duration: seconds, user_id: author.id)
noteable.save!
def spend_time!(seconds)
noteable.spend_time(duration: seconds, user_id: author.id)
noteable.save!
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