Class: SemanticCache::Stats
- Inherits:
-
Object
- Object
- SemanticCache::Stats
- Includes:
- MonitorMixin
- Defined in:
- lib/semantic_cache/stats.rb
Instance Attribute Summary collapse
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#last_event ⇒ Object
readonly
Returns the value of attribute last_event.
-
#misses ⇒ Object
readonly
Returns the value of attribute misses.
-
#total_savings ⇒ Object
readonly
Returns the value of attribute total_savings.
Instance Method Summary collapse
- #avg_cached_response_time ⇒ Object
- #avg_response_time ⇒ Object
- #hit_rate ⇒ Object
-
#initialize ⇒ Stats
constructor
A new instance of Stats.
- #record_hit(saved_cost: 0.0, response_time: nil) ⇒ Object
- #record_miss(response_time: nil) ⇒ Object
- #report ⇒ Object
- #reset! ⇒ Object
- #to_h ⇒ Object
- #total_queries ⇒ Object
Constructor Details
#initialize ⇒ Stats
Returns a new instance of Stats.
11 12 13 14 15 16 17 18 19 |
# File 'lib/semantic_cache/stats.rb', line 11 def initialize super() # Initialize MonitorMixin @hits = 0 @misses = 0 @total_savings = 0.0 @last_event = nil @response_times = [] @cached_response_times = [] end |
Instance Attribute Details
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
9 10 11 |
# File 'lib/semantic_cache/stats.rb', line 9 def hits @hits end |
#last_event ⇒ Object (readonly)
Returns the value of attribute last_event.
9 10 11 |
# File 'lib/semantic_cache/stats.rb', line 9 def last_event @last_event end |
#misses ⇒ Object (readonly)
Returns the value of attribute misses.
9 10 11 |
# File 'lib/semantic_cache/stats.rb', line 9 def misses @misses end |
#total_savings ⇒ Object (readonly)
Returns the value of attribute total_savings.
9 10 11 |
# File 'lib/semantic_cache/stats.rb', line 9 def total_savings @total_savings end |
Instance Method Details
#avg_cached_response_time ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/semantic_cache/stats.rb', line 59 def avg_cached_response_time synchronize do return 0.0 if @cached_response_times.empty? (@cached_response_times.sum / @cached_response_times.length).round(2) end end |
#avg_response_time ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/semantic_cache/stats.rb', line 51 def avg_response_time synchronize do return 0.0 if @response_times.empty? (@response_times.sum / @response_times.length).round(2) end end |
#hit_rate ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/semantic_cache/stats.rb', line 42 def hit_rate synchronize do total = @hits + @misses return 0.0 if total.zero? (@hits.to_f / total * 100).round(1) end end |
#record_hit(saved_cost: 0.0, response_time: nil) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/semantic_cache/stats.rb', line 21 def record_hit(saved_cost: 0.0, response_time: nil) synchronize do @hits += 1 @total_savings += saved_cost @last_event = :hit @cached_response_times << response_time if response_time end end |
#record_miss(response_time: nil) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/semantic_cache/stats.rb', line 30 def record_miss(response_time: nil) synchronize do @misses += 1 @last_event = :miss @response_times << response_time if response_time end end |
#report ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/semantic_cache/stats.rb', line 83 def report synchronize do lines = [] lines << "Total queries: #{@hits + @misses}" lines << "Cache hits: #{@hits}" lines << "Cache misses: #{@misses}" lines << "Hit rate: #{hit_rate}%" lines << "Total savings: #{format("$%.2f", @total_savings)}" lines << "Avg API response time: #{avg_response_time}ms" unless @response_times.empty? lines << "Avg cached response time: #{avg_cached_response_time}ms" unless @cached_response_times.empty? lines.join("\n") end end |
#reset! ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/semantic_cache/stats.rb', line 97 def reset! synchronize do @hits = 0 @misses = 0 @total_savings = 0.0 @last_event = nil @response_times = [] @cached_response_times = [] end end |
#to_h ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/semantic_cache/stats.rb', line 67 def to_h synchronize do { hits: @hits, misses: @misses, total_queries: @hits + @misses, hit_rate: hit_rate, savings: format("$%.2f", @total_savings), total_savings: @total_savings, last_hit: @last_event == :hit, avg_response_time_ms: avg_response_time, avg_cached_response_time_ms: avg_cached_response_time } end end |
#total_queries ⇒ Object
38 39 40 |
# File 'lib/semantic_cache/stats.rb', line 38 def total_queries synchronize { @hits + @misses } end |