From 64891c6c40c6b670c2b50aab8ba56e3d47e30076 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sat, 21 Mar 2015 23:48:08 -0700 Subject: Replace commits calendar with contributions calendar * count opening of issues and merge requests * dont trigger git repository - use events from database * much-much faster since does not affected by repository size --- lib/gitlab/commits_calendar.rb | 41 ----------------------- lib/gitlab/contributions_calendar.rb | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 41 deletions(-) delete mode 100644 lib/gitlab/commits_calendar.rb create mode 100644 lib/gitlab/contributions_calendar.rb (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb deleted file mode 100644 index 8963d346b6f..00000000000 --- a/lib/gitlab/commits_calendar.rb +++ /dev/null @@ -1,41 +0,0 @@ -module Gitlab - class CommitsCalendar - attr_reader :timestamps - - def initialize(projects, user) - @timestamps = {} - date_timestamps = [] - - projects.reject(&:forked?).each do |project| - date_timestamps << ProjectContributions.new(project, user).commits_log - end - - # Sumarrize commits from all projects per days - date_timestamps = date_timestamps.inject do |collection, date| - collection.merge(date) { |k, old_v, new_v| old_v + new_v } - end - - date_timestamps ||= [] - date_timestamps.each do |date, commits| - timestamp = Date.parse(date).to_time.to_i.to_s rescue nil - @timestamps[timestamp] = commits if timestamp - end - end - - def self.get_commits_for_date(projects, user, date) - user_commits = {} - projects.reject(&:forked?).each do |project| - user_commits[project] = ProjectContributions.new(project, user).user_commits_on_date(date) - end - user_commits - end - - def starting_year - (Time.now - 1.year).strftime("%Y") - end - - def starting_month - Date.today.strftime("%m").to_i - end - end -end diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb new file mode 100644 index 00000000000..8ca5115d43a --- /dev/null +++ b/lib/gitlab/contributions_calendar.rb @@ -0,0 +1,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 -- cgit v1.2.1 From 43afe46bbd19b1edf60abf3f104fb2b0d29af564 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 22 Mar 2015 13:55:00 -0700 Subject: Refactor contributions events and write tests for calendar --- lib/gitlab/contributions_calendar.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb index 8ca5115d43a..ea41644811f 100644 --- a/lib/gitlab/contributions_calendar.rb +++ b/lib/gitlab/contributions_calendar.rb @@ -14,7 +14,7 @@ module Gitlab date_from = 1.year.ago date_to = Date.today - events = Event.where(author_id: user.id).where(action: event_type). + events = Event.contributions.where(author_id: user.id). where("created_at > ?", date_from).where(project_id: projects) grouped_events = events.to_a.group_by { |event| event.created_at.to_date.to_s } @@ -41,7 +41,7 @@ module Gitlab end def events_by_date(date) - events = Event.where(author_id: user.id).where(action: event_type). + events = Event.contributions.where(author_id: user.id). where("created_at > ? AND created_at < ?", date.beginning_of_day, date.end_of_day). where(project_id: projects) @@ -57,9 +57,5 @@ module Gitlab def starting_month Date.today.strftime("%m").to_i end - - def event_type - [Event::PUSHED, Event::CREATED, Event::CLOSED, Event::MERGED] - end end end -- cgit v1.2.1 From 54aca18cf856a18bdd121a3db25d2a64b9e0844d Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 22 Mar 2015 14:35:27 -0700 Subject: Contribution calendar will use events instead of commits to count contributions --- lib/gitlab/contributions_calendar.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb index ea41644811f..79e0a514f9e 100644 --- a/lib/gitlab/contributions_calendar.rb +++ b/lib/gitlab/contributions_calendar.rb @@ -15,25 +15,20 @@ module Gitlab date_to = Date.today events = Event.contributions.where(author_id: user.id). - where("created_at > ?", date_from).where(project_id: projects) + where("created_at > ?", date_from).where(project_id: projects). + group('date(created_at)'). + select('date(created_at), count(id) as total_amount'). + reorder(nil).map(&:attributes) - 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 + day_events = events.find { |day_events| day_events["date"] == date } - 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 + if day_events + @timestamps[date_id] = day_events["total_amount"] end end -- cgit v1.2.1 From 8494170550063c2d0308963cbd14cb46a292c401 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 22 Mar 2015 14:52:44 -0700 Subject: Improve contribution calendar per day info --- lib/gitlab/contributions_calendar.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/contributions_calendar.rb b/lib/gitlab/contributions_calendar.rb index 79e0a514f9e..3fd0823df06 100644 --- a/lib/gitlab/contributions_calendar.rb +++ b/lib/gitlab/contributions_calendar.rb @@ -14,11 +14,11 @@ module Gitlab date_from = 1.year.ago date_to = Date.today - events = Event.contributions.where(author_id: user.id). + events = Event.reorder(nil).contributions.where(author_id: user.id). where("created_at > ?", date_from).where(project_id: projects). group('date(created_at)'). select('date(created_at), count(id) as total_amount'). - reorder(nil).map(&:attributes) + map(&:attributes) dates = (1.year.ago.to_date..(Date.today + 1.day)).to_a -- cgit v1.2.1