##// END OF EJS Templates
Fixed: nil error on 'commits per month' graph....
Jean-Philippe Lang -
r639:5b35bfd4344e
parent child
Show More
@@ -1,231 +1,232
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'SVG/Graph/Bar'
19 19 require 'SVG/Graph/BarHorizontal'
20 20 require 'digest/sha1'
21 21
22 22 class RepositoriesController < ApplicationController
23 23 layout 'base'
24 24 before_filter :find_project, :except => [:update_form]
25 25 before_filter :authorize, :except => [:update_form, :stats, :graph]
26 26 before_filter :check_project_privacy, :only => [:stats, :graph]
27 27
28 28 def show
29 29 # check if new revisions have been committed in the repository
30 30 @repository.fetch_changesets if Setting.autofetch_changesets?
31 31 # get entries for the browse frame
32 32 @entries = @repository.entries('')
33 33 # latest changesets
34 34 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
35 35 show_error and return unless @entries || @changesets.any?
36 36 end
37 37
38 38 def browse
39 39 @entries = @repository.entries(@path, @rev)
40 40 show_error and return unless @entries
41 41 end
42 42
43 43 def changes
44 44 @entry = @repository.scm.entry(@path, @rev)
45 45 show_error and return unless @entry
46 46 @changes = Change.find(:all, :include => :changeset,
47 47 :conditions => ["repository_id = ? AND path = ?", @repository.id, @path.with_leading_slash],
48 48 :order => "committed_on DESC")
49 49 end
50 50
51 51 def revisions
52 52 @changeset_count = @repository.changesets.count
53 53 @changeset_pages = Paginator.new self, @changeset_count,
54 54 25,
55 55 params['page']
56 56 @changesets = @repository.changesets.find(:all,
57 57 :limit => @changeset_pages.items_per_page,
58 58 :offset => @changeset_pages.current.offset)
59 59
60 60 render :action => "revisions", :layout => false if request.xhr?
61 61 end
62 62
63 63 def entry
64 64 @content = @repository.scm.cat(@path, @rev)
65 65 show_error and return unless @content
66 66 if 'raw' == params[:format]
67 67 send_data @content, :filename => @path.split('/').last
68 68 end
69 69 end
70 70
71 71 def revision
72 72 @changeset = @repository.changesets.find_by_revision(@rev)
73 73 show_error and return unless @changeset
74 74 @changes_count = @changeset.changes.size
75 75 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
76 76 @changes = @changeset.changes.find(:all,
77 77 :limit => @changes_pages.items_per_page,
78 78 :offset => @changes_pages.current.offset)
79 79
80 80 render :action => "revision", :layout => false if request.xhr?
81 81 end
82 82
83 83 def diff
84 84 @rev_to = params[:rev_to] ? params[:rev_to].to_i : (@rev - 1)
85 85 @diff_type = ('sbs' == params[:type]) ? 'sbs' : 'inline'
86 86
87 87 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
88 88 unless read_fragment(@cache_key)
89 89 @diff = @repository.diff(@path, @rev, @rev_to, type)
90 90 show_error and return unless @diff
91 91 end
92 92 end
93 93
94 94 def stats
95 95 end
96 96
97 97 def graph
98 98 data = nil
99 99 case params[:graph]
100 100 when "commits_per_month"
101 101 data = graph_commits_per_month(@repository)
102 102 when "commits_per_author"
103 103 data = graph_commits_per_author(@repository)
104 104 end
105 105 if data
106 106 headers["Content-Type"] = "image/svg+xml"
107 107 send_data(data, :type => "image/svg+xml", :disposition => "inline")
108 108 else
109 109 render_404
110 110 end
111 111 end
112 112
113 113 def update_form
114 114 @repository = Repository.factory(params[:repository_scm])
115 115 render :partial => 'projects/repository', :locals => {:repository => @repository}
116 116 end
117 117
118 118 private
119 119 def find_project
120 120 @project = Project.find(params[:id])
121 121 @repository = @project.repository
122 122 render_404 and return false unless @repository
123 123 @path = params[:path].squeeze('/') if params[:path]
124 124 @path ||= ''
125 125 @rev = params[:rev].to_i if params[:rev]
126 126 rescue ActiveRecord::RecordNotFound
127 127 render_404
128 128 end
129 129
130 130 def show_error
131 131 flash.now[:error] = l(:notice_scm_error)
132 132 render :nothing => true, :layout => true
133 133 end
134 134
135 135 def graph_commits_per_month(repository)
136 136 @date_to = Date.today
137 @date_from = @date_to << 12
137 @date_from = @date_to << 11
138 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
138 139 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
139 140 commits_by_month = [0] * 12
140 141 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
141 142
142 changes_by_day = repository.changes.count(:all, :group => :commit_date)
143 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
143 144 changes_by_month = [0] * 12
144 145 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
145 146
146 147 fields = []
147 148 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
148 149 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
149 150
150 151 graph = SVG::Graph::Bar.new(
151 152 :height => 300,
152 153 :width => 500,
153 154 :fields => fields.reverse,
154 155 :stack => :side,
155 156 :scale_integers => true,
156 157 :step_x_labels => 2,
157 158 :show_data_values => false,
158 159 :graph_title => l(:label_commits_per_month),
159 160 :show_graph_title => true
160 161 )
161 162
162 163 graph.add_data(
163 164 :data => commits_by_month[0..11].reverse,
164 165 :title => l(:label_revision_plural)
165 166 )
166 167
167 168 graph.add_data(
168 169 :data => changes_by_month[0..11].reverse,
169 170 :title => l(:label_change_plural)
170 171 )
171 172
172 173 graph.burn
173 174 end
174 175
175 176 def graph_commits_per_author(repository)
176 177 commits_by_author = repository.changesets.count(:all, :group => :committer)
177 178 commits_by_author.sort! {|x, y| x.last <=> y.last}
178 179
179 180 changes_by_author = repository.changes.count(:all, :group => :committer)
180 181 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
181 182
182 183 fields = commits_by_author.collect {|r| r.first}
183 184 commits_data = commits_by_author.collect {|r| r.last}
184 185 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
185 186
186 187 fields = fields + [""]*(10 - fields.length) if fields.length<10
187 188 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
188 189 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
189 190
190 191 graph = SVG::Graph::BarHorizontal.new(
191 192 :height => 300,
192 193 :width => 500,
193 194 :fields => fields,
194 195 :stack => :side,
195 196 :scale_integers => true,
196 197 :show_data_values => false,
197 198 :rotate_y_labels => false,
198 199 :graph_title => l(:label_commits_per_author),
199 200 :show_graph_title => true
200 201 )
201 202
202 203 graph.add_data(
203 204 :data => commits_data,
204 205 :title => l(:label_revision_plural)
205 206 )
206 207
207 208 graph.add_data(
208 209 :data => changes_data,
209 210 :title => l(:label_change_plural)
210 211 )
211 212
212 213 graph.burn
213 214 end
214 215
215 216 end
216 217
217 218 class Date
218 219 def months_ago(date = Date.today)
219 220 (date.year - self.year)*12 + (date.month - self.month)
220 221 end
221 222
222 223 def weeks_ago(date = Date.today)
223 224 (date.year - self.year)*52 + (date.cweek - self.cweek)
224 225 end
225 226 end
226 227
227 228 class String
228 229 def with_leading_slash
229 230 starts_with?('/') ? self : "/#{self}"
230 231 end
231 232 end
General Comments 0
You need to be logged in to leave comments. Login now