Class: RakeAudit::Adapters::RedisAdapter

Inherits:
Base
  • Object
show all
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

Constructor Details

#initialize(client:) ⇒ RedisAdapter

Returns a new instance of RedisAdapter.

Parameters:

  • client (Redis)

    a connected Redis client instance.



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_msFloat?

Returns:

  • (Float, nil)


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

#countInteger

Returns:

  • (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

Parameters:

  • status (String)

Returns:

  • (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

Parameters:

  • id (String)

    UUID stored at write time.

Returns:

Raises:



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

Parameters:

  • filters (Hash) (defaults to: {})

    pre-validated non-blank filter values.

  • page (Integer, String, nil) (defaults to: nil)
  • per_page (Integer) (defaults to: 25)

Returns:

  • (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.

Parameters:



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

#statsHash

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.

Returns:

  • (Hash)


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)>

Parameters:

  • limit (Integer) (defaults to: 10)

Returns:

  • (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