Its better to load graph with ajax since it requires ~10 seconds for bigger projects to finish up

This commit is contained in:
Dmitriy Zaporozhets 2013-06-05 15:50:11 +03:00
parent 64457799c5
commit f1e0d37b15
11 changed files with 79 additions and 161 deletions

View file

@ -23,7 +23,7 @@ gem 'omniauth-github'
# Extracting information from a git repository # Extracting information from a git repository
# Provide access to Gitlab::Git library # Provide access to Gitlab::Git library
gem 'gitlab_git', '~> 1.2.1' gem 'gitlab_git', '~> 1.3.0'
# Ruby/Rack Git Smart-HTTP Server Handler # Ruby/Rack Git Smart-HTTP Server Handler
gem 'gitlab-grack', '~> 1.0.0', require: 'grack' gem 'gitlab-grack', '~> 1.0.0', require: 'grack'
@ -183,7 +183,7 @@ group :development, :test do
gem 'poltergeist', '~> 1.3.0' gem 'poltergeist', '~> 1.3.0'
gem 'spork', '~> 1.0rc' gem 'spork', '~> 1.0rc'
gem 'jasmine' gem 'jasmine'
end end
group :test do group :test do

View file

@ -164,7 +164,7 @@ GEM
gitlab-pygments.rb (0.3.2) gitlab-pygments.rb (0.3.2)
posix-spawn (~> 0.3.6) posix-spawn (~> 0.3.6)
yajl-ruby (~> 1.1.0) yajl-ruby (~> 1.1.0)
gitlab_git (1.2.1) gitlab_git (1.3.0)
activesupport (~> 3.2.13) activesupport (~> 3.2.13)
github-linguist (~> 2.3.4) github-linguist (~> 2.3.4)
gitlab-grit (~> 2.5.1) gitlab-grit (~> 2.5.1)
@ -543,7 +543,7 @@ DEPENDENCIES
gitlab-gollum-lib (~> 1.0.0) gitlab-gollum-lib (~> 1.0.0)
gitlab-grack (~> 1.0.0) gitlab-grack (~> 1.0.0)
gitlab-pygments.rb (~> 0.3.2) gitlab-pygments.rb (~> 0.3.2)
gitlab_git (~> 1.2.1) gitlab_git (~> 1.3.0)
gitlab_meta (= 5.0) gitlab_meta (= 5.0)
gitlab_omniauth-ldap (= 1.0.2) gitlab_omniauth-ldap (= 1.0.2)
gon gon

View file

@ -1,14 +1,17 @@
class StatGraphController < ProjectResourceController class StatGraphController < ProjectResourceController
# Authorize # Authorize
before_filter :authorize_read_project! before_filter :authorize_read_project!
before_filter :authorize_code_access! before_filter :authorize_code_access!
before_filter :require_non_empty_project before_filter :require_non_empty_project
def show def show
@repo = @project.repository respond_to do |format|
@stats = Gitlab::GitStats.new(@repo.raw, @repo.root_ref) format.html
@log = @stats.parsed_log.to_json format.js do
@repo = @project.repository
@stats = Gitlab::Git::GitStats.new(@repo.raw, @repo.root_ref)
@log = @stats.parsed_log.to_json
end
end
end end
end
end

View file

@ -1,29 +1,31 @@
.header.clearfix .loading-graph
.right %center
%select .loading
%option{:value => "commits"} Commits %h3.page_title Building repository graph. Please wait a moment.
%option{:value => "additions"} Additions
%option{:value => "deletions"} Deletions
%h3#date_header
%input#brush_change{:type => "hidden"}
.graphs .stat-graph
#contributors-master .header.clearfix
#contributors.clearfix .right
%ol.contributors-list.clearfix %select
%option{:value => "commits"} Commits
%option{:value => "additions"} Additions
%option{:value => "deletions"} Deletions
%h3#date_header
%input#brush_change{:type => "hidden"}
.graphs
#contributors-master
#contributors.clearfix
%ol.contributors-list.clearfix
:javascript :javascript
controller = new ContributorsStatGraph $(".stat-graph").hide();
controller.init(#{@log})
$("select").change( function () { $.ajax({
var field = $(this).val() type: "GET",
controller.set_current_field(field) url: location.href,
controller.redraw_master() complete: function() {
controller.redraw_authors() $(".loading-graph").hide();
}) $(".stat-graph").show();
},
$("#brush_change").change( function () { dataType: "script"
controller.change_date_header() });
controller.redraw_authors()
})

View file

@ -0,0 +1,16 @@
:plain
controller = new ContributorsStatGraph
controller.init(#{@log})
$("select").change( function () {
var field = $(this).val()
controller.set_current_field(field)
controller.redraw_master()
controller.redraw_authors()
})
$("#brush_change").change( function () {
controller.change_date_header()
controller.redraw_authors()
})

View file

@ -0,0 +1,9 @@
Feature: Project Graph
Background:
Given I sign in as a user
And I own project "Shop"
And I visit project "Shop" graph page
@javascript
Scenario: I should see project graphs
Then page should have graphs

View file

@ -0,0 +1,13 @@
class ProjectGraph < Spinach::FeatureSteps
include SharedAuthentication
include SharedProject
Then 'page should have graphs' do
page.should have_selector ".stat-graph"
end
When 'I visit project "Shop" graph page' do
project = Project.find_by_name("Shop")
visit project_stat_graph_path(project, "master")
end
end

View file

@ -1,20 +0,0 @@
require 'gitlab/git_stats_log_parser'
module Gitlab
class GitStats
attr_accessor :repo, :ref
def initialize repo, ref
@repo, @ref = repo, ref
end
def log
args = ['--format=%aN%x0a%ad', '--date=short', '--shortstat', '--no-merges']
repo.git.run(nil, 'log', nil, {}, args)
end
def parsed_log
LogParser.parse_log(log)
end
end
end

View file

@ -1,32 +0,0 @@
class LogParser
#Parses the log file into a collection of commits
#Data model: {author, date, additions, deletions}
def self.parse_log log_from_git
log = log_from_git.split("\n")
i = 0
collection = []
entry = {}
while i <= log.size do
pos = i % 4
case pos
when 0
unless i == 0
collection.push(entry)
entry = {}
end
entry[:author] = log[i].to_s
when 1
entry[:date] = log[i].to_s
when 3
changes = log[i].split(",")
entry[:additions] = changes[1].to_i unless changes[1].nil?
entry[:deletions] = changes[2].to_i unless changes[2].nil?
end
i += 1
end
collection
end
end

View file

@ -1,37 +0,0 @@
require 'spec_helper'
require 'gitlab/git_stats_log_parser'
describe LogParser do
describe "#self.parse_log" do
context "log_from_git is a valid log" do
it "returns the correct log" do
fake_log = "Karlo Soriano
2013-05-09
14 files changed, 471 insertions(+)
Dmitriy Zaporozhets
2013-05-08
1 file changed, 6 insertions(+), 1 deletion(-)
Dmitriy Zaporozhets
2013-05-08
6 files changed, 19 insertions(+), 3 deletions(-)
Dmitriy Zaporozhets
2013-05-08
3 files changed, 29 insertions(+), 3 deletions(-)";
lp = LogParser.parse_log(fake_log)
lp.should eq([
{author: "Karlo Soriano", date: "2013-05-09", additions: 471},
{author: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 6, deletions: 1},
{author: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 19, deletions: 3},
{author: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 29, deletions: 3}])
end
end
end
end

View file

@ -1,36 +0,0 @@
require 'spec_helper'
describe Gitlab::GitStats do
describe "#parsed_log" do
let(:stats) { Gitlab::GitStats.new(nil, nil) }
before(:each) do
stats.stub(:log).and_return("anything")
end
context "LogParser#parse_log returns 'test'" do
it "returns 'test'" do
LogParser.stub(:parse_log).and_return("test")
stats.parsed_log.should eq("test")
end
end
end
describe "#log" do
let(:repo) { Repository.new(nil, nil) }
let(:gs) { Gitlab::GitStats.new(repo.raw, repo.root_ref) }
before(:each) do
repo.stub(:raw).and_return(nil)
repo.stub(:root_ref).and_return(nil)
repo.raw.stub(:git)
end
context "repo.git.run returns 'test'" do
it "returns 'test'" do
repo.raw.git.stub(:run).and_return("test")
gs.log.should eq("test")
end
end
end
end