Class: Gitballs::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/gitballs/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_dir, tarball_size = nil) ⇒ Stats

Returns a new instance of Stats.



7
8
9
10
# File 'lib/gitballs/stats.rb', line 7

def initialize(repo_dir, tarball_size = nil)
  @repo_dir = repo_dir
  @tarball_size = tarball_size
end

Instance Attribute Details

#repo_dirObject (readonly)

Returns the value of attribute repo_dir.



5
6
7
# File 'lib/gitballs/stats.rb', line 5

def repo_dir
  @repo_dir
end

#tarball_sizeObject (readonly)

Returns the value of attribute tarball_size.



5
6
7
# File 'lib/gitballs/stats.rb', line 5

def tarball_size
  @tarball_size
end

Instance Method Details

#compression_ratioObject



24
25
26
27
28
# File 'lib/gitballs/stats.rb', line 24

def compression_ratio
  return nil unless @tarball_size && @tarball_size > 0

  ((@tarball_size - repo_size).to_f / @tarball_size * 100).round(1)
end

#format_size(bytes) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/gitballs/stats.rb', line 50

def format_size(bytes)
  return "0B" if bytes.zero?

  units = %w[B KB MB GB]
  exp = (Math.log(bytes) / Math.log(1024)).to_i
  exp = units.size - 1 if exp >= units.size
  "%.1f%s" % [bytes.to_f / (1024**exp), units[exp]]
end

#release_countObject



12
13
14
15
16
# File 'lib/gitballs/stats.rb', line 12

def release_count
  return 0 unless File.directory?(File.join(@repo_dir, ".git"))

  `git -C #{@repo_dir} rev-list --count HEAD 2>/dev/null`.strip.to_i
end

#repo_sizeObject



18
19
20
21
22
# File 'lib/gitballs/stats.rb', line 18

def repo_size
  return 0 unless File.directory?(@repo_dir)

  `du -sk #{@repo_dir}`.strip.split.first.to_i * 1024
end

#to_hObject



30
31
32
33
34
35
36
37
# File 'lib/gitballs/stats.rb', line 30

def to_h
  {
    releases: release_count,
    repo_size: repo_size,
    tarball_size: tarball_size,
    compression_ratio: compression_ratio
  }
end

#to_sObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/gitballs/stats.rb', line 39

def to_s
  lines = []
  lines << "releases: #{release_count}"
  lines << "repo size: #{format_size(repo_size)}"
  if @tarball_size && @tarball_size > 0
    lines << "tarball size: #{format_size(@tarball_size)}"
    lines << "savings: #{compression_ratio}%"
  end
  lines.join("\n")
end