Class: OrgLangStats

Inherits:
Object
  • Object
show all
Defined in:
lib/org_lang_stats.rb,
lib/org_lang_stats/version.rb

Overview

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

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, org) ⇒ OrgLangStats

Returns a new instance of OrgLangStats.



12
13
14
15
16
17
# File 'lib/org_lang_stats.rb', line 12

def initialize(api, org)
    @aggr = nil
    @repos = nil
    @api = api
    @org = org
end

Class Method Details

.proccess(api, org, threads, percent) ⇒ Object



8
9
10
# File 'lib/org_lang_stats.rb', line 8

def self.proccess(api, org, threads, percent)
    OrgLangStats.new(api, org).proccess(percent, threads)
end

Instance Method Details

#bytes_to_percentObject



55
56
57
58
59
60
61
# 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_statsObject



49
50
51
52
53
# 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) ⇒ Object



63
64
65
66
67
68
# 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_reposObject



41
42
43
# 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) ⇒ Object



70
71
72
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



19
20
21
22
23
24
# 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_urlObject



45
46
47
# File 'lib/org_lang_stats.rb', line 45

def repos_url
    "https://api.github.com/orgs/#{@org}/repos"
end