Class: Profiler::Storage::FileStore
- Defined in:
- lib/profiler/storage/file_store.rb
Overview
File-based profile storage backend. Persists each profile as a JSON file under tmp_path and evicts the oldest files when total size exceeds max_size.
Instance Method Summary collapse
- #cleanup(older_than: 24 * 60 * 60) ⇒ Object
-
#clear(type: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #delete(token) ⇒ Object
- #find_by_parent(parent_token) ⇒ Object
-
#initialize(options = {}) ⇒ FileStore
constructor
A new instance of FileStore.
- #list(limit: 50, offset: 0) ⇒ Object
- #load(token) ⇒ Object
- #save(token, profile) ⇒ Object
Methods inherited from BaseStore
Constructor Details
#initialize(options = {}) ⇒ FileStore
Returns a new instance of FileStore.
13 14 15 16 17 18 |
# File 'lib/profiler/storage/file_store.rb', line 13 def initialize( = {}) super() @path = [:path] || default_path @max_size = [:max_size] || (100 * 1024 * 1024) # 100 MB ensure_directory_exists end |
Instance Method Details
#cleanup(older_than: 24 * 60 * 60) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/profiler/storage/file_store.rb', line 48 def cleanup(older_than: 24 * 60 * 60) cutoff_time = Time.now - older_than profile_files.each do |file| File.delete(file) if File.mtime(file) < cutoff_time rescue Errno::ENOENT nil end end |
#clear(type: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/profiler/storage/file_store.rb', line 69 def clear(type: nil) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity if type.nil? profile_files.each do |f| File.delete(f) rescue StandardError nil end else profile_files.each do |f| profile = load(File.basename(f, ".json")) File.delete(f) if profile&.profile_type == type.to_s rescue StandardError nil end end end |
#delete(token) ⇒ Object
65 66 67 |
# File 'lib/profiler/storage/file_store.rb', line 65 def delete(token) FileUtils.rm_f(profile_file_path(token)) end |
#find_by_parent(parent_token) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/profiler/storage/file_store.rb', line 57 def find_by_parent(parent_token) profile_files .map { |f| load(File.basename(f, ".json")) } .compact .select { |profile| profile.parent_token == parent_token } .sort_by(&:started_at) end |
#list(limit: 50, offset: 0) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/profiler/storage/file_store.rb', line 38 def list(limit: 50, offset: 0) profile_files .sort_by { |f| File.mtime(f) } .reverse .drop(offset) .take(limit) .map { |f| load(File.basename(f, ".json")) } .compact end |
#load(token) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/profiler/storage/file_store.rb', line 27 def load(token) file_path = profile_file_path(token) return nil unless File.exist?(file_path) json_data = File.read(file_path) Models::Profile.from_json(json_data) rescue StandardError => e warn "Failed to load profile #{token}: #{e.}" nil end |
#save(token, profile) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/profiler/storage/file_store.rb', line 20 def save(token, profile) file_path = profile_file_path(token) File.write(file_path, profile.to_json) cleanup_if_needed token end |