Commit 915bcd8e authored by Fabio Pelosin's avatar Fabio Pelosin

[pod-list] tweaks and clean up

parent e666f0e9
...@@ -15,7 +15,7 @@ module Pod ...@@ -15,7 +15,7 @@ module Pod
end end
def version def version
@set.version.last @set.versions.last
end end
def versions def versions
...@@ -38,6 +38,10 @@ module Pod ...@@ -38,6 +38,10 @@ module Pod
spec.source.reject {|k,_| k == :commit || k == :tag }.values.first spec.source.reject {|k,_| k == :commit || k == :tag }.values.first
end end
def creation_date
Pod::Specification::Statistics.instance.creation_date(@set)
end
def github_watchers def github_watchers
Pod::Specification::Statistics.instance.github_watchers(@set) Pod::Specification::Statistics.instance.github_watchers(@set)
end end
......
...@@ -4,7 +4,6 @@ require 'yaml' ...@@ -4,7 +4,6 @@ require 'yaml'
module Pod module Pod
class Specification class Specification
class Statistics class Statistics
include Config::Mixin
def self.instance def self.instance
@instance ||= new @instance ||= new
...@@ -14,66 +13,84 @@ module Pod ...@@ -14,66 +13,84 @@ module Pod
@instance = instance @instance = instance
end end
attr_accessor :cache_file, :cache_expiration
def initialize def initialize
@cache = cache_file.exist? ? YAML::load(cache_file.read) : {} @cache_file = Config.instance.repos_dir + 'statistics.yml'
@cache_expiration = 60 * 60 * 24 * 3
end
def creation_date(set)
compute_creation_date(set)
end end
def creation_dates(sets) def creation_dates(sets)
creation_dates = {} dates = {}
sets.each do |set| sets.each { |set| dates[set.name] = compute_creation_date(set, false) }
@cache[set.name] ||= {} save_cache
date = @cache[set.name][:creation_date] ||= compute_creation_date(set) dates
creation_dates[set.name] = date
end
save_cache_file
creation_dates
end end
def github_watchers(set) def github_watchers(set)
compute_github_stats_if_needed(set) github_stats_if_needed(set)
@cache[set.name][:github_watchers] if @cache[set.name] get_value(set, :gh_watchers)
end end
def github_forks(set) def github_forks(set)
compute_github_stats_if_needed(set) github_stats_if_needed(set)
@cache[set.name][:github_forks] if @cache[set.name] get_value(set, :gh_forks)
end end
private private
def cache_file def cache
Config.instance.repos_dir + 'statistics.yml' @cache ||= cache_file.exist? ? YAML::load(cache_file.read) : {}
end end
def save_cache_file def get_value(set, key)
File.open(cache_file, 'w') {|f| f.write(YAML::dump(@cache)) } if cache[set.name] && cache[set.name][key]
cache[set.name][key]
end
end end
def compute_creation_date(set) def set_value(set, key, value)
Dir.chdir(set.pod_dir.dirname) do cache[set.name] ||= {}
Time.at(`git log --first-parent --format=%ct #{set.name}`.split("\n").last.to_i) cache[set.name][key] = value
end
end end
def compute_github_stats_if_needed(set) def save_cache
if @cache[set.name] && @cache[set.name][:github_check_date] && @cache[set.name][:github_check_date] > Time.now - 60 * 60 * 24 File.open(cache_file, 'w') { |f| f.write(YAML::dump(cache)) }
return end
end
spec = set.specification.part_of_other_pod? ? set.specification.part_of_specification : set.specification def compute_creation_date(set, save = true)
source_url = spec.source.reject {|k,_| k == :commit || k == :tag }.values.first date = get_value(set, :creation_date)
github_url, username, reponame = *(source_url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a) unless date
if github_url Dir.chdir(set.pod_dir.dirname) do
github_response = Net::HTTP.get('github.com', "/api/v2/json/repos/show/#{username}/#{reponame}") date = Time.at(`git log --first-parent --format=%ct #{set.name}`.split("\n").last.to_i)
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
set_value(set, :creation_date, date)
end end
save_cache if save
date
end
def github_stats_if_needed(set)
return if get_value(set, :gh_date) && get_value(set, :gh_date) > Time.now - cache_expiration
spec = set.specification.part_of_other_pod? ? set.specification.part_of_specification : set.specification
url = spec.source.reject {|k,_| k == :commit || k == :tag }.values.first
gh_url, username, reponame = *(url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a)
return unless gh_url
response = Net::HTTP.get('github.com', "/api/v2/json/repos/show/#{username}/#{reponame}")
watchers = response.match(/\"watchers\"\W*:\W*([0-9]+)/).to_a[1]
forks = response.match(/\"forks\"\W*:\W*([0-9]+)/).to_a[1]
return unless watchers && forks
cache[set.name] ||= {}
set_value(set, :gh_watchers, watchers)
set_value(set, :gh_forks, forks)
set_value(set, :gh_date, Time.now)
save_cache
end 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