Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include stats in API #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 44 additions & 1 deletion lib/api_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def api_error(status, message)
end

# API routes that don't require authentication
AUTHENTICATION_WHITELIST_ROUTES = ["/api/commits/"]
AUTHENTICATION_WHITELIST_ROUTES = ["/api/commits/", "/api/stats"]
# API routes that require admin
ADMIN_ROUTES = ["/api/add_repo"]
# How out of date an API call may be before it is rejected
Expand Down Expand Up @@ -59,6 +59,11 @@ def api_error(status, message)
format_commit_data(commit, params[:repo_name], fields).to_json
end

get "/api/stats" do
since = params[:since] ? params[:since] : Time.now - 60 * 60 * 24 * 30
format_stats(since).to_json
end

# NOTE(caleb): Large GET requests are rejected by the Ruby web servers we use. (Unicorn, in particular,
# doesn't seem to like paths > 1k and rejects them silently.) Hence, to batch-request commit data, we must
# use a POST.
Expand Down Expand Up @@ -91,6 +96,44 @@ def format_commit_data(commit, repo_name, fields)
fields ? commit_data.select { |key, value| fields.include? key.to_s } : commit_data
end

def format_user_data(user_ids_and_counts)
user_ids_and_counts.map do |id_and_count|
{:count => id_and_count[1],
:name => id_and_count[0].name,
:email => id_and_count[0].email}
end
end

def format_stats(since)
dataset = Commit.
join(:comments, :commit_id => :id).
filter("`comments`.`created_at` > ?", since).
join(:git_repos, :id => :commits__git_repo_id).
group_and_count(:commits__sha, :git_repos__name___repo).order(:count.desc).limit(10)
chatty_commits = dataset.all
chatty_commits.map! do |commit|
{:sha => commit.sha,
:comment_count => commit[:count],
:repo_name => commit[:repo]
}
end
chatty_commits.map! do |commit|
commit_obj = Commit.prefix_match commit[:repo_name], commit[:sha]
{:sha => commit_obj.sha,
:comment_count => commit_obj.comment_count,
:message => commit_obj.message,
:repo_name => commit[:repo_name],
:approved => commit_obj.approved?}
end
data = {"num_commits" => Stats.num_commits(since),
"num_unreviewed_commits" => Stats.num_unreviewed_commits(since),
"num_reviewed_without_lgtm_commits" => Stats.num_reviewed_without_lgtm_commits(since),
"num_lgtm_commits" => Stats.num_lgtm_commits(since),
"chatty_commits" => chatty_commits,
"top_reviewers" => format_user_data(Stats.top_reviewers(since)),
"top_approvers" => format_user_data(Stats.top_approvers(since))}
end

# Check that an authenticated request is properly formed and correctly signed. Returns the user if
# everything is OK.
def ensure_properly_signed(request, params)
Expand Down