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:
rickyandClaude Opus 5.5 committed 2026-11-06 21:05:19 -05:00
1 parent 2d068e1a63
commit 1c6c2f88c0
54 files changed
+2794 -286

No files matched your search

+82 -10
View File
@@ -1,8 +1,14 @@
# A spectrographic oil analysis lab report for one oil sample.
#
# Results are stored per element ({ "iron" => 44.0, … } in ppm), with the lab's
# average for similar engines alongside, so any lab's element list fits (AvLab
# tests 12 elements, Blackstone 20). Oil properties (viscosity, flash point,
# fuel/water %) are stored the same way.
class OilAnalysis < ApplicationRecord
include UploadValidation
# Element → [label, what it usually indicates].
# Element → [label, what it usually indicates]. The first twelve are the
# wear-metal set every lab reports; the rest are mostly oil additives.
ELEMENTS = {
"iron" => [ "Iron", "Cylinders, cam, lifters, gears" ],
"copper" => [ "Copper", "Bearings, bushings, oil cooler" ],
@@ -15,9 +21,30 @@ class OilAnalysis < ApplicationRecord
"silicon" => [ "Silicon", "Dirt (air filter), sealant" ],
"titanium" => [ "Titanium", "Hardened parts" ],
"tin" => [ "Tin", "Bearings, piston pin bushings" ],
"molybdenum" => [ "Moly", "Rings, additive" ]
"molybdenum" => [ "Moly", "Rings, additive" ],
"manganese" => [ "Manganese", "Steel alloys (valves, gears)" ],
"potassium" => [ "Potassium", "Coolant, additive" ],
"boron" => [ "Boron", "Additive" ],
"sodium" => [ "Sodium", "Additive, dirt" ],
"calcium" => [ "Calcium", "Detergent additive" ],
"phosphorus" => [ "Phosphorus", "Anti-wear additive" ],
"zinc" => [ "Zinc", "Anti-wear additive, brass" ],
"barium" => [ "Barium", "Additive" ]
}.freeze
STATUSES = { "normal" => "Normal", "elevated" => "Elevated", "high" => "High" }.freeze
COMMON_ELEMENTS = ELEMENTS.keys.first(12).freeze
# Property → [label, unit].
PROPERTIES = {
"viscosity_sus_210f" => [ "Viscosity @ 210°F", "SUS" ],
"viscosity_cst_100c" => [ "Viscosity @ 100°C", "cSt" ],
"flashpoint_f" => [ "Flash point", "°F" ],
"fuel_percent" => [ "Fuel", "%" ],
"antifreeze_percent" => [ "Antifreeze", "%" ],
"water_percent" => [ "Water", "%" ],
"insolubles_percent" => [ "Insolubles", "%" ],
"total_base_number" => [ "TBN", "" ],
"total_acid_number" => [ "TAN", "" ]
}.freeze
STATUSES = { "normal" => "Normal", "monitor" => "Monitor", "action_required" => "Action required" }.freeze
# A reading this far above the lab's average for similar engines gets flagged.
FLAG_RATIO = 1.5
# Wear metals worth charting together (lead is on its own scale: it comes from fuel).
@@ -28,20 +55,42 @@ class OilAnalysis < ApplicationRecord
has_one_attached :report
validates :sample_date, presence: true
validates :status, inclusion: { in: STATUSES.keys }
validates(*ELEMENTS.keys.flat_map { [ _1, :"#{_1}_avg" ] }, :engine_hours, :oil_hours, :filter_hours, :oil_added,
numericality: { greater_than_or_equal_to: 0 }, allow_nil: true)
validates :status, inclusion: { in: STATUSES.keys }, allow_nil: true
validates :engine_hours, :oil_hours, :filter_hours, :oil_added, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
validate :results_are_numbers
validate :component_belongs_to_aircraft
validates_uploads :report
normalizes :lab, :sample_number, with: ->(v) { v.strip.presence }
normalizes :lab, :sample_number, :oil_type, with: ->(v) { v.strip.presence }
normalizes :status, with: ->(v) { v.presence }
scope :recent_first, -> { order(sample_date: :desc, created_at: :desc) }
def self.element_label(el) = ELEMENTS.fetch(el)[0]
def value(el) = self[el]
def average(el) = self[:"#{el}_avg"]
# Elements any of these samples has a reading for, in display order.
def self.elements_in(analyses) = ELEMENTS.keys.select { |el| analyses.any? { _1.value(el) } }
def elements = super || {}
def element_averages = super || {}
def oil_properties = super || {}
# Accept form or parser input: blanks dropped, numbers kept as numbers.
def elements=(values)
super(clean(values, ELEMENTS, :elements))
end
def element_averages=(values)
super(clean(values, ELEMENTS, :element_averages))
end
def oil_properties=(values)
super(clean(values, PROPERTIES, :oil_properties))
end
def value(el) = elements[el]
def average(el) = element_averages[el]
def property(key) = oil_properties[key]
# Above the lab average by FLAG_RATIO (needs an average to compare against).
def flagged?(el)
@@ -50,10 +99,33 @@ class OilAnalysis < ApplicationRecord
end
def flagged_elements = ELEMENTS.keys.select { flagged?(_1) }
def any_results? = ELEMENTS.keys.any? { value(_1) }
def any_results? = elements.any?
def status_label = STATUSES[status]
private
def clean(values, allowed, attribute)
(@invalid_results ||= {})[attribute] = []
values.to_h.each_with_object({}) do |(key, raw), out|
key = key.to_s
next unless allowed.key?(key)
next if raw.blank?
number = raw.is_a?(Numeric) ? raw.to_f : Float(raw.to_s.strip, exception: false)
if number && number >= 0
out[key] = number
else
@invalid_results[attribute] << key
end
end
end
def results_are_numbers
(@invalid_results || {}).each do |attribute, keys|
labels = keys.map { (ELEMENTS[_1] || PROPERTIES[_1]).first }
errors.add(attribute, "must be numbers of 0 or more (#{labels.to_sentence})") if keys.any?
end
end
def component_belongs_to_aircraft
errors.add(:component, "must be on this aircraft") if component && component.aircraft_id != aircraft_id
end