commit events and spec

This commit is contained in:
James Lopez 2016-10-19 12:47:09 +02:00
parent 1d6068a1c3
commit 3b5d947730
4 changed files with 53 additions and 10 deletions

View file

@ -9,15 +9,25 @@ module Gitlab
@fetcher = EventsFetcher.new(project: project, from: from)
end
#TODO: backend pagination - specially for commits, etc...
def issue_events
@fetcher.fetch_issues.each do |event|
event['issue_diff'] = interval_in_words(event['issue_diff'])
#TODO figure out what the frontend needs for displaying the avatar
@fetcher.fetch_issue_events.each do |event|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['created_at'] = interval_in_words(event['created_at'])
end
end
def plan_events
# TODO sort out 1st referenced commit and parse stuff
@fetcher.fetch_plan_events
end
private
def interval_in_words(diff)
"#{distance_of_time_in_words( diff.to_f)} ago"
"#{distance_of_time_in_words(diff.to_f)} ago"
end
end
end

View file

@ -8,22 +8,39 @@ module Gitlab
@from = from
end
def fetch_issues
def fetch_issue_events
base_query = base_query_for(:issue)
diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], metric_attributes)
diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], issue_attributes)
query = base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])).
project(extract_epoch(diff_fn).as('issue_diff'), *issue_projections).
project(extract_epoch(diff_fn).as('total_time'), *issue_projections).
order(issue_table[:created_at].desc)
ActiveRecord::Base.connection.execute(query.to_sql).to_a
end
def metric_attributes
def fetch_plan_events
base_query = base_query_for(:plan)
diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], plan_attributes)
query = base_query.join(merge_request_diff_table).on(merge_request_diff_table[:merge_request_id].eq(merge_request_table[:id])).
project(merge_request_diff_table[:st_commits].as(:commits), extract_epoch(diff_fn).as('total_time')).
order(issue_table[:created_at].desc)
ActiveRecord::Base.connection.execute(query.to_sql).to_a
end
private
def issue_attributes
[issue_metrics_table[:first_associated_with_milestone_at],
issue_metrics_table[:first_added_to_board_at]]
end
def plan_attributes
issue_attributes + [issue_metrics_table[:first_mentioned_in_commit_at]]
end
def issue_projections
[issue_table[:title], issue_table[:iid], issue_table[:created_at], User.arel_table[:name]]
end

View file

@ -56,6 +56,10 @@ module Gitlab
MergeRequest.arel_table
end
def merge_request_diff_table
MergeRequestDiff.arel_table
end
def mr_closing_issues_table
MergeRequestsClosingIssues.arel_table
end

View file

@ -11,11 +11,11 @@ describe Gitlab::CycleAnalytics::Events do
setup(context)
end
describe '#issue' do
describe '#issue_events' do
let!(:context) { create(:issue, project: project, created_at: 2.days.ago) }
it 'has an issue diff' do
expect(subject.issue_events.first['issue_diff']).to eq('2 days ago')
it 'has the total time' do
expect(subject.issue_events.first['total_time']).to eq('2 days')
end
it 'has a title' do
@ -35,6 +35,18 @@ describe Gitlab::CycleAnalytics::Events do
end
end
describe '#plan_events' do
let!(:context) { create(:issue, project: project, created_at: 2.days.ago) }
xit 'has the first referenced commit' do
expect(subject.plan_events.first['commit']).to eq(project.commit)
end
it 'has the total time' do
expect(subject.plan_events.first['total_time']).to eq('2 days')
end
end
def setup(context)
milestone = create(:milestone, project: project)
context.update(milestone: milestone)