diff --git a/lib/gitlab/cycle_analytics/events.rb b/lib/gitlab/cycle_analytics/events.rb index 0cf3067d48e..04cd599f092 100644 --- a/lib/gitlab/cycle_analytics/events.rb +++ b/lib/gitlab/cycle_analytics/events.rb @@ -1,6 +1,8 @@ module Gitlab module CycleAnalytics class Events + include ActionView::Helpers::DateHelper + def initialize(project:, from:) @project = project @from = from @@ -8,7 +10,9 @@ module Gitlab end def issue_events - @fetcher.fetch_issues + @fetcher.fetch_issues.each do |event| + event['issue_diff'] = distance_of_time_in_words(event['issue_diff'].to_f) + end end end end diff --git a/lib/gitlab/cycle_analytics/events_fetcher.rb b/lib/gitlab/cycle_analytics/events_fetcher.rb index eb3cf9f2476..14405f1356a 100644 --- a/lib/gitlab/cycle_analytics/events_fetcher.rb +++ b/lib/gitlab/cycle_analytics/events_fetcher.rb @@ -13,10 +13,10 @@ module Gitlab diff_fn = subtract_datetimes_diff(base_query, issue_table[:created_at], metric_attributes) query = base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])). - project(diff_fn.as('issue_diff'), *issue_projections). + project(extract_epoch(diff_fn).as('issue_diff'), *issue_projections). order(issue_table[:created_at].desc) - ActiveRecord::Base.connection.execute(query.to_sql).first + ActiveRecord::Base.connection.execute(query.to_sql) end def metric_attributes @@ -31,6 +31,10 @@ module Gitlab def user_table User.arel_table end + + def extract_epoch(arel_attribute) + Arel.sql(%Q{EXTRACT(EPOCH FROM (#{arel_attribute.to_sql}))}) + 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 b32c4d6baa0..543259f7cc2 100644 --- a/spec/lib/gitlab/cycle_analytics/events_spec.rb +++ b/spec/lib/gitlab/cycle_analytics/events_spec.rb @@ -12,10 +12,10 @@ describe Gitlab::CycleAnalytics::Events do end describe '#issue' do - let!(:context) { create(:issue, project: project) } + let!(:context) { create(:issue, project: project, created_at: 2.days.ago) } it 'has an issue diff' do - expect(subject.issue_events['issue_diff']).to eq("-00:00:00.339259") + expect(subject.issue_events['issue_diff']).to eq('2 days ago') end it 'has a title' do @@ -23,11 +23,11 @@ describe Gitlab::CycleAnalytics::Events do end it 'has an iid' do - expect(subject.issue_events['iid']).to eq(context.iid) + expect(subject.issue_events['iid']).to eq(context.iid.to_s) end it 'has a created_at timestamp' do - expect(subject.issue_events['created_at']).to eq(context.created_at) + expect(subject.issue_events['created_at']).to eq('2 days ago') end it "has the author's name" do