Class: Aidp::Analyze::JsonFileStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/analyze/json_file_storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_dir = Dir.pwd, storage_dir = ".aidp/json") ⇒ JsonFileStorage

Returns a new instance of JsonFileStorage.



9
10
11
12
13
# File 'lib/aidp/analyze/json_file_storage.rb', line 9

def initialize(project_dir = Dir.pwd, storage_dir = ".aidp/json")
  @project_dir = project_dir
  @storage_dir = File.join(project_dir, storage_dir)
  ensure_storage_directory
end

Instance Method Details

#analysis_session(session_id) ⇒ Object

Get analysis session data



122
123
124
# File 'lib/aidp/analyze/json_file_storage.rb', line 122

def analysis_session(session_id)
  data("sessions/#{session_id}.json")
end

#cache(cache_key) ⇒ Object

Get cache data (respects TTL)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/aidp/analyze/json_file_storage.rb', line 164

def cache(cache_key)
  cache_file_data = data("cache/#{cache_key}.json")
  return nil unless cache_file_data

  # Check TTL if specified
  if cache_file_data["ttl_seconds"]
    cached_at = Time.parse(cache_file_data["cached_at"])
    if Time.now - cached_at > cache_file_data["ttl_seconds"]
      # Cache expired, delete it
      delete_data("cache/#{cache_key}.json")
      return nil
    end
  end

  cache_file_data["data"]
end

#clear_expired_cacheObject

Clear expired cache entries



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/aidp/analyze/json_file_storage.rb', line 182

def clear_expired_cache
  cache_dir = File.join(@storage_dir, "cache")
  return 0 unless Dir.exist?(cache_dir)

  cleared_count = 0
  Dir.glob(File.join(cache_dir, "*.json")).each do |file_path|
    cache_data = JSON.parse(File.read(file_path))
    if cache_data["ttl_seconds"] && cache_data["cached_at"]
      cached_at = Time.parse(cache_data["cached_at"])
      if Time.now - cached_at > cache_data["ttl_seconds"]
        File.delete(file_path)
        cleared_count += 1
      end
    end
  rescue JSON::ParserError
    # Invalid JSON, delete the file
    File.delete(file_path)
    cleared_count += 1
  end

  cleared_count
end

#data(filename) ⇒ Object

Retrieve data from a JSON file



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aidp/analyze/json_file_storage.rb', line 34

def data(filename)
  file_path = file_path(filename)

  return nil unless File.exist?(file_path)

  begin
    JSON.parse(File.read(file_path))
  rescue JSON::ParserError => e
    raise "Invalid JSON in file #{filename}: #{e.message}"
  end
end

#data_exists?(filename) ⇒ Boolean

Check if a JSON file exists

Returns:

  • (Boolean)


47
48
49
# File 'lib/aidp/analyze/json_file_storage.rb', line 47

def data_exists?(filename)
  File.exist?(file_path(filename))
end

#delete_data(filename) ⇒ Object

Delete a JSON file



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aidp/analyze/json_file_storage.rb', line 52

def delete_data(filename)
  file_path = file_path(filename)

  if File.exist?(file_path)
    File.delete(file_path)
    {
      filename: filename,
      deleted: true,
      deleted_at: Time.now
    }
  else
    {
      filename: filename,
      deleted: false,
      message: "File does not exist"
    }
  end
end

#export_all_data(export_filename = "aidp_data_export.json") ⇒ Object

Export all data to a single JSON file



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aidp/analyze/json_file_storage.rb', line 220

def export_all_data(export_filename = "aidp_data_export.json")
  export_data = {
    "exported_at" => Time.now.iso8601,
    "storage_directory" => @storage_dir,
    "files" => {}
  }

  files = list_files
  files.each do |file_info|
    data = data(file_info[:filename])
    export_data["files"][file_info[:filename]] = {
      "data" => data,
      "metadata" => {
        "size" => file_info[:size],
        "modified_at" => file_info[:modified_at]
      }
    }
  end

  export_path = File.join(@storage_dir, export_filename)
  File.write(export_path, JSON.pretty_generate(export_data))

  {
    export_filename: export_filename,
    export_path: export_path,
    files_exported: files.length,
    exported_at: Time.now
  }
end

#import_data(import_filename) ⇒ Object

Import data from an exported JSON file



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/aidp/analyze/json_file_storage.rb', line 251

def import_data(import_filename)
  import_path = file_path(import_filename)

  unless File.exist?(import_path)
    raise "Import file does not exist: #{import_filename}"
  end

  begin
    import_data = JSON.parse(File.read(import_path))
  rescue JSON::ParserError => e
    raise "Invalid JSON in import file: #{e.message}"
  end

  unless import_data["files"]
    raise "Invalid import file format: missing 'files' key"
  end

  imported_count = 0
  import_data["files"].each do |filename, file_data|
    store_data(filename, file_data["data"])
    imported_count += 1
  end

  {
    imported_count: imported_count,
    imported_at: Time.now,
    success: true
  }
end

#list_analysis_sessionsObject

List analysis sessions



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/aidp/analyze/json_file_storage.rb', line 127

def list_analysis_sessions
  sessions_dir = File.join(@storage_dir, "sessions")
  return [] unless Dir.exist?(sessions_dir)

  Dir.glob(File.join(sessions_dir, "*.json")).map do |file_path|
    session_id = File.basename(file_path, ".json")
    {
      session_id: session_id,
      file_path: file_path,
      size: File.size(file_path),
      modified_at: File.mtime(file_path)
    }
  end
end

#list_filesObject

List all JSON files in the storage directory



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aidp/analyze/json_file_storage.rb', line 72

def list_files
  return [] unless Dir.exist?(@storage_dir)

  Dir.glob(File.join(@storage_dir, "**", "*.json")).map do |file_path|
    relative_path = file_path.sub("#{@storage_dir}/", "")
    {
      filename: relative_path,
      file_path: file_path,
      size: File.size(file_path),
      modified_at: File.mtime(file_path)
    }
  end
end

#project_configObject

Get project configuration



92
93
94
# File 'lib/aidp/analyze/json_file_storage.rb', line 92

def project_config
  data("project_config.json")
end

#runtime_statusObject

Get runtime status



102
103
104
# File 'lib/aidp/analyze/json_file_storage.rb', line 102

def runtime_status
  data("runtime_status.json")
end

#simple_metricsObject

Get simple metrics



112
113
114
# File 'lib/aidp/analyze/json_file_storage.rb', line 112

def simple_metrics
  data("simple_metrics.json")
end

#storage_statisticsObject

Get storage statistics



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/aidp/analyze/json_file_storage.rb', line 206

def storage_statistics
  files = list_files

  {
    total_files: files.length,
    total_size: files.sum { |f| f[:size] },
    storage_directory: @storage_dir,
    oldest_file: files.min_by { |f| f[:modified_at] }&.dig(:modified_at),
    newest_file: files.max_by { |f| f[:modified_at] }&.dig(:modified_at),
    file_types: files.group_by { |f| File.extname(f[:filename]) }.transform_values(&:count)
  }
end

#store_analysis_session(session_id, session_data) ⇒ Object

Store analysis session data



117
118
119
# File 'lib/aidp/analyze/json_file_storage.rb', line 117

def store_analysis_session(session_id, session_data)
  store_data("sessions/#{session_id}.json", session_data)
end

#store_cache(cache_key, cache_data, ttl_seconds = nil) ⇒ Object

Store cache data



153
154
155
156
157
158
159
160
161
# File 'lib/aidp/analyze/json_file_storage.rb', line 153

def store_cache(cache_key, cache_data, ttl_seconds = nil)
  cache_data_with_ttl = {
    data: cache_data,
    cached_at: Time.now.iso8601,
    ttl_seconds: ttl_seconds
  }

  store_data("cache/#{cache_key}.json", cache_data_with_ttl)
end

#store_data(filename, data) ⇒ Object

Store data in a JSON file



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aidp/analyze/json_file_storage.rb', line 16

def store_data(filename, data)
  file_path = file_path(filename)

  # Ensure directory exists
  FileUtils.mkdir_p(File.dirname(file_path))

  # Write data as pretty JSON
  File.write(file_path, JSON.pretty_generate(data))

  {
    filename: filename,
    file_path: file_path,
    stored_at: Time.now,
    success: true
  }
end

#store_project_config(config_data) ⇒ Object

Store project configuration



87
88
89
# File 'lib/aidp/analyze/json_file_storage.rb', line 87

def store_project_config(config_data)
  store_data("project_config.json", config_data)
end

#store_runtime_status(status_data) ⇒ Object

Store runtime status



97
98
99
# File 'lib/aidp/analyze/json_file_storage.rb', line 97

def store_runtime_status(status_data)
  store_data("runtime_status.json", status_data)
end

#store_simple_metrics(metrics_data) ⇒ Object

Store simple metrics



107
108
109
# File 'lib/aidp/analyze/json_file_storage.rb', line 107

def store_simple_metrics(metrics_data)
  store_data("simple_metrics.json", metrics_data)
end

#store_user_preferences(preferences_data) ⇒ Object

Store user preferences



143
144
145
# File 'lib/aidp/analyze/json_file_storage.rb', line 143

def store_user_preferences(preferences_data)
  store_data("user_preferences.json", preferences_data)
end

#user_preferencesObject

Get user preferences



148
149
150
# File 'lib/aidp/analyze/json_file_storage.rb', line 148

def user_preferences
  data("user_preferences.json")
end