Class: SwarmMemory::Optimization::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_memory/optimization/analyzer.rb

Overview

Analyzes memory health and generates statistics

Provides insights about memory state, quality, and organization.

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ Analyzer

Initialize analyzer

Parameters:



12
13
14
# File 'lib/swarm_memory/optimization/analyzer.rb', line 12

def initialize(adapter:)
  @adapter = adapter
end

Instance Method Details

#analyzeHash

Analyze overall memory health

Examples:

stats = analyzer.analyze
stats[:health_score] # => 75
stats[:total_entries] # => 42

Returns:

  • (Hash)

    Comprehensive statistics and health metrics



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
# File 'lib/swarm_memory/optimization/analyzer.rb', line 24

def analyze
  # List entries (handle errors gracefully)
  entries = @adapter.list
  return empty_analysis if entries.empty?

  stats = {
    total_entries: entries.size,
    total_size: @adapter.total_size,
    by_type: Hash.new(0),
    by_confidence: Hash.new(0),
    with_frontmatter: 0,
    with_embeddings: 0,
    with_tags: 0,
    with_links: 0,
    quality_scores: [],
  }

  # Analyze each entry
  entries.each do ||
    analyze_entry([:path], stats)
  end

  # Calculate averages
  stats[:average_quality] = if stats[:quality_scores].empty?
    0
  else
    stats[:quality_scores].sum / stats[:quality_scores].size
  end

  # Calculate health score
  stats[:health_score] = calculate_health_score(stats)

  stats.except(:quality_scores) # Remove internal array
end

#health_reportString

Generate formatted health report

Returns:

  • (String)

    Human-readable health report



62
63
64
65
# File 'lib/swarm_memory/optimization/analyzer.rb', line 62

def health_report
  stats = analyze
  format_health_report(stats)
end