mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
New Oil Analysis tab: one record per oil sample with dates, hours, the lab's verdict and comments, ppm results for 12 wear metals with the lab's averages, and the report PDF. Shows wear-metal trends, a side-by-side history table, and flags readings 1.5x or more above the lab average. AvLab report PDFs can be imported: every sample on the report (current and previous) is added unless already recorded, the engine is matched by serial number, and reports for another tail number are refused. The parser reads positioned text and drops the hidden color-code layer that overlaps results. Adds the pdf-reader gem. Test fixture is a rebuilt report with a dummy header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.4 KiB
Ruby
61 lines
2.4 KiB
Ruby
# A spectrographic oil analysis lab report for one oil sample.
|
|
class OilAnalysis < ApplicationRecord
|
|
include UploadValidation
|
|
|
|
# Element → [label, what it usually indicates].
|
|
ELEMENTS = {
|
|
"iron" => [ "Iron", "Cylinders, cam, lifters, gears" ],
|
|
"copper" => [ "Copper", "Bearings, bushings, oil cooler" ],
|
|
"nickel" => [ "Nickel", "Valve guides, cylinder plating" ],
|
|
"chromium" => [ "Chromium", "Rings, chromed cylinders" ],
|
|
"silver" => [ "Silver", "Bearing plating" ],
|
|
"magnesium" => [ "Magnesium", "Accessory case, additive" ],
|
|
"aluminum" => [ "Aluminum", "Pistons, case" ],
|
|
"lead" => [ "Lead", "Fuel (100LL) blow-by" ],
|
|
"silicon" => [ "Silicon", "Dirt (air filter), sealant" ],
|
|
"titanium" => [ "Titanium", "Hardened parts" ],
|
|
"tin" => [ "Tin", "Bearings, piston pin bushings" ],
|
|
"molybdenum" => [ "Moly", "Rings, additive" ]
|
|
}.freeze
|
|
STATUSES = { "normal" => "Normal", "elevated" => "Elevated", "high" => "High" }.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).
|
|
TREND_ELEMENTS = %w[iron copper nickel chromium aluminum silicon tin].freeze
|
|
|
|
belongs_to :aircraft
|
|
belongs_to :component, optional: true
|
|
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)
|
|
validate :component_belongs_to_aircraft
|
|
validates_uploads :report
|
|
|
|
normalizes :lab, :sample_number, with: ->(v) { v.strip.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"]
|
|
|
|
# Above the lab average by FLAG_RATIO (needs an average to compare against).
|
|
def flagged?(el)
|
|
v, avg = value(el), average(el)
|
|
v && avg&.positive? && v >= avg * FLAG_RATIO
|
|
end
|
|
|
|
def flagged_elements = ELEMENTS.keys.select { flagged?(_1) }
|
|
def any_results? = ELEMENTS.keys.any? { value(_1) }
|
|
|
|
private
|
|
|
|
def component_belongs_to_aircraft
|
|
errors.add(:component, "must be on this aircraft") if component && component.aircraft_id != aircraft_id
|
|
end
|
|
end
|