public
Last active

  • Download Gist
radiant census
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
task :census => ['census:snippets', 'census:layouts']
 
namespace :census do
desc "Show usage stats for Snippets"
task :snippets => :environment do
snippets, missing_snippets = snippet_usage
longest_name = snippets.keys.inject(0) do |max, name|
max = name.to_s.size > max ? name.to_s.size : max
end
puts print_snippet_headings(longest_name)
if ENV['ORDER'] == "name"
sorted_snippets = snippets.to_a.sort {|a,b| a.first.to_s <=> b.first.to_s }
else
sorted_snippets = snippets.to_a.sort {|a,b| b.last[:total] <=> a.last[:total] }
end
sorted_snippets.each do |snippet|
puts print_row(snippet, longest_name)
end
unless missing_snippets.empty?
puts "\nThe following snippets have been called, but they do not exist!"
missing_snippets.to_a.sort {|a,b| a.first.to_s <=> b.first.to_s }.each do |snippet|
puts print_row(snippet, longest_name)
end
end
end
desc "Show usage stats for Layouts"
task :layouts => :environment do
layouts = {}
Layout.find(:all).each { |l| layouts[l.name] = 0 }
Page.find(:all).each do |page|
layouts[page.layout.name] += 1
end
longest_name = layouts.keys.inject(0) { |max, name| max = name.size > max ? name.size : max }
puts "Layout".ljust(longest_name) + "Usage".rjust(5)
puts "------".ljust(longest_name) + "-----".rjust(5)
layouts.to_a.sort {|a,b| b.last <=> a.last }.each do |layout|
puts layout.first.ljust(longest_name) + layout.last.to_s.rjust(5)
end
end
end
 
task :usage => ['usage:snippets','usage:layouts']
 
namespace :usage do
desc "Show usage stats for Snippets"
task :snippets => :environment do
snippets, missing_snippets = snippet_usage
longest_name = snippets.keys.inject(0) do |max, name|
max = name.to_s.size > max ? name.to_s.size : max
end
if ENV['ORDER'] == "name"
sorted_snippets = snippets.to_a.sort {|a,b| a.first.to_s <=> b.first.to_s }
else
sorted_snippets = snippets.to_a.sort {|a,b| b.last[:total] <=> a.last[:total] }
end
padding = 3
sp = " " * padding
sorted_snippets.to_a.each do |snippet|
layouts = snippet.last[:layouts]; snippets = snippet.last[:snippets]; pages = snippet.last[:pages]
next if layouts.empty? and snippets.empty? and pages.empty?
puts snippet.first
puts "-" * longest_name
unless layouts.empty?
puts "\n#{sp}layouts:"
layouts.each{ |l| puts "#{sp*2}#{l.name}" }
end
 
unless snippets.empty?
puts "\n#{sp}snippets:"
snippets.each{ |s| puts "#{sp*2}#{s.name}" }
end
unless pages.empty?
puts "\n#{sp}pages"
pages.each{ |p| puts "#{sp*2}#{p.url}"}
end
puts "\n\n"
end
end
desc "List all layouts, with each page that uses the layout"
task :layouts => :environment do
layouts = {}
Layout.find(:all).each { |l| layouts[l.name] = [] }
Page.find(:all).each do |page|
layouts[page.layout.name] << page
end
layouts.to_a.sort {|a,b| b.last.size <=> a.last.size }.each do |layout|
next if layout.last.empty?
puts layout.first
layout.last.each do |page|
puts "#{page.id.to_s.rjust(4)} #{page.url}"
end
puts ''
end
end
end
 
def snippet_usage
snippets = {}; missing_snippets = {}
Snippet.find(:all).each do |s|
snippets[s.name.to_sym] = snippet_statistic_hash
end
pattern = /<r\:snippet\s+name\=[\"|\'](\S+)[\"\']/
[Layout, Snippet, PagePart].each do |model|
model.find(:all).each do |item|
item.content.grep(pattern).each do |match|
match =~ pattern
snippet_name = $1
if model == PagePart
model_name = item.page
model_type = :pages
else
model_name = item
model_type = model.to_s.downcase.pluralize.to_sym
end
if snippets.has_key?(snippet_name.to_sym)
snippets[snippet_name.to_sym][model_type] << model_name
snippets[snippet_name.to_sym][:total] += 1
else
unless missing_snippets.has_key?(snippet_name.to_sym)
missing_snippets[snippet_name.to_sym] = snippet_statistic_hash
end
missing_snippets[snippet_name.to_sym][model_type] << model_name
missing_snippets[snippet_name.to_sym][:total] += 1
end
end
end
end
[snippets, missing_snippets]
end
 
def snippet_statistic_hash
{
:pages => [],
:layouts => [],
:snippets => [],
:total => 0
}
end
 
def print_row(snippet, longest_name, padding=3)
row = snippet.first.to_s.ljust(longest_name)
total_pages = snippet.last[:pages].size
row += total_pages.to_s.rjust("pages".length + padding)
total_layouts = snippet.last[:layouts].size
row += total_layouts.to_s.rjust("layouts".length + padding)
total_snippets = snippet.last[:snippets].size
row += total_snippets.to_s.rjust("snippets".length + padding)
total = snippet.last[:total]
row += total.to_s.rjust("total".length + padding)
end
 
def print_snippet_headings(longest_name, padding=3)
sp = " " * padding
s = "Snippet".ljust(longest_name) + "#{sp}Pages#{sp}Layouts#{sp}Snippets#{sp}Total\n"
s += "-------".ljust(longest_name) + "#{sp}-----#{sp}-------#{sp}--------#{sp}-----"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.