Welcome to mirror list, hosted at ThFree Co, Russian Federation.

contributions_calendar.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ca5115d43ac0155fc4ac39b55238f5136451baf (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
module Gitlab
  class ContributionsCalendar
    attr_reader :timestamps, :projects, :user

    def initialize(projects, user)
      @projects = projects
      @user = user
    end

    def timestamps
      return @timestamps if @timestamps.present?

      @timestamps = {}
      date_from = 1.year.ago
      date_to = Date.today

      events = Event.where(author_id: user.id).where(action: event_type).
        where("created_at > ?", date_from).where(project_id: projects)

      grouped_events = events.to_a.group_by { |event| event.created_at.to_date.to_s }
      dates = (1.year.ago.to_date..(Date.today + 1.day)).to_a

      dates.each do |date|
        date_id = date.to_time.to_i.to_s
        @timestamps[date_id] = 0

        if grouped_events.has_key?(date.to_s)
          grouped_events[date.to_s].each do |event|
            if event.created_at.to_date == date
              if event.issue? || event.merge_request?
                @timestamps[date_id] += 1
              elsif event.push?
                @timestamps[date_id] += event.commits_count
              end
            end
          end
        end
      end

      @timestamps
    end

    def events_by_date(date)
      events = Event.where(author_id: user.id).where(action: event_type).
        where("created_at > ? AND created_at < ?", date.beginning_of_day, date.end_of_day).
        where(project_id: projects)

      events.select do |event|
        event.push? || event.issue? || event.merge_request?
      end
    end

    def starting_year
      (Time.now - 1.year).strftime("%Y")
    end

    def starting_month
      Date.today.strftime("%m").to_i
    end

    def event_type
      [Event::PUSHED, Event::CREATED, Event::CLOSED, Event::MERGED]
    end
  end
end