Class: Aidp::Storage::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/storage/file_manager.rb

Overview

Simple file manager that provides easy access to JSON and CSV storage

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = ".aidp") ⇒ FileManager

Returns a new instance of FileManager.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/aidp/storage/file_manager.rb', line 10

def initialize(base_dir = ".aidp")
  @base_dir = sanitize_base_dir(base_dir)
  @json_storage = JsonStorage.new(@base_dir)
  @csv_storage = CsvStorage.new(@base_dir)
  # Normalize base_dir if storages had to fallback
  json_dir = @json_storage.instance_variable_get(:@base_dir)
  csv_dir = @csv_storage.instance_variable_get(:@base_dir)
  if json_dir != @base_dir || csv_dir != @base_dir
    @base_dir = json_dir # Prefer JSON storage directory
    warn_storage("[AIDP Storage] Base directory normalized to #{@base_dir} after fallback.")
  end
end

Instance Method Details

#append_csv(filename, row_data) ⇒ Object

CSV operations for tabular data



45
46
47
# File 'lib/aidp/storage/file_manager.rb', line 45

def append_csv(filename, row_data)
  @csv_storage.append(filename, row_data)
end

#backup_to(destination_dir) ⇒ Object

Backup and restore



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/aidp/storage/file_manager.rb', line 193

def backup_to(destination_dir)
  FileUtils.mkdir_p(destination_dir)
  # Copy contents of base_dir to destination_dir, avoiding recursive copying
  if Dir.exist?(@base_dir)
    Dir.glob(File.join(@base_dir, "*")).each do |item|
      next if File.expand_path(item) == File.expand_path(destination_dir)
      FileUtils.cp_r(item, destination_dir)
    end
  end
  {success: true, backup_location: destination_dir}
rescue => error
  {success: false, error: error.message}
end

#csv_exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/aidp/storage/file_manager.rb', line 61

def csv_exists?(filename)
  @csv_storage.exists?(filename)
end

#csv_summary(filename) ⇒ Object



57
58
59
# File 'lib/aidp/storage/file_manager.rb', line 57

def csv_summary(filename)
  @csv_storage.summary(filename)
end

#get_metrics(filters = {}) ⇒ Object



106
107
108
# File 'lib/aidp/storage/file_manager.rb', line 106

def get_metrics(filters = {})
  read_csv("metrics", filters)
end

#get_metrics_summaryObject



110
111
112
# File 'lib/aidp/storage/file_manager.rb', line 110

def get_metrics_summary
  csv_summary("metrics")
end

#get_provider_activities(filters = {}) ⇒ Object



151
152
153
# File 'lib/aidp/storage/file_manager.rb', line 151

def get_provider_activities(filters = {})
  read_csv("provider_activities", filters)
end

#get_provider_activities_summaryObject



155
156
157
# File 'lib/aidp/storage/file_manager.rb', line 155

def get_provider_activities_summary
  csv_summary("provider_activities")
end

#get_step_executions(filters = {}) ⇒ Object



127
128
129
# File 'lib/aidp/storage/file_manager.rb', line 127

def get_step_executions(filters = {})
  read_csv("step_executions", filters)
end

#get_step_executions_summaryObject



131
132
133
# File 'lib/aidp/storage/file_manager.rb', line 131

def get_step_executions_summary
  csv_summary("step_executions")
end

#json_exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/aidp/storage/file_manager.rb', line 36

def json_exists?(filename)
  @json_storage.exists?(filename)
end

#json_metadata(filename) ⇒ Object



40
41
42
# File 'lib/aidp/storage/file_manager.rb', line 40

def (filename)
  @json_storage.(filename)
end

#list_all_filesObject



185
186
187
188
189
190
# File 'lib/aidp/storage/file_manager.rb', line 185

def list_all_files
  {
    json_files: list_json_files,
    csv_files: list_csv_files
  }
end

#list_csv_filesObject



181
182
183
# File 'lib/aidp/storage/file_manager.rb', line 181

def list_csv_files
  @csv_storage.list
end

#list_json_filesObject

List all files



177
178
179
# File 'lib/aidp/storage/file_manager.rb', line 177

def list_json_files
  @json_storage.list
end

#load_analysis_resultObject



77
78
79
# File 'lib/aidp/storage/file_manager.rb', line 77

def load_analysis_result
  load_json("analysis_results")
end

#load_configObject



164
165
166
# File 'lib/aidp/storage/file_manager.rb', line 164

def load_config
  load_json("config")
end

#load_embeddingsObject



90
91
92
# File 'lib/aidp/storage/file_manager.rb', line 90

def load_embeddings
  load_json("embeddings")
end

#load_json(filename) ⇒ Object



28
29
30
# File 'lib/aidp/storage/file_manager.rb', line 28

def load_json(filename)
  @json_storage.load(filename)
end

#load_statusObject



172
173
174
# File 'lib/aidp/storage/file_manager.rb', line 172

def load_status
  load_json("status")
end

#read_csv(filename, filters = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/aidp/storage/file_manager.rb', line 49

def read_csv(filename, filters = {})
  if filters.empty?
    @csv_storage.read_all(filename)
  else
    @csv_storage.read_filtered(filename, filters)
  end
end

#record_metric(step_name, metric_name, value, metadata = {}) ⇒ Object

Metrics (tabular data)



95
96
97
98
99
100
101
102
103
104
# File 'lib/aidp/storage/file_manager.rb', line 95

def record_metric(step_name, metric_name, value,  = {})
  row_data = {
    step_name: step_name,
    metric_name: metric_name,
    value: value,
    recorded_at: Time.now.iso8601
  }.merge()

  append_csv("metrics", row_data)
end

#record_provider_activity(provider_name, step_name, start_time, end_time, duration, final_state, stuck_detected = false) ⇒ Object

Provider activities (tabular data)



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aidp/storage/file_manager.rb', line 136

def record_provider_activity(provider_name, step_name, start_time, end_time, duration, final_state, stuck_detected = false)
  row_data = {
    provider_name: provider_name,
    step_name: step_name,
    start_time: start_time&.iso8601,
    end_time: end_time&.iso8601,
    duration: duration,
    final_state: final_state,
    stuck_detected: stuck_detected,
    created_at: Time.now.iso8601
  }

  append_csv("provider_activities", row_data)
end

#record_step_execution(step_name, provider_name, duration, success, metadata = {}) ⇒ Object

Step executions (tabular data)



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/aidp/storage/file_manager.rb', line 115

def record_step_execution(step_name, provider_name, duration, success,  = {})
  row_data = {
    step_name: step_name,
    provider_name: provider_name,
    duration: duration,
    success: success,
    created_at: Time.now.iso8601
  }.merge()

  append_csv("step_executions", row_data)
end

#restore_from(source_dir) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/aidp/storage/file_manager.rb', line 207

def restore_from(source_dir)
  return {success: false, error: "Source directory does not exist"} unless Dir.exist?(source_dir)

  # Clear existing data
  FileUtils.rm_rf(@base_dir) if Dir.exist?(@base_dir)

  # Copy from source
  FileUtils.cp_r(source_dir, @base_dir)
  {success: true, restored_from: source_dir}
rescue => error
  {success: false, error: error.message}
end

#store_analysis_result(step_name, data, metadata = {}) ⇒ Object

Analysis results (structured data)



68
69
70
71
72
73
74
75
# File 'lib/aidp/storage/file_manager.rb', line 68

def store_analysis_result(step_name, data,  = {})
  result = {
    step_name: step_name,
    data: data,
    metadata: 
  }
  store_json("analysis_results", result)
end

#store_config(config_data) ⇒ Object

Configuration and status (structured data)



160
161
162
# File 'lib/aidp/storage/file_manager.rb', line 160

def store_config(config_data)
  store_json("config", config_data)
end

#store_embeddings(step_name, embeddings_data) ⇒ Object

Embeddings (structured data)



82
83
84
85
86
87
88
# File 'lib/aidp/storage/file_manager.rb', line 82

def store_embeddings(step_name, embeddings_data)
  result = {
    step_name: step_name,
    embeddings_data: embeddings_data
  }
  store_json("embeddings", result)
end

#store_json(filename, data) ⇒ Object

JSON operations for structured data



24
25
26
# File 'lib/aidp/storage/file_manager.rb', line 24

def store_json(filename, data)
  @json_storage.store(filename, data)
end

#store_status(status_data) ⇒ Object



168
169
170
# File 'lib/aidp/storage/file_manager.rb', line 168

def store_status(status_data)
  store_json("status", status_data)
end

#update_json(filename, data) ⇒ Object



32
33
34
# File 'lib/aidp/storage/file_manager.rb', line 32

def update_json(filename, data)
  @json_storage.update(filename, data)
end