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