class OrgLangStats

This class contains the business logic of the project. It utilizes components such as the Threadpool and the GithubAPI to put everything together…fast

Constants

VERSION

Public Class Methods

new(api, org) click to toggle source
# File lib/org_lang_stats.rb, line 12
def initialize(api, org)
    @aggr = nil
    @repos = nil
    @api = api
    @org = org
end
proccess(api, org, threads, percent) click to toggle source
# File lib/org_lang_stats.rb, line 8
def self.proccess(api, org, threads, percent)
    OrgLangStats.new(api, org).proccess(percent, threads)
end

Public Instance Methods

bytes_to_percent() click to toggle source
# File lib/org_lang_stats.rb, line 55
def bytes_to_percent
    @repos = @repos.map do |r_name, stats|
        Hash[r_name, hash_values_to_percent(stats)]
    end.inject(:merge)

    @aggr = hash_values_to_percent @aggr
end
calc_aggregate_stats() click to toggle source
# File lib/org_lang_stats.rb, line 49
def calc_aggregate_stats
    @repos.values.each_with_object(Hash.new(0)) do |repo, hash|
        repo.each { |lang, bytes| hash[lang] += bytes }
    end
end
hash_values_to_percent(hash) click to toggle source
# File lib/org_lang_stats.rb, line 63
def hash_values_to_percent(hash)
    total = hash.values.inject(:+)
    hash.map do |key, value|
        Hash[key, num_to_percent(value, total)]
    end.inject(:merge)
end
non_forked_repos() click to toggle source
# File lib/org_lang_stats.rb, line 41
def non_forked_repos
    @api.get_all_pages(url: repos_url, params: { type: 'source' })
end
num_to_percent(num, num_total) click to toggle source
# File lib/org_lang_stats.rb, line 70
def num_to_percent(num, num_total)
    (100.0 * num / num_total).round(1).to_s + '%'
end
parallel_get_lang_stats(threads) click to toggle source
# File lib/org_lang_stats.rb, line 26
def parallel_get_lang_stats(threads)
    result = {}
    pool = ThreadPool.new threads

    non_forked_repos.map do |repo|
        pool.run do
            repo_stats = @api.get_all_pages(url: repo['languages_url'])
            result.merge! Hash[repo['name'], repo_stats]
        end
    end

    pool.join_workers
    result
end
proccess(percent, threads) click to toggle source
# File lib/org_lang_stats.rb, line 19
def proccess(percent, threads)
    @repos = parallel_get_lang_stats(threads)
    @aggr = calc_aggregate_stats
    bytes_to_percent if percent
    { @org => { 'aggregate_stats' => @aggr, 'repositories' => @repos } }
end
repos_url() click to toggle source
# File lib/org_lang_stats.rb, line 45
def repos_url
    "https://api.github.com/orgs/#{@org}/repos"
end