Skip to content
Snippets Groups Projects
Commit 1a4ff5d7 authored by James Lopez's avatar James Lopez
Browse files

Added code events spec and logic. Also fixed SQL issues and refactored the code a bit.

parent 72660d58
No related branches found
No related tags found
No related merge requests found
Loading
@@ -9,10 +9,10 @@ module Gitlab
Loading
@@ -9,10 +9,10 @@ module Gitlab
@fetcher = EventsFetcher.new(project: project, from: from) @fetcher = EventsFetcher.new(project: project, from: from)
end end
   
#TODO: backend pagination - specially for commits, etc... # TODO: backend pagination - specially for commits, etc...
   
def issue_events def issue_events
#TODO figure out what the frontend needs for displaying the avatar # TODO figure out what the frontend needs for displaying the avatar
@fetcher.fetch_issue_events.each do |event| @fetcher.fetch_issue_events.each do |event|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f) event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['created_at'] = interval_in_words(event['created_at']) event['created_at'] = interval_in_words(event['created_at'])
Loading
@@ -27,6 +27,13 @@ module Gitlab
Loading
@@ -27,6 +27,13 @@ module Gitlab
end end
end end
   
def code_events
@fetcher.fetch_code_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
private private
   
def first_time_reference_commit(commits, event) def first_time_reference_commit(commits, event)
Loading
Loading
Loading
@@ -16,18 +16,33 @@ module Gitlab
Loading
@@ -16,18 +16,33 @@ module Gitlab
project(extract_epoch(diff_fn).as('total_time'), *issue_projections). project(extract_epoch(diff_fn).as('total_time'), *issue_projections).
order(issue_table[:created_at].desc) order(issue_table[:created_at].desc)
   
ActiveRecord::Base.connection.execute(query.to_sql).to_a execute(query)
end end
   
def fetch_plan_events def fetch_plan_events
base_query = base_query_for(:plan) base_query = base_query_for(:plan)
diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], plan_attributes) diff_fn = subtract_datetimes_diff(base_query,
issue_metrics_table[:first_associated_with_milestone_at],
plan_attributes)
   
query = base_query.join(mr_diff_table).on(mr_diff_table[:merge_request_id].eq(mr_table[:id])). query = base_query.join(mr_diff_table).on(mr_diff_table[:merge_request_id].eq(mr_table[:id])).
project(extract_epoch(diff_fn).as('total_time'), *plan_projections). project(extract_epoch(diff_fn).as('total_time'), *plan_projections).
order(issue_table[:created_at].desc) order(issue_metrics_table[:first_associated_with_milestone_at].desc)
   
ActiveRecord::Base.connection.execute(query.to_sql).to_a execute(query)
end
def fetch_code_events
base_query = base_query_for(:code)
diff_fn = subtract_datetimes_diff(base_query,
issue_metrics_table[:first_mentioned_in_commit_at],
issue_table[:created_at])
query = base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])).
project(extract_epoch(diff_fn).as('total_time'), *code_projections).
order(mr_table[:created_at].desc)
execute(query)
end end
   
private private
Loading
@@ -38,7 +53,8 @@ module Gitlab
Loading
@@ -38,7 +53,8 @@ module Gitlab
end end
   
def plan_attributes def plan_attributes
issue_attributes + [issue_metrics_table[:first_mentioned_in_commit_at]] [issue_metrics_table[:first_added_to_board_at],
issue_metrics_table[:first_mentioned_in_commit_at]]
end end
   
def issue_projections def issue_projections
Loading
@@ -49,6 +65,10 @@ module Gitlab
Loading
@@ -49,6 +65,10 @@ module Gitlab
[mr_diff_table[:st_commits].as('commits'), issue_metrics_table[:first_mentioned_in_commit_at]] [mr_diff_table[:st_commits].as('commits'), issue_metrics_table[:first_mentioned_in_commit_at]]
end end
   
def code_projections
[mr_table[:title], mr_table[:iid], mr_table[:created_at], User.arel_table[:name]]
end
def user_table def user_table
User.arel_table User.arel_table
end end
Loading
@@ -56,6 +76,10 @@ module Gitlab
Loading
@@ -56,6 +76,10 @@ module Gitlab
def extract_epoch(arel_attribute) def extract_epoch(arel_attribute)
Arel.sql(%Q{EXTRACT(EPOCH FROM (#{arel_attribute.to_sql}))}) Arel.sql(%Q{EXTRACT(EPOCH FROM (#{arel_attribute.to_sql}))})
end end
def execute(query)
ActiveRecord::Base.connection.execute(query.to_sql).to_a
end
end end
end end
end end
Loading
@@ -17,7 +17,7 @@ module Gitlab
Loading
@@ -17,7 +17,7 @@ module Gitlab
# cycle analytics stage. # cycle analytics stage.
interval_query = Arel::Nodes::As.new( interval_query = Arel::Nodes::As.new(
cte_table, cte_table,
subtract_datetimes(base_query_for(name), end_time_attrs, start_time_attrs, name.to_s)) subtract_datetimes(base_query_for(name), start_time_attrs, end_time_attrs, name.to_s))
   
median_datetime(cte_table, interval_query, name) median_datetime(cte_table, interval_query, name)
end end
Loading
Loading
Loading
@@ -7,13 +7,13 @@ module Gitlab
Loading
@@ -7,13 +7,13 @@ module Gitlab
# #
# Note: For MySQL, the interval is returned in seconds. # Note: For MySQL, the interval is returned in seconds.
# For PostgreSQL, the interval is returned as an INTERVAL type. # For PostgreSQL, the interval is returned as an INTERVAL type.
def subtract_datetimes(query_so_far, end_time_attrs, start_time_attrs, as) def subtract_datetimes(query_so_far, start_time_attrs, end_time_attrs, as)
diff_fn = subtract_datetimes_diff(query_so_far, end_time_attrs, start_time_attrs) diff_fn = subtract_datetimes_diff(query_so_far, end_time_attrs, start_time_attrs)
   
query_so_far.project(diff_fn.as(as)) query_so_far.project(diff_fn.as(as))
end end
   
def subtract_datetimes_diff(query_so_far, end_time_attrs, start_time_attrs) def subtract_datetimes_diff(query_so_far, start_time_attrs, end_time_attrs)
if Gitlab::Database.postgresql? if Gitlab::Database.postgresql?
Arel::Nodes::Subtraction.new( Arel::Nodes::Subtraction.new(
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)), Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
Loading
Loading
Loading
@@ -43,7 +43,35 @@ describe Gitlab::CycleAnalytics::Events do
Loading
@@ -43,7 +43,35 @@ describe Gitlab::CycleAnalytics::Events do
end end
   
it 'has the total time' do it 'has the total time' do
expect(subject.plan_events.first['total_time']).to eq('2 days') expect(subject.plan_events.first['total_time']).to eq('less than a minute')
end
end
describe '#code_events' do
let!(:context) { create(:issue, project: project, created_at: 2.days.ago) }
before do
create_commit_referencing_issue(context)
end
it 'has the total time' do
expect(subject.code_events.first['total_time']).to eq('2 days')
end
it 'has a title' do
expect(subject.code_events.first['title']).to eq('Awesome merge_request')
end
it 'has an iid' do
expect(subject.code_events.first['iid']).to eq(context.iid.to_s)
end
it 'has a created_at timestamp' do
expect(subject.code_events.first['created_at']).to end_with('ago')
end
it "has the author's name" do
expect(subject.code_events.first['name']).to eq(context.author.name)
end end
end end
   
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment