hubstats 0.3.17 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +0 -1
  3. data/CHANGELOG.markdown +10 -0
  4. data/MIT-LICENSE +3 -1
  5. data/README.md +2 -2
  6. data/app/assets/javascripts/hubstats/application.js +80 -24
  7. data/app/assets/javascripts/hubstats/pull_requests.js +35 -3
  8. data/app/assets/javascripts/hubstats/select2.js +8 -1
  9. data/app/assets/javascripts/hubstats/users.js +17 -5
  10. data/app/assets/stylesheets/hubstats/application.css +39 -1
  11. data/app/assets/stylesheets/hubstats/bootstrap.css +0 -6
  12. data/app/assets/stylesheets/hubstats/deploys.css +2 -0
  13. data/app/assets/stylesheets/hubstats/label.css +2 -0
  14. data/app/assets/stylesheets/hubstats/octicons.css.erb +2 -0
  15. data/app/assets/stylesheets/hubstats/pull_requests.css +2 -0
  16. data/app/controllers/hubstats/application_controller.rb +12 -4
  17. data/app/controllers/hubstats/base_controller.rb +8 -0
  18. data/app/controllers/hubstats/deploys_controller.rb +31 -8
  19. data/app/controllers/hubstats/events_controller.rb +11 -0
  20. data/app/controllers/hubstats/pull_requests_controller.rb +9 -1
  21. data/app/controllers/hubstats/repos_controller.rb +17 -3
  22. data/app/controllers/hubstats/users_controller.rb +15 -0
  23. data/app/models/hubstats/comment.rb +13 -5
  24. data/app/models/hubstats/deploy.rb +29 -6
  25. data/app/models/hubstats/label.rb +16 -4
  26. data/app/models/hubstats/pull_request.rb +44 -2
  27. data/app/models/hubstats/repo.rb +48 -4
  28. data/app/models/hubstats/user.rb +130 -32
  29. data/app/views/hubstats/deploys/index.html.erb +1 -3
  30. data/app/views/hubstats/deploys/show.html.erb +7 -2
  31. data/app/views/hubstats/partials/_comment-condensed.html.erb +3 -0
  32. data/app/views/hubstats/partials/_comment.html.erb +3 -0
  33. data/app/views/hubstats/partials/_deploy-condensed.html.erb +2 -2
  34. data/app/views/hubstats/partials/_deploy.html.erb +1 -1
  35. data/app/views/hubstats/partials/_footer.html.erb +22 -0
  36. data/app/views/hubstats/partials/_header.html.erb +18 -10
  37. data/app/views/hubstats/partials/_pull-condensed.html.erb +2 -0
  38. data/app/views/hubstats/partials/_pull.html.erb +2 -0
  39. data/app/views/hubstats/partials/_quick_addition_stats.html.erb +2 -0
  40. data/app/views/hubstats/partials/_quick_stats.html.erb +1 -0
  41. data/app/views/hubstats/partials/_repo.html.erb +2 -3
  42. data/app/views/hubstats/partials/_user-condensed.html.erb +4 -0
  43. data/app/views/hubstats/partials/_user.html.erb +4 -2
  44. data/app/views/hubstats/pull_requests/index.html.erb +6 -1
  45. data/app/views/hubstats/pull_requests/show.html.erb +5 -0
  46. data/app/views/hubstats/repos/dashboard.html.erb +6 -4
  47. data/app/views/hubstats/repos/show.html.erb +6 -1
  48. data/app/views/hubstats/tables/_comments-condensed.html.erb +2 -1
  49. data/app/views/hubstats/tables/_comments.html.erb +2 -1
  50. data/app/views/hubstats/tables/_deploys-condensed.html.erb +1 -0
  51. data/app/views/hubstats/tables/_deploys.html.erb +1 -0
  52. data/app/views/hubstats/tables/_grouped_deploys.html.erb +1 -0
  53. data/app/views/hubstats/tables/_grouped_pulls.html.erb +1 -0
  54. data/app/views/hubstats/tables/_pulls-condensed.html.erb +1 -0
  55. data/app/views/hubstats/tables/_pulls.html.erb +1 -0
  56. data/app/views/hubstats/tables/_repos-condensed.html.erb +1 -0
  57. data/app/views/hubstats/tables/_repos.html.erb +1 -0
  58. data/app/views/hubstats/tables/_users-condensed.html.erb +1 -0
  59. data/app/views/hubstats/tables/_users.html.erb +1 -1
  60. data/app/views/hubstats/users/index.html.erb +3 -2
  61. data/app/views/hubstats/users/show.html.erb +7 -1
  62. data/app/views/layouts/hubstats/application.html.erb +4 -4
  63. data/config/routes.rb +2 -1
  64. data/db/seeds.rb +41 -0
  65. data/hubstats.gemspec +1 -0
  66. data/lib/generators/hubstats/install_generator.rb +2 -1
  67. data/lib/hub_helper.rb +18 -0
  68. data/lib/hubstats.rb +0 -30
  69. data/lib/hubstats/events_handler.rb +25 -7
  70. data/lib/hubstats/github_api.rb +43 -15
  71. data/lib/hubstats/version.rb +1 -1
  72. data/lib/tasks/hubstats_tasks.rake +1 -0
  73. data/lib/tasks/populate_task.rake +2 -1
  74. data/spec/controllers/hubstats/deploys_controller_spec.rb +0 -3
  75. data/spec/controllers/hubstats/pull_requests_controller_spec.rb +3 -4
  76. data/spec/controllers/hubstats/repos_controller_spec.rb +4 -5
  77. data/spec/factories/comment.rb +2 -1
  78. data/spec/factories/deploys.rb +1 -0
  79. data/spec/factories/pull_requests.rb +1 -0
  80. data/spec/factories/repo.rb +1 -0
  81. data/spec/factories/users.rb +2 -1
  82. data/spec/lib/hubstats/github_api_spec.rb +5 -5
  83. metadata +41 -26
  84. data/date_range +0 -0
@@ -1,5 +1,7 @@
1
1
  module Hubstats
2
2
  class PullRequest < ActiveRecord::Base
3
+
4
+ # Various checks that can be used to filter, sort, and find info about pull requests.
3
5
  scope :closed_in_date_range, lambda {|start_date, end_date| where("hubstats_pull_requests.closed_at BETWEEN ? AND ?", start_date, end_date)}
4
6
  scope :updated_in_date_range, lambda {|start_date, end_date| where("hubstats_pull_requests.updated_at BETWEEN ? AND ?", start_date, end_date)}
5
7
  scope :created_in_date_range, lambda {|start_date, end_date| where("hubstats_pull_requests.created_at BETWEEN ? AND ?", start_date, end_date)}
@@ -16,7 +18,7 @@ module Hubstats
16
18
  scope :with_repo_name, select('DISTINCT hubstats_repos.name as repo_name, hubstats_pull_requests.*').joins("LEFT JOIN hubstats_repos ON hubstats_repos.id = hubstats_pull_requests.repo_id")
17
19
  scope :with_user_name, select('DISTINCT hubstats_users.login as user_name, hubstats_pull_requests.*').joins("LEFT JOIN hubstats_users ON hubstats_users.id = hubstats_pull_requests.user_id")
18
20
 
19
- attr_accessible :id, :url, :html_url, :diff_url, :patch_url, :issue_url, :commits_url,
21
+ attr_accessible :id, :url, :html_url, :diff_url, :patch_url, :issue_url, :commits_url,
20
22
  :review_comments_url, :review_comment_url, :comments_url, :statuses_url, :number,
21
23
  :state, :title, :body, :created_at, :updated_at, :closed_at, :merged_at,
22
24
  :merge_commit_sha, :merged, :mergeable, :comments, :commits, :additions,
@@ -27,6 +29,12 @@ module Hubstats
27
29
  belongs_to :deploy
28
30
  has_and_belongs_to_many :labels, :join_table => "hubstats_labels_pull_requests"
29
31
 
32
+ # Public - Makes a new pull request from a GitHub webhook. Finds user_id and repo_id based on users and repos
33
+ # that are already in the Hubstats database. Also updates the user_id of a deploy if the pull request has been merged in a deploy.
34
+ #
35
+ # github_pull - the info that is from Github about the new or updated pull request
36
+ #
37
+ # Returns - the pull request
30
38
  def self.create_or_update(github_pull)
31
39
  github_pull = github_pull.to_h.with_indifferent_access if github_pull.respond_to? :to_h
32
40
 
@@ -41,7 +49,6 @@ module Hubstats
41
49
 
42
50
  pull = where(:id => pull_data[:id]).first_or_create(pull_data)
43
51
 
44
- # Updates the merged_by part of the pull request and the user_id of the deploy
45
52
  if github_pull[:merged_by] && github_pull[:merged_by][:id]
46
53
  pull_data[:merged_by] = github_pull[:merged_by][:id]
47
54
  deploy = Hubstats::Deploy.where(id: pull.deploy_id, user_id: nil).first
@@ -55,21 +62,51 @@ module Hubstats
55
62
  Rails.logger.warn pull.errors.inspect
56
63
  end
57
64
 
65
+ # Public - Adds any labels to the labels database if the labels passed in aren't already there.
66
+ #
67
+ # labels - the labels to be added to the db
68
+ #
69
+ # Returns - the new labels
58
70
  def add_labels(labels)
59
71
  labels.map!{|label| Hubstats::Label.first_or_create(label) }
60
72
  self.labels = labels
61
73
  end
62
74
 
75
+ # Public - Filters all of the pull requests between start_date and end_date that are the given state
76
+ # (passed as part of params) that belong to the params' users and repos.
77
+ #
78
+ # params - the params that are passed in (likely from the URL)
79
+ # start_date - the start of the date range
80
+ # end_date - the end of the data range
81
+ #
82
+ # Returns - the PRs that fit all of the below sql queries
63
83
  def self.all_filtered(params, start_date, end_date)
64
84
  filter_based_on_date_range(start_date, end_date, params[:state])
65
85
  .belonging_to_users(params[:users])
66
86
  .belonging_to_repos(params[:repos])
67
87
  end
68
88
 
89
+ # Public - Finds all of the PRs with the current state, and then filters to ones that have been updated in
90
+ # between the start_date and end_date.
91
+ #
92
+ # start_date - the start of the date range
93
+ # end_date - the end of the data range
94
+ # state - the current state (open, closed, or all) of the PRs
95
+ #
96
+ # Returns - the PRs that are within the date range and that are the state
69
97
  def self.filter_based_on_date_range(start_date, end_date, state)
70
98
  with_state(state).updated_in_date_range(start_date, end_date)
71
99
  end
72
100
 
101
+ # Public - Finds all of the PRs that have the current state, then filters to PRs that were updated within the
102
+ # start_date and end_date. Lastly, it orders all of them based on the given order.
103
+ #
104
+ # start_date - the start of the date range
105
+ # end_date - the end of the data range
106
+ # state - the current state (open, closed, or all) of the PRs
107
+ # order - the order (descending or ascending) that the PRs should be ordered in
108
+ #
109
+ # Returns - the PRs that are within the date range and that are the state ordered
73
110
  def self.state_based_order(start_date, end_date, state, order)
74
111
  order = ["ASC","DESC"].detect{|order_type| order_type.to_s == order.to_s.upcase } || "DESC"
75
112
  if state == "closed"
@@ -79,6 +116,11 @@ module Hubstats
79
116
  end
80
117
  end
81
118
 
119
+ # Public - Groups the PRs by either username or repo name, based on the string passed in.
120
+ #
121
+ # group - string that is the designated grouping
122
+ #
123
+ # Returns - the data grouped
82
124
  def self.group_by(group)
83
125
  if group == 'user'
84
126
  with_user_name.order("user_name ASC")
@@ -1,9 +1,16 @@
1
1
  module Hubstats
2
2
  class Repo < ActiveRecord::Base
3
3
 
4
+ # Various checks that can be used to filter and find info about repos.
4
5
  scope :with_recent_activity, lambda {|start_date, end_date| where("hubstats_repos.updated_at BETWEEN ? AND ?", start_date, end_date).order("updated_at DESC")}
5
6
  scope :with_id, lambda {|repo_id| where(id: repo_id.split(',')) if repo_id}
6
7
 
8
+ # Public - Counts all of the deploys for selected repo that occurred between the start_date and end_date.
9
+ #
10
+ # start_date - the start of the date range
11
+ # end_date - the end of the data range
12
+ #
13
+ # Returns - count of deploys
7
14
  scope :deploys_count, lambda {|start_date, end_date|
8
15
  select("hubstats_repos.id as repo_id")
9
16
  .select("IFNULL(COUNT(DISTINCT hubstats_deploys.id),0) AS deploy_count")
@@ -11,6 +18,12 @@ module Hubstats
11
18
  .group("hubstats_repos.id")
12
19
  }
13
20
 
21
+ # Public - Counts all of the comments for selected repo that occurred between the start_date and end_date.
22
+ #
23
+ # start_date - the start of the date range
24
+ # end_date - the end of the data range
25
+ #
26
+ # Returns - count of comments
14
27
  scope :comments_count, lambda {|start_date, end_date|
15
28
  select("hubstats_repos.id as repo_id")
16
29
  .select("IFNULL(COUNT(DISTINCT hubstats_comments.id),0) AS comment_count")
@@ -18,15 +31,26 @@ module Hubstats
18
31
  .group("hubstats_repos.id")
19
32
  }
20
33
 
34
+ # Public - Counts all of the merged pull requests for selected repo that occurred between the start_date and end_date.
35
+ #
36
+ # start_date - the start of the date range
37
+ # end_date - the end of the data range
38
+ #
39
+ # Returns - count of pull requests
21
40
  scope :pull_requests_count, lambda {|start_date, end_date|
22
41
  select("hubstats_repos.id as repo_id")
23
42
  .select("IFNULL(COUNT(DISTINCT hubstats_pull_requests.id),0) AS pull_request_count")
24
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.repo_id = hubstats_repos.id AND (hubstats_pull_requests.merged_at BETWEEN ? AND ?)", start_date, end_date]))
25
- .where("hubstats_pull_requests.merged = '1'")
43
+ .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.repo_id = hubstats_repos.id AND (hubstats_pull_requests.merged_at BETWEEN ? AND ?) AND hubstats_pull_requests.merged = '1'", start_date, end_date]))
26
44
  .group("hubstats_repos.id")
27
45
  }
28
46
 
29
- scope :averages, lambda {|start_date, end_date|
47
+ # Public - Averages all of the additions and deletions of the merged PRs for selected repo.
48
+ #
49
+ # start_date - the start of the date range
50
+ # end_date - the end of the data range
51
+ #
52
+ # Returns - the average additions and deletions
53
+ scope :averages, lambda {
30
54
  select("hubstats_repos.id as repo_id")
31
55
  .select("ROUND(IFNULL(AVG(hubstats_pull_requests.additions),0)) AS average_additions")
32
56
  .select("ROUND(IFNULL(AVG(hubstats_pull_requests.deletions),0)) AS average_deletions")
@@ -34,9 +58,15 @@ module Hubstats
34
58
  .group("hubstats_repos.id")
35
59
  }
36
60
 
61
+ # Public - Joins all of the metrics together for selected repository: average additions and deletions, comments, pull requests, and deploys.
62
+ #
63
+ # start_date - the start of the date range
64
+ # end_date - the end of the data range
65
+ #
66
+ # Returns - all of the stats about the repo
37
67
  scope :with_all_metrics, lambda {|start_date, end_date|
38
68
  select("hubstats_repos.*, deploy_count, pull_request_count, comment_count, average_additions, average_deletions")
39
- .joins("LEFT JOIN (#{averages(start_date, end_date).to_sql}) AS averages ON averages.repo_id = hubstats_repos.id")
69
+ .joins("LEFT JOIN (#{averages.to_sql}) AS averages ON averages.repo_id = hubstats_repos.id")
40
70
  .joins("LEFT JOIN (#{pull_requests_count(start_date, end_date).to_sql}) AS pull_requests ON pull_requests.repo_id = hubstats_repos.id")
41
71
  .joins("LEFT JOIN (#{comments_count(start_date, end_date).to_sql}) AS comments ON comments.repo_id = hubstats_repos.id")
42
72
  .joins("LEFT JOIN (#{deploys_count(start_date, end_date).to_sql}) AS deploys ON deploys.repo_id = hubstats_repos.id")
@@ -56,6 +86,11 @@ module Hubstats
56
86
  has_many :comments
57
87
  belongs_to :owner, :class_name => "User", :foreign_key => "id"
58
88
 
89
+ # Public - Makes a new repository based on a GitHub webhook. Sets a user (owner) based on users that are already in the database.
90
+ #
91
+ # github_repo - the info about the new or updated repository
92
+ #
93
+ # Returns - the repository
59
94
  def self.create_or_update(github_repo)
60
95
  github_repo = github_repo.to_h.with_indifferent_access if github_repo.respond_to? :to_h
61
96
  repo_data = github_repo.slice(*column_names.map(&:to_sym))
@@ -70,6 +105,12 @@ module Hubstats
70
105
  Rails.logger.warn repo.errors.inspect
71
106
  end
72
107
 
108
+ # Public - Designed so that the list of repositories can be ordered based on deploys, pulls, comments, additions, deletions, or name.
109
+ # if none of these are selected, then the default is to order by pull request count in descending order.
110
+ #
111
+ # order_params - the param of what the repos should be sorted by
112
+ #
113
+ # Returns - the repo data ordered
73
114
  def self.custom_order(order_params)
74
115
  if order_params
75
116
  order = order_params.include?('asc') ? "ASC" : "DESC"
@@ -94,6 +135,9 @@ module Hubstats
94
135
  end
95
136
  end
96
137
 
138
+ # Public - Designed to make a path for the show page when a repository is selected.
139
+ #
140
+ # Returns - the show page of self.name
97
141
  def to_param
98
142
  self.name
99
143
  end
@@ -1,8 +1,17 @@
1
1
  module Hubstats
2
2
  class User < ActiveRecord::Base
3
3
 
4
+ # Various checks that can be used to filter and find info about users.
4
5
  scope :with_id, lambda {|user_id| where(id: user_id.split(',')) if user_id}
5
-
6
+ scope :only_active, having("comment_count > 0 OR pull_request_count > 0 OR deploy_count > 0")
7
+ scope :with_contributions, lambda {|start_date, end_date, repo_id| with_all_metrics_repos(start_date, end_date, repo_id) if repo_id}
8
+
9
+ # Public - Counts all of the deploys for selected user that occurred between the start_date and end_date.
10
+ #
11
+ # start_date - the start of the date range
12
+ # end_date - the end of the data range
13
+ #
14
+ # Returns - count of deploys
6
15
  scope :deploys_count, lambda {|start_date, end_date|
7
16
  select("hubstats_users.id as user_id")
8
17
  .select("IFNULL(COUNT(DISTINCT hubstats_deploys.id),0) AS deploy_count")
@@ -10,6 +19,12 @@ module Hubstats
10
19
  .group("hubstats_users.id")
11
20
  }
12
21
 
22
+ # Public - Counts all of the comments for selected user that occurred between the start_date and end_date.
23
+ #
24
+ # start_date - the start of the date range
25
+ # end_date - the end of the data range
26
+ #
27
+ # Returns - count of comments
13
28
  scope :comments_count, lambda {|start_date, end_date|
14
29
  select("hubstats_users.id as user_id")
15
30
  .select("IFNULL(COUNT(DISTINCT hubstats_comments.id),0) AS comment_count")
@@ -17,39 +32,94 @@ module Hubstats
17
32
  .group("hubstats_users.id")
18
33
  }
19
34
 
35
+ # Public - Counts all of the merged pull requests for selected user that occurred between the start_date and end_date.
36
+ #
37
+ # start_date - the start of the date range
38
+ # end_date - the end of the data range
39
+ #
40
+ # Returns - count of pull requests
20
41
  scope :pull_requests_count, lambda {|start_date, end_date|
21
42
  select("hubstats_users.id as user_id")
22
43
  .select("IFNULL(COUNT(DISTINCT hubstats_pull_requests.id),0) AS pull_request_count")
23
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.user_id = hubstats_users.id AND (hubstats_pull_requests.merged_at BETWEEN ? AND ?)", start_date, end_date]))
24
- .where("hubstats_pull_requests.merged = '1'")
44
+ .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.user_id = hubstats_users.id AND (hubstats_pull_requests.merged_at BETWEEN ? AND ?) AND hubstats_pull_requests.merged = '1'", start_date, end_date]))
25
45
  .group("hubstats_users.id")
26
46
  }
27
47
 
28
- scope :pull_requests_count_by_repo, lambda {|start_date, end_date, repo_id|
29
- select("hubstats_users.id as user_id")
30
- .select("IFNULL(COUNT(DISTINCT hubstats_pull_requests.id),0) AS pull_request_count")
31
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.user_id = hubstats_users.id AND (hubstats_pull_requests.created_at BETWEEN ? AND ?)", start_date, end_date]))
32
- .where("hubstats_pull_requests.repo_id = ? AND hubstats_pull_requests.merged = '1'", repo_id)
48
+ # Public - Counts all of the merged pull requests, deploys, and comments that occurred between the start_date and end_date that belong to a user.
49
+ #
50
+ # start_date - the start of the date range
51
+ # end_date - the end of the data range
52
+ #
53
+ # Returns - the count of deploys, pull requests, and comments
54
+ scope :pull_comment_deploy_count, lambda {|start_date, end_date|
55
+ select("hubstats_users.*, pull_request_count, comment_count, deploy_count")
56
+ .joins("LEFT JOIN (#{pull_requests_count(start_date, end_date).to_sql}) AS pull_requests ON pull_requests.user_id = hubstats_users.id")
57
+ .joins("LEFT JOIN (#{comments_count(start_date, end_date).to_sql}) AS comments ON comments.user_id = hubstats_users.id")
58
+ .joins("LEFT JOIN (#{deploys_count(start_date, end_date).to_sql}) AS deploys ON deploys.user_id = hubstats_users.id")
33
59
  .group("hubstats_users.id")
34
60
  }
35
61
 
62
+ # Public - Counts all of the deploys for selected user that occurred between the start_date and end_date and belong to the repos.
63
+ #
64
+ # start_date - the start of the date range
65
+ # end_date - the end of the data range
66
+ #
67
+ # Returns - count of deploys
68
+ scope :deploys_count_by_repo, lambda {|start_date, end_date, repo_id|
69
+ select("hubstats_users.id as user_id")
70
+ .select("IFNULL(COUNT(DISTINCT hubstats_deploys.id),0) AS deploy_count")
71
+ .joins(sanitize_sql_array(["LEFT JOIN hubstats_deploys ON hubstats_deploys.user_id = hubstats_users.id AND (hubstats_deploys.deployed_at BETWEEN ? AND ?) AND (hubstats_deploys.repo_id LIKE ?)", start_date, end_date, repo_id]))
72
+ .group("hubstats_users.id")
73
+ }
74
+
75
+ # Public - Counts all of the comments for selected user that occurred between the start_date and end_date and belong to the repos.
76
+ #
77
+ # start_date - the start of the date range
78
+ # end_date - the end of the data range
79
+ #
80
+ # Returns - count of comments
36
81
  scope :comments_count_by_repo, lambda {|start_date, end_date, repo_id|
37
82
  select("hubstats_users.id as user_id")
38
83
  .select("COUNT(DISTINCT hubstats_comments.id) AS comment_count")
39
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_comments ON hubstats_comments.user_id = hubstats_users.id AND (hubstats_comments.created_at BETWEEN ? AND ?)", start_date, end_date]))
40
- .where("hubstats_comments.repo_id = ?", repo_id)
84
+ .joins(sanitize_sql_array(["LEFT JOIN hubstats_comments ON hubstats_comments.user_id = hubstats_users.id AND (hubstats_comments.created_at BETWEEN ? AND ?) AND (hubstats_comments.repo_id LIKE ?)", start_date, end_date, repo_id]))
41
85
  .group("hubstats_users.id")
42
86
  }
43
87
 
44
- scope :pulls_reviewed_count, lambda {|start_date, end_date|
88
+ # Public - Counts all of the pull requests for selected user that occurred between the start_date and end_date and belong to the repos.
89
+ #
90
+ # start_date - the start of the date range
91
+ # end_date - the end of the data range
92
+ #
93
+ # Returns - count of pull requests
94
+ scope :pull_requests_count_by_repo, lambda {|start_date, end_date, repo_id|
45
95
  select("hubstats_users.id as user_id")
46
- .select("COUNT(DISTINCT hubstats_pull_requests.id) as reviews_count")
47
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_comments ON hubstats_comments.user_id = hubstats_users.id AND (hubstats_comments.created_at BETWEEN ? AND ?)", start_date, end_date]))
48
- .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.id = hubstats_comments.pull_request_id AND (hubstats_pull_requests.closed_at BETWEEN ? AND ?)", start_date, end_date]))
49
- .where("hubstats_pull_requests.user_id != hubstats_users.id")
96
+ .select("IFNULL(COUNT(DISTINCT hubstats_pull_requests.id),0) AS pull_request_count")
97
+ .joins(sanitize_sql_array(["LEFT JOIN hubstats_pull_requests ON hubstats_pull_requests.user_id = hubstats_users.id AND (hubstats_pull_requests.merged_at BETWEEN ? AND ?) AND (hubstats_pull_requests.repo_id LIKE ?) AND hubstats_pull_requests.merged = '1'", start_date, end_date, repo_id]))
98
+ .group("hubstats_users.id")
99
+ }
100
+
101
+ # Public - Counts all of the merged pull requests, deploys, and comments that belong to a repository and belong to a user and occurred between
102
+ # the start_date and end_date.
103
+ #
104
+ # start_date - the start of the date range
105
+ # end_date - the end of the data range
106
+ #
107
+ # Returns - the count of deploys, pull requests, and comments
108
+ scope :pull_comment_deploy_count_by_repo, lambda {|start_date, end_date, repo_id|
109
+ select("hubstats_users.*, pull_request_count, comment_count, deploy_count")
110
+ .joins("LEFT JOIN (#{pull_requests_count_by_repo(start_date, end_date, repo_id).to_sql}) AS pull_requests ON pull_requests.user_id = hubstats_users.id")
111
+ .joins("LEFT JOIN (#{comments_count_by_repo(start_date, end_date, repo_id).to_sql}) AS comments ON comments.user_id = hubstats_users.id")
112
+ .joins("LEFT JOIN (#{deploys_count_by_repo(start_date, end_date, repo_id).to_sql}) AS deploys ON deploys.user_id = hubstats_users.id")
50
113
  .group("hubstats_users.id")
51
114
  }
52
115
 
116
+ # Public - Counts all of the addtiions and deletions made from PRs by the selected user that have been merged between the start_date
117
+ # and the end_date.
118
+ #
119
+ # start_date - the start of the date range
120
+ # end_date - the end of the data range
121
+ #
122
+ # Returns - the additions and deletions
53
123
  scope :net_additions_count, lambda {|start_date, end_date|
54
124
  select("hubstats_users.id as user_id")
55
125
  .select("SUM(IFNULL(hubstats_pull_requests.additions, 0)) AS additions")
@@ -58,20 +128,28 @@ module Hubstats
58
128
  .group("hubstats_users.id")
59
129
  }
60
130
 
61
- scope :pull_and_comment_count, lambda {|start_date, end_date|
62
- select("hubstats_users.*, pull_request_count, comment_count")
63
- .joins("LEFT JOIN (#{pull_requests_count(start_date, end_date).to_sql}) AS pull_requests ON pull_requests.user_id = hubstats_users.id")
64
- .joins("LEFT JOIN (#{comments_count(start_date, end_date).to_sql}) AS comments ON comments.user_id = hubstats_users.id")
65
- .group("hubstats_users.id")
66
- }
67
-
68
- scope :pull_and_comment_count_by_repo, lambda {|start_date, end_date, repo_id|
69
- select("hubstats_users.*, pull_request_count, comment_count")
70
- .joins("LEFT JOIN (#{pull_requests_count_by_repo(start_date, end_date, repo_id).to_sql}) AS pull_requests ON pull_requests.user_id = hubstats_users.id")
71
- .joins("LEFT JOIN (#{comments_count_by_repo(start_date, end_date, repo_id).to_sql}) AS comments ON comments.user_id = hubstats_users.id")
131
+ # Public - Joins all of the metrics together for selected repository: average additions and deletions, comments, pull requests, and deploys.
132
+ # However, will only count those that also have something to do with the repos passed in.
133
+ #
134
+ # start_date - the start of the date range
135
+ # end_date - the end of the data range
136
+ #
137
+ # Returns - all of the stats about the user
138
+ scope :with_all_metrics_repos, lambda {|start_date, end_date, repos|
139
+ select("hubstats_users.*, deploy_count, pull_request_count, comment_count, additions, deletions")
140
+ .joins("LEFT JOIN (#{net_additions_count(start_date, end_date).to_sql}) AS net_additions ON net_additions.user_id = hubstats_users.id")
141
+ .joins("LEFT JOIN (#{pull_requests_count_by_repo(start_date, end_date, repos).to_sql}) AS pull_requests ON pull_requests.user_id = hubstats_users.id")
142
+ .joins("LEFT JOIN (#{comments_count_by_repo(start_date, end_date, repos).to_sql}) AS comments ON comments.user_id = hubstats_users.id")
143
+ .joins("LEFT JOIN (#{deploys_count_by_repo(start_date, end_date, repos).to_sql}) AS deploys ON deploys.user_id = hubstats_users.id")
72
144
  .group("hubstats_users.id")
73
145
  }
74
146
 
147
+ # Public - Joins all of the metrics together for selected repository: average additions and deletions, comments, pull requests, and deploys.
148
+ #
149
+ # start_date - the start of the date range
150
+ # end_date - the end of the data range
151
+ #
152
+ # Returns - all of the stats about the user
75
153
  scope :with_all_metrics, lambda {|start_date, end_date|
76
154
  select("hubstats_users.*, deploy_count, pull_request_count, comment_count, additions, deletions")
77
155
  .joins("LEFT JOIN (#{net_additions_count(start_date, end_date).to_sql}) AS net_additions ON net_additions.user_id = hubstats_users.id")
@@ -81,9 +159,6 @@ module Hubstats
81
159
  .group("hubstats_users.id")
82
160
  }
83
161
 
84
- scope :only_active, having("comment_count > 0 OR pull_request_count > 0")
85
- scope :weighted_sort, order("(pull_request_count)*2 + (comment_count) DESC")
86
-
87
162
  attr_accessible :login, :id, :avatar_url, :gravatar_id, :url, :html_url, :followers_url,
88
163
  :following_url, :gists_url, :starred_url, :subscriptions_url, :organizations_url,
89
164
  :repos_url, :events_url, :received_events_url, :role, :site_admin
@@ -95,6 +170,11 @@ module Hubstats
95
170
  has_many :pull_requests
96
171
  has_many :deploys
97
172
 
173
+ # Public - Creates a new user form a GitHub webhook.
174
+ #
175
+ # github_user - the info from Github about the new or updated user
176
+ #
177
+ # Returns - the user
98
178
  def self.create_or_update(github_user)
99
179
  github_user[:role] = github_user.delete :type ##changing :type in to :role
100
180
  github_user = github_user.to_h.with_indifferent_access unless github_user.is_a? Hash
@@ -106,14 +186,29 @@ module Hubstats
106
186
  Rails.logger.warn user.errors.inspect
107
187
  end
108
188
 
109
- def self.with_pulls_or_comments(start_date, end_date, repo_id = nil)
189
+ # Public - If a repo_id is provided, will sort/filter the users based on the number of comments, deploys, and pull requests
190
+ # on that repo within the start_date and end_date. If no repo_id is provided, will still sort, just considering all PRs and comments
191
+ # within the two dates.
192
+ #
193
+ # start_date - the start of the date range
194
+ # end_date - the end of the data range
195
+ # repo_id - the id of the repository (optional)
196
+ #
197
+ # Returns - the count of data that fulfills the sql queries
198
+ def self.with_pulls_or_comments_or_deploys(start_date, end_date, repo_id = nil)
110
199
  if repo_id
111
- pull_and_comment_count_by_repo(start_date, end_date, repo_id).weighted_sort
200
+ pull_comment_deploy_count_by_repo(start_date, end_date, repo_id)
112
201
  else
113
- pull_and_comment_count(start_date, end_date).weighted_sort
202
+ pull_comment_deploy_count(start_date, end_date)
114
203
  end
115
204
  end
116
205
 
206
+ # Public - Designed so that the list of users can be ordered based on deploys, pulls, comments, net additions, or name.
207
+ # if none of these are selected, then the default is to order by pull request count in descending order.
208
+ #
209
+ # order_params - the param of what the users should be sorted by
210
+ #
211
+ # Returns - the user data ordered
117
212
  def self.custom_order(order_params)
118
213
  if order_params
119
214
  order = order_params.include?('asc') ? "ASC" : "DESC"
@@ -136,6 +231,9 @@ module Hubstats
136
231
  end
137
232
  end
138
233
 
234
+ # Public - Designed to make a path for the show page when a repository is selected.
235
+ #
236
+ # Returns - the show page of self.name
139
237
  def to_param
140
238
  self.login
141
239
  end