summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/events.rb
blob: 66b30250c0f1f7dbd05bcc5bf898fd5da4c89382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Gitlab
  module CycleAnalytics
    class Events
      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

      def plan_events
        @fetcher.fetch(stage: :plan).each do |event|
          event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
          commits = event.delete('commits')
          event['commit'] = first_time_reference_commit(commits, event)
        end
      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

      def review_events
        @fetcher.fetch(stage: :review).each { |event| parse_event(event) }
      end

      def staging_events
        @fetcher.fetch(stage: :staging).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

      def production_events
        @fetcher.fetch(stage: :production).each { |event| parse_event(event) }
      end

      private

      def parse_event(event)
        event['url'] = Gitlab::LightUrlBuilder.build(entity: :issue, project: @project, id: event['id'])
        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')
      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

      def interval_in_words(diff)
        "#{distance_of_time_in_words(diff.to_f)} ago"
      end
    end
  end
end