Commit 33b2ea6e77d0986b35168be89a3d945992396b86

Merge commit 'krawek/graph-generator'

Commit diff

app/helpers/application_helper.rb

 
8989 end
9090
9191 def commit_graph_tag(repository, ref = "master")
92 filename = "#{repository.project.slug}_#{repository.name}_#{h(ref)}_commit_count.png"
92 filename = Gitorious::Graphs::CommitsBuilder.filename(repository, ref)
9393 if File.exist?(File.join(Gitorious::Graphs::Builder.graph_dir, filename))
9494 image_tag("graphs/#{filename}")
9595 end
9696 end
9797
9898 def commit_graph_by_author_tag(repository, ref = "master")
99 filename = "#{repository.project.slug}_#{repository.name}_#{h(ref)}_commit_count_by_author.png"
99 filename = Gitorious::Graphs::CommitsByAuthorBuilder.filename(repository, ref)
100100 if File.exist?(File.join(Gitorious::Graphs::Builder.graph_dir, filename))
101101 image_tag("graphs/#{filename}")
102102 end
toggle raw diff

lib/gitorious/graphs/builder.rb

 
66
77module Gitorious
88 module Graphs
9 class Builder
9 class Builder
10 GENERATORS = [CommitsBuilder, CommitsByAuthorBuilder]
1011 def self.generate_all_for(repository)
11 CommitsBuilder.generate_for(repository)
12 CommitsByAuthorBuilder.generate_for(repository)
12 GENERATORS.each do |generator|
13 generator.generate_for(repository)
14 end
1315 end
1416
1517 def self.graph_dir
1618 File.join(RAILS_ROOT, "public/images/graphs/")
1719 end
20
21 def self.construct_filename(repository, branch, name)
22 "#{repository.project.slug}_#{repository.name}_#{branch}_#{name}.png"
23 end
24
25 def self.status_file(repository, branch = "master")
26 File.join(RAILS_ROOT, "tmp", "graph_generator",
27 "#{repository.project.slug}_#{repository.name}_#{repository.git.commit_count(branch)}_#{self.name}.status")
28 end
1829
1930 def self.default_theme
2031 {
toggle raw diff

lib/gitorious/graphs/commits_builder.rb

 
33
44 class CommitsBuilder < Gitorious::Graphs::Builder
55 def self.generate_for(repository)
6 if repository.has_commits?
7 builder = new(repository, repository.head_candidate.name)
6 head = repository.head_candidate
7 return if head.nil?
8
9 branch = head.name
10 if !File.exist?(self.status_file(repository, branch)) && repository.has_commits?
11 builder = new(repository, branch)
812 builder.build
913 builder.write
14 FileUtils.touch(self.status_file(repository, branch))
1015 end
1116 end
1217
3838 @graph.labels = build_labels(week_numbers)
3939 end
4040
41 def self.filename(repository, branch)
42 Builder.construct_filename(repository, branch, "commit_count")
43 end
44
4145 def construct_filename
42 "#{@repository.project.slug}_#{@repository.name}_#{@branch}_commit_count.png"
46 CommitsBuilder.filename(@repository, @branch)
4347 end
4448
4549 private
toggle raw diff

lib/gitorious/graphs/commits_by_author_builder.rb

 
55 def self.generate_for(repository)
66 if repository.has_commits?
77 repository.git.heads.each do |head|
8 builder = new(repository, head.name)
9 builder.build
10 builder.write
8 branch = head.name
9 if !File.exist?(self.status_file(repository, branch))
10 builder = new(repository, branch)
11 builder.build
12 builder.write
13 FileUtils.touch(self.status_file(repository, branch))
14 end
1115 end
1216 end
1317 end
3535 end
3636 end
3737
38 def self.filename(repository, branch)
39 Builder.construct_filename(repository, branch, "commit_count_by_author")
40 end
41
3842 def construct_filename
39 "#{@repository.project.slug}_#{@repository.name}_#{@branch}_commit_count_by_author.png"
43 CommitsByAuthorBuilder.filename(@repository, @branch)
4044 end
4145 end
4246
toggle raw diff

script/graph_generator

 
11#!/usr/bin/env ruby
22
3require 'tmpdir'
4require "fileutils"
5
6LOCK_FILE_PATH = File.join(Dir.tmpdir, "gitorious_graphgenerator_lockfile")
7if File.exist?(LOCK_FILE_PATH)
8 $stderr.puts "Lockfile '#{LOCK_FILE_PATH}' exists!"
9 exit(1)
10end
11
12FileUtils.touch(LOCK_FILE_PATH)
13
314ENV["RAILS_ENV"] ||= "production"
415require File.dirname(__FILE__) + "/../config/environment"
516
1919log.level = Logger::INFO
2020log.formatter.datetime_format = "%Y-%m-%d %H:%M:%S"
2121
22STATUS_FILE = File.join(RAILS_ROOT, "tmp", "graph_generator.status")
23update_status = !File.exist?(STATUS_FILE)
24tmpdir = File.join(RAILS_ROOT, "tmp", "graph_generator")
25if !File.directory?(tmpdir)
26 FileUtils.mkdir_p(tmpdir)
27elsif !update_status
28 mtime = File.mtime(STATUS_FILE).utc
29
30 days = (Time.now.utc - mtime) / (3600*24)
31 if days >= 5
32 log.info "Cleaning '#{tmpdir}'..."
33 Dir.glob(File.join(tmpdir, "*.status")) do |file|
34 FileUtils.rm(file)
35 end
36
37 update_status = true
38 end
39end
40
41if update_status
42 FileUtils.touch(STATUS_FILE)
43end
44
2245log.info "Starting graph generation run..."
2346Repository.find(:all).each_with_index do |repository, index|
2447 begin
5959# exit(1)
6060 end
6161end
62log.info "Done with graph generation run"
62log.info "Done with graph generation run"
63
64FileUtils.rm(LOCK_FILE_PATH)
toggle raw diff