Class: Aidp::Storage::CsvStorage
- Inherits:
-
Object
- Object
- Aidp::Storage::CsvStorage
- Includes:
- RescueLogging
- Defined in:
- lib/aidp/storage/csv_storage.rb
Overview
Simple CSV file storage for tabular data
Instance Method Summary collapse
-
#append(filename, row_data) ⇒ Object
Append a row to CSV file.
-
#count_rows(filename) ⇒ Object
Count rows in CSV file.
-
#delete(filename) ⇒ Object
Delete file.
-
#exists?(filename) ⇒ Boolean
Check if file exists.
-
#initialize(base_dir = ".aidp") ⇒ CsvStorage
constructor
A new instance of CsvStorage.
-
#list ⇒ Object
List all CSV files.
-
#read_all(filename) ⇒ Object
Read all rows from CSV file.
-
#read_filtered(filename, filters = {}) ⇒ Object
Read rows with filtering.
-
#summary(filename) ⇒ Object
Get summary statistics.
-
#unique_values(filename, column) ⇒ Object
Get unique values for a column.
Methods included from RescueLogging
__log_rescue_impl, log_rescue, #log_rescue
Constructor Details
#initialize(base_dir = ".aidp") ⇒ CsvStorage
Returns a new instance of CsvStorage.
13 14 15 16 |
# File 'lib/aidp/storage/csv_storage.rb', line 13 def initialize(base_dir = ".aidp") @base_dir = sanitize_base_dir(base_dir) ensure_directory_exists end |
Instance Method Details
#append(filename, row_data) ⇒ Object
Append a row to CSV file
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/aidp/storage/csv_storage.rb', line 19 def append(filename, row_data) file_path = get_file_path(filename) FileUtils.mkdir_p(File.dirname(file_path)) # Add timestamp if not present row_data["created_at"] ||= Time.now.iso8601 # Convert all values to strings row_data = row_data.transform_values(&:to_s) # If file doesn't exist, write headers first if !File.exist?(file_path) CSV.open(file_path, "w") do |csv| csv << row_data.keys end end # Append the row CSV.open(file_path, "a") do |csv| csv << row_data.values end { filename: filename, file_path: file_path, row_count: count_rows(filename), success: true } rescue => error log_rescue(error, component: "csv_storage", action: "append", fallback: {success: false}, filename: filename, path: file_path) {filename: filename, error: error., success: false} end |
#count_rows(filename) ⇒ Object
Count rows in CSV file
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/aidp/storage/csv_storage.rb', line 88 def count_rows(filename) file_path = get_file_path(filename) return 0 unless File.exist?(file_path) count = 0 CSV.foreach(file_path) { count += 1 } count - 1 # Subtract 1 for header row rescue => error log_rescue(error, component: "csv_storage", action: "count_rows", fallback: 0, filename: filename, path: (defined?(file_path) ? file_path : nil)) 0 end |
#delete(filename) ⇒ Object
Delete file
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/aidp/storage/csv_storage.rb', line 160 def delete(filename) file_path = get_file_path(filename) return {success: true, message: "File does not exist"} unless File.exist?(file_path) File.delete(file_path) {success: true, message: "File deleted"} rescue => error log_rescue(error, component: "csv_storage", action: "delete", fallback: {success: false}, filename: filename, path: file_path) {success: false, error: error.} end |
#exists?(filename) ⇒ Boolean
Check if file exists
155 156 157 |
# File 'lib/aidp/storage/csv_storage.rb', line 155 def exists?(filename) File.exist?(get_file_path(filename)) end |
#list ⇒ Object
List all CSV files
177 178 179 180 181 182 183 |
# File 'lib/aidp/storage/csv_storage.rb', line 177 def list return [] unless Dir.exist?(@base_dir) Dir.glob(File.join(@base_dir, "**", "*.csv")).map do |file| File.basename(file, ".csv") end end |
#read_all(filename) ⇒ Object
Read all rows from CSV file
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/aidp/storage/csv_storage.rb', line 58 def read_all(filename) file_path = get_file_path(filename) return [] unless File.exist?(file_path) rows = [] CSV.foreach(file_path, headers: true) do |row| rows << row.to_h end rows rescue => error log_rescue(error, component: "csv_storage", action: "read_all", fallback: [], filename: filename, path: (defined?(file_path) ? file_path : nil)) [] end |
#read_filtered(filename, filters = {}) ⇒ Object
Read rows with filtering
78 79 80 81 82 83 84 85 |
# File 'lib/aidp/storage/csv_storage.rb', line 78 def read_filtered(filename, filters = {}) all_rows = read_all(filename) return all_rows if filters.empty? all_rows.select do |row| filters.all? { |key, value| row[key.to_s] == value.to_s } end end |
#summary(filename) ⇒ Object
Get summary statistics
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/aidp/storage/csv_storage.rb', line 112 def summary(filename) file_path = get_file_path(filename) return nil unless File.exist?(file_path) rows = read_all(filename) return nil if rows.empty? headers = rows.first.keys numeric_columns = headers.select do |col| rows.all? { |row| row[col] =~ /^-?\d+\.?\d*$/ } end summary_data = { filename: filename, file_path: file_path, total_rows: rows.length, columns: headers, numeric_columns: numeric_columns, file_size: File.size(file_path) } # Add basic stats for numeric columns numeric_columns.each do |col| values = rows.map { |row| row[col].to_f } summary_data["#{col}_stats"] = { min: values.min, max: values.max, avg: values.sum / values.length } end summary_data rescue => error log_rescue(error, component: "csv_storage", action: "summary", fallback: nil, filename: filename, path: (defined?(file_path) ? file_path : nil)) nil end |
#unique_values(filename, column) ⇒ Object
Get unique values for a column
106 107 108 109 |
# File 'lib/aidp/storage/csv_storage.rb', line 106 def unique_values(filename, column) all_rows = read_all(filename) all_rows.map { |row| row[column.to_s] }.compact.uniq end |