Class: FastExists::MultiTenant::RecommendationEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_exists/multi_tenant/recommendation_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tenant_records = {}) ⇒ RecommendationEngine

Returns a new instance of RecommendationEngine.



10
11
12
# File 'lib/fast_exists/multi_tenant/recommendation_engine.rb', line 10

def initialize(tenant_records = {})
  @tenant_records = tenant_records
end

Class Method Details

.analyze(tenant_records = {}) ⇒ Object



6
7
8
# File 'lib/fast_exists/multi_tenant/recommendation_engine.rb', line 6

def self.analyze(tenant_records = {})
  new(tenant_records).generate_recommendations
end

Instance Method Details

#generate_recommendationsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fast_exists/multi_tenant/recommendation_engine.rb', line 14

def generate_recommendations
  total_tenants = @tenant_records.size

  counts = { tiny: 0, small: 0, medium: 0, large: 0 }
  promotions = []
  demotions = []

  adaptive_strategy = FastExists::MultiTenant::Strategies::AdaptiveStrategy.new

  @tenant_records.each do |tenant_id, record_count|
    cat = adaptive_strategy.classify_tenant(tenant_id, record_count)
    counts[cat] += 1

    # Promotion check (e.g. record count grew past bucket limit)
    if cat == :large
      promotions << {
        tenant_id: tenant_id,
        current_category: :medium,
        recommended_category: :large,
        reason: "Tenant record count (#{record_count}) exceeded 1,000,000 threshold. Recommend dedicated Bloom filter."
      }
    end
  end

  current_strategy = FastExists.configuration.tenant_strategy

  # Calculate estimated Redis key reduction
  per_tenant_keys = [total_tenants, 1].max
  adaptive_keys = 3 + counts[:large] # tiny, small, medium pools + dedicated large filters

  key_reduction_pct = per_tenant_keys > 0 ? (((per_tenant_keys - adaptive_keys).to_f / per_tenant_keys) * 100).round(1) : 0.0
  memory_savings_pct = current_strategy == :per_tenant ? 78.0 : 0.0

  recommended_strategy = (counts[:tiny] + counts[:small] > 0.7 * [total_tenants, 1].max) ? :adaptive : current_strategy

  {
    total_tenants: total_tenants,
    buckets: counts,
    current_strategy: current_strategy,
    recommended_strategy: recommended_strategy,
    estimated_memory_savings_pct: [memory_savings_pct, 0.0].max,
    estimated_redis_keys: adaptive_keys,
    redis_key_reduction_pct: [key_reduction_pct, 0.0].max,
    promotions: promotions,
    demotions: demotions,
    advice: generate_advice(current_strategy, recommended_strategy, total_tenants, adaptive_keys, key_reduction_pct)
  }
end