Class: Solidstats::LogSizeMonitorService

Inherits:
Object
  • Object
show all
Defined in:
app/services/solidstats/log_size_monitor_service.rb

Constant Summary collapse

CACHE_FILE =
"logs.json"
SUMMARY_FILE =
"summary.json"
WARNING_SIZE =

Size thresholds in bytes

500.megabytes
ERROR_SIZE =
1.gigabyte

Class Method Summary collapse

Class Method Details

.get_logs_dataObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/services/solidstats/log_size_monitor_service.rb', line 10

def self.get_logs_data
  cache_file_path = solidstats_cache_path(CACHE_FILE)

  if File.exist?(cache_file_path) && cache_fresh?(cache_file_path)
    JSON.parse(File.read(cache_file_path))
  else
    scan_and_cache
  end
rescue JSON::ParserError, Errno::ENOENT
  Rails.logger.error("Error reading logs cache, regenerating...")
  scan_and_cache
end

.scan_and_cacheObject



23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/solidstats/log_size_monitor_service.rb', line 23

def self.scan_and_cache
  logs_data = scan_log_files

  # Cache the logs data
  cache_logs_data(logs_data)

  # Update summary.json with log monitoring card
  update_summary_json(logs_data)

  logs_data
end

.truncate_log(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/solidstats/log_size_monitor_service.rb', line 35

def self.truncate_log(filename)
  return { status: "error", message: "Filename required" } if filename.blank?

  log_file_path = find_log_file(filename)
  return { status: "error", message: "Log file not found" } unless log_file_path

  begin
    # Check if file is writable
    unless File.writable?(log_file_path)
      return { status: "error", message: "File is not writable" }
    end

    # Get original size for reporting
    original_size = File.size(log_file_path)

    # Truncate the file
    File.truncate(log_file_path, 0)

    # Update cache after truncation
    scan_and_cache

    {
      status: "success",
      message: "Log file truncated successfully",
      original_size: format_file_size(original_size),
      filename: filename
    }
  rescue => e
    Rails.logger.error("Error truncating log file #{filename}: #{e.message}")
    { status: "error", message: "Failed to truncate: #{e.message}" }
  end
end