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 ae319aa9eb44
......@@ -9,10 +9,10 @@ def initialize(project:, from:)
@fetcher = EventsFetcher.new(project: project, from: from)
end
#TODO: backend pagination - specially for commits, etc...
# TODO: backend pagination - specially for commits, etc...
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|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['created_at'] = interval_in_words(event['created_at'])
......@@ -27,6 +27,13 @@ def plan_events
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
def first_time_reference_commit(commits, event)
......
......@@ -16,18 +16,33 @@ def fetch_issue_events
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
execute(query)
end
def fetch_plan_events
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])).
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
private
......@@ -38,7 +53,8 @@ def issue_attributes
end
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
def issue_projections
......@@ -49,6 +65,10 @@ def plan_projections
[mr_diff_table[:st_commits].as('commits'), issue_metrics_table[:first_mentioned_in_commit_at]]
end
def code_projections
[mr_table[:title], mr_table[:iid], mr_table[:created_at], User.arel_table[:name]]
end
def user_table
User.arel_table
end
......@@ -56,6 +76,10 @@ def user_table
def extract_epoch(arel_attribute)
Arel.sql(%Q{EXTRACT(EPOCH FROM (#{arel_attribute.to_sql}))})
end
def execute(query)
ActiveRecord::Base.connection.execute(query.to_sql).to_a
end
end
end
end
......@@ -17,7 +17,7 @@ def calculate_metric(name, start_time_attrs, end_time_attrs)
# cycle analytics stage.
interval_query = Arel::Nodes::As.new(
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)
end
......
......@@ -7,13 +7,13 @@ module DateTime
#
# Note: For MySQL, the interval is returned in seconds.
# 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)
query_so_far.project(diff_fn.as(as))
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?
Arel::Nodes::Subtraction.new(
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
......
......@@ -43,7 +43,35 @@
end
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
......
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