gitlab-org--gitlab-foss/lib/gitlab/cycle_analytics/events.rb

81 lines
2.9 KiB
Ruby
Raw Normal View History

module Gitlab
module CycleAnalytics
class Events
2016-10-17 12:03:17 -04:00
include ActionView::Helpers::DateHelper
def initialize(project:, from:)
@project = project
@from = from
@fetcher = EventsFetcher.new(project: project, from: from)
end
def issue_events
@fetcher.fetch(stage: :issue).each { |event| parse_event(event) }
end
2016-10-18 04:06:42 -04:00
2016-10-19 06:47:09 -04:00
def plan_events
@fetcher.fetch(stage: :plan).each do |event|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
commit = first_time_reference_commit(event.delete('commits'), event)
event['title'] = commit.title
event['url'] = Gitlab::LightUrlBuilder.build(entity: :commit_url, project: @project, id: commit.id)
event['sha'] = commit.short_id
event['author_name'] = commit.author.name
event['author_profile_url'] = Gitlab::LightUrlBuilder.build(entity: :user, id: commit.author.username)
event['author_avatar_url'] = Gitlab::LightUrlBuilder.build(entity: :user_avatar_url, id: commit.author.id)
end
2016-10-19 06:47:09 -04:00
end
def code_events
@fetcher.fetch(stage: :code).each { |event| parse_event(event) }
end
def test_events
@fetcher.fetch(stage: :test).each do |event|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['pipeline'] = ::Ci::Pipeline.find_by_id(event['ci_commit_id']) # we may not have a pipeline
end
end
2016-10-21 02:50:27 -04:00
def review_events
@fetcher.fetch(stage: :review).each { |event| parse_event(event) }
2016-10-21 02:50:27 -04:00
end
2016-10-21 05:33:37 -04:00
def staging_events
@fetcher.fetch(stage: :staging).each do |event|
2016-10-21 05:33:37 -04:00
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['pipeline'] = ::Ci::Pipeline.find_by_id(event['ci_commit_id']) # we may not have a pipeline
end
end
def production_events
@fetcher.fetch(stage: :production).each { |event| parse_event(event) }
end
2016-10-21 03:44:04 -04:00
private
2016-10-21 02:50:27 -04:00
def parse_event(event)
event['url'] = Gitlab::LightUrlBuilder.build(entity: :issue, project: @project, id: event['id'])
2016-10-21 02:50:27 -04:00
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
event['created_at'] = interval_in_words(event['created_at'])
event['author_profile_url'] = Gitlab::LightUrlBuilder.build(entity: :user, id: event['author_username'])
event['author_avatar_url'] = Gitlab::LightUrlBuilder.build(entity: :user_avatar_url, id: event['author_id'])
event.except!('author_id', 'author_username')
2016-10-21 02:50:27 -04:00
end
def first_time_reference_commit(commits, event)
st_commit = YAML.load(commits).detect do |commit|
commit['created_at'] == event['first_mentioned_in_commit_at']
end
Commit.new(Gitlab::Git::Commit.new(st_commit), @project)
end
2016-10-18 04:06:42 -04:00
def interval_in_words(diff)
2016-10-19 06:47:09 -04:00
"#{distance_of_time_in_words(diff.to_f)} ago"
2016-10-18 04:06:42 -04:00
end
end
end
end