From 847d2796cfeda0347aae0649fed16250f6188ca9 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Mon, 17 Oct 2016 14:57:23 +0200 Subject: [PATCH] fixed spec and SQL query --- lib/gitlab/cycle_analytics/events_fetcher.rb | 22 ++++++++++--------- .../lib/gitlab/cycle_analytics/events_spec.rb | 20 +++++++++++++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/gitlab/cycle_analytics/events_fetcher.rb b/lib/gitlab/cycle_analytics/events_fetcher.rb index ceed5823371..eb3cf9f2476 100644 --- a/lib/gitlab/cycle_analytics/events_fetcher.rb +++ b/lib/gitlab/cycle_analytics/events_fetcher.rb @@ -9,26 +9,28 @@ module Gitlab end def fetch_issues - cte_table = Arel::Table.new("cte_table_for_issue") - - # Build a `SELECT` query. We find the first of the `end_time_attrs` that isn't `NULL` (call this end_time). - # Next, we find the first of the start_time_attrs that isn't `NULL` (call this start_time). - # We compute the (end_time - start_time) interval, and give it an alias based on the current - # cycle analytics stage. - base_query = base_query_for(:issue) - diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], metric_attributes) - query = base_query.project(diff_fn.as('issue_diff')) + query = base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])). + project(diff_fn.as('issue_diff'), *issue_projections). + order(issue_table[:created_at].desc) - ActiveRecord::Base.connection.execute(query.to_sql) + ActiveRecord::Base.connection.execute(query.to_sql).first end def metric_attributes [issue_metrics_table[:first_associated_with_milestone_at], issue_metrics_table[:first_added_to_board_at]] end + + def issue_projections + [issue_table[:title], issue_table[:iid], issue_table[:created_at], User.arel_table[:name]] + end + + def user_table + User.arel_table + end end end end diff --git a/spec/lib/gitlab/cycle_analytics/events_spec.rb b/spec/lib/gitlab/cycle_analytics/events_spec.rb index 64387ef10d3..b32c4d6baa0 100644 --- a/spec/lib/gitlab/cycle_analytics/events_spec.rb +++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb @@ -14,8 +14,24 @@ describe Gitlab::CycleAnalytics::Events do describe '#issue' do let!(:context) { create(:issue, project: project) } - xit 'does something' do - expect(subject.issue_events).to eq([context]) + it 'has an issue diff' do + expect(subject.issue_events['issue_diff']).to eq("-00:00:00.339259") + end + + it 'has a title' do + expect(subject.issue_events['title']).to eq(context.title) + end + + it 'has an iid' do + expect(subject.issue_events['iid']).to eq(context.iid) + end + + it 'has a created_at timestamp' do + expect(subject.issue_events['created_at']).to eq(context.created_at) + end + + it "has the author's name" do + expect(subject.issue_events['name']).to eq(context.author.name) end end