Class: Gloo::Utils::Stats

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

Constant Summary collapse

DIR_NOT_FOUND_ERR =
'The folder was not found!'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(engine, dir, types, skip = []) ⇒ Stats

Create a stats utility class for the given directory.



20
21
22
23
24
25
# File 'lib/gloo/utils/stats.rb', line 20

def initialize( engine, dir, types, skip = [] )
  @engine = engine
  @dir = dir
  setup_loc types
  @skip = skip
end

Instance Method Details

#busy_folders(count = 17) ⇒ Object

Get a list of the busiest folders. Count is how many results we want.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gloo/utils/stats.rb', line 64

def busy_folders( count = 17 )
  return unless valid?

  generate
  puts "\nBusy Folders:".yellow

  @folders.sort! { |a, b| a[ :cnt ] <=> b[ :cnt ] }
  @folders.reverse!
  @folders[ 0..count ].each do |f|
    puts "  #{f[ :cnt ]} - #{f[ :name ]}"
  end
end

#count_lines(file) ⇒ Object

Count lines of code in file.



200
201
202
# File 'lib/gloo/utils/stats.rb', line 200

def count_lines( file )
  return `wc -l #{file}`.split.first.to_i
end

#file_typesObject

Show file types and how many of each there are.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gloo/utils/stats.rb', line 80

def file_types
  return unless valid?

  generate
  puts "\nFiles by Type:".yellow

  @types = @types.sort_by( &:last )
  @types.reverse!
  @types.each do |o|
    puts "  #{o[ 1 ]} - #{o[ 0 ]}" unless o[ 0 ].empty?
  end
end

#generateObject

Generate stat data unless we’ve already done so.



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gloo/utils/stats.rb', line 137

def generate
  return if @folders

  @engine.log.debug 'Generating...'
  @folders = []
  @types = {}
  @file_cnt = 0
  @dir_cnt = 0

  generate_for Pathname.new( @dir )
end

#generate_for(path) ⇒ Object

Generate data for the given path. NOTE: this is a recursive function. It traverses all sub-direcctories.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gloo/utils/stats.rb', line 154

def generate_for( path )
  return if @skip.include?( File.basename( path ) )

  cnt = 0
  path.children.each do |f|
    if f.directory?
      @dir_cnt += 1
      generate_for( f )
    else
      @file_cnt += 1
      cnt += 1
      handle_file( f )
      inc_type( File.extname( f ) )
    end
  end
  @folders << { name: path, cnt: cnt }
end

#handle_file(file) ⇒ Object

Consider code file types.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gloo/utils/stats.rb', line 186

def handle_file( file )
  ext = File.extname( file )
  return unless ext

  ext = ext[ 1..-1 ]
  return unless @loc.key?( ext )

  lines = count_lines( file )
  @loc[ ext ][ :lines ] += lines
  @loc[ ext ][ :files ] << { lines: lines, file: file }
end

#inc_type(type) ⇒ Object

Increment the file count.



175
176
177
178
179
180
181
# File 'lib/gloo/utils/stats.rb', line 175

def inc_type( type )
  if @types[ type ]
    @types[ type ] += 1
  else
    @types[ type ] = 1
  end
end

#locObject

Show Lines of Code



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gloo/utils/stats.rb', line 96

def loc
  return unless valid?

  generate
  total = 0

  @loc.each do |k, v|
    puts "\n #{k} Lines of Code".yellow
    total += v[ :lines ]
    formatted = Gloo::Utils::Format.number( v[ :lines ] )
    puts " ** #{formatted} in #{v[ :files ].count} #{k} files ** "

    puts "\n Busy #{k} files:".yellow
    files = v[ :files ].sort! { |a, b| a[ :lines ] <=> b[ :lines ] }
    files.reverse!
    files[ 0..12 ].each do |f|
      puts "  #{f[ :lines ]} - #{f[ :file ]}"
    end
  end

  formatted = Gloo::Utils::Format.number( total )
  puts "\n #{formatted} Total Lines of Code".white
end

#setup_loc(types) ⇒ Object

Setup counters for lines of code by file type.



126
127
128
129
130
131
132
# File 'lib/gloo/utils/stats.rb', line 126

def setup_loc( types )
  @loc = {}

  types.split( ' ' ).each do |t|
    @loc[ t ] = { lines: 0, files: [] }
  end
end

#show_allObject

Show all stat data for the project.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gloo/utils/stats.rb', line 47

def show_all
  return unless valid?

  generate
  puts "Showing All stats for #{@dir}".white
  puts "\n ** #{@dir_cnt} Total Folders ** "
  puts " ** #{@file_cnt} Total Files ** "

  busy_folders( 7 )
  file_types
  loc
end

#valid?Boolean

Is the stats utility valid? Does it have a valid root directory.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/gloo/utils/stats.rb', line 35

def valid?
  return true if @dir && File.directory?( @dir )

  @engine.err DIR_NOT_FOUND_ERR
  @engine.heap.it.set_to false

  return false
end