Class: Dbviewer::Storage::FileStorage
- Defined in:
- lib/dbviewer/storage/file_storage.rb
Overview
FileStorage implements QueryStorage for storing queries in a log file
Instance Method Summary collapse
-
#add(query) ⇒ Object
Add a new query to the storage.
-
#all ⇒ Object
Get all stored queries Note: This reads the entire log file - could be inefficient for large files.
-
#clear ⇒ Object
Clear all stored queries.
-
#filter(limit: 100, table_filter: nil, request_id: nil, min_duration: nil) ⇒ Object
Filter the queries based on provided criteria.
-
#initialize ⇒ FileStorage
constructor
A new instance of FileStorage.
Constructor Details
#initialize ⇒ FileStorage
Returns a new instance of FileStorage.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/dbviewer/storage/file_storage.rb', line 5 def initialize @mutex = Mutex.new @log_path = Dbviewer.configuration.query_log_path || "log/dbviewer.log" # Ensure directory exists FileUtils.mkdir_p(File.dirname(@log_path)) # Touch the file if it doesn't exist FileUtils.touch(@log_path) unless File.exist?(@log_path) end |
Instance Method Details
#add(query) ⇒ Object
Add a new query to the storage
25 26 27 28 29 30 31 32 33 |
# File 'lib/dbviewer/storage/file_storage.rb', line 25 def add(query) @mutex.synchronize do # Convert query to JSON and append to file query_json = query.to_json File.open(@log_path, "a") do |file| file.puts(query_json) end end end |
#all ⇒ Object
Get all stored queries Note: This reads the entire log file - could be inefficient for large files
18 19 20 21 22 |
# File 'lib/dbviewer/storage/file_storage.rb', line 18 def all @mutex.synchronize do read_queries_from_file end end |
#clear ⇒ Object
Clear all stored queries
36 37 38 39 40 41 |
# File 'lib/dbviewer/storage/file_storage.rb', line 36 def clear @mutex.synchronize do # Simply truncate the file File.open(@log_path, "w") { } end end |
#filter(limit: 100, table_filter: nil, request_id: nil, min_duration: nil) ⇒ Object
Filter the queries based on provided criteria
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dbviewer/storage/file_storage.rb', line 44 def filter(limit: 100, table_filter: nil, request_id: nil, min_duration: nil) result = all # Apply filters if provided result = filter_by_table(result, table_filter) if table_filter.present? result = filter_by_request_id(result, request_id) if request_id.present? result = filter_by_duration(result, min_duration) if min_duration.present? # Return most recent queries first, limited to requested amount result.reverse.first(limit) end |