Class: SentinelRb::Analyzer
- Inherits:
-
Object
- Object
- SentinelRb::Analyzer
- Defined in:
- lib/sentinel_rb/analyzer.rb
Overview
Main analyzer engine that coordinates prompt analysis
Constant Summary collapse
- ANALYZERS =
{ "A1" => Analyzers::IrrelevantInfo, "A2" => Analyzers::Misinformation, "A3" => Analyzers::FewShotBias, "A4" => Analyzers::BaseModelUsage, "A5" => Analyzers::DangerousTools }.freeze
Instance Method Summary collapse
-
#analyze_file(file_path, analyzer_ids: nil) ⇒ Hash
Analyze a file containing prompt content.
-
#analyze_files(pattern, analyzer_ids: nil) ⇒ Array<Hash>
Analyze multiple files matching a glob pattern.
-
#analyze_prompt(prompt, analyzer_ids: nil) ⇒ Array<Hash>
Analyze a single prompt string.
-
#initialize(config = nil) ⇒ Analyzer
constructor
A new instance of Analyzer.
-
#summarize_results(results) ⇒ Hash
Get summary statistics for analysis results.
Constructor Details
Instance Method Details
#analyze_file(file_path, analyzer_ids: nil) ⇒ Hash
Analyze a file containing prompt content
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sentinel_rb/analyzer.rb', line 57 def analyze_file(file_path, analyzer_ids: nil) unless File.exist?(file_path) return { file: file_path, error: "File not found", findings: [] } end begin content = File.read(file_path, encoding: "UTF-8") puts "Debug: File size: #{content.length}, Content preview: #{content[0..100].inspect}" if ENV["DEBUG"] findings = analyze_prompt(content, analyzer_ids: analyzer_ids) { file: file_path, size: content.length, findings: findings, analyzed_at: Time.now } rescue StandardError => e { file: file_path, error: "Failed to read or analyze file: #{e.}", findings: [] } end end |
#analyze_files(pattern, analyzer_ids: nil) ⇒ Array<Hash>
Analyze multiple files matching a glob pattern
90 91 92 93 94 95 96 |
# File 'lib/sentinel_rb/analyzer.rb', line 90 def analyze_files(pattern, analyzer_ids: nil) files = Dir.glob(pattern).reject { |f| should_skip_file?(f) } files.map do |file| analyze_file(file, analyzer_ids: analyzer_ids) end end |
#analyze_prompt(prompt, analyzer_ids: nil) ⇒ Array<Hash>
Analyze a single prompt string
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sentinel_rb/analyzer.rb', line 31 def analyze_prompt(prompt, analyzer_ids: nil) return [] if prompt.nil? || prompt.strip.empty? analyzers_to_run = determine_analyzers(analyzer_ids) findings = [] analyzers_to_run.each do |analyzer_class| analyzer = analyzer_class.new(prompt, @config, @client) findings.concat(analyzer.call) rescue StandardError => e # Log error but continue with other analyzers findings << { id: "ERROR", level: :error, message: "Analyzer #{analyzer_class.name} failed: #{e.}", details: { error_class: e.class.name, backtrace: e.backtrace.first(3) } } end findings end |
#summarize_results(results) ⇒ Hash
Get summary statistics for analysis results
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sentinel_rb/analyzer.rb', line 101 def summarize_results(results) total_files = results.length files_with_issues = results.count { |r| r[:findings]&.any? } total_findings = results.sum { |r| r[:findings]&.length || 0 } findings_by_level = results .flat_map { |r| r[:findings] || [] } .group_by { |f| f[:level] } .transform_values(&:count) { total_files: total_files, files_with_issues: files_with_issues, total_findings: total_findings, findings_by_level: findings_by_level, pass_rate: total_files.positive? ? ((total_files - files_with_issues).to_f / total_files * 100).round(1) : 100.0 } end |