Class: StorageGuardian::BloatDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/storage_guardian/bloat_detector.rb

Instance Method Summary collapse

Constructor Details

#initialize(entries, budget) ⇒ BloatDetector

Returns a new instance of BloatDetector.



5
6
7
8
# File 'lib/storage_guardian/bloat_detector.rb', line 5

def initialize(entries, budget)
  @entries = entries
  @budget  = budget
end

Instance Method Details

#detectObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/storage_guardian/bloat_detector.rb', line 10

def detect
  findings = []

  # Oversized files
  max_file = @budget.limit_for(:max_file_size_mb) * 1024 * 1024
  @entries.each do |e|
    if e.size > max_file
      findings << {
        type:     :oversized_file,
        severity: :high,
        path:     e.path,
        size_mb:  (e.size / 1024.0 / 1024.0).round(2),
        limit_mb: @budget.limit_for(:max_file_size_mb),
        message:  "File #{e.path} is #{(e.size / 1024.0 / 1024.0).round(2)}MB (limit: #{@budget.limit_for(:max_file_size_mb)}MB)"
      }
    end
  end

  # Bloated directories
  max_dir = @budget.limit_for(:max_dir_size_mb) * 1024 * 1024
  dir_sizes = @entries.each_with_object(Hash.new(0)) { |e, h| h[File.dirname(e.path)] += e.size }
  dir_sizes.each do |dir, size|
    if size > max_dir
      findings << {
        type:     :bloated_directory,
        severity: :medium,
        path:     dir,
        size_mb:  (size / 1024.0 / 1024.0).round(2),
        limit_mb: @budget.limit_for(:max_dir_size_mb),
        message:  "Directory #{dir} is #{(size / 1024.0 / 1024.0).round(2)}MB (limit: #{@budget.limit_for(:max_dir_size_mb)}MB)"
      }
    end
  end

  findings
end