Class: Solidstats::LoadLensService

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

Overview

LoadLens - Development Performance Monitoring Service Parses Rails development logs to extract performance metrics

Constant Summary collapse

DATA_DIR =
Rails.root.join("solidstats")
LOG_FILE =
Rails.root.join("log", "development.log")
POSITION_FILE =
DATA_DIR.join("last_position.txt")
CACHE_FILE =
"loadlens.json"
SUMMARY_FILE =
"summary.json"
RETENTION_DAYS =
7
REQUEST_START_REGEX =

Regex patterns for parsing Rails development log

/Started\s+(\w+)\s+"([^"]+)"\s+for\s+[\d\.:]+\s+at\s+([\d\-\s:]+)/
CONTROLLER_ACTION_REGEX =
/Processing\s+by\s+([^#]+)#(\w+)\s+as/
COMPLETED_REGEX =

Updated to capture all timing info from the completion line

/Completed\s+(\d+)\s+\w+\s+in\s+([\d\.]+)ms(?:\s+\(([^)]+)\))?/
VIEW_RENDERING_REGEX =

Separate patterns for extracting times from the completion line details

/Views:\s+([\d\.]+)ms/
ACTIVERECORD_REGEX =
/ActiveRecord:\s+([\d\.]+)ms/

Class Method Summary collapse

Class Method Details

.get_performance_dataObject



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

def self.get_performance_data
  cache_file_path = solidstats_cache_path(CACHE_FILE)

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

.parse_log_and_saveObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/solidstats/load_lens_service.rb', line 56

def self.parse_log_and_save
  return unless Rails.env.development?
  return unless File.exist?(LOG_FILE)

  ensure_data_directory
  cleanup_old_files

  begin
    last_position = read_last_position
    processed_count = 0
    current_requests = []

    File.open(LOG_FILE, "r") do |file|
      file.seek(last_position)

      file.each_line do |line|
        process_line(line.strip, current_requests)
        update_last_position(file.pos)
      end
    end

    # Process any remaining incomplete requests
    current_requests.each do |req|
      if req[:completed]
        save_request(req)
        processed_count += 1
      end
    end

    Rails.logger.info("DevLogParser: Processed #{processed_count} requests")
    { success: true, processed: processed_count }
  rescue => e
    Rails.logger.error("DevLogParser: Failed to parse development log: #{e.message}")
    { success: false, error: e.message }
  end
end

.refresh_dataObject



49
50
51
52
53
54
# File 'app/services/solidstats/load_lens_service.rb', line 49

def self.refresh_data
  # Force refresh by removing cache and regenerating
  cache_file_path = solidstats_cache_path(CACHE_FILE)
  File.delete(cache_file_path) if File.exist?(cache_file_path)
  scan_and_cache
end

.scan_and_cacheObject



37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/solidstats/load_lens_service.rb', line 37

def self.scan_and_cache
  performance_data = scan_development_log

  # Cache the performance data
  cache_performance_data(performance_data)

  # Update summary.json with performance monitoring card
  update_summary_json(performance_data)

  deep_indifferent_access(performance_data)
end