Module: Legion::Extensions::Knowledge::Runners::Maintenance

Included in:
Client
Defined in:
lib/legion/extensions/knowledge/runners/maintenance.rb

Overview

rubocop:disable Legion/Extension/RunnerIncludeHelpers

Class Method Summary collapse

Class Method Details

.cleanup_orphans(path:, dry_run: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/knowledge/runners/maintenance.rb', line 27

def cleanup_orphans(path:, dry_run: true)
  detection = detect_orphans(path: path)
  return detection unless detection[:success]
  return detection.merge(archived: 0, files_cleaned: 0, dry_run: dry_run) if detection[:orphan_count].zero?
  return detection.merge(archived: detection[:orphan_count], files_cleaned: detection[:orphan_files].size, dry_run: true) if dry_run

  archived = archive_orphan_entries(detection[:orphan_files])

  { success: true, archived: archived, files_cleaned: detection[:orphan_files].size, dry_run: false }
rescue StandardError => e
  { success: false, error: e.message }
end

.detect_orphans(path:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/knowledge/runners/maintenance.rb', line 10

def detect_orphans(path:)
  manifest_files = load_manifest_files(path)
  apollo_files   = load_apollo_source_files

  orphan_files = apollo_files - manifest_files

  {
    success:              true,
    orphan_count:         orphan_files.size,
    orphan_files:         orphan_files,
    total_apollo_chunks:  count_apollo_chunks,
    total_manifest_files: manifest_files.size
  }
rescue StandardError => e
  { success: false, error: e.message }
end

.health(path:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/knowledge/runners/maintenance.rb', line 49

def health(path:)
  resolved = path || (Legion::Settings.dig(:knowledge, :corpus_path) if defined?(Legion::Settings))
  return { success: false, error: 'corpus_path is required' } if resolved.nil? || resolved.to_s.empty?

  scan_entries  = Helpers::Manifest.scan(path: resolved)
  store_path    = Helpers::ManifestStore.store_path(corpus_path: resolved)
  manifest_file = ::File.exist?(store_path)
  last_ingest   = manifest_file ? ::File.mtime(store_path).iso8601 : nil

  {
    success: true,
    local:   build_local_stats(resolved, scan_entries, manifest_file, last_ingest),
    apollo:  build_apollo_stats,
    sync:    build_sync_stats(resolved, scan_entries)
  }
rescue StandardError => e
  { success: false, error: e.message }
end

.quality_report(limit: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/knowledge/runners/maintenance.rb', line 68

def quality_report(limit: nil)
  resolved_limit = limit || settings_quality_limit

  {
    success:        true,
    hot_chunks:     hot_chunks(resolved_limit),
    cold_chunks:    cold_chunks(resolved_limit),
    low_confidence: low_confidence_chunks(resolved_limit),
    poor_retrieval: [],
    summary:        quality_summary
  }
rescue StandardError => e
  { success: false, error: e.message }
end

.reindex(path:) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/knowledge/runners/maintenance.rb', line 40

def reindex(path:)
  store_path = Helpers::ManifestStore.store_path(corpus_path: path)
  ::FileUtils.rm_f(store_path)

  Runners::Ingest.ingest_corpus(path: path, force: true)
rescue StandardError => e
  { success: false, error: e.message }
end