Commit cf3bcecb authored by Fabio Pelosin's avatar Fabio Pelosin

[pod-list] caching for statistics & refactoring

parent 29a517d1
......@@ -37,10 +37,13 @@ module Pod
days = [1,2,3,5,8]
dates, groups = {}, {}
days.each {|d| dates[d] = Time.now - 60 * 60 * 24 * d}
Source.all_sets.each do |set|
set_date = Pod::Specification::Statistics.new(set).creation_date
sets = Source.all_sets
creation_dates = Pod::Specification::Statistics.instance.creation_dates(sets)
sets.each do |set|
set_date = creation_dates[set.name]
days.each do |d|
if set_date > dates[d]
if set_date >= dates[d]
groups[d] = [] unless groups[d]
groups[d] << set
break
......@@ -52,7 +55,7 @@ module Pod
sets = groups[d]
next unless sets
puts "Pods added in the last #{d == 1 ? '1 day' : "#{d} days"}".yellow
@presenter.present_sets(sets.sort_by {|set| Pod::Specification::Statistics.new(set).creation_date})
@presenter.present_sets(sets.sort_by {|set| creation_dates[set.name]})
end
end
......
module Pod
class Specification
class Set
def main_specification
specification.part_of_other_pod? ? specification.part_of_specification : specification
end
def homepage
main_specification.homepage
end
def description
main_specification.description
end
def summary
main_specification.summary
end
def source_url
main_specification.source.reject {|k,_| k == :commit || k == :tag }.values.first
end
def github_watchers
Pod::Specification::Statistics.instance.github_watchers(self)
end
def github_forks
Pod::Specification::Statistics.instance.github_watchers(self)
end
end
end
end
module Pod
class Command
class Presenter
......@@ -15,16 +49,17 @@ module Pod
end
def present_set(set)
stats = Pod::Specification::Statistics.new(set)
puts "--> #{set.name} (#{set.versions.reverse.join(", ")})".green
puts wrap_string(stats.summary)
puts_detail('Homepage', stats.homepage)
puts_detail('Source', stats.source_url)
puts_detail('Watchers', stats.github_watchers) if @stats
puts_detail('Forks', stats.github_forks) if @stats
puts wrap_string(set.summary)
puts_detail('Homepage', set.homepage)
puts_detail('Source', set.source_url)
puts_detail('Watchers', set.github_watchers) if @stats
puts_detail('Forks', set.github_forks) if @stats
puts
end
private
# adapted from http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
def wrap_string(txt, col = 80, indentation = 4)
indent = ' ' * indentation
......
......@@ -13,7 +13,7 @@ module Pod
end
def all_sets
all.map! {|source| source.pod_sets}.flatten
all.map {|source| source.pod_sets}.flatten
end
def search(dependency)
......
require 'net/http'
require 'yaml'
module Pod
class Specification
class Statistics
def initialize(set)
@set = set
@spec = set.specification.part_of_other_pod? ? set.specification.part_of_specification : set.specification
end
include Config::Mixin
def creation_date
Dir.chdir(@set.pod_dir.dirname) do
@creation_date ||= Time.at(`git log --format=%ct ./#{@set.name} | tail -1`.to_i)
def self.instance
@instance ||= new
end
def self.instance=(instance)
@instance = instance
end
def homepage
@spec.homepage
def initialize
@cache = cache_file.exist? ? YAML::load(cache_file.read) : {}
end
def description
@spec.description
def creation_dates(sets)
creation_dates = {}
sets.each do |set|
@cache[set.name] ||= {}
date = @cache[set.name][:creation_date] ||= compute_creation_date(set)
creation_dates[set.name] = date
end
save_cache_file
creation_dates
end
def summary
@spec.summary
def github_watchers(set)
compute_github_stats_if_needed(set)
@cache[set.name][:github_watchers] if @cache[set.name]
end
def source_url
@spec.source.reject {|k,_| k == :commit || k == :tag }.values.first
def github_forks(set)
compute_github_stats_if_needed(set)
@cache[set.name][:github_forks] if @cache[set.name]
end
def github_response
return @github_response if @github_response
github_url, username, reponame = *(source_url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a)
if github_url
@github_response = Net::HTTP.get('github.com', "/api/v2/json/repos/show/#{username}/#{reponame}")
private
def cache_file
Config.instance.repos_dir + 'statistics.yml'
end
def save_cache_file
File.open(cache_file, 'w') {|f| f.write(YAML::dump(@cache)) }
end
def github_watchers
github_response.match(/\"watchers\"\W*:\W*([0-9]+)/).to_a[1] if github_response
def compute_creation_date(set)
Dir.chdir(set.pod_dir.dirname) do
Time.at(`git log --first-parent --format=%ct #{set.name}`.split("\n").last.to_i)
end
end
def github_forks
github_response.match(/\"forks\"\W*:\W*([0-9]+)/).to_a[1] if github_response
def compute_github_stats_if_needed(set)
if @cache[set.name] && @cache[set.name][:github_check_date] && @cache[set.name][:github_check_date] > Time.now - 60 * 60
return
end
spec = set.specification.part_of_other_pod? ? set.specification.part_of_specification : set.specification
source_url = spec.source.reject {|k,_| k == :commit || k == :tag }.values.first
github_url, username, reponame = *(source_url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a)
if github_url
github_response = Net::HTTP.get('github.com', "/api/v2/json/repos/show/#{username}/#{reponame}")
watchers = github_response.match(/\"watchers\"\W*:\W*([0-9]+)/).to_a[1]
forks = github_response.match(/\"forks\"\W*:\W*([0-9]+)/).to_a[1]
if (watchers && forks)
@cache[set.name] ||= {}
@cache[set.name][:github_watchers] = watchers
@cache[set.name][:github_forks] = forks
@cache[set.name][:github_check_date] = Time.now
save_cache_file
end
end
end
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