Commit 96294925 authored by Robert May's avatar Robert May

Merge branch 'id-cache-commit-stats' into 'master'

Cache commit stats for a single commit

See merge request gitlab-org/gitlab!67592
parents 1b85b853 8aa2681f
......@@ -14,21 +14,23 @@ module Gitlab
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/323
def initialize(repo, commit)
@id = commit.id
@additions = 0
@deletions = 0
@total = 0
wrapped_gitaly_errors do
gitaly_stats(repo, commit)
end
end
additions, deletions = fetch_stats(repo, commit)
def gitaly_stats(repo, commit)
stats = repo.gitaly_commit_client.commit_stats(@id)
@additions = stats.additions
@deletions = stats.deletions
@additions = additions.to_i
@deletions = deletions.to_i
@total = @additions + @deletions
end
def fetch_stats(repo, commit)
Rails.cache.fetch("commit_stats:#{repo.gl_project_path}:#{@id}") do
stats = wrapped_gitaly_errors do
repo.gitaly_commit_client.commit_stats(@id)
end
[stats.additions, stats.deletions]
end
end
end
end
end
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Gitlab::Git::CommitStats, :seed_helper do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') }
let(:commit) { Gitlab::Git::Commit.find(repository, SeedRepo::Commit::ID) }
def verify_stats!
stats = described_class.new(repository, commit)
expect(stats).to have_attributes(
additions: eq(11),
deletions: eq(6),
total: eq(17)
)
end
it 'returns commit stats and caches them', :use_clean_rails_redis_caching do
expect(repository.gitaly_commit_client).to receive(:commit_stats).with(commit.id).and_call_original
verify_stats!
expect(Rails.cache.fetch("commit_stats:group/project:#{commit.id}")).to eq([11, 6])
expect(repository.gitaly_commit_client).not_to receive(:commit_stats)
verify_stats!
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment