contributions_calendar.rb 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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

16
      events = Event.reorder(nil).contributions.where(author_id: user.id).
17 18
        where("created_at > ?", date_from).where(project_id: projects).
        group('date(created_at)').
19
        select('date(created_at) as date, count(id) as total_amount').
20
        map(&:attributes)
21

22
      dates = (1.year.ago.to_date..Date.today).to_a
23 24 25

      dates.each do |date|
        date_id = date.to_time.to_i.to_s
26
        day_events = events.find { |day_events| day_events["date"] == date }
27

28 29
        if day_events
          @timestamps[date_id] = day_events["total_amount"]
30 31 32 33 34 35 36
        end
      end

      @timestamps
    end

    def events_by_date(date)
37
      events = Event.contributions.where(author_id: user.id).
38 39 40 41 42 43 44 45 46
        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
47
      1.year.ago.year
48 49 50
    end

    def starting_month
51
      Date.today.month
52 53 54
    end
  end
end