mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15: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
@@ -16,6 +16,7 @@ class Aircraft < ApplicationRecord
|
||||
has_many :components, dependent: :destroy
|
||||
has_many :squawks, dependent: :destroy
|
||||
has_many :logbook_entries, dependent: :destroy
|
||||
has_many :logbook_imports, dependent: :destroy
|
||||
has_many :document_collections, dependent: :destroy
|
||||
has_many :documents, dependent: :destroy
|
||||
has_many :inspection_records, dependent: :destroy
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# One upload of an aircraft archive (.sam.zip), imported by AircraftImportJob.
|
||||
class AircraftImport < ApplicationRecord
|
||||
belongs_to :user, optional: true
|
||||
belongs_to :aircraft, optional: true
|
||||
has_one_attached :archive # staged upload; purged once imported
|
||||
|
||||
STATUSES = %w[queued running done failed].freeze
|
||||
|
||||
validates :status, inclusion: { in: STATUSES }
|
||||
|
||||
def in_progress? = status.in?(%w[queued running])
|
||||
def done? = status == "done"
|
||||
def failed? = status == "failed"
|
||||
|
||||
# "12 components, 140 logbook entries, …" for whatever was imported.
|
||||
def summary
|
||||
counts.select { |_, n| n.to_i.positive? }.map { |key, n| "#{n} #{key.to_s.humanize(capitalize: false).then { n == 1 ? _1.singularize : _1 }}" }.to_sentence
|
||||
end
|
||||
end
|
||||
@@ -7,6 +7,10 @@ class Component < ApplicationRecord
|
||||
has_many :child_components, class_name: "Component", foreign_key: :parent_component_id, dependent: :destroy
|
||||
has_many :squawks, dependent: :nullify
|
||||
has_many :oil_analyses, dependent: :nullify
|
||||
# Records about this component outlive it (their foreign keys would otherwise block deleting it).
|
||||
has_many :ad_compliances, dependent: :nullify
|
||||
has_many :inspection_records, dependent: :nullify
|
||||
has_many :major_records, dependent: :nullify
|
||||
has_and_belongs_to_many :inspection_types
|
||||
has_and_belongs_to_many :airworthiness_directives
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# Scanned logbook pages uploaded as a document, and (optionally) their
|
||||
# transcription into logbook entries by Claude. See LogbookTranscriptionJob.
|
||||
class LogbookImport < ApplicationRecord
|
||||
belongs_to :aircraft
|
||||
belongs_to :user, optional: true
|
||||
belongs_to :document, optional: true
|
||||
|
||||
STATUSES = %w[queued running done failed].freeze
|
||||
|
||||
validates :status, inclusion: { in: STATUSES }
|
||||
validates :log_type, inclusion: { in: LogbookEntry::LOG_TYPES.keys }, allow_nil: true
|
||||
|
||||
normalizes :log_type, with: ->(v) { v.presence }
|
||||
|
||||
# Transcription needs an Anthropic API key in the environment.
|
||||
def self.ai_available? = ENV["ANTHROPIC_API_KEY"].present?
|
||||
|
||||
def in_progress? = status.in?(%w[queued running])
|
||||
def done? = status == "done"
|
||||
def failed? = status == "failed"
|
||||
def progress_percent = pages_total.zero? ? 0 : (100.0 * pages_done / pages_total).round
|
||||
end
|
||||
+82
-10
@@ -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
|
||||
|
||||
Reference in new issue
Block a user