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.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dbviewer/storage/file_storage.rb', line 8 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
28 29 30 31 32 33 34 35 36 |
# File 'lib/dbviewer/storage/file_storage.rb', line 28 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
21 22 23 24 25 |
# File 'lib/dbviewer/storage/file_storage.rb', line 21 def all @mutex.synchronize do read_queries_from_file end end |
#clear ⇒ Object
Clear all stored queries
39 40 41 42 43 44 |
# File 'lib/dbviewer/storage/file_storage.rb', line 39 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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dbviewer/storage/file_storage.rb', line 47 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 |