Class: SmartCsvImport::StabilityReport

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_csv_import/stability_report.rb

Constant Summary collapse

STABILITY_THRESHOLD =
0.9
ANALYZABLE_STATUSES =
%w[completed partial_failure].freeze

Instance Method Summary collapse

Constructor Details

#initialize(import_type:, lookback: 20) ⇒ StabilityReport

Returns a new instance of StabilityReport.



12
13
14
15
# File 'lib/smart_csv_import/stability_report.rb', line 12

def initialize(import_type:, lookback: 20)
  @import_type = import_type
  @lookback = lookback
end

Instance Method Details

#analyzeObject



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
# File 'lib/smart_csv_import/stability_report.rb', line 17

def analyze
  imports = fetch_imports
  header_tallies = tally_header_mappings(imports)
  total = imports.length

  stable_fields = []
  unstable_fields = []

  header_tallies.each do |csv_header, target_counts|
    top_target, top_count = target_counts.max_by { |_, count| count }
    consistency_rate = (top_count.to_f / total).round(4)

    if consistency_rate >= STABILITY_THRESHOLD
      stable_fields << StableField.new(
        csv_header: csv_header,
        target_field: top_target.to_sym,
        strategy: nil,
        consistency_rate: consistency_rate
      )
    else
      resolutions = target_counts.map { |target, count| { target: target, count: count } }
      unstable_fields << UnstableField.new(
        csv_header: csv_header,
        resolutions: resolutions
      )
    end
  end

  StabilityAnalysis.new(
    import_type: @import_type,
    imports_analyzed: total,
    stable_fields: stable_fields,
    unstable_fields: unstable_fields
  )
end

#summaryObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/smart_csv_import/stability_report.rb', line 53

def summary
  analysis = analyze

  if analysis.imports_analyzed.zero?
    return "No completed imports found for #{@import_type}."
  end

  lines = ["Stability report for #{@import_type} (#{analysis.imports_analyzed} imports analyzed):"]

  if analysis.stable_fields.any?
    lines << "  Stable fields (#{analysis.stable_fields.length}):"
    analysis.stable_fields.each do |field|
      lines << "    - #{field.csv_header}#{field.target_field} (#{(field.consistency_rate * 100).round(1)}% consistent)"
    end
  end

  if analysis.unstable_fields.any?
    lines << "  Unstable fields (#{analysis.unstable_fields.length}):"
    analysis.unstable_fields.each do |field|
      resolutions_desc = field.resolutions.map { |r| "#{r[:target]}(#{r[:count]})" }.join(", ")
      lines << "    - #{field.csv_header}: #{resolutions_desc}"
    end
  end

  lines.join("\n")
end