Class: RakeAudit::Adapters::RedisAdapter
- Defined in:
- lib/rake_audit/adapters/redis_adapter.rb
Overview
Persists and queries TaskExecutionRecord instances via Redis.
Each record is serialized as JSON (with a generated UUID id field) and prepended to the rake_audit:executions list key via LPUSH. Query methods read the full list and filter/sort/paginate in Ruby, making this adapter suitable for low-to-moderate write volumes where a full DB is unavailable but basic Web UI introspection is still desired.
Instance Method Summary collapse
- #average_duration_ms ⇒ Float?
- #count ⇒ Integer
- #count_by_status(status) ⇒ Integer
- #find(id) ⇒ RakeAudit::Adapters::ExecutionRecord
-
#initialize(client:) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
- #query(filters: {}, page: nil, per_page: 25) ⇒ Kaminari::PaginatableArray
-
#save(record) ⇒ Object
Push the serialized record onto the executions list.
-
#stats ⇒ Hash
Single-pass override: reads the full Redis list once and derives all five dashboard metrics in memory, replacing the default five-separate-LRANGE implementation inherited from Base.
- #top_failed_tasks(limit: 10) ⇒ Array<Array(String, Integer)>
Constructor Details
#initialize(client:) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
20 21 22 23 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 20 def initialize(client:) super() @client = client end |
Instance Method Details
#average_duration_ms ⇒ Float?
73 74 75 76 77 78 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 73 def average_duration_ms records = all_records return nil if records.empty? records.sum { |r| r[:duration_ms].to_f } / records.size end |
#count ⇒ Integer
62 63 64 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 62 def count all_records.size end |
#count_by_status(status) ⇒ Integer
68 69 70 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 68 def count_by_status(status) all_records.count { |r| r[:status].to_s == status } end |
#find(id) ⇒ RakeAudit::Adapters::ExecutionRecord
54 55 56 57 58 59 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 54 def find(id) raw = all_records.find { |r| r[:id].to_s == id.to_s } raise RakeAudit::RecordNotFound, "Couldn't find execution with id=#{id}" unless raw to_execution_record(raw) end |
#query(filters: {}, page: nil, per_page: 25) ⇒ Kaminari::PaginatableArray
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 38 def query(filters: {}, page: nil, per_page: 25) require 'kaminari' records = filtered_and_sorted(filters) total = records.size current = [page.to_i, 1].max paged = records.slice((current - 1) * per_page, per_page) || [] Kaminari .paginate_array(paged.map { |r| to_execution_record(r) }, total_count: total) .page(current) .per(per_page) end |
#save(record) ⇒ Object
Push the serialized record onto the executions list. A UUID id is generated and embedded so the Web UI can link to details.
29 30 31 32 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 29 def save(record) data = record.to_h.merge(id: SecureRandom.uuid) @client.lpush('rake_audit:executions', data.to_json) end |
#stats ⇒ Hash
Single-pass override: reads the full Redis list once and derives all five dashboard metrics in memory, replacing the default five-separate-LRANGE implementation inherited from Base.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 91 def stats records = all_records total = records.size failure_recs = records.select { |r| r[:status].to_s == 'failure' } { total: total, success_count: records.count { |r| r[:status].to_s == 'success' }, failure_count: failure_recs.size, average_duration_ms: avg_duration(records, total), top_failed_tasks: top_failed_from(failure_recs) } end |
#top_failed_tasks(limit: 10) ⇒ Array<Array(String, Integer)>
82 83 84 |
# File 'lib/rake_audit/adapters/redis_adapter.rb', line 82 def top_failed_tasks(limit: 10) top_failed_from(all_records.select { |r| r[:status].to_s == 'failure' }, limit: limit) end |