Class: MudisQL::MetricsScope
- Inherits:
-
Object
- Object
- MudisQL::MetricsScope
- Defined in:
- lib/mudis-ql/metrics_scope.rb
Overview
MetricsScope provides queryable access to mudis metrics data
Instance Attribute Summary collapse
-
#metrics_data ⇒ Object
readonly
Returns the value of attribute metrics_data.
Instance Method Summary collapse
-
#bucket_distribution ⇒ Hash
Get bucket distribution statistics.
-
#buckets ⇒ Scope
Query bucket metrics.
-
#efficiency ⇒ Hash
Get cache efficiency metrics.
-
#high_key_buckets(threshold) ⇒ Array<Hash>
Find buckets with many keys.
-
#high_memory_buckets(threshold) ⇒ Array<Hash>
Find buckets with high memory usage.
-
#hit_rate ⇒ Float
Get hit rate as a percentage.
-
#initialize ⇒ MetricsScope
constructor
A new instance of MetricsScope.
-
#least_touched ⇒ Scope
Query least touched keys.
-
#never_accessed_keys ⇒ Array<String>
Get keys that have never been accessed.
-
#refresh ⇒ MetricsScope
Refresh metrics data.
-
#summary ⇒ Hash
Get top-level metrics (hits, misses, evictions, etc.).
-
#total_keys ⇒ Integer
Get total number of keys across all buckets.
-
#total_memory ⇒ Integer
Get total memory across all buckets.
Constructor Details
#initialize ⇒ MetricsScope
Returns a new instance of MetricsScope.
8 9 10 11 12 13 14 15 |
# File 'lib/mudis-ql/metrics_scope.rb', line 8 def initialize @metrics_data = Mudis.metrics @conditions = [] @order_by = nil @order_direction = :asc @limit_value = nil @offset_value = 0 end |
Instance Attribute Details
#metrics_data ⇒ Object (readonly)
Returns the value of attribute metrics_data.
6 7 8 |
# File 'lib/mudis-ql/metrics_scope.rb', line 6 def metrics_data @metrics_data end |
Instance Method Details
#bucket_distribution ⇒ Hash
Get bucket distribution statistics
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mudis-ql/metrics_scope.rb', line 104 def bucket_distribution buckets_data = @metrics_data[:buckets] || [] return default_distribution if buckets_data.empty? key_counts = buckets_data.map { |b| (b[:keys] || b["keys"]) || 0 } memory_values = buckets_data.map { |b| (b[:memory_bytes] || b["memory_bytes"]) || 0 } { total_buckets: buckets_data.size, avg_keys_per_bucket: (key_counts.sum.to_f / buckets_data.size).round(2), max_keys_per_bucket: key_counts.max || 0, min_keys_per_bucket: key_counts.min || 0, avg_memory_per_bucket: (memory_values.sum.to_f / buckets_data.size).round(2), max_memory_per_bucket: memory_values.max || 0, min_memory_per_bucket: memory_values.min || 0 } end |
#buckets ⇒ Scope
Query bucket metrics
35 36 37 38 39 40 41 42 |
# File 'lib/mudis-ql/metrics_scope.rb', line 35 def buckets data = @metrics_data[:buckets] || [] # Normalize to symbol keys normalized = data.map do |bucket| bucket.transform_keys(&:to_sym) end create_scope_for_array(normalized) end |
#efficiency ⇒ Hash
Get cache efficiency metrics
74 75 76 77 78 79 80 81 |
# File 'lib/mudis-ql/metrics_scope.rb', line 74 def efficiency { hit_rate: hit_rate, miss_rate: (100 - hit_rate).round(2), eviction_rate: eviction_rate, rejection_rate: rejection_rate } end |
#high_key_buckets(threshold) ⇒ Array<Hash>
Find buckets with many keys
96 97 98 99 |
# File 'lib/mudis-ql/metrics_scope.rb', line 96 def high_key_buckets(threshold) buckets_data = @metrics_data[:buckets] || [] buckets_data.select { |b| ((b[:keys] || b["keys"]) || 0) > threshold } end |
#high_memory_buckets(threshold) ⇒ Array<Hash>
Find buckets with high memory usage
87 88 89 90 |
# File 'lib/mudis-ql/metrics_scope.rb', line 87 def high_memory_buckets(threshold) buckets_data = @metrics_data[:buckets] || [] buckets_data.select { |b| ((b[:memory_bytes] || b["memory_bytes"]) || 0) > threshold } end |
#hit_rate ⇒ Float
Get hit rate as a percentage
62 63 64 65 66 67 68 69 |
# File 'lib/mudis-ql/metrics_scope.rb', line 62 def hit_rate hits = @metrics_data[:hits] || 0 misses = @metrics_data[:misses] || 0 total = hits + misses return 0.0 if total.zero? (hits.to_f / total * 100).round(2) end |
#least_touched ⇒ Scope
Query least touched keys
27 28 29 30 |
# File 'lib/mudis-ql/metrics_scope.rb', line 27 def least_touched data = @metrics_data[:least_touched] || [] create_scope_for_array(data.map { |key, count| { key: key, access_count: count } }) end |
#never_accessed_keys ⇒ Array<String>
Get keys that have never been accessed
125 126 127 128 |
# File 'lib/mudis-ql/metrics_scope.rb', line 125 def never_accessed_keys data = @metrics_data[:least_touched] || [] data.select { |_key, count| count.zero? }.map(&:first) end |
#refresh ⇒ MetricsScope
Refresh metrics data
133 134 135 136 |
# File 'lib/mudis-ql/metrics_scope.rb', line 133 def refresh @metrics_data = Mudis.metrics self end |
#summary ⇒ Hash
Get top-level metrics (hits, misses, evictions, etc.)
20 21 22 |
# File 'lib/mudis-ql/metrics_scope.rb', line 20 def summary @metrics_data.reject { |k, _| [:least_touched, :buckets].include?(k) } end |
#total_keys ⇒ Integer
Get total number of keys across all buckets
47 48 49 50 |
# File 'lib/mudis-ql/metrics_scope.rb', line 47 def total_keys buckets_data = @metrics_data[:buckets] || [] buckets_data.sum { |b| (b[:keys] || b["keys"] || 0) } end |
#total_memory ⇒ Integer
Get total memory across all buckets
55 56 57 |
# File 'lib/mudis-ql/metrics_scope.rb', line 55 def total_memory @metrics_data[:total_memory] || 0 end |