From f1e0d37b15914ce7f17a612c8afb4452a501ecd7 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 5 Jun 2013 15:50:11 +0300 Subject: [PATCH] Its better to load graph with ajax since it requires ~10 seconds for bigger projects to finish up --- Gemfile | 4 +-- Gemfile.lock | 4 +-- app/controllers/stat_graph_controller.rb | 17 +++++---- app/views/stat_graph/show.html.haml | 52 +++++++++++++++------------- app/views/stat_graph/show.js.haml | 16 +++++++++ features/project/graph.feature | 9 +++++ features/steps/project/project_graph.rb | 13 +++++++ lib/gitlab/git_stats.rb | 20 ----------- lib/gitlab/git_stats_log_parser.rb | 32 ----------------- spec/lib/gitlab/git_stats_log_parser_spec.rb | 37 -------------------- spec/lib/gitlab/git_stats_spec.rb | 36 ------------------- 11 files changed, 79 insertions(+), 161 deletions(-) create mode 100644 app/views/stat_graph/show.js.haml create mode 100644 features/project/graph.feature create mode 100644 features/steps/project/project_graph.rb delete mode 100644 lib/gitlab/git_stats.rb delete mode 100644 lib/gitlab/git_stats_log_parser.rb delete mode 100644 spec/lib/gitlab/git_stats_log_parser_spec.rb delete mode 100644 spec/lib/gitlab/git_stats_spec.rb diff --git a/Gemfile b/Gemfile index a242ea2db..855959f93 100644 --- a/Gemfile +++ b/Gemfile @@ -23,7 +23,7 @@ gem 'omniauth-github' # Extracting information from a git repository # 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 gem 'gitlab-grack', '~> 1.0.0', require: 'grack' @@ -183,7 +183,7 @@ group :development, :test do gem 'poltergeist', '~> 1.3.0' gem 'spork', '~> 1.0rc' - gem 'jasmine' + gem 'jasmine' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index c14d64748..734c515b5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -164,7 +164,7 @@ GEM gitlab-pygments.rb (0.3.2) posix-spawn (~> 0.3.6) yajl-ruby (~> 1.1.0) - gitlab_git (1.2.1) + gitlab_git (1.3.0) activesupport (~> 3.2.13) github-linguist (~> 2.3.4) gitlab-grit (~> 2.5.1) @@ -543,7 +543,7 @@ DEPENDENCIES gitlab-gollum-lib (~> 1.0.0) gitlab-grack (~> 1.0.0) gitlab-pygments.rb (~> 0.3.2) - gitlab_git (~> 1.2.1) + gitlab_git (~> 1.3.0) gitlab_meta (= 5.0) gitlab_omniauth-ldap (= 1.0.2) gon diff --git a/app/controllers/stat_graph_controller.rb b/app/controllers/stat_graph_controller.rb index 2a7440980..f076f4726 100644 --- a/app/controllers/stat_graph_controller.rb +++ b/app/controllers/stat_graph_controller.rb @@ -1,14 +1,17 @@ class StatGraphController < ProjectResourceController - # Authorize before_filter :authorize_read_project! before_filter :authorize_code_access! before_filter :require_non_empty_project - + def show - @repo = @project.repository - @stats = Gitlab::GitStats.new(@repo.raw, @repo.root_ref) - @log = @stats.parsed_log.to_json + respond_to do |format| + format.html + 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 \ No newline at end of file +end diff --git a/app/views/stat_graph/show.html.haml b/app/views/stat_graph/show.html.haml index b7b27387c..21af1153f 100644 --- a/app/views/stat_graph/show.html.haml +++ b/app/views/stat_graph/show.html.haml @@ -1,29 +1,31 @@ -.header.clearfix - .right - %select - %option{:value => "commits"} Commits - %option{:value => "additions"} Additions - %option{:value => "deletions"} Deletions - %h3#date_header - %input#brush_change{:type => "hidden"} +.loading-graph + %center + .loading + %h3.page_title Building repository graph. Please wait a moment. -.graphs - #contributors-master - #contributors.clearfix - %ol.contributors-list.clearfix +.stat-graph + .header.clearfix + .right + %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 - controller = new ContributorsStatGraph - controller.init(#{@log}) + $(".stat-graph").hide(); - $("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() - }) \ No newline at end of file + $.ajax({ + type: "GET", + url: location.href, + complete: function() { + $(".loading-graph").hide(); + $(".stat-graph").show(); + }, + dataType: "script" + }); diff --git a/app/views/stat_graph/show.js.haml b/app/views/stat_graph/show.js.haml new file mode 100644 index 000000000..b7c9b4113 --- /dev/null +++ b/app/views/stat_graph/show.js.haml @@ -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() + }) + diff --git a/features/project/graph.feature b/features/project/graph.feature new file mode 100644 index 000000000..cda95f5dd --- /dev/null +++ b/features/project/graph.feature @@ -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 diff --git a/features/steps/project/project_graph.rb b/features/steps/project/project_graph.rb new file mode 100644 index 000000000..489485b1d --- /dev/null +++ b/features/steps/project/project_graph.rb @@ -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 diff --git a/lib/gitlab/git_stats.rb b/lib/gitlab/git_stats.rb deleted file mode 100644 index 1f303f6de..000000000 --- a/lib/gitlab/git_stats.rb +++ /dev/null @@ -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 diff --git a/lib/gitlab/git_stats_log_parser.rb b/lib/gitlab/git_stats_log_parser.rb deleted file mode 100644 index 784e08c27..000000000 --- a/lib/gitlab/git_stats_log_parser.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/spec/lib/gitlab/git_stats_log_parser_spec.rb b/spec/lib/gitlab/git_stats_log_parser_spec.rb deleted file mode 100644 index c97b32275..000000000 --- a/spec/lib/gitlab/git_stats_log_parser_spec.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/spec/lib/gitlab/git_stats_spec.rb b/spec/lib/gitlab/git_stats_spec.rb deleted file mode 100644 index f9c70da2b..000000000 --- a/spec/lib/gitlab/git_stats_spec.rb +++ /dev/null @@ -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 \ No newline at end of file -- 2.11.0