OSDN Git Service

Its better to load graph with ajax since it requires ~10 seconds for bigger projects...
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 5 Jun 2013 12:50:11 +0000 (15:50 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 5 Jun 2013 12:50:11 +0000 (15:50 +0300)
Gemfile
Gemfile.lock
app/controllers/stat_graph_controller.rb
app/views/stat_graph/show.html.haml
app/views/stat_graph/show.js.haml [new file with mode: 0644]
features/project/graph.feature [new file with mode: 0644]
features/steps/project/project_graph.rb [new file with mode: 0644]
lib/gitlab/git_stats.rb [deleted file]
lib/gitlab/git_stats_log_parser.rb [deleted file]
spec/lib/gitlab/git_stats_log_parser_spec.rb [deleted file]
spec/lib/gitlab/git_stats_spec.rb [deleted file]

diff --git a/Gemfile b/Gemfile
index a242ea2..855959f 100644 (file)
--- 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
index c14d647..734c515 100644 (file)
@@ -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
index 2a74409..f076f47 100644 (file)
@@ -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
index b7b2738..21af115 100644 (file)
@@ -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 (file)
index 0000000..b7c9b41
--- /dev/null
@@ -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 (file)
index 0000000..cda95f5
--- /dev/null
@@ -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 (file)
index 0000000..489485b
--- /dev/null
@@ -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 (file)
index 1f303f6..0000000
+++ /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 (file)
index 784e08c..0000000
+++ /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 (file)
index c97b322..0000000
+++ /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 (file)
index f9c70da..0000000
+++ /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