Class: Sentiero::Stores::File
- Inherits:
-
Sentiero::Store
- Object
- Sentiero::Store
- Sentiero::Stores::File
- Defined in:
- lib/sentiero/stores/file.rb
Constant Summary
Constants inherited from Sentiero::Store
Sentiero::Store::MAX_METADATA_KEYS, Sentiero::Store::MAX_METADATA_VALUE_SIZE, Sentiero::Store::PROBLEM_TITLE_MAX, Sentiero::Store::VALID_ID, Sentiero::Store::VALID_STATUS
Instance Attribute Summary
Attributes inherited from Sentiero::Store
Instance Method Summary collapse
- #clear! ⇒ Object
- #count_occurrences(problem_id, after: nil) ⇒ Object
- #delete_session(session_id) ⇒ Object
- #delete_window(ref) ⇒ Object
- #get_events(ref, after: nil, limit: nil) ⇒ Object
- #get_occurrences(problem_id, after: nil, limit: nil) ⇒ Object
- #get_problem(problem_id) ⇒ Object
- #get_server_event(event_id) ⇒ Object
- #get_session(session_id) ⇒ Object
-
#initialize(path:, limits: nil) ⇒ File
constructor
File-based store, single-process dev/test only.
- #list_problems(project:, limit:, offset: 0, status: nil, sort_by: nil, search: nil, since: nil, until_time: nil) ⇒ Object
- #list_server_events(project:, limit:, name: nil, level: nil, session_id: nil, after: nil) ⇒ Object
- #list_sessions(limit:, offset: 0, since: nil, until_time: nil, sort_by: nil, search: nil) ⇒ Object
- #occurrences_for_session(session_id, limit: nil) ⇒ Object
-
#purge_older_than(seconds) ⇒ Object
Scan meta.json directly: the base list_sessions path is capped and newest-first, so it would never see the oldest (stale) sessions.
- #save_events(ref, events) ⇒ Object
- #save_metadata(session_id, metadata) ⇒ Object
- #save_occurrence(occurrence) ⇒ Object
- #save_server_event(event) ⇒ Object
- #server_events_for_session(session_id, limit: nil) ⇒ Object
- #session_ids_for_problem(problem_id, limit: nil) ⇒ Object
- #update_problem_status(problem_id, status) ⇒ Object
Methods included from Sentiero::Store::SessionStore
Constructor Details
#initialize(path:, limits: nil) ⇒ File
File-based store, single-process dev/test only. Each session is a directory: meta.json (timestamps, metadata) + window_id.jsonl (one JSON event per line).
14 15 16 17 18 |
# File 'lib/sentiero/stores/file.rb', line 14 def initialize(path:, limits: nil) @limits = limits @root = ::File.(path) FileUtils.mkdir_p(@root) end |
Instance Method Details
#clear! ⇒ Object
290 291 292 293 |
# File 'lib/sentiero/stores/file.rb', line 290 def clear! FileUtils.rm_rf(Dir.glob(::File.join(@root, "*"))) nil end |
#count_occurrences(problem_id, after: nil) ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'lib/sentiero/stores/file.rb', line 219 def count_occurrences(problem_id, after: nil) validate_id!(problem_id) _problems, occurrences, _server_events = read_error_data list = occurrences[problem_id] || [] return list.size unless after after_f = after.to_f list.count { |occ| occ["timestamp"].to_f > after_f } end |
#delete_session(session_id) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sentiero/stores/file.rb', line 144 def delete_session(session_id) validate_id!(session_id) dir = session_path(session_id) FileUtils.rm_rf(dir) if ::File.directory?(dir) update_error_data do |_problems, occurrences, server_events| occurrences.each_value { |list| list.reject! { |occ| occ["session_id"] == session_id } } server_events.reject! { |event| event["session_id"] == session_id } end nil end |
#delete_window(ref) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/sentiero/stores/file.rb', line 157 def delete_window(ref) validate_window_ref!(ref) session_id = ref.session_id window_id = ref.window_id FileUtils.rm_f(window_path(session_id, window_id)) # Only the session directory (meta.json + window files) is replay # data; error data lives in the shared root-level JSON files, so # this must not go through delete_session (which also erases those). remaining = list_window_ids(session_id) FileUtils.rm_rf(session_path(session_id)) if remaining.empty? nil end |
#get_events(ref, after: nil, limit: nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/sentiero/stores/file.rb', line 115 def get_events(ref, after: nil, limit: nil) validate_window_ref!(ref) session_id = ref.session_id window_id = ref.window_id events = read_window_events(session_id, window_id).sort_by { |event| event["timestamp"].to_f } if after idx = events.index { |event| event["timestamp"].to_f > after.to_f } events = idx ? events[idx..] : [] end limit ? events.first(limit) : events end |
#get_occurrences(problem_id, after: nil, limit: nil) ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/sentiero/stores/file.rb', line 210 def get_occurrences(problem_id, after: nil, limit: nil) validate_id!(problem_id) _problems, occurrences, _server_events = read_error_data list = occurrences[problem_id] || [] result = list.sort_by { |occ| occ["timestamp"].to_f } result = result.select { |occ| occ["timestamp"].to_f > after.to_f } if after limit ? result.first(limit) : result end |
#get_problem(problem_id) ⇒ Object
204 205 206 207 208 |
# File 'lib/sentiero/stores/file.rb', line 204 def get_problem(problem_id) validate_id!(problem_id) problems, _occurrences, _server_events = read_error_data problems[problem_id]&.dup end |
#get_server_event(event_id) ⇒ Object
253 254 255 256 257 |
# File 'lib/sentiero/stores/file.rb', line 253 def get_server_event(event_id) validate_id!(event_id) _problems, _occurrences, server_events = read_error_data server_events.find { |e| e["id"] == event_id } end |
#get_session(session_id) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/sentiero/stores/file.rb', line 93 def get_session(session_id) validate_id!(session_id) = (session_id) return nil unless window_ids = list_window_ids(session_id) return nil if window_ids.empty? window_data = window_ids.map { |wid| events = read_window_events(session_id, wid) = events.filter_map { |event| event["timestamp"]&.to_f } window = {window_id: wid, event_count: events.size} if .any? window[:first_event_at] = .min window[:last_event_at] = .max end window } {session_id: session_id, windows: window_data}.merge(()) end |
#list_problems(project:, limit:, offset: 0, status: nil, sort_by: nil, search: nil, since: nil, until_time: nil) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/sentiero/stores/file.rb', line 189 def list_problems(project:, limit:, offset: 0, status: nil, sort_by: nil, search: nil, since: nil, until_time: nil) problems, _occurrences, _server_events = read_error_data filter_and_page_problems( problems.values, project: project, status: status, since: since, until_time: until_time, search: search, sort_by: sort_by, offset: offset, limit: limit ) end |
#list_server_events(project:, limit:, name: nil, level: nil, session_id: nil, after: nil) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/sentiero/stores/file.rb', line 259 def list_server_events(project:, limit:, name: nil, level: nil, session_id: nil, after: nil) _problems, _occurrences, server_events = read_error_data filter_server_events( server_events, project: project, name: name, level: level, session_id: session_id, after: after, limit: limit ) end |
#list_sessions(limit:, offset: 0, since: nil, until_time: nil, sort_by: nil, search: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sentiero/stores/file.rb', line 63 def list_sessions(limit:, offset: 0, since: nil, until_time: nil, sort_by: nil, search: nil) session_ids = list_session_ids return [] if session_ids.empty? summaries = session_ids.filter_map { |sid| build_summary(sid) } if since since_f = since.to_f summaries = summaries.select { |summary| summary[:updated_at] >= since_f } end if until_time until_f = until_time.to_f summaries = summaries.select { |summary| summary[:updated_at] <= until_f } end if search && !search.empty? summaries = summaries.select { |summary| session_matches_search?(summary, search) } end case sort_by when "created_at" summaries.sort_by! { |summary| -summary[:created_at] } when "event_count" summaries.sort_by! { |summary| -summary[:event_count] } else summaries.sort_by! { |summary| -summary[:updated_at] } end summaries.slice(offset, limit) || [] end |
#occurrences_for_session(session_id, limit: nil) ⇒ Object
272 273 274 275 276 |
# File 'lib/sentiero/stores/file.rb', line 272 def occurrences_for_session(session_id, limit: nil) validate_id!(session_id) _problems, occurrences, _server_events = read_error_data rows_for_session(occurrences.values.flatten, session_id, limit: limit) end |
#purge_older_than(seconds) ⇒ Object
Scan meta.json directly: the base list_sessions path is capped and newest-first, so it would never see the oldest (stale) sessions.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/sentiero/stores/file.rb', line 297 def purge_older_than(seconds) cutoff = Time.now.to_f - seconds stale = list_session_ids.select { |sid| updated_at = (sid)&.fetch("updated_at", nil) updated_at && updated_at < cutoff } stale.each { |sid| delete_session(sid) } deleted = stale.size purge_error_data_older_than!(cutoff) deleted end |
#save_events(ref, events) ⇒ Object
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 56 57 58 59 60 61 |
# File 'lib/sentiero/stores/file.rb', line 20 def save_events(ref, events) return if events.nil? || events.empty? validate_window_ref!(ref) session_id = ref.session_id window_id = ref.window_id now = Time.now.to_f session_dir = session_path(session_id) FileUtils.mkdir_p(session_dir) = events.filter_map { |event| event["timestamp"]&.to_f } batch_min = .min batch_max = .max ::File.open(window_path(session_id, window_id), "a") do |file| file.flock(::File::LOCK_EX) file.write(serialize_events(events)) end (session_id) do || if ["updated_at"] = now ["first_event_at"] = [["first_event_at"], batch_min].compact.min if batch_min ["last_event_at"] = [["last_event_at"], batch_max].compact.max if batch_max else = { "created_at" => now, "updated_at" => now, "first_event_at" => batch_min, "last_event_at" => batch_max, "metadata" => nil } end end enforce_max_events(session_id) enforce_max_sessions nil end |
#save_metadata(session_id, metadata) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sentiero/stores/file.rb', line 129 def (session_id, ) return unless .is_a?(Hash) && !.empty? validate_id!(session_id) () (session_id) do || next nil unless existing = ["metadata"] || {} ["metadata"] = existing.merge(.transform_keys(&:to_s)) end nil end |
#save_occurrence(occurrence) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sentiero/stores/file.rb', line 171 def save_occurrence(occurrence) validate_occurrence!(occurrence) fp = occurrence["fingerprint"] ts = occurrence["timestamp"].to_f occ_id = SecureRandom.uuid stored = occurrence.merge("id" => occ_id) update_error_data do |problems, occurrences, _server_events| existing = problems[fp] problems[fp] = existing ? touched_problem_attrs(existing, occurrence, ts) : new_problem_attrs(occurrence, ts) occurrences[fp] ||= [] occurrences[fp] << stored evict_oldest_problems!(problems, occurrences, limits.max_problems) end (occurrence["session_id"], {"has_errors" => true}) if occurrence["session_id"] fp end |
#save_server_event(event) ⇒ Object
243 244 245 246 247 248 249 250 251 |
# File 'lib/sentiero/stores/file.rb', line 243 def save_server_event(event) validate_server_event!(event) stored = event.merge("id" => SecureRandom.uuid) update_error_data do |_problems, _occurrences, server_events| server_events << stored enforce_max_server_events!(server_events) end nil end |
#server_events_for_session(session_id, limit: nil) ⇒ Object
278 279 280 281 282 |
# File 'lib/sentiero/stores/file.rb', line 278 def server_events_for_session(session_id, limit: nil) validate_id!(session_id) _problems, _occurrences, server_events = read_error_data rows_for_session(server_events, session_id, limit: limit) end |
#session_ids_for_problem(problem_id, limit: nil) ⇒ Object
284 285 286 287 288 |
# File 'lib/sentiero/stores/file.rb', line 284 def session_ids_for_problem(problem_id, limit: nil) validate_id!(problem_id) _problems, occurrences, _server_events = read_error_data latest_session_ids(occurrences[problem_id] || [], limit: limit) end |
#update_problem_status(problem_id, status) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/sentiero/stores/file.rb', line 228 def update_problem_status(problem_id, status) validate_id!(problem_id) validate_status!(status) update_error_data do |problems, _occurrences, _server_events| existing = problems[problem_id] next unless existing problems[problem_id] = existing.merge( status: status, resolved_at: (status == "resolved") ? Time.now.to_f : nil ) end nil end |