##// END OF EJS Templates
Wider SVG graphs in repository stats....
Jean-Philippe Lang -
r1587:94cf4f258ff6
parent child
Show More
@@ -1,313 +1,313
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 ChangesetNotFound < Exception; end
23 23 class InvalidRevisionParam < Exception; end
24 24
25 25 class RepositoriesController < ApplicationController
26 26 layout 'base'
27 27 menu_item :repository
28 28 before_filter :find_repository, :except => :edit
29 29 before_filter :find_project, :only => :edit
30 30 before_filter :authorize
31 31 accept_key_auth :revisions
32 32
33 33 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
34 34
35 35 def edit
36 36 @repository = @project.repository
37 37 if !@repository
38 38 @repository = Repository.factory(params[:repository_scm])
39 39 @repository.project = @project if @repository
40 40 end
41 41 if request.post? && @repository
42 42 @repository.attributes = params[:repository]
43 43 @repository.save
44 44 end
45 45 render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
46 46 end
47 47
48 48 def destroy
49 49 @repository.destroy
50 50 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
51 51 end
52 52
53 53 def show
54 54 # check if new revisions have been committed in the repository
55 55 @repository.fetch_changesets if Setting.autofetch_changesets?
56 56 # root entries
57 57 @entries = @repository.entries('', @rev)
58 58 # latest changesets
59 59 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
60 60 show_error_not_found unless @entries || @changesets.any?
61 61 end
62 62
63 63 def browse
64 64 @entries = @repository.entries(@path, @rev)
65 65 if request.xhr?
66 66 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
67 67 else
68 68 show_error_not_found and return unless @entries
69 69 render :action => 'browse'
70 70 end
71 71 end
72 72
73 73 def changes
74 74 @entry = @repository.entry(@path, @rev)
75 75 show_error_not_found and return unless @entry
76 76 @changesets = @repository.changesets_for_path(@path)
77 77 end
78 78
79 79 def revisions
80 80 @changeset_count = @repository.changesets.count
81 81 @changeset_pages = Paginator.new self, @changeset_count,
82 82 per_page_option,
83 83 params['page']
84 84 @changesets = @repository.changesets.find(:all,
85 85 :limit => @changeset_pages.items_per_page,
86 86 :offset => @changeset_pages.current.offset)
87 87
88 88 respond_to do |format|
89 89 format.html { render :layout => false if request.xhr? }
90 90 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
91 91 end
92 92 end
93 93
94 94 def entry
95 95 @entry = @repository.entry(@path, @rev)
96 96 show_error_not_found and return unless @entry
97 97
98 98 # If the entry is a dir, show the browser
99 99 browse and return if @entry.is_dir?
100 100
101 101 @content = @repository.cat(@path, @rev)
102 102 show_error_not_found and return unless @content
103 103 if 'raw' == params[:format] || @content.is_binary_data?
104 104 # Force the download if it's a binary file
105 105 send_data @content, :filename => @path.split('/').last
106 106 else
107 107 # Prevent empty lines when displaying a file with Windows style eol
108 108 @content.gsub!("\r\n", "\n")
109 109 end
110 110 end
111 111
112 112 def annotate
113 113 @annotate = @repository.scm.annotate(@path, @rev)
114 114 render_error l(:error_scm_annotate) and return if @annotate.nil? || @annotate.empty?
115 115 end
116 116
117 117 def revision
118 118 @changeset = @repository.changesets.find_by_revision(@rev)
119 119 raise ChangesetNotFound unless @changeset
120 120 @changes_count = @changeset.changes.size
121 121 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
122 122 @changes = @changeset.changes.find(:all,
123 123 :limit => @changes_pages.items_per_page,
124 124 :offset => @changes_pages.current.offset)
125 125
126 126 respond_to do |format|
127 127 format.html
128 128 format.js {render :layout => false}
129 129 end
130 130 rescue ChangesetNotFound
131 131 show_error_not_found
132 132 end
133 133
134 134 def diff
135 135 if params[:format] == 'diff'
136 136 @diff = @repository.diff(@path, @rev, @rev_to)
137 137 show_error_not_found and return unless @diff
138 138 filename = "changeset_r#{@rev}"
139 139 filename << "_r#{@rev_to}" if @rev_to
140 140 send_data @diff.join, :filename => "#{filename}.diff",
141 141 :type => 'text/x-patch',
142 142 :disposition => 'attachment'
143 143 else
144 144 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
145 145 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
146 146
147 147 # Save diff type as user preference
148 148 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
149 149 User.current.pref[:diff_type] = @diff_type
150 150 User.current.preference.save
151 151 end
152 152
153 153 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
154 154 unless read_fragment(@cache_key)
155 155 @diff = @repository.diff(@path, @rev, @rev_to)
156 156 show_error_not_found unless @diff
157 157 end
158 158 end
159 159 end
160 160
161 161 def stats
162 162 end
163 163
164 164 def graph
165 165 data = nil
166 166 case params[:graph]
167 167 when "commits_per_month"
168 168 data = graph_commits_per_month(@repository)
169 169 when "commits_per_author"
170 170 data = graph_commits_per_author(@repository)
171 171 end
172 172 if data
173 173 headers["Content-Type"] = "image/svg+xml"
174 174 send_data(data, :type => "image/svg+xml", :disposition => "inline")
175 175 else
176 176 render_404
177 177 end
178 178 end
179 179
180 180 private
181 181 def find_project
182 182 @project = Project.find(params[:id])
183 183 rescue ActiveRecord::RecordNotFound
184 184 render_404
185 185 end
186 186
187 187 REV_PARAM_RE = %r{^[a-f0-9]*$}
188 188
189 189 def find_repository
190 190 @project = Project.find(params[:id])
191 191 @repository = @project.repository
192 192 render_404 and return false unless @repository
193 193 @path = params[:path].join('/') unless params[:path].nil?
194 194 @path ||= ''
195 195 @rev = params[:rev]
196 196 @rev_to = params[:rev_to]
197 197 raise InvalidRevisionParam unless @rev.to_s.match(REV_PARAM_RE) && @rev.to_s.match(REV_PARAM_RE)
198 198 rescue ActiveRecord::RecordNotFound
199 199 render_404
200 200 rescue InvalidRevisionParam
201 201 show_error_not_found
202 202 end
203 203
204 204 def show_error_not_found
205 205 render_error l(:error_scm_not_found)
206 206 end
207 207
208 208 # Handler for Redmine::Scm::Adapters::CommandFailed exception
209 209 def show_error_command_failed(exception)
210 210 render_error l(:error_scm_command_failed, exception.message)
211 211 end
212 212
213 213 def graph_commits_per_month(repository)
214 214 @date_to = Date.today
215 215 @date_from = @date_to << 11
216 216 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
217 217 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
218 218 commits_by_month = [0] * 12
219 219 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
220 220
221 221 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
222 222 changes_by_month = [0] * 12
223 223 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
224 224
225 225 fields = []
226 226 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
227 227 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
228 228
229 229 graph = SVG::Graph::Bar.new(
230 230 :height => 300,
231 :width => 500,
231 :width => 800,
232 232 :fields => fields.reverse,
233 233 :stack => :side,
234 234 :scale_integers => true,
235 235 :step_x_labels => 2,
236 236 :show_data_values => false,
237 237 :graph_title => l(:label_commits_per_month),
238 238 :show_graph_title => true
239 239 )
240 240
241 241 graph.add_data(
242 242 :data => commits_by_month[0..11].reverse,
243 243 :title => l(:label_revision_plural)
244 244 )
245 245
246 246 graph.add_data(
247 247 :data => changes_by_month[0..11].reverse,
248 248 :title => l(:label_change_plural)
249 249 )
250 250
251 251 graph.burn
252 252 end
253 253
254 254 def graph_commits_per_author(repository)
255 255 commits_by_author = repository.changesets.count(:all, :group => :committer)
256 256 commits_by_author.sort! {|x, y| x.last <=> y.last}
257 257
258 258 changes_by_author = repository.changes.count(:all, :group => :committer)
259 259 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
260 260
261 261 fields = commits_by_author.collect {|r| r.first}
262 262 commits_data = commits_by_author.collect {|r| r.last}
263 263 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
264 264
265 265 fields = fields + [""]*(10 - fields.length) if fields.length<10
266 266 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
267 267 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
268 268
269 269 # Remove email adress in usernames
270 270 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
271 271
272 272 graph = SVG::Graph::BarHorizontal.new(
273 :height => 300,
274 :width => 500,
273 :height => 400,
274 :width => 800,
275 275 :fields => fields,
276 276 :stack => :side,
277 277 :scale_integers => true,
278 278 :show_data_values => false,
279 279 :rotate_y_labels => false,
280 280 :graph_title => l(:label_commits_per_author),
281 281 :show_graph_title => true
282 282 )
283 283
284 284 graph.add_data(
285 285 :data => commits_data,
286 286 :title => l(:label_revision_plural)
287 287 )
288 288
289 289 graph.add_data(
290 290 :data => changes_data,
291 291 :title => l(:label_change_plural)
292 292 )
293 293
294 294 graph.burn
295 295 end
296 296
297 297 end
298 298
299 299 class Date
300 300 def months_ago(date = Date.today)
301 301 (date.year - self.year)*12 + (date.month - self.month)
302 302 end
303 303
304 304 def weeks_ago(date = Date.today)
305 305 (date.year - self.year)*52 + (date.cweek - self.cweek)
306 306 end
307 307 end
308 308
309 309 class String
310 310 def with_leading_slash
311 311 starts_with?('/') ? self : "/#{self}"
312 312 end
313 313 end
@@ -1,13 +1,12
1 1 <h2><%= l(:label_statistics) %></h2>
2 2
3 <table width="100%">
4 <tr><td>
5 <%= tag("embed", :width => 500, :height => 300, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_month")) %>
6 </td><td>
7 <%= tag("embed", :width => 500, :height => 300, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_author")) %>
8 </td></tr>
9 </table>
10 <br />
3 <p>
4 <%= tag("embed", :width => 800, :height => 300, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_month")) %>
5 </p>
6 <p>
7 <%= tag("embed", :width => 800, :height => 400, :type => "image/svg+xml", :src => url_for(:controller => 'repositories', :action => 'graph', :id => @project, :graph => "commits_per_author")) %>
8 </p>
9
11 10 <p><%= link_to l(:button_back), :action => 'show', :id => @project %></p>
12 11
13 12 <% html_title(l(:label_repository), l(:label_statistics)) -%>
General Comments 0
You need to be logged in to leave comments. Login now