Commit 96c627ed authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #3169 from hiroponz/refactor-network-graph

Refactor network graph
parents 8e8372d5 c0d312be
...@@ -117,59 +117,57 @@ ...@@ -117,59 +117,57 @@
// Draw lines // Draw lines
for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) { for (var j = 0, jj = this.commits[i].parents.length; j < jj; j++) {
c = this.preparedCommits[this.commits[i].parents[j][0]]; c = this.preparedCommits[this.commits[i].parents[j][0]];
ps = this.commits[i].parent_spaces[j]; ps = this.commits[i].parents[j][1];
if (c) { var cx = offsetX + 20 * c.time
var cx = offsetX + 20 * c.time , cy = offsetY + 10 * c.space
, cy = offsetY + 10 * c.space , psy = offsetY + 10 * ps;
, psy = offsetY + 10 * ps; if (c.space == this.commits[i].space && c.space == ps) {
if (c.space == this.commits[i].space && c.space == ps) { r.path([
r.path([ "M", x, y,
"M", x, y, "L", cx, cy
"L", cx, cy ]).attr({
]).attr({ stroke: this.colors[c.space],
stroke: this.colors[c.space], "stroke-width": 2
"stroke-width": 2 });
});
} else if (c.space < this.commits[i].space) { } else if (c.space < this.commits[i].space) {
if (y == psy) { if (y == psy) {
r.path([ r.path([
"M", x - 5, y, "M", x - 5, y,
"l-5,-2,0,4,5,-2", "l-5,-2,0,4,5,-2",
"L", x - 10, y, "L", x - 10, y,
"L", x - 15, psy, "L", x - 15, psy,
"L", cx + 5, psy, "L", cx + 5, psy,
"L", cx, cy]) "L", cx, cy])
.attr({ .attr({
stroke: this.colors[this.commits[i].space], stroke: this.colors[this.commits[i].space],
"stroke-width": 2 "stroke-width": 2
}); });
} else {
r.path([
"M", x - 3, y - 6,
"l-4,-3,4,-2,0,5",
"L", x - 5, y - 10,
"L", x - 10, psy,
"L", cx + 5, psy,
"L", cx, cy])
.attr({
stroke: this.colors[this.commits[i].space],
"stroke-width": 2
});
}
} else { } else {
r.path([ r.path([
"M", x - 3, y + 6, "M", x - 3, y - 6,
"l-4,3,4,2,0,-5", "l-4,-3,4,-2,0,5",
"L", x - 5, y + 10, "L", x - 5, y - 10,
"L", x - 10, psy, "L", x - 10, psy,
"L", cx + 5, psy, "L", cx + 5, psy,
"L", cx, cy]) "L", cx, cy])
.attr({ .attr({
stroke: this.colors[c.space], stroke: this.colors[this.commits[i].space],
"stroke-width": 2 "stroke-width": 2
}); });
} }
} else {
r.path([
"M", x - 3, y + 6,
"l-4,3,4,2,0,-5",
"L", x - 5, y + 10,
"L", x - 10, psy,
"L", cx + 5, psy,
"L", cx, cy])
.attr({
stroke: this.colors[c.space],
"stroke-width": 2
});
} }
} }
......
...@@ -8,24 +8,21 @@ class GraphController < ProjectResourceController ...@@ -8,24 +8,21 @@ class GraphController < ProjectResourceController
before_filter :require_non_empty_project before_filter :require_non_empty_project
def show def show
if params.has_key?(:q) && params[:q].blank?
redirect_to project_graph_path(@project, params[:id])
return
end
if params.has_key?(:q) if params.has_key?(:q)
if params[:q].blank?
redirect_to project_graph_path(@project, params[:id])
return
end
@q = params[:q] @q = params[:q]
@commit = @project.repository.commit(@q) || @commit @commit = @project.repository.commit(@q) || @commit
end end
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json do
graph = Graph::JsonBuilder.new(project, @ref, @commit) @graph = Network::Graph.new(project, @ref, @commit)
graph.commits.each do |c|
c.icon = gravatar_icon(c.author.email)
end
render :json => graph.to_json
end end
end end
end end
......
module GraphHelper
def join_with_space(ary)
ary.collect{|r|r.name}.join(" ") unless ary.nil?
end
def parents_zip_spaces(parents, parent_spaces)
ids = parents.map { |p| p.id }
ids.zip(parent_spaces)
end
end
require "grit"
module Graph
class Commit
include ActionView::Helpers::TagHelper
attr_accessor :time, :spaces, :refs, :parent_spaces, :icon
def initialize(commit)
@_commit = commit
@time = -1
@spaces = []
@parent_spaces = []
end
def method_missing(m, *args, &block)
@_commit.send(m, *args, &block)
end
def to_graph_hash
h = {}
h[:parents] = self.parents.collect do |p|
[p.id,0,0]
end
h[:author] = {
name: author.name,
email: author.email,
icon: icon
}
h[:time] = time
h[:space] = spaces.first
h[:parent_spaces] = parent_spaces
h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil?
h[:id] = sha
h[:date] = date
h[:message] = message
h
end
def add_refs(ref_cache, repo)
if ref_cache.empty?
repo.refs.each do |ref|
ref_cache[ref.commit.id] ||= []
ref_cache[ref.commit.id] << ref
end
end
@refs = ref_cache[@_commit.id] if ref_cache.include?(@_commit.id)
@refs ||= []
end
def space
if @spaces.size > 0
@spaces.first
else
0
end
end
end
end
require "grit"
module Network
class Commit
include ActionView::Helpers::TagHelper
attr_reader :refs
attr_accessor :time, :spaces, :parent_spaces
def initialize(raw_commit, refs)
@commit = ::Commit.new(raw_commit)
@time = -1
@spaces = []
@parent_spaces = []
@refs = refs || []
end
def method_missing(m, *args, &block)
@commit.send(m, *args, &block)
end
def space
if @spaces.size > 0
@spaces.first
else
0
end
end
def parents(map)
@commit.parents.map do |p|
if map.include?(p.id)
map[p.id]
end
end
.compact
end
end
end
require "grit" require "grit"
module Graph module Network
class JsonBuilder class Graph
attr_accessor :days, :commits, :ref_cache, :repo attr_reader :days, :commits, :map
def self.max_count def self.max_count
@max_count ||= 650 @max_count ||= 650
...@@ -13,80 +13,68 @@ module Graph ...@@ -13,80 +13,68 @@ module Graph
@ref = ref @ref = ref
@commit = commit @commit = commit
@repo = project.repo @repo = project.repo
@ref_cache = {}
@commits = collect_commits @commits = collect_commits
@days = index_commits @days = index_commits
end end
def to_json(*args) protected
{
days: @days.compact.map { |d| [d.day, d.strftime("%b")] },
commits: @commits.map(&:to_graph_hash)
}.to_json(*args)
end
protected
# Get commits from repository # Get commits from repository
# #
def collect_commits def collect_commits
refs_cache = build_refs_cache
@commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
Grit::Commit.find_all(
# Decorate with app/models/commit.rb @repo,
@commits.map! { |commit| Commit.new(commit) } nil,
{
# Decorate with lib/gitlab/graph/commit.rb date_order: true,
@commits.map! { |commit| Graph::Commit.new(commit) } max_count: self.class.max_count,
skip: count_to_display_commit_in_center
# add refs to each commit }
@commits.each { |commit| commit.add_refs(ref_cache, repo) } )
.map do |commit|
@commits # Decorate with app/model/network/commit.rb
Network::Commit.new(commit, refs_cache[commit.id])
end
end end
# Method is adding time and space on the # Method is adding time and space on the
# list of commits. As well as returns date list # list of commits. As well as returns date list
# corelated with time set on commits. # corelated with time set on commits.
# #
# @param [Array<Graph::Commit>] commits to index
#
# @return [Array<TimeDate>] list of commit dates corelated with time on commits # @return [Array<TimeDate>] list of commit dates corelated with time on commits
def index_commits def index_commits
days, times = [], [] days = []
map = {} @map = {}
commits.reverse.each_with_index do |c,i| @commits.reverse.each_with_index do |c,i|
c.time = i c.time = i
days[i] = c.committed_date days[i] = c.committed_date
map[c.id] = c @map[c.id] = c
times[i] = c
end end
@_reserved = {} @reserved = {}
days.each_index do |i| days.each_index do |i|
@_reserved[i] = [] @reserved[i] = []
end end
commits_sort_by_ref.each do |commit| commits_sort_by_ref.each do |commit|
if map.include? commit.id then place_chain(commit)
place_chain(map[commit.id], map)
end
end end
# find parent spaces for not overlap lines # find parent spaces for not overlap lines
times.each do |c| @commits.each do |c|
c.parent_spaces.concat(find_free_parent_spaces(c, map, times)) c.parent_spaces.concat(find_free_parent_spaces(c))
end end
days days
end end
# Skip count that the target commit is displayed in center. # Skip count that the target commit is displayed in center.
def to_commit def count_to_display_commit_in_center
commits = Grit::Commit.find_all(repo, nil, {date_order: true}) commit_index = Grit::Commit.find_all(@repo, nil, {date_order: true}).index do |c|
commit_index = commits.index do |c|
c.id == @commit.id c.id == @commit.id
end end
...@@ -99,7 +87,7 @@ module Graph ...@@ -99,7 +87,7 @@ module Graph
end end
def commits_sort_by_ref def commits_sort_by_ref
commits.sort do |a,b| @commits.sort do |a,b|
if include_ref?(a) if include_ref?(a)
-1 -1
elsif include_ref?(b) elsif include_ref?(b)
...@@ -122,46 +110,42 @@ module Graph ...@@ -122,46 +110,42 @@ module Graph
heads.include?(@ref) heads.include?(@ref)
end end
def find_free_parent_spaces(commit, map, times) def find_free_parent_spaces(commit)
spaces = [] spaces = []
commit.parents.each do |p| commit.parents(@map).each do |parent|
if map.include?(p.id) then range = if commit.time < parent.time then
parent = map[p.id] commit.time..parent.time
else
range = if commit.time < parent.time then parent.time..commit.time
commit.time..parent.time end
else
parent.time..commit.time space = if commit.space >= parent.space then
end find_free_parent_space(range, parent.space, -1, commit.space)
else
space = if commit.space >= parent.space then find_free_parent_space(range, commit.space, -1, parent.space)
find_free_parent_space(range, parent.space, -1, commit.space, times) end
else
find_free_parent_space(range, commit.space, -1, parent.space, times) mark_reserved(range, space)
end spaces << space
mark_reserved(range, space)
spaces << space
end
end end
spaces spaces
end end
def find_free_parent_space(range, space_base, space_step, space_default, times) def find_free_parent_space(range, space_base, space_step, space_default)
if is_overlap?(range, times, space_default) then if is_overlap?(range, space_default) then
find_free_space(range, space_step, space_base, space_default) find_free_space(range, space_step, space_base, space_default)
else else
space_default space_default
end end
end end
def is_overlap?(range, times, overlap_space) def is_overlap?(range, overlap_space)
range.each do |i| range.each do |i|
if i != range.first && if i != range.first &&
i != range.last && i != range.last &&
times[i].spaces.include?(overlap_space) then @commits[reversed_index(i)].spaces.include?(overlap_space) then
return true; return true;
end end
...@@ -172,39 +156,31 @@ module Graph ...@@ -172,39 +156,31 @@ module Graph
# Add space mark on commit and its parents # Add space mark on commit and its parents
# #
# @param [Graph::Commit] the commit object. # @param [::Commit] the commit object.
# @param [Hash<String,Graph::Commit>] map of commits def place_chain(commit, parent_time = nil)
def place_chain(commit, map, parent_time = nil) leaves = take_left_leaves(commit)
leaves = take_left_leaves(commit, map)
if leaves.empty? if leaves.empty?
return return
end end
time_range = leaves.last.time..leaves.first.time time_range = leaves.last.time..leaves.first.time
space_base = get_space_base(leaves, map) space_base = get_space_base(leaves)
space = find_free_space(time_range, 2, space_base) space = find_free_space(time_range, 2, space_base)
leaves.each do |l| leaves.each do |l|
l.spaces << space l.spaces << space
# Also add space to parent # Also add space to parent
l.parents.each do |p| l.parents(@map).each do |parent|
if map.include?(p.id) if parent.space > 0
parent = map[p.id] parent.spaces << space
if parent.space > 0
parent.spaces << space
end
end end
end end
end end
# and mark it as reserved # and mark it as reserved
min_time = leaves.last.time min_time = leaves.last.time
parents = leaves.last.parents.collect leaves.last.parents(@map).each do |parent|
parents.each do |p| if parent.time < min_time
if map.include? p.id min_time = parent.time
parent = map[p.id]
if parent.time < min_time
min_time = parent.time
end
end end
end end
...@@ -217,22 +193,19 @@ module Graph ...@@ -217,22 +193,19 @@ module Graph
# Visit branching chains # Visit branching chains
leaves.each do |l| leaves.each do |l|
parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?} parents = l.parents(@map).select{|p| p.space.zero?}
for p in parents for p in parents
place_chain(map[p.id], map, l.time) place_chain(p, l.time)
end end
end end
end end
def get_space_base(leaves, map) def get_space_base(leaves)
space_base = 1 space_base = 1
if leaves.last.parents.size > 0 parents = leaves.last.parents(@map)
first_parent = leaves.last.parents.first if parents.size > 0
if map.include?(first_parent.id) if parents.first.space > 0
first_p = map[first_parent.id] space_base = parents.first.space
if first_p.space > 0
space_base = first_p.space
end
end end
end end
space_base space_base
...@@ -240,7 +213,7 @@ module Graph ...@@ -240,7 +213,7 @@ module Graph
def mark_reserved(time_range, space) def mark_reserved(time_range, space)
for day in time_range for day in time_range
@_reserved[day].push(space) @reserved[day].push(space)
end end
end end
...@@ -249,7 +222,7 @@ module Graph ...@@ -249,7 +222,7 @@ module Graph
reserved = [] reserved = []
for day in time_range for day in time_range
reserved += @_reserved[day] reserved += @reserved[day]
end end
reserved.uniq! reserved.uniq!
...@@ -268,24 +241,36 @@ module Graph ...@@ -268,24 +241,36 @@ module Graph
# Takes most left subtree branch of commits # Takes most left subtree branch of commits
# which don't have space mark yet. # which don't have space mark yet.
# #
# @param [Graph::Commit] the commit object. # @param [::Commit] the commit object.
# @param [Hash<String,Graph::Commit>] map of commits
# #
# @return [Array<Graph::Commit>] list of branch commits # @return [Array<Network::Commit>] list of branch commits
def take_left_leaves(commit, map) def take_left_leaves(raw_commit)
commit = @map[raw_commit.id]
leaves = [] leaves = []
leaves.push(commit) if commit.space.zero? leaves.push(commit) if commit.space.zero?
while true while true
return leaves if commit.parents.count.zero? return leaves if commit.parents(@map).count.zero?
return leaves unless map.include? commit.parents.first.id
commit = map[commit.parents.first.id] commit = commit.parents(@map).first
return leaves unless commit.space.zero? return leaves unless commit.space.zero?
leaves.push(commit) leaves.push(commit)
end end
end end
def build_refs_cache
refs_cache = {}
@repo.refs.each do |ref|
refs_cache[ref.commit.id] = [] unless refs_cache.include?(ref.commit.id)
refs_cache[ref.commit.id] << ref
end
refs_cache
end
def reversed_index(index)
-index - 1
end
end end
end end
<% self.formats = ["html"] %>
<%= raw(
{
days: @graph.days.compact.map { |d| [d.day, d.strftime("%b")] },
commits: @graph.commits.map do |c|
{
parents: parents_zip_spaces(c.parents(@graph.map), c.parent_spaces),
author: {
name: c.author.name,
email: c.author.email,
icon: gravatar_icon(c.author.email, 20)
},
time: c.time,
space: c.spaces.first,
refs: join_with_space(c.refs),
id: c.sha,
date: c.date,
message: c.message,
}
end
}.to_json
) %>
...@@ -8,8 +8,8 @@ class ProjectNetworkGraph < Spinach::FeatureSteps ...@@ -8,8 +8,8 @@ class ProjectNetworkGraph < Spinach::FeatureSteps
end end
When 'I visit project "Shop" network page' do When 'I visit project "Shop" network page' do
# Stub Graph::JsonBuilder max_size to speed up test (10 commits vs. 650) # Stub Graph max_size to speed up test (10 commits vs. 650)
Graph::JsonBuilder.stub(max_count: 10) Network::Graph.stub(max_count: 10)
project = Project.find_by_name("Shop") project = Project.find_by_name("Shop")
visit project_graph_path(project, "master") visit project_graph_path(project, "master")
...@@ -25,7 +25,7 @@ class ProjectNetworkGraph < Spinach::FeatureSteps ...@@ -25,7 +25,7 @@ class ProjectNetworkGraph < Spinach::FeatureSteps
end end
end end
And 'I switch ref to "stable"' do When 'I switch ref to "stable"' do
page.select 'stable', :from => 'ref' page.select 'stable', :from => 'ref'
sleep 2 sleep 2
end end
...@@ -40,7 +40,7 @@ class ProjectNetworkGraph < Spinach::FeatureSteps ...@@ -40,7 +40,7 @@ class ProjectNetworkGraph < Spinach::FeatureSteps
end end
end end
And 'I looking for a commit by SHA of "v2.1.0"' do When 'I looking for a commit by SHA of "v2.1.0"' do
within ".content .search" do within ".content .search" do
fill_in 'q', :with => '98d6492' fill_in 'q', :with => '98d6492'
find('button').click find('button').click
......
...@@ -142,8 +142,8 @@ module SharedPaths ...@@ -142,8 +142,8 @@ module SharedPaths
end end
Given "I visit my project's network page" do Given "I visit my project's network page" do
# Stub Graph::JsonBuilder max_size to speed up test (10 commits vs. 650) # Stub Graph max_size to speed up test (10 commits vs. 650)
Graph::JsonBuilder.stub(max_count: 10) Network::Graph.stub(max_count: 10)
visit project_graph_path(@project, root_ref) visit project_graph_path(@project, root_ref)
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