Class: MudisQL::MetricsScope

Inherits:
Object
  • Object
show all
Defined in:
lib/mudis-ql/metrics_scope.rb

Overview

MetricsScope provides queryable access to mudis metrics data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsScope

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_dataObject (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_distributionHash

Get bucket distribution statistics

Returns:

  • (Hash)

    distribution stats



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

#bucketsScope

Query bucket metrics

Returns:

  • (Scope)

    a new scope for buckets array



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

#efficiencyHash

Get cache efficiency metrics

Returns:

  • (Hash)

    efficiency statistics



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

Parameters:

  • threshold (Integer)

    key count threshold

Returns:

  • (Array<Hash>)

    buckets exceeding threshold



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

Parameters:

  • threshold (Integer)

    memory threshold in bytes

Returns:

  • (Array<Hash>)

    buckets exceeding threshold



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_rateFloat

Get hit rate as a percentage

Returns:

  • (Float)

    hit rate 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_touchedScope

Query least touched keys

Returns:

  • (Scope)

    a new scope for least_touched array



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_keysArray<String>

Get keys that have never been accessed

Returns:

  • (Array<String>)

    keys with 0 access count



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

#refreshMetricsScope

Refresh metrics data

Returns:



133
134
135
136
# File 'lib/mudis-ql/metrics_scope.rb', line 133

def refresh
  @metrics_data = Mudis.metrics
  self
end

#summaryHash

Get top-level metrics (hits, misses, evictions, etc.)

Returns:

  • (Hash)

    top-level metrics



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_keysInteger

Get total number of keys across all buckets

Returns:

  • (Integer)

    total key count



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_memoryInteger

Get total memory across all buckets

Returns:

  • (Integer)

    total memory in bytes



55
56
57
# File 'lib/mudis-ql/metrics_scope.rb', line 55

def total_memory
  @metrics_data[:total_memory] || 0
end