mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Blackstone oil analysis, .sam.zip export/import, AI logbook transcription
- Oil analysis: Blackstone PDF import (20 elements, oil properties, universal averages), results stored per element, oil type, "leave out of trends", the original app's statuses; AvLab fixes (multi-line comments, unrelated labels no longer steal values). Existing records migrate. - Aircraft archives in the original app's .sam.zip format (schema v2): export from Access & Sharing, background import from the Fleet page, rake tasks. Archives move both ways between this app and the original. - Scanned logbook pages (images or PDFs) upload as a document; with ANTHROPIC_API_KEY set, Claude transcribes them into logbook entries. - Fix: deleting a component or aircraft failed when AD compliances or inspection records pointed at a component. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d068e1a63
commit
1c6c2f88c0
54 files changed
+2794
-286
No files matched your search
@@ -1,207 +1,43 @@
|
||||
require "pdf-reader"
|
||||
|
||||
# Reads an AvLab (Aviation Laboratories) oil analysis report PDF. A report
|
||||
# holds the current sample plus up to two previous ones, each with its dates,
|
||||
# hours, lab verdict, comments and a ppm table (value, lab average beneath).
|
||||
# Reads an oil analysis lab report PDF into sample records. Supported labs:
|
||||
# - Aviation Laboratories (AvLab): the current sample plus up to two previous
|
||||
# ones, each with its own verdict, comments and lab averages.
|
||||
# - Blackstone Laboratories: a grid of up to ~5 samples (newest first) with 20
|
||||
# elements, oil properties, and the lab's universal averages.
|
||||
#
|
||||
# The PDF's text layer is messy: it hides the cell color codes ("#00CC00") as
|
||||
# larger text on top of the results. So this works from positioned characters,
|
||||
# drops the color-code layer by font size, rebuilds words and lines, and maps
|
||||
# each result to its element column by x position.
|
||||
# Every sample comes back as a hash of OilAnalysis attributes plus :current.
|
||||
class OilReportParser
|
||||
class Error < StandardError; end
|
||||
|
||||
LAB = "AvLab".freeze
|
||||
COLOR_CODE_FONT_SIZE = 8.5 # the hidden color codes are 9pt; real text is 7–8pt
|
||||
COLUMNS = {
|
||||
"Iron" => "iron", "Copper" => "copper", "Nickel" => "nickel", "Chromium" => "chromium",
|
||||
"Silver" => "silver", "Magnesium" => "magnesium", "Aluminum" => "aluminum", "Lead" => "lead",
|
||||
"Silicon" => "silicon", "Titanium" => "titanium", "Tin" => "tin", "Moly." => "molybdenum"
|
||||
}.freeze
|
||||
LABELS = {
|
||||
"Sample Date:" => :sample_date, "Analysis Date:" => :analysis_date, "Sample Number:" => :sample_number,
|
||||
"TSN/TSO:" => :engine_hours, "Oil Hours:" => :oil_hours, "Filter Hours:" => :filter_hours,
|
||||
"Oil Added:" => :oil_added
|
||||
}.freeze
|
||||
LABEL_MAX_X = 108 # left-hand labels end before here…
|
||||
VALUE_X = 105..160 # …and their values sit in this column
|
||||
|
||||
Char = Struct.new(:text, :x, :endx, :y, :page)
|
||||
Word = Struct.new(:text, :x, :endx, :y, :page) do
|
||||
def center = (x + endx) / 2.0
|
||||
end
|
||||
Line = Struct.new(:y, :page, :words) do
|
||||
def text = words.map(&:text).join(" ")
|
||||
end
|
||||
Report = Struct.new(:lab, :tail_number, :engine_serial, :engine_model, :samples, keyword_init: true)
|
||||
Report = Struct.new(:lab, :tail_number, :engine_serial, :engine_model, :oil_type, :samples, keyword_init: true)
|
||||
|
||||
def self.from_pdf(io)
|
||||
reader = PDF::Reader.new(io)
|
||||
chars = reader.pages.each_with_index.flat_map do |page, index|
|
||||
receiver = PDF::Reader::PageTextReceiver.new
|
||||
page.walk(receiver)
|
||||
# Keep overlapping glyphs: a real "0" under a hidden one would otherwise be dropped.
|
||||
receiver.runs(merge: false, skip_overlapping: false).filter_map do |run|
|
||||
next if run.font_size >= COLOR_CODE_FONT_SIZE || run.text.strip.empty?
|
||||
Char.new(run.text, run.x, run.endx, run.y, index)
|
||||
end
|
||||
receivers = reader.pages.map { |page| PDF::Reader::PageTextReceiver.new.tap { page.walk(_1) } }
|
||||
text = receivers.flat_map { |r| r.runs(merge: true, skip_overlapping: false).map(&:text) }.join(" ")
|
||||
|
||||
if text.match?(/blackstone-labs\.com|BLACKSTONE LABORATORIES/i)
|
||||
Blackstone.new(receivers.first.runs(merge: true, skip_overlapping: false)).parse
|
||||
elsif text.match?(/OIL ANALYSIS RESULTS IN PARTS PER MILLION|AVIATION LABORATORIES|avlab\.com/i)
|
||||
Avlab.from_receivers(receivers)
|
||||
else
|
||||
raise Error, "Not a recognized lab report (AvLab and Blackstone PDFs are supported)"
|
||||
end
|
||||
new(chars).parse
|
||||
rescue PDF::Reader::MalformedPDFError, PDF::Reader::UnsupportedFeatureError => e
|
||||
raise Error, "Couldn't read the PDF (#{e.message})"
|
||||
end
|
||||
|
||||
def initialize(chars)
|
||||
@lines = build_lines(chars)
|
||||
# "1,903" → 1903, "<0.5" → 0.5, "5 qts" → 5, ".3" → 0.3; nil for "-", "N/A"…
|
||||
def self.number(text)
|
||||
digits = text.to_s.delete(",").strip.sub(/\A[<>]\s*/, "")[/\A\d*\.?\d+/] or return nil
|
||||
BigDecimal(digits.sub(/\A\./, "0."))
|
||||
end
|
||||
|
||||
def parse
|
||||
unless @lines.any? { _1.text.include?("OIL ANALYSIS RESULTS IN PARTS PER MILLION") }
|
||||
raise Error, "Not an AvLab oil analysis report"
|
||||
end
|
||||
samples = sample_blocks.map { parse_sample(_1) }.select { _1[:sample_date] }
|
||||
raise Error, "No samples found in the report" if samples.empty?
|
||||
|
||||
Report.new(lab: LAB, tail_number: after("Tail No.:"), engine_serial: after("Engine S/N:"),
|
||||
engine_model: after("Engine Model:", words: 3), samples: samples)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Characters → lines (same baseline ±1pt) → words (glyphs closer than a space).
|
||||
def build_lines(chars)
|
||||
lines = []
|
||||
chars.sort_by { [ _1.page, -_1.y ] }.each do |c|
|
||||
line = lines.reverse_each.find { _1.page == c.page && (_1.y - c.y).abs <= 1.0 }
|
||||
line ? line.words << c : lines << Line.new(c.y, c.page, [ c ])
|
||||
end
|
||||
lines.each do |line|
|
||||
line.words = line.words.sort_by(&:x).each_with_object([]) do |c, words|
|
||||
last = words.last
|
||||
if last && c.x - last.endx <= 1.6
|
||||
last.text += c.text
|
||||
last.endx = c.endx
|
||||
else
|
||||
words << Word.new(c.text.dup, c.x, c.endx, c.y, c.page)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def header_lines
|
||||
first = @lines.find { _1.words.first&.text.in?(%w[CURRENT PREVIOUS]) }
|
||||
first ? @lines.take_while { _1 != first } : @lines
|
||||
end
|
||||
|
||||
# The word(s) following a label on the same line, e.g. "Tail No.:" → "N12345".
|
||||
def after(label, words: 1)
|
||||
parts = label.split
|
||||
header_lines.each do |line|
|
||||
texts = line.words.map(&:text)
|
||||
texts.each_cons(parts.size).with_index do |window, i|
|
||||
next unless window == parts
|
||||
found = texts[i + parts.size, words]&.take_while { !_1.end_with?(":") }
|
||||
return found.join(" ").presence if found&.any?
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# Lines from each "CURRENT SAMPLE" / "PREVIOUS SAMPLE n" heading to the next.
|
||||
def sample_blocks
|
||||
starts = @lines.each_index.select { |i| @lines[i].words.first&.text.in?(%w[CURRENT PREVIOUS]) && @lines[i].words[1]&.text == "SAMPLE" }
|
||||
starts.each_with_index.map { |s, n| @lines[s...(starts[n + 1] || @lines.size)] }
|
||||
end
|
||||
|
||||
def parse_sample(block)
|
||||
heading = block.first
|
||||
verdict = heading.words.select { _1.x > 140 && _1.x < 470 }.map(&:text).join(" ")
|
||||
fields = labeled_values(block)
|
||||
comments = block.find { _1.words.first&.text == "Comments:" }&.words&.drop(1)&.map(&:text)&.join(" ")
|
||||
|
||||
{
|
||||
current: heading.words.first.text == "CURRENT",
|
||||
sample_date: date(fields[:sample_date]),
|
||||
analysis_date: date(fields[:analysis_date]),
|
||||
sample_number: fields[:sample_number],
|
||||
engine_hours: number(fields[:engine_hours]),
|
||||
oil_hours: number(fields[:oil_hours]),
|
||||
filter_hours: number(fields[:filter_hours]),
|
||||
oil_added: number(fields[:oil_added]),
|
||||
status: status(verdict),
|
||||
lab_comments: [ (verdict unless status(verdict) == "normal"), comments ].compact_blank.join("\n").presence
|
||||
}.merge(results(block))
|
||||
end
|
||||
|
||||
# Left-hand "Label:" lines and the values beside them. A value isn't always on
|
||||
# the label's baseline, so each value goes to the nearest label above or below.
|
||||
def labeled_values(block)
|
||||
labels = block.filter_map do |line|
|
||||
text = line.words.select { _1.x < LABEL_MAX_X - 20 || (_1.x < LABEL_MAX_X && _1.text.end_with?(":")) }.map(&:text).join(" ")
|
||||
[ LABELS[text], line.y ] if LABELS[text]
|
||||
end
|
||||
values = block.drop(1).flat_map { |line| line.words.select { VALUE_X.cover?(_1.x) } } # skip the heading
|
||||
values.each_with_object({}) do |word, found|
|
||||
key, = labels.min_by { (_2 - word.y).abs }
|
||||
found[key] ||= word.text if key
|
||||
end
|
||||
end
|
||||
|
||||
# The ppm row under the element headings, and the (average) row under that.
|
||||
def results(block)
|
||||
header = block.find { |line| COLUMNS.keys.count { line.words.map(&:text).include?(_1) } >= 6 } or return {}
|
||||
columns = header.words.filter_map { |w| [ COLUMNS[w.text], w.center ] if COLUMNS[w.text] }
|
||||
below = block.select { _1.y < header.y && _1.y > header.y - 25 }
|
||||
|
||||
below.each_with_object({}) do |line, out|
|
||||
# Results start right of the label column ("Cylinder Type: steel" shares these lines).
|
||||
merge_less_than(line.words.select { _1.x >= VALUE_X.end }).each do |word|
|
||||
element, = columns.min_by { (_2 - word.center).abs }
|
||||
if word.text.start_with?("(")
|
||||
out[:"#{element}_avg"] = ppm(word.text.delete("()"))
|
||||
else
|
||||
out[element.to_sym] = ppm(word.text)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# "<" and "0.1" are drawn as separate words.
|
||||
def merge_less_than(words)
|
||||
words.each_with_object([]) do |w, out|
|
||||
if out.last&.text == "<"
|
||||
out.last.text = "< #{w.text}"
|
||||
out.last.endx = w.endx
|
||||
else
|
||||
out << w.dup
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# "N/A" → nil; "< 0.1" (below detection) → 0; ".3" → 0.3
|
||||
def ppm(text)
|
||||
return nil if text.blank? || text == "N/A"
|
||||
return 0 if text.start_with?("<")
|
||||
number(text)
|
||||
end
|
||||
|
||||
def number(text)
|
||||
BigDecimal(text.to_s.strip.sub(/\A\./, "0."))
|
||||
rescue ArgumentError, TypeError
|
||||
nil
|
||||
end
|
||||
|
||||
def date(text)
|
||||
# M/D/YYYY
|
||||
def self.date(text)
|
||||
Date.strptime(text, "%m/%d/%Y") if text
|
||||
rescue Date::Error
|
||||
nil
|
||||
end
|
||||
|
||||
def status(verdict)
|
||||
case verdict.upcase
|
||||
when /ABNORMAL|HIGH|CRITICAL|SEVERE|PROBLEM/ then "high"
|
||||
when /APPEARS NORMAL/ then "normal"
|
||||
else "elevated"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user