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
+2793 -285

No files matched your search

+2
View File
@@ -10,3 +10,5 @@ FORCE_SSL=false
# SMTP_USERNAME= # SMTP_USERNAME=
# SMTP_PASSWORD= # SMTP_PASSWORD=
# MAIL_FROM=aircraft@example.com # MAIL_FROM=aircraft@example.com
# Optional: Claude transcribes scanned logbook pages
# ANTHROPIC_API_KEY=
+2
View File
@@ -75,3 +75,5 @@ gem "caxlsx", "~> 4.5", group: :test
gem "webmock", "~> 3.34", group: :test gem "webmock", "~> 3.34", group: :test
gem "pdf-reader", "~> 2.16" gem "pdf-reader", "~> 2.16"
gem "rubyzip", ">= 2.4", "< 3" # aircraft archives (.sam.zip)
gem "anthropic", "~> 2.6" # AI logbook transcription (Claude)
+13
View File
@@ -79,6 +79,11 @@ GEM
addressable (2.9.2) addressable (2.9.2)
public_suffix (>= 2.0.2, < 8.0) public_suffix (>= 2.0.2, < 8.0)
afm (1.0.1) afm (1.0.1)
anthropic (2.6.0)
base64 (~> 0.1)
connection_pool (>= 2, < 4)
opentelemetry-api (>= 1.2.4, < 2)
standardwebhooks (~> 1.0)
ast (2.4.3) ast (2.4.3)
base64 (0.3.0) base64 (0.3.0)
bcrypt (3.1.22) bcrypt (3.1.22)
@@ -208,6 +213,8 @@ GEM
racc (~> 1.4) racc (~> 1.4)
nokogiri (1.19.7-x86_64-linux-musl) nokogiri (1.19.7-x86_64-linux-musl)
racc (~> 1.4) racc (~> 1.4)
opentelemetry-api (1.8.0)
logger
ostruct (0.6.3) ostruct (0.6.3)
parallel (1.28.0) parallel (1.28.0)
parser (3.3.13.0) parser (3.3.13.0)
@@ -355,6 +362,7 @@ GEM
net-sftp (>= 2.1.2) net-sftp (>= 2.1.2)
net-ssh (>= 2.8.0) net-ssh (>= 2.8.0)
ostruct ostruct
standardwebhooks (1.0.1)
stimulus-rails (1.3.4) stimulus-rails (1.3.4)
railties (>= 6.0.0) railties (>= 6.0.0)
tailwindcss-rails (4.7.0) tailwindcss-rails (4.7.0)
@@ -415,6 +423,7 @@ PLATFORMS
x86_64-linux-musl x86_64-linux-musl
DEPENDENCIES DEPENDENCIES
anthropic (~> 2.6)
bcrypt (~> 3.1.7) bcrypt (~> 3.1.7)
bootsnap bootsnap
brakeman brakeman
@@ -432,6 +441,7 @@ DEPENDENCIES
rails (~> 8.1.0) rails (~> 8.1.0)
roo (~> 2.10) roo (~> 2.10)
rubocop-rails-omakase rubocop-rails-omakase
rubyzip (>= 2.4, < 3)
selenium-webdriver selenium-webdriver
solid_cable solid_cable
solid_cache solid_cache
@@ -461,6 +471,7 @@ CHECKSUMS
activesupport (8.1.4.1) sha256=16ac4f341ca65c85a1fcdb8ac73f68891aae9e3c4a6f9c3a27daed8b00017ed7 activesupport (8.1.4.1) sha256=16ac4f341ca65c85a1fcdb8ac73f68891aae9e3c4a6f9c3a27daed8b00017ed7
addressable (2.9.2) sha256=9117c7e7d6ee07230e75ce41fb9401ae55354dc654fcd0e862ba41d539e9a610 addressable (2.9.2) sha256=9117c7e7d6ee07230e75ce41fb9401ae55354dc654fcd0e862ba41d539e9a610
afm (1.0.1) sha256=4fc98c6ea9cad60c25ae12bad517513b0d6fdbedc099e25be88bf47977b7d879 afm (1.0.1) sha256=4fc98c6ea9cad60c25ae12bad517513b0d6fdbedc099e25be88bf47977b7d879
anthropic (2.6.0) sha256=0bbc1cf9b71c82dc72b552ccd0d5e253b76a55fd2d80f8ea4666a9d836a492cb
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
bcrypt (3.1.22) sha256=1f0072e88c2d705d94aff7f2c5cb02eb3f1ec4b8368671e19112527489f29032 bcrypt (3.1.22) sha256=1f0072e88c2d705d94aff7f2c5cb02eb3f1ec4b8368671e19112527489f29032
@@ -525,6 +536,7 @@ CHECKSUMS
nokogiri (1.19.7-x86_64-darwin) sha256=508e81e8c5f84aaa240dce981bf1c43faae60808726f0ce7024ee92effc5cafe nokogiri (1.19.7-x86_64-darwin) sha256=508e81e8c5f84aaa240dce981bf1c43faae60808726f0ce7024ee92effc5cafe
nokogiri (1.19.7-x86_64-linux-gnu) sha256=25865ccb462f714e712fdf8906fde07849cb6704a808e3d629d75baa5af5dd3b nokogiri (1.19.7-x86_64-linux-gnu) sha256=25865ccb462f714e712fdf8906fde07849cb6704a808e3d629d75baa5af5dd3b
nokogiri (1.19.7-x86_64-linux-musl) sha256=464d1f3dfb0d53cf54c0c6a7e23e8e91cf68ec3cafc2b27dc4a7682f2d9bf546 nokogiri (1.19.7-x86_64-linux-musl) sha256=464d1f3dfb0d53cf54c0c6a7e23e8e91cf68ec3cafc2b27dc4a7682f2d9bf546
opentelemetry-api (1.8.0) sha256=3af51183daf0f56a164bc1579782245be70a40678566b9a393cbe5af28ea87c6
ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912 ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912
parallel (1.28.0) sha256=33e6de1484baf2524792d178b0913fc8eb94c628d6cfe45599ad4458c638c970 parallel (1.28.0) sha256=33e6de1484baf2524792d178b0913fc8eb94c628d6cfe45599ad4458c638c970
parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca
@@ -574,6 +586,7 @@ CHECKSUMS
sqlite3 (2.10.2-x86_64-linux-gnu) sha256=ca92731ab464df9cae503fa1a439a549e2724f6953c8c05a7769b59366e38f15 sqlite3 (2.10.2-x86_64-linux-gnu) sha256=ca92731ab464df9cae503fa1a439a549e2724f6953c8c05a7769b59366e38f15
sqlite3 (2.10.2-x86_64-linux-musl) sha256=b9f8fc62f3e0eb8966b41de251b492b4c8e9d1b23559beb6e150ad7f8d590941 sqlite3 (2.10.2-x86_64-linux-musl) sha256=b9f8fc62f3e0eb8966b41de251b492b4c8e9d1b23559beb6e150ad7f8d590941
sshkit (1.25.1) sha256=be3f10b9d6eb0b44d5eaba3f7cbe41bc6bb894bce4339688ac20124391455b78 sshkit (1.25.1) sha256=be3f10b9d6eb0b44d5eaba3f7cbe41bc6bb894bce4339688ac20124391455b78
standardwebhooks (1.0.1) sha256=35db391599d9318747e1e75e77b96236b3fc25241b92387c9a8d31bd974f3d7d
stimulus-rails (1.3.4) sha256=765676ffa1f33af64ce026d26b48e8ffb2e0b94e0f50e9119e11d6107d67cb06 stimulus-rails (1.3.4) sha256=765676ffa1f33af64ce026d26b48e8ffb2e0b94e0f50e9119e11d6107d67cb06
tailwindcss-rails (4.7.0) sha256=9b09bd677d7cd226fc8ff3f7d2d91c6dc6c3da75dd74dc4cab5f94711f69ba41 tailwindcss-rails (4.7.0) sha256=9b09bd677d7cd226fc8ff3f7d2d91c6dc6c3da75dd74dc4cab5f94711f69ba41
tailwindcss-ruby (4.3.3) sha256=ee0a64030749862deb501acab4c4aaf5adbee13865746a33299d46d7b5d0952a tailwindcss-ruby (4.3.3) sha256=ee0a64030749862deb501acab4c4aaf5adbee13865746a33299d46d7b5d0952a
+9 -1
View File
@@ -55,6 +55,8 @@ The app is a standard Rails app. Run `bin/rails db:prepare && bin/thrust bin/rai
| `FORCE_SSL` | `true` (default) redirects to HTTPS and sets secure cookies. Use `false` for plain HTTP. | | `FORCE_SSL` | `true` (default) redirects to HTTPS and sets secure cookies. Use `false` for plain HTTP. |
| `APP_HOST` | Host used in password-reset email links. | | `APP_HOST` | Host used in password-reset email links. |
| `SMTP_ADDRESS`, `SMTP_PORT`, `SMTP_USERNAME`, `SMTP_PASSWORD`, `MAIL_FROM` | Optional. They enable password-reset emails. Without them an admin resets passwords from **Users**. | | `SMTP_ADDRESS`, `SMTP_PORT`, `SMTP_USERNAME`, `SMTP_PASSWORD`, `MAIL_FROM` | Optional. They enable password-reset emails. Without them an admin resets passwords from **Users**. |
| `ANTHROPIC_API_KEY` | Optional. Lets Claude transcribe scanned logbook pages into entries (**Logbook → Import scans**). Without it, scans are only uploaded. |
| `LOGBOOK_AI_MODEL`, `LOGBOOK_AI_EFFORT` | Optional. The Claude model and effort used for transcription (default `claude-opus-5-5`, `high`). |
### Backups ### Backups
@@ -68,6 +70,8 @@ rsync -a --exclude '*.sqlite3*' storage/ backup/ # uploaded files
Alternatively, stop the container and copy the volume. Alternatively, stop the container and copy the volume.
To back up or move a single aircraft with all its files, use **Access & Sharing → Download .sam.zip** (or `bin/rails "sam:export[N12345]"`). Import it from **Fleet → Import** (or `bin/rails "sam:import[file.sam.zip,owner@example.com]"`). The archive format is the original Simple Aircraft Manager's, so archives move between the two apps.
## Features ## Features
| Area | Notes | | Area | Notes |
@@ -124,7 +128,11 @@ Differences from the original:
### Not yet ported ### Not yet ported
AI logbook transcription, oil-analysis PDF import, the MCP server for AI agents, OIDC single sign-on, invitation codes, aircraft import/export, the plugin system, and the desktop builds. The MCP server for AI agents, OIDC single sign-on, invitation codes, per-aircraft feature switches, the plugin system, and the desktop builds.
### Added in this port
AirTrail sync and export, an airport database, FAA DRS AD import, spreadsheet flight import, engine monitor data (CGR-30P/C) with interactive charts, AvLab and Blackstone oil analysis import with trends, multiple squawk attachments with a photo viewer, AI logbook transcription with Claude (images and PDFs), and an installable PWA.
## License ## License
@@ -0,0 +1,19 @@
# Download an aircraft as a .sam.zip archive (owners and admins).
class AircraftExportsController < ApplicationController
include AircraftScoped
before_action :require_owner!
EXPORT_DIR = Rails.root.join("tmp/exports")
def show
exporter = AircraftArchive::Exporter.new(@aircraft, source: request.host)
# A plain file (not a Tempfile) so it's still there while send_file streams it;
# older exports are swept on the next one.
FileUtils.mkdir_p(EXPORT_DIR)
Dir[EXPORT_DIR.join("*.zip")].each { |old| File.delete(old) if File.mtime(old) < 1.hour.ago }
path = EXPORT_DIR.join("#{SecureRandom.hex(12)}.zip").to_s
exporter.write_to(path)
log_event("aircraft", "Aircraft exported (.sam.zip)")
send_file path, filename: exporter.filename, type: "application/zip", disposition: "attachment"
end
end
@@ -0,0 +1,39 @@
# Upload a .sam.zip (from this app or the original Simple Aircraft Manager) to
# create an aircraft with all its records. The archive is checked right away;
# the import itself runs in the background (AircraftImportJob).
class AircraftImportsController < ApplicationController
before_action :set_import, only: :show
def new; end
def create
file = params[:archive]
return redirect_to(new_aircraft_import_path, alert: "Choose a .sam.zip file.") if file.blank?
preview = AircraftArchive::Importer.check(file.tempfile.path)
tail = params[:tail_number].to_s.strip.upcase.presence || preview.tail_number.to_s.upcase
if Aircraft.where("UPPER(tail_number) = ?", tail).exists?
@preview = preview
@conflict = tail
flash.now[:alert] = "An aircraft #{tail} already exists. Choose another tail number for the imported one."
return render :new, status: :unprocessable_entity
end
import = AircraftImport.create!(user: current_user, filename: file.original_filename, tail_number: tail, source: preview.source)
import.archive.attach(io: file.tempfile.tap(&:rewind), filename: file.original_filename.presence || "archive.sam.zip", content_type: "application/zip")
AircraftImportJob.perform_later(import.id)
redirect_to aircraft_import_path(import)
rescue AircraftArchive::Error => e
redirect_to new_aircraft_import_path, alert: "That archive can't be imported: #{e.message}"
end
def show; end
private
# Users see their own imports; admins see all.
def set_import
scope = current_user.admin? ? AircraftImport.all : AircraftImport.where(user: current_user)
@import = scope.find(params[:id])
end
end
@@ -0,0 +1,55 @@
# Upload scanned logbook pages (images or PDFs) as a document and, when an
# Anthropic API key is configured, have Claude transcribe them into logbook
# entries in the background (LogbookTranscriptionJob).
class LogbookImportsController < ApplicationController
include AircraftScoped
before_action :require_owner!
def new
@collections = @aircraft.document_collections.ordered
end
def create
files = Array(params[:pages]).compact_blank
return redirect_to(new_aircraft_logbook_import_path(@aircraft), alert: "Choose the scanned pages to upload.") if files.empty?
# Page order follows the file names ("page-002" before "page-010").
files = files.sort_by { |f| f.original_filename.to_s.downcase.scan(/\d+|\D+/).map { _1.match?(/\d/) ? [ 0, _1.to_i ] : [ 1, _1 ] } }
collection = find_or_build_collection
document = @aircraft.documents.new(name: params[:document_name].presence || collection.name, doc_type: doc_type,
document_collection: collection)
document.pages.attach(files)
unless collection.valid? && document.valid?
@collections = @aircraft.document_collections.ordered
flash.now[:alert] = (collection.errors.full_messages + document.errors.full_messages).to_sentence
return render :new, status: :unprocessable_entity
end
import = nil
ActiveRecord::Base.transaction do
collection.save!
document.save!
import = @aircraft.logbook_imports.create!(user: current_user, document: document, log_type: params[:log_type],
transcribe: params[:transcribe] == "1" && LogbookImport.ai_available?)
end
log_event("document", "Logbook pages uploaded: #{document.name} (#{files.size} #{'page'.pluralize(files.size)})")
LogbookTranscriptionJob.perform_later(import.id)
redirect_to aircraft_logbook_import_path(@aircraft, import)
end
def show
@import = @aircraft.logbook_imports.find(params[:id])
end
private
def find_or_build_collection
if params[:collection_id].present? && params[:collection_id] != "new"
@aircraft.document_collections.find(params[:collection_id])
else
@aircraft.document_collections.new(name: params[:collection_name].presence || "Logbooks", visibility: "maintenance")
end
end
def doc_type = Document::TYPES.key?(params[:doc_type]) ? params[:doc_type] : "LOG"
end
+7 -6
View File
@@ -31,8 +31,8 @@ class OilAnalysesController < ApplicationController
end end
end end
# Create records from a lab report PDF. AvLab reports include the two previous # Create records from a lab report PDF (AvLab or Blackstone). Reports repeat
# samples too, so samples already on file are skipped rather than duplicated. # earlier samples, so samples already on file are skipped rather than duplicated.
def import def import
file = params[:report] file = params[:report]
return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: "Choose a lab report PDF.") if file.blank? return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: "Choose a lab report PDF.") if file.blank?
@@ -50,7 +50,7 @@ class OilAnalysesController < ApplicationController
OilAnalysis.transaction do OilAnalysis.transaction do
report.samples.each do |sample| report.samples.each do |sample|
record = existing_sample(sample) || record = existing_sample(sample) ||
@aircraft.oil_analyses.create!(sample.except(:current).merge(lab: report.lab, component: engine)).tap { created << _1 } @aircraft.oil_analyses.create!(sample.except(:current).merge(lab: report.lab, oil_type: report.oil_type, component: engine)).tap { created << _1 }
current_record = record if sample[:current] current_record = record if sample[:current]
end end
end end
@@ -108,9 +108,10 @@ class OilAnalysesController < ApplicationController
def analysis_params def analysis_params
params.require(:oil_analysis).permit( params.require(:oil_analysis).permit(
:component_id, :sample_date, :analysis_date, :lab, :sample_number, :engine_hours, :oil_hours, :component_id, :sample_date, :analysis_date, :lab, :sample_number, :oil_type, :engine_hours, :oil_hours,
:filter_hours, :oil_added, :status, :lab_comments, :notes, :report, :filter_hours, :oil_added, :status, :lab_comments, :notes, :report, :excluded_from_averages,
*OilAnalysis::ELEMENTS.keys.flat_map { [ _1, :"#{_1}_avg" ] } elements: OilAnalysis::ELEMENTS.keys, element_averages: OilAnalysis::ELEMENTS.keys,
oil_properties: OilAnalysis::PROPERTIES.keys
).tap { |p| p[:component_id] = scoped_id(@aircraft.components, p[:component_id]) if p.key?(:component_id) } ).tap { |p| p[:component_id] = scoped_id(@aircraft.components, p[:component_id]) if p.key?(:component_id) }
end end
end end
+13 -3
View File
@@ -1,9 +1,18 @@
module OilAnalysesHelper module OilAnalysesHelper
STATUS_BADGES = { "normal" => "badge-green", "elevated" => "badge-orange", "high" => "badge-red" }.freeze STATUS_BADGES = { "normal" => "badge-green", "monitor" => "badge-orange", "action_required" => "badge-red" }.freeze
TREND_COLORS = %w[#0ea5e9 #f97316 #22c55e #a855f7 #eab308 #ec4899 #64748b].freeze TREND_COLORS = %w[#0ea5e9 #f97316 #22c55e #a855f7 #eab308 #ec4899 #64748b].freeze
# The lab's verdict; Blackstone reports don't rate samples, so there may be none.
def oil_status_badge(analysis) def oil_status_badge(analysis)
tag.span OilAnalysis::STATUSES[analysis.status], class: STATUS_BADGES[analysis.status] return tag.span("Not rated", class: "badge-gray") unless analysis.status
tag.span analysis.status_label, class: STATUS_BADGES[analysis.status]
end
def oil_property(analysis, key)
value = analysis.property(key) or return "—"
_, unit = OilAnalysis::PROPERTIES.fetch(key)
number = number_with_precision(value, precision: 2, strip_insignificant_zeros: true)
unit.present? ? "#{number} #{unit}" : number
end end
def ppm(value) def ppm(value)
@@ -28,8 +37,9 @@ module OilAnalysesHelper
"#{sign}#{ppm(delta)}" "#{sign}#{ppm(delta)}"
end end
# Samples marked "exclude from averages" (e.g. right after an overhaul) are left out.
def oil_trend_series(analyses) def oil_trend_series(analyses)
chronological = analyses.sort_by(&:sample_date) chronological = analyses.reject(&:excluded_from_averages?).sort_by(&:sample_date)
OilAnalysis::TREND_ELEMENTS.filter_map do |el| OilAnalysis::TREND_ELEMENTS.filter_map do |el|
data = chronological.filter_map { |a| [ a.sample_date, a.value(el).to_f ] if a.value(el) } data = chronological.filter_map { |a| [ a.sample_date, a.value(el).to_f ] if a.value(el) }
{ name: OilAnalysis.element_label(el), data: data } if data.size >= 2 { name: OilAnalysis.element_label(el), data: data } if data.size >= 2
+24
View File
@@ -0,0 +1,24 @@
# Imports a staged .sam.zip (see AircraftArchive::Importer) and records the outcome.
class AircraftImportJob < ApplicationJob
queue_as :default
def perform(import_id)
import = AircraftImport.find_by(id: import_id) or return
return unless import.status == "queued"
import.update!(status: "running", started_at: Time.current)
result = import.archive.open do |file|
AircraftArchive::Importer.new(file.path, owner: import.user, tail_number: import.tail_number,
progress: ->(counts) { import.update_columns(counts: counts, updated_at: Time.current) }).run
end
import.update!(status: "done", finished_at: Time.current, aircraft: result[:aircraft],
counts: result[:counts], warnings: result[:warnings].first(200))
rescue AircraftArchive::Error => e
import&.update!(status: "failed", finished_at: Time.current, error: e.message)
rescue StandardError => e
import&.update!(status: "failed", finished_at: Time.current, error: "Import failed: #{e.class}: #{e.message}".truncate(1000))
Rails.logger.error("[aircraft import] #{e.class}: #{e.message}\n#{e.backtrace&.first(10)&.join("\n")}")
ensure
import&.archive&.purge_later if import&.reload&.status.in?(%w[done failed])
end
end
+162
View File
@@ -0,0 +1,162 @@
# Transcribes an uploaded logbook document into LogbookEntry records with
# Claude (LogbookTranscriber).
#
# Images go up in batches of a few pages. Consecutive batches overlap by one
# page so an entry running onto the next page is read whole: entries seen on a
# batch's last page are held back and handed to the next batch, which returns
# them completed. Each PDF goes up as one request.
class LogbookTranscriptionJob < ApplicationJob
queue_as :default
class_attribute :batch_pages, default: 6
BATCH_BYTES = 18.megabytes # base64 grows this by a third; requests cap at 32 MB
def perform(import_id)
@import = LogbookImport.find_by(id: import_id) or return
return unless @import.status == "queued"
@document = @import.document
pages = @document ? @document.pages.attachments.includes(:blob).sort_by(&:id) : []
@import.update!(status: "running", started_at: Time.current, pages_total: pages.size, model: (LogbookTranscriber::MODEL if @import.transcribe))
transcribe(pages) if @import.transcribe && pages.any?
finish
rescue StandardError => e
@import&.update!(status: "failed", finished_at: Time.current, error: "#{e.class}: #{e.message}".truncate(1000))
Rails.logger.error("[logbook import] #{e.class}: #{e.message}\n#{e.backtrace&.first(10)&.join("\n")}")
end
private
def transcribe(pages)
@transcriber = LogbookTranscriber.new
@last_date = nil
readable = pages.each_with_index.filter_map do |attachment, index|
type = attachment.blob.content_type
if type == "application/pdf"
next warn("Page #{index + 1} (#{attachment.filename}) is over #{LogbookTranscriber::MAX_PDF_BYTES / 1.megabyte} MB, too large to transcribe.") if attachment.blob.byte_size > LogbookTranscriber::MAX_PDF_BYTES
elsif !LogbookTranscriber::IMAGE_TYPES.include?(type)
next warn("Page #{index + 1} (#{attachment.filename}) isn't a JPEG, PNG, GIF, WebP or PDF, so it wasn't transcribed.")
elsif attachment.blob.byte_size > LogbookTranscriber::MAX_IMAGE_BYTES
next warn("Page #{index + 1} (#{attachment.filename}) is over 5 MB, too large to transcribe. Scan it at a lower resolution.")
end
{ attachment: attachment, number: index + 1, pdf: type == "application/pdf" }
end
tick(pages.size - readable.size)
readable.slice_when { |a, b| a[:pdf] || b[:pdf] }.each do |run|
run.first[:pdf] ? transcribe_pdf(run.first) : transcribe_images(run)
end
end
def transcribe_pdf(page)
result = @transcriber.transcribe([ payload(page) ])
note_skipped(result, ->(i) { "#{page[:attachment].filename} page #{i + 1}" })
result.entries.sort_by { _1["page_start"].to_i }.each { save(_1, page[:number]) }
rescue LogbookTranscriber::Error => e
warn("#{page[:attachment].filename} wasn't transcribed: #{e.message}.")
ensure
tick(1)
end
def transcribe_images(pages)
batches = []
start = 0
while start < pages.size
batch = [ pages[start] ]
bytes = pages[start][:attachment].blob.byte_size
while (nxt = pages[start + batch.size]) && batch.size < batch_pages && bytes + nxt[:attachment].blob.byte_size <= BATCH_BYTES
batch << nxt
bytes += nxt[:attachment].blob.byte_size
end
batches << batch
break if start + batch.size >= pages.size
start += [ batch.size - 1, 1 ].max # overlap by one page
end
held = [] # entries read from the previous batch's last page
batches.each_with_index do |batch, n|
last_batch = n == batches.size - 1
begin
result = @transcriber.transcribe(batch.map { payload(_1) }, carried: held.map { _1[:entry] })
rescue LogbookTranscriber::Error => e
held.each { save(_1[:entry], _1[:number]) } # keep what the previous batch read
held = []
pages = batch.size == 1 ? "Page #{batch.first[:number]} wasn't" : "Pages #{batch.first[:number]}–#{batch.last[:number]} weren't"
warn("#{pages} transcribed: #{e.message}.")
tick(n.zero? ? batch.size : batch.size - 1)
next
end
note_skipped(result, ->(i) { "page #{batch[i]&.dig(:number) || '?'}" })
entries = result.entries.sort_by { _1["page_start"].to_i }
held = []
entries.each do |entry|
page = batch[entry["page_start"].to_i.clamp(0, batch.size - 1)]
if !last_batch && entry["page_end"].to_i >= batch.size - 1
held << { entry: entry, number: page[:number] } # may continue on the next batch
else
save(entry, page[:number])
end
end
tick(n.zero? ? batch.size : batch.size - 1)
end
held.each { save(_1[:entry], _1[:number]) }
end
def payload(page)
{ data: page[:attachment].blob.download, media_type: page[:attachment].blob.content_type }
end
def save(entry, page_number)
date = parse_date(entry["date"])
text = entry["text"].to_s.strip.presence || "[no text read]"
unless date
text = "[date unreadable] #{text}"
warn("An entry on page #{page_number} had no readable date#{@last_date ? "; it was given the previous entry's date (#{@last_date})" : ' and was dated today'}. Please check it.")
end
date ||= @last_date || Date.current
@last_date = date
log_type = @import.log_type || (LogbookEntry::LOG_TYPES.key?(entry["log_type"]) ? entry["log_type"] : "AC")
@import.aircraft.logbook_entries.create!(
date: date, text: text, log_type: log_type,
entry_type: LogbookEntry::ENTRY_TYPES.key?(entry["entry_type"]) ? entry["entry_type"] : "OTHER",
signoff_person: entry["signoff_person"].presence, signoff_location: entry["signoff_location"].presence,
aircraft_hours_at_entry: entry["aircraft_hours"].is_a?(Numeric) && entry["aircraft_hours"] >= 0 ? entry["aircraft_hours"] : nil,
document: @document, page_number: page_number
)
@import.increment!(:entries_created)
warn("The #{date} entry on page #{page_number} was hard to read. Please check it against the scan.") if entry["confidence"] == "low"
end
def note_skipped(result, label)
forms = result.non_logbook_pages.map { label.(_1) }
unreadable = result.unparseable_pages.map { label.(_1) }
warn("Not logbook entries (forms, tags…), so nothing was transcribed from: #{forms.uniq.to_sentence}.") if forms.any?
warn("Too hard to read: #{unreadable.uniq.to_sentence}.") if unreadable.any?
end
def parse_date(value)
date = Date.iso8601(value.to_s) if value.present?
date if date && date.year > 1900 && date <= Date.current + 1.year
rescue Date::Error
nil
end
def warn(message)
@import.warnings = @import.warnings + [ message ]
@import.save!
nil
end
def tick(pages)
@import.update_columns(pages_done: [ @import.pages_done + pages, @import.pages_total ].min, updated_at: Time.current) if pages.positive?
end
def finish
@import.update!(status: "done", finished_at: Time.current, pages_done: @import.pages_total)
if @import.transcribe
@import.aircraft.log_event!("logbook", "Logbook pages transcribed (#{@import.entries_created} entries)",
notes: @document&.name, user: @import.user)
end
end
end
+1
View File
@@ -16,6 +16,7 @@ class Aircraft < ApplicationRecord
has_many :components, dependent: :destroy has_many :components, dependent: :destroy
has_many :squawks, dependent: :destroy has_many :squawks, dependent: :destroy
has_many :logbook_entries, dependent: :destroy has_many :logbook_entries, dependent: :destroy
has_many :logbook_imports, dependent: :destroy
has_many :document_collections, dependent: :destroy has_many :document_collections, dependent: :destroy
has_many :documents, dependent: :destroy has_many :documents, dependent: :destroy
has_many :inspection_records, dependent: :destroy has_many :inspection_records, dependent: :destroy
+19
View File
@@ -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
+4
View File
@@ -7,6 +7,10 @@ class Component < ApplicationRecord
has_many :child_components, class_name: "Component", foreign_key: :parent_component_id, dependent: :destroy has_many :child_components, class_name: "Component", foreign_key: :parent_component_id, dependent: :destroy
has_many :squawks, dependent: :nullify has_many :squawks, dependent: :nullify
has_many :oil_analyses, 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 :inspection_types
has_and_belongs_to_many :airworthiness_directives has_and_belongs_to_many :airworthiness_directives
+22
View File
@@ -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
View File
@@ -1,8 +1,14 @@
# A spectrographic oil analysis lab report for one oil sample. # 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 class OilAnalysis < ApplicationRecord
include UploadValidation 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 = { ELEMENTS = {
"iron" => [ "Iron", "Cylinders, cam, lifters, gears" ], "iron" => [ "Iron", "Cylinders, cam, lifters, gears" ],
"copper" => [ "Copper", "Bearings, bushings, oil cooler" ], "copper" => [ "Copper", "Bearings, bushings, oil cooler" ],
@@ -15,9 +21,30 @@ class OilAnalysis < ApplicationRecord
"silicon" => [ "Silicon", "Dirt (air filter), sealant" ], "silicon" => [ "Silicon", "Dirt (air filter), sealant" ],
"titanium" => [ "Titanium", "Hardened parts" ], "titanium" => [ "Titanium", "Hardened parts" ],
"tin" => [ "Tin", "Bearings, piston pin bushings" ], "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 }.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. # A reading this far above the lab's average for similar engines gets flagged.
FLAG_RATIO = 1.5 FLAG_RATIO = 1.5
# Wear metals worth charting together (lead is on its own scale: it comes from fuel). # 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 has_one_attached :report
validates :sample_date, presence: true validates :sample_date, presence: true
validates :status, inclusion: { in: STATUSES.keys } validates :status, inclusion: { in: STATUSES.keys }, allow_nil: true
validates(*ELEMENTS.keys.flat_map { [ _1, :"#{_1}_avg" ] }, :engine_hours, :oil_hours, :filter_hours, :oil_added, validates :engine_hours, :oil_hours, :filter_hours, :oil_added, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
numericality: { greater_than_or_equal_to: 0 }, allow_nil: true) validate :results_are_numbers
validate :component_belongs_to_aircraft validate :component_belongs_to_aircraft
validates_uploads :report 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) } scope :recent_first, -> { order(sample_date: :desc, created_at: :desc) }
def self.element_label(el) = ELEMENTS.fetch(el)[0] def self.element_label(el) = ELEMENTS.fetch(el)[0]
def value(el) = self[el] # Elements any of these samples has a reading for, in display order.
def average(el) = self[:"#{el}_avg"] 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). # Above the lab average by FLAG_RATIO (needs an average to compare against).
def flagged?(el) def flagged?(el)
@@ -50,10 +99,33 @@ class OilAnalysis < ApplicationRecord
end end
def flagged_elements = ELEMENTS.keys.select { flagged?(_1) } 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 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 def component_belongs_to_aircraft
errors.add(:component, "must be on this aircraft") if component && component.aircraft_id != aircraft_id errors.add(:component, "must be on this aircraft") if component && component.aircraft_id != aircraft_id
end end
+43
View File
@@ -0,0 +1,43 @@
# Aircraft archives (.sam.zip): a whole aircraft's records and files in one ZIP,
# in the format of the original Simple Aircraft Manager (manifest schema v2), so
# archives move both ways between this app and the original.
#
# manifest.json all records, with archive-local IDs linking them
# attachments/… pictures, document pages, squawk files, track logs…
#
# Records carry a few extra keys for data only this app has (all squawk
# attachments, engine monitor files, oil report PDFs and lab averages, newer AD
# fields). The original ignores keys it doesn't know inside records, but rejects
# unknown top-level keys, so none are added there.
module AircraftArchive
class Error < StandardError; end
SCHEMA_VERSION = 2
TOP_LEVEL_KEYS = %w[
schema_version exported_at source_instance aircraft component_types components document_collections
documents document_images logbook_entries squawks inspection_types inspection_records ads ad_compliances
consumable_records major_records notes oil_analysis_reports flight_logs features
].freeze
OPTIONAL_KEYS = %w[features].freeze # and flight_logs before schema v2
MANIFEST_MAX_BYTES = 50.megabytes
FILE_MAX_BYTES = 512.megabytes
ARCHIVE_MAX_BYTES = 10.gigabytes # uncompressed
MAX_RATIO = 100 # compression ratio above this looks like a zip bomb
# Leading bytes per extension; text formats have none to check.
MAGIC = {
"jpg" => [ "\xFF\xD8\xFF".b ], "jpeg" => [ "\xFF\xD8\xFF".b ], "png" => [ "\x89PNG\r\n\x1A\n".b ],
"gif" => [ "GIF8".b ], "webp" => [ "RIFF".b ], "bmp" => [ "BM".b ], "tif" => [ "II*\x00".b, "MM\x00*".b ],
"tiff" => [ "II*\x00".b, "MM\x00*".b ], "pdf" => [ "%PDF-".b ]
}.freeze
TEXT_EXTENSIONS = %w[txt csv gpx kml xml].freeze
EXTENSIONS = (MAGIC.keys + TEXT_EXTENSIONS).freeze
def self.extension(path) = File.extname(path.to_s).delete(".").downcase
def self.content_matches?(data, ext)
return EXTENSIONS.include?(ext) unless MAGIC.key?(ext)
MAGIC[ext].any? { data.byteslice(0, _1.bytesize) == _1 }
end
end
+191
View File
@@ -0,0 +1,191 @@
require "zip"
module AircraftArchive
# Writes an aircraft and everything recorded for it to a .sam.zip.
#
# AircraftArchive::Exporter.new(aircraft).write_to(path)
class Exporter
def initialize(aircraft, source: nil)
@aircraft = aircraft
@source = source
@files = {} # archive path → attachment
@missing = 0
end
attr_reader :missing # files that were attached but missing from storage
def filename = "#{@aircraft.tail_number.to_s.downcase.gsub(/[^a-z0-9-]/, '')}-#{Date.current.iso8601}.sam.zip"
def write_to(path)
manifest = build_manifest
Zip::OutputStream.open(path) do |zip|
zip.put_next_entry("manifest.json")
zip.write(JSON.pretty_generate(manifest))
@files.each do |archive_path, attachment|
zip.put_next_entry(archive_path)
attachment.blob.download { zip.write(_1) }
end
end
path
end
def build_manifest
a = @aircraft
components = a.components.includes(:component_type).to_a
component_ids = components.map(&:id)
inspection_types = (a.inspection_types.to_a + InspectionType.joins(:components).where(components: { id: component_ids }).to_a +
InspectionType.where(id: a.inspection_records.select(:inspection_type_id)).to_a).uniq
ads = (a.airworthiness_directives.to_a + AirworthinessDirective.joins(:components).where(components: { id: component_ids }).to_a +
AirworthinessDirective.where(id: a.ad_compliances.select(:airworthiness_directive_id)).to_a).uniq
{
schema_version: SCHEMA_VERSION,
exported_at: Time.current.iso8601,
source_instance: @source.to_s,
aircraft: aircraft_hash(a),
component_types: components.map(&:component_type).uniq.map { { id: id(_1), name: _1.name, consumable: _1.consumable } },
components: components.map { component_hash(_1) },
document_collections: a.document_collections.map do |c|
{ id: id(c), aircraft_id: id(a), name: c.name, description: c.description, visibility: c.visibility, starred: c.starred, components: [] }
end,
documents: a.documents.map do |d|
{ id: id(d), aircraft_id: id(a), collection_id: id(d.document_collection_id), doc_type: d.doc_type, name: d.name,
description: d.description, visibility: d.visibility, components: [] }
end,
document_images: a.documents.includes(pages_attachments: :blob).flat_map do |d|
d.pages.map { |page| { id: id(page), document_id: id(d), notes: "", image: file(page, "health/documents") } }
end,
logbook_entries: a.logbook_entries.map { logbook_hash(_1) },
squawks: a.squawks.includes(:reported_by, attachments_attachments: :blob).map { squawk_hash(_1) },
inspection_types: inspection_types.map { inspection_type_hash(_1, component_ids) },
inspection_records: a.inspection_records.map do |r|
{ id: id(r), date: r.date&.iso8601, aircraft_hours: dec(r.aircraft_hours), inspection_type_id: id(r.inspection_type_id),
logbook_entry_id: id(r.logbook_entry_id), aircraft_id: id(a), documents: [], component: [ id(r.component_id) ].compact,
notes: r.notes }
end,
ads: ads.map { ad_hash(_1, component_ids) },
ad_compliances: a.ad_compliances.map do |c|
{ id: id(c), ad_id: id(c.airworthiness_directive_id), date_complied: c.date_complied&.iso8601, compliance_notes: c.compliance_notes,
permanent: c.permanent, next_due_at_time: dec(c.next_due_at_time), aircraft_hours_at_compliance: dec(c.aircraft_hours_at_compliance),
logbook_entry_id: id(c.logbook_entry_id), inspection_record_id: id(c.inspection_record_id), aircraft_id: id(a),
component_id: id(c.component_id), not_applicable: c.not_applicable }
end,
consumable_records: a.consumable_records.map do |r|
{ id: id(r), record_type: r.record_type, aircraft_id: id(a), date: r.date&.iso8601, quantity_added: dec(r.quantity_added),
level_after: dec(r.level_after), consumable_type: r.consumable_type, flight_hours: dec(r.flight_hours), notes: r.notes,
excluded_from_averages: r.excluded_from_averages, flight_log_id: id(r.flight_log_id) }
end,
major_records: a.major_records.map do |r|
{ id: id(r), aircraft_id: id(a), record_type: r.record_type, title: r.title, description: r.description,
date_performed: r.date_performed&.iso8601, performed_by: r.performed_by, component_id: id(r.component_id),
form_337_document_id: id(r.form_337_document_id), stc_number: r.stc_number, stc_holder: r.stc_holder,
stc_document_id: id(r.stc_document_id), logbook_entry_id: id(r.logbook_entry_id), aircraft_hours: dec(r.aircraft_hours),
notes: r.notes, has_ica: r.has_ica, ica_notes: r.ica_notes }
end,
notes: a.notes.includes(:user).map do |n|
{ id: id(n), aircraft_id: id(a), text: n.text, public: n.public, added_by_display: n.user&.display_name,
added_timestamp: n.created_at&.iso8601, edited_at: n.edited_at&.iso8601 }
end,
oil_analysis_reports: a.oil_analyses.includes(report_attachment: :blob).map { oil_hash(_1) },
flight_logs: a.flight_logs.includes(:user, track_log_attachment: :blob, engine_data_attachment: :blob).map { flight_hash(_1) },
features: [ { feature: "airworthiness_enforcement", enabled: a.airworthiness_enforcement } ]
}
end
private
def id(record_or_id)
value = record_or_id.respond_to?(:id) ? record_or_id.id : record_or_id
value&.to_s
end
def dec(value) = value&.to_s
# Queue an attachment for the archive and return its archive path. Each file
# gets its own folder so original filenames survive. A file missing from
# storage is left out (and counted) rather than failing the export.
def file(attachable, folder)
attachment = attachable.is_a?(ActiveStorage::Attachment) ? attachable : attachable&.attachment
return nil unless attachment
unless attachment.blob.service.exist?(attachment.blob.key)
@missing += 1
return nil
end
name = attachment.filename.sanitized.presence || "file"
path = "attachments/#{folder}/#{attachment.blob.key}/#{name}"
@files[path] = attachment
path
end
def aircraft_hash(a)
{ id: id(a), tail_number: a.tail_number, make: a.make, model: a.model, serial_number: a.serial_number,
description: a.description, purchased: a.purchased&.iso8601, status: a.status, tach_time: dec(a.tach_time),
tach_time_offset: dec(a.tach_time_offset), hobbs_time: dec(a.hobbs_time), hobbs_time_offset: dec(a.hobbs_time_offset),
picture: file(a.picture, "aircraft_pictures"), icao_type: a.icao_type }
end
def component_hash(c)
{ id: id(c), aircraft_id: id(c.aircraft_id), parent_component_id: id(c.parent_component_id), component_type_id: id(c.component_type_id),
manufacturer: c.manufacturer, model: c.model, serial_number: c.serial_number, install_location: c.install_location,
notes: c.notes, status: c.status, date_in_service: c.date_in_service&.iso8601, hours_in_service: dec(c.hours_in_service),
hours_since_overhaul: dec(c.hours_since_overhaul), overhaul_date: c.overhaul_date&.iso8601, tbo_hours: c.tbo_hours,
tbo_days: c.tbo_days, inspection_hours: c.inspection_hours, inspection_days: c.inspection_days,
replacement_hours: c.replacement_hours, replacement_days: c.replacement_days, tbo_critical: c.tbo_critical,
on_condition: c.on_condition, inspection_critical: c.inspection_critical, replacement_critical: c.replacement_critical }
end
def logbook_hash(e)
{ id: id(e), aircraft_id: id(e.aircraft_id), log_type: e.log_type, date: e.date&.iso8601, text: e.text,
signoff_person: e.signoff_person, signoff_location: e.signoff_location, log_image_id: id(e.document_id),
aircraft_hours_at_entry: dec(e.aircraft_hours_at_entry), component_hours: {}, entry_type: e.entry_type,
page_number: e.page_number, components: [], related_documents: [] }
end
def squawk_hash(s)
paths = s.attachments.map { file(_1, "health/squawks") }
{ id: id(s), aircraft_id: id(s.aircraft_id), component_id: id(s.component_id), priority: s.priority,
issue_reported: s.issue_reported, attachment: paths.first, created_at: s.created_at&.iso8601,
reported_by_display: s.reported_by&.display_name, resolved: s.resolved, notes: s.notes, logbook_entries: [],
attachments: paths, resolved_at: s.resolved_at&.iso8601 }
end
def inspection_type_hash(t, component_ids)
{ id: id(t), name: t.name, recurring: t.recurring, required: t.required, recurring_hours: dec(t.recurring_hours),
recurring_days: t.recurring_days, recurring_months: t.recurring_months,
applicable_aircraft: t.aircraft_ids.include?(@aircraft.id) ? [ id(@aircraft) ] : [],
applicable_component: (t.component_ids & component_ids).map { id(_1) } }
end
def ad_hash(ad, component_ids)
{ id: id(ad), name: ad.name, short_description: ad.short_description, required_action: ad.required_action,
compliance_type: ad.compliance_type, trigger_condition: ad.trigger_condition, recurring: ad.recurring,
recurring_hours: dec(ad.recurring_hours), recurring_months: ad.recurring_months, recurring_days: ad.recurring_days,
bulletin_type: ad.bulletin_type, mandatory: ad.mandatory,
document_id: (id(ad.document_id) if ad.document&.aircraft_id == @aircraft.id), on_inspection_type: [],
applicable_aircraft: ad.aircraft_ids.include?(@aircraft.id) ? [ id(@aircraft) ] : [],
applicable_component: (ad.component_ids & component_ids).map { id(_1) },
effective_date: ad.effective_date&.iso8601, supersedes: ad.supersedes, superseded_by: ad.superseded_by,
docket_number: ad.docket_number, applicable_models: ad.applicable_models, product_type: ad.product_type,
document_url: ad.document_url }
end
def oil_hash(o)
{ id: id(o), aircraft_id: id(o.aircraft_id), component_id: id(o.component_id), sample_date: o.sample_date&.iso8601,
analysis_date: o.analysis_date&.iso8601, lab: o.lab.to_s, lab_number: o.sample_number.to_s, oil_type: o.oil_type.to_s,
oil_hours: dec(o.oil_hours), engine_hours: dec(o.engine_hours), oil_added_quarts: dec(o.oil_added),
elements_ppm: o.elements, oil_properties: o.oil_properties.presence, lab_comments: o.lab_comments.to_s,
status: o.status, notes: o.notes.to_s, excluded_from_averages: o.excluded_from_averages,
filter_hours: dec(o.filter_hours), element_averages: o.element_averages, report: file(o.report, "oil_reports") }
end
def flight_hash(f)
{ id: id(f), aircraft_id: id(f.aircraft_id), date: f.date&.iso8601, tach_time: dec(f.tach_time), tach_out: dec(f.tach_out),
tach_in: dec(f.tach_in), hobbs_time: dec(f.hobbs_time), hobbs_out: dec(f.hobbs_out), hobbs_in: dec(f.hobbs_in),
departure_location: f.departure_location.to_s, destination_location: f.destination_location.to_s, route: f.route.to_s,
oil_added: dec(f.oil_added), oil_added_type: f.oil_added_type.to_s, oil_level_after: dec(f.oil_level_after),
fuel_added: dec(f.fuel_added), fuel_added_type: f.fuel_added_type.to_s, fuel_level_after: dec(f.fuel_level_after),
track_log: file(f.track_log, "track_logs"), notes: f.notes.to_s, created_at: f.created_at&.iso8601,
engine_data: file(f.engine_data, "engine_data"), pilot_display: f.user&.display_name }
end
end
end
+479
View File
@@ -0,0 +1,479 @@
require "zip"
module AircraftArchive
# Reads a .sam.zip (from this app or the original Simple Aircraft Manager) and
# creates the aircraft with all its records and files.
#
# preview = AircraftArchive::Importer.check(path) # fast safety checks
# AircraftArchive::Importer.new(path, owner:, tail_number:).run
#
# Archive IDs are only used to link records inside the archive; everything
# gets new IDs here. Component types, inspection types and ADs are shared
# across aircraft, so existing ones with the same name are reused.
#
# No single transaction wraps the import (SQLite would block other writers for
# its whole length). If it fails part-way, the new aircraft and anything else
# it created are removed again.
class Importer
Preview = Struct.new(:tail_number, :make, :model, :source, :exported_at, :counts, keyword_init: true)
RECORD_SECTIONS = %w[components document_collections documents document_images logbook_entries squawks inspection_types
inspection_records ads ad_compliances consumable_records major_records notes oil_analysis_reports flight_logs].freeze
# Safety and format checks that don't import anything. Raises Error.
def self.check(path)
manifest = with_zip(path) { |zip| validate_zip(zip) }
a = manifest["aircraft"]
Preview.new(tail_number: a["tail_number"], make: a["make"], model: a["model"], source: manifest["source_instance"].presence,
exported_at: (Time.zone.parse(manifest["exported_at"].to_s) rescue nil),
counts: RECORD_SECTIONS.to_h { [ _1, Array(manifest[_1]).size ] })
end
def self.with_zip(path)
Zip.validate_entry_sizes = true # a lying size header raises instead of inflating forever
Zip::File.open(path) { |zip| yield zip }
rescue Zip::Error => e
raise Error, "Not a valid .sam.zip archive (#{e.message})"
end
def self.validate_zip(zip)
uncompressed = compressed = 0
zip.each do |entry|
raise Error, "The archive contains a link (#{entry.name}), which isn't allowed." if entry.symlink?
raise Error, "The archive contains an unsafe path: #{entry.name}" unless safe_name?(entry.name)
uncompressed += entry.size
compressed += entry.compressed_size
raise Error, "The archive is too large once unpacked." if uncompressed > ARCHIVE_MAX_BYTES
end
raise Error, "The archive is compressed suspiciously well (over #{MAX_RATIO}:1)." if compressed.positive? && uncompressed / compressed > MAX_RATIO
entry = zip.find_entry("manifest.json") or raise Error, "The archive has no manifest.json. Is it a .sam.zip export?"
raise Error, "manifest.json is too large." if entry.size > MANIFEST_MAX_BYTES
manifest = begin
JSON.parse(entry.get_input_stream.read.force_encoding("UTF-8"))
rescue JSON::ParserError => e
raise Error, "manifest.json couldn't be read (#{e.message.truncate(120)})"
end
raise Error, "manifest.json isn't a JSON object." unless manifest.is_a?(Hash)
version = manifest["schema_version"]
unless version.is_a?(Integer) && version.between?(1, SCHEMA_VERSION)
raise Error, "Unsupported archive version #{version.inspect} (this app reads versions 1–#{SCHEMA_VERSION})."
end
required = TOP_LEVEL_KEYS - OPTIONAL_KEYS - (version < 2 ? %w[flight_logs] : [])
missing = required - manifest.keys
raise Error, "manifest.json is missing #{missing.to_sentence}." if missing.any?
raise Error, "manifest.json has no aircraft tail number." unless manifest["aircraft"].is_a?(Hash) && manifest["aircraft"]["tail_number"].present?
RECORD_SECTIONS.each { |key| raise Error, "manifest.json #{key} must be a list." unless manifest[key].nil? || manifest[key].is_a?(Array) }
manifest
end
# Relative, no "..", valid UTF-8 (zip entry names arrive as raw bytes).
def self.safe_name?(name)
name = name.to_s.dup.force_encoding(Encoding::UTF_8)
return false if name.blank? || !name.valid_encoding? || name.start_with?("/", "\\") || name.match?(/\A[A-Za-z]:/)
name.unicode_normalize(:nfc).tr("\\", "/").split("/").exclude?("..")
end
attr_reader :counts, :warnings, :aircraft
def initialize(path, owner:, tail_number: nil, progress: nil)
@path = path
@owner = owner
@tail_number = tail_number
@progress = progress || ->(_counts) { }
@counts = Hash.new(0)
@warnings = []
@map = Hash.new { |h, k| h[k] = {} } # section → archive id → new record id
@created_blobs = []
@created_shared = [] # component types / inspection types / ADs made by this import
end
def run
self.class.with_zip(@path) do |zip|
@zip = zip
@manifest = self.class.validate_zip(zip)
import_all
end
{ aircraft: @aircraft, counts: @counts, warnings: @warnings }
rescue StandardError
clean_up
raise
end
private
def m(key) = Array(@manifest[key]).select { _1.is_a?(Hash) }
def import_all
import_aircraft
import_component_types
import_components
import_documents
import_logbook
import_squawks
import_inspections
import_ads
import_consumables
import_major_records
import_notes
import_oil_analyses
import_flight_logs
@aircraft.roles.find_or_create_by!(user: @owner, role: "owner") if @owner
@aircraft.log_event!("aircraft", "Aircraft imported from a .sam.zip archive",
notes: [ @manifest["source_instance"].presence && "Source: #{@manifest['source_instance']}", summary ].compact.join(". "),
user: @owner)
end
def summary = @counts.select { _2.positive? }.map { |k, n| "#{n} #{k.tr('_', ' ')}" }.join(", ")
def tick(section, n = 1)
@counts[section] += n
@progress.call(@counts) if (@counts.values.sum % 25).zero?
end
# ---- records ----
def import_aircraft
a = @manifest["aircraft"]
@aircraft = Aircraft.new(
tail_number: (@tail_number.presence || a["tail_number"]).to_s.strip.upcase,
make: a["make"].to_s, model: a["model"].to_s, serial_number: a["serial_number"].to_s, description: a["description"].to_s,
purchased: date(a["purchased"]), status: Aircraft::STATUSES.key?(a["status"]) ? a["status"] : "AVAILABLE",
tach_time: num(a["tach_time"] || a["flight_time"]) || 0, tach_time_offset: num(a["tach_time_offset"]) || 0,
hobbs_time: num(a["hobbs_time"]) || 0, hobbs_time_offset: num(a["hobbs_time_offset"]) || 0,
icao_type: a["icao_type"].to_s.upcase.presence&.then { _1.match?(/\A[A-Z0-9]{1,4}\z/) ? _1 : nil }
)
feature = m("features").find { _1["feature"] == "airworthiness_enforcement" }
@aircraft.airworthiness_enforcement = feature["enabled"] if feature && !feature["enabled"].nil? && @aircraft.has_attribute?(:airworthiness_enforcement)
@aircraft.save!(validate: false)
attach(@aircraft.picture, a["picture"], "aircraft picture") unless a["_missing"]
end
def import_component_types
m("component_types").each do |data|
name = data["name"].to_s.strip
next if name.blank?
type = ComponentType.where("LOWER(name) = ?", name.downcase).first ||
ComponentType.create!(name: name, consumable: data["consumable"] == true).tap { @created_shared << _1 }
@map[:component_type][data["id"].to_s] = type.id
end
end
def import_components
by_id = m("components").index_by { _1["id"].to_s }
ordered = []
visit = lambda do |data, seen = []|
return if ordered.include?(data) || seen.include?(data)
parent = by_id[data["parent_component_id"].to_s]
visit.(parent, seen + [ data ]) if parent
ordered << data
end
by_id.each_value { visit.(_1) }
ordered.each do |data|
type_id = @map[:component_type][data["component_type_id"].to_s]
next warn("Component #{data['manufacturer']} #{data['model']}: unknown component type, skipped.") unless type_id
c = @aircraft.components.new(
component_type_id: type_id, parent_component_id: @map[:component][data["parent_component_id"].to_s],
manufacturer: data["manufacturer"].to_s, model: data["model"].to_s, serial_number: data["serial_number"].to_s,
install_location: data["install_location"].to_s, notes: data["notes"].to_s,
status: Component::STATUSES.key?(data["status"]) ? data["status"] : "SPARE",
date_in_service: date(data["date_in_service"]) || Date.new(1900, 1, 1), hours_in_service: num(data["hours_in_service"]) || 0,
hours_since_overhaul: num(data["hours_since_overhaul"]) || 0, overhaul_date: date(data["overhaul_date"]),
**data.slice(*%w[tbo_hours tbo_days inspection_hours inspection_days replacement_hours replacement_days]).symbolize_keys,
**data.slice(*%w[tbo_critical on_condition inspection_critical replacement_critical]).transform_values { _1 == true }.symbolize_keys
)
c.save!(validate: false)
@map[:component][data["id"].to_s] = c.id
tick("components")
end
end
def import_documents
m("document_collections").each do |data|
c = @aircraft.document_collections.new(name: data["name"].to_s.presence || "Untitled", description: data["description"].to_s,
visibility: visibility(data["visibility"]) || "private", starred: data["starred"] == true)
c.save!(validate: false)
@map[:collection][data["id"].to_s] = c.id
end
documents = {}
m("documents").each do |data|
d = @aircraft.documents.new(name: data["name"].to_s.presence || "Untitled", description: data["description"].to_s,
doc_type: data["doc_type"].presence, visibility: visibility(data["visibility"]),
document_collection_id: @map[:collection][data["collection_id"].to_s])
d.doc_type = "OTHER" unless Document::TYPES.key?(d.doc_type)
d.save!(validate: false)
@map[:document][data["id"].to_s] = d.id
documents[d.id] = d
tick("documents")
end
m("document_images").each do |data|
document = documents[@map[:document][data["document_id"].to_s]] or next
next if data["_missing"]
tick("document_pages") if attach(document.pages, data["image"], "document #{document.name}")
end
end
def import_logbook
linked = 0
m("logbook_entries").each do |data|
e = @aircraft.logbook_entries.new(
log_type: LogbookEntry::LOG_TYPES.key?(data["log_type"]) ? data["log_type"] : "AC",
entry_type: LogbookEntry::ENTRY_TYPES.key?(data["entry_type"]) ? data["entry_type"] : LogbookEntry::ENTRY_TYPES.keys.first,
date: date(data["date"]) || Date.new(1900, 1, 1), text: data["text"].to_s.presence || "—",
signoff_person: data["signoff_person"], signoff_location: data["signoff_location"],
aircraft_hours_at_entry: num(data["aircraft_hours_at_entry"]), page_number: data["page_number"],
document_id: @map[:document][data["log_image_id"].to_s]
)
e.save!(validate: false)
@map[:logbook][data["id"].to_s] = e.id
linked += 1 if Array(data["components"]).any? || data["component_hours"].present?
tick("logbook_entries")
end
warn("#{linked} logbook #{linked == 1 ? 'entry was' : 'entries were'} linked to components; those links aren't kept here.") if linked.positive?
end
def import_squawks
m("squawks").each do |data|
s = @aircraft.squawks.new(
priority: Squawk::PRIORITIES.key?(data["priority"]) ? data["priority"] : 3, issue_reported: data["issue_reported"].to_s.presence || "—",
notes: data["notes"], resolved: data["resolved"] == true, component_id: @map[:component][data["component_id"].to_s],
created_at: time(data["created_at"]) || Time.current
)
s.resolved_at = time(data["resolved_at"]) || (s.created_at if s.resolved)
s.save!(validate: false)
paths = Array(data["attachments"]).presence || [ data["attachment"] ].compact
paths.each { attach(s.attachments, _1, "squawk “#{s.issue_reported.truncate(40)}”") } unless data["_missing"]
tick("squawks")
end
end
def import_inspections
m("inspection_types").each do |data|
name = data["name"].to_s.strip
next if name.blank?
type = InspectionType.where("LOWER(name) = ?", name.downcase).first
if type
warn("Inspection type “#{name}” already existed here and was reused.") if type.recurring != (data["recurring"] == true)
else
type = InspectionType.new(name: name, recurring: data["recurring"] == true, required: data["required"] != false,
recurring_hours: num(data["recurring_hours"]), recurring_days: data["recurring_days"],
recurring_months: data["recurring_months"])
type.save!(validate: false)
@created_shared << type
end
type.aircraft << @aircraft if Array(data["applicable_aircraft"]).any? && !type.aircraft.include?(@aircraft)
components(data["applicable_component"]).each { |c| type.components << c unless type.components.include?(c) }
@map[:inspection_type][data["id"].to_s] = type.id
end
m("inspection_records").each do |data|
type_id = @map[:inspection_type][data["inspection_type_id"].to_s]
next warn("An inspection record from #{data['date']} has no inspection type and was skipped.") unless type_id
r = @aircraft.inspection_records.new(
inspection_type_id: type_id, date: date(data["date"]) || Date.new(1900, 1, 1), aircraft_hours: num(data["aircraft_hours"]),
logbook_entry_id: @map[:logbook][data["logbook_entry_id"].to_s], notes: data["notes"],
component_id: @map[:component][Array(data["component"]).first.to_s]
)
r.save!(validate: false)
@map[:inspection_record][data["id"].to_s] = r.id
tick("inspection_records")
end
end
def import_ads
m("ads").each do |data|
name = data["name"].to_s.strip
next if name.blank?
ad = AirworthinessDirective.where("LOWER(name) = ?", name.downcase).first
unless ad
ad = AirworthinessDirective.new(
name: name, short_description: data["short_description"].to_s, required_action: data["required_action"].to_s,
compliance_type: AirworthinessDirective::COMPLIANCE_TYPES.key?(data["compliance_type"]) ? data["compliance_type"] : "standard",
trigger_condition: data["trigger_condition"].to_s, recurring: data["recurring"] == true,
recurring_hours: num(data["recurring_hours"]), recurring_months: data["recurring_months"], recurring_days: data["recurring_days"],
bulletin_type: AirworthinessDirective::BULLETIN_TYPES.key?(data["bulletin_type"]) ? data["bulletin_type"] : "ad",
mandatory: data["mandatory"] != false, document_id: @map[:document][data["document_id"].to_s],
effective_date: date(data["effective_date"]), supersedes: data["supersedes"], superseded_by: data["superseded_by"],
docket_number: data["docket_number"], applicable_models: data["applicable_models"], product_type: data["product_type"],
document_url: data["document_url"]
)
ad.save!(validate: false)
@created_shared << ad
end
ad.aircraft << @aircraft if Array(data["applicable_aircraft"]).any? && !ad.aircraft.include?(@aircraft)
components(data["applicable_component"]).each { |c| ad.components << c unless ad.components.include?(c) }
@map[:ad][data["id"].to_s] = ad.id
tick("ads")
end
m("ad_compliances").each do |data|
ad_id = @map[:ad][data["ad_id"].to_s] or next
c = @aircraft.ad_compliances.new(
airworthiness_directive_id: ad_id, date_complied: date(data["date_complied"]) || Date.new(1900, 1, 1),
compliance_notes: data["compliance_notes"].to_s, permanent: data["permanent"] == true,
next_due_at_time: num(data["next_due_at_time"]) || 0, aircraft_hours_at_compliance: num(data["aircraft_hours_at_compliance"]),
logbook_entry_id: @map[:logbook][data["logbook_entry_id"].to_s],
inspection_record_id: @map[:inspection_record][data["inspection_record_id"].to_s],
component_id: @map[:component][data["component_id"].to_s], not_applicable: data["not_applicable"] == true
)
c.save!(validate: false)
tick("ad_compliances")
end
end
def import_consumables
@consumable_flight_links = {}
m("consumable_records").each do |data|
next unless ConsumableRecord::TYPES.key?(data["record_type"])
r = @aircraft.consumable_records.new(
record_type: data["record_type"], date: date(data["date"]) || Date.new(1900, 1, 1), quantity_added: num(data["quantity_added"]) || 0,
level_after: num(data["level_after"]), consumable_type: data["consumable_type"].to_s, flight_hours: num(data["flight_hours"]) || 0,
notes: data["notes"].to_s, excluded_from_averages: data["excluded_from_averages"] == true
)
r.save!(validate: false)
@consumable_flight_links[r.id] = data["flight_log_id"].to_s if data["flight_log_id"]
tick("consumable_records")
end
end
def import_major_records
m("major_records").each do |data|
r = @aircraft.major_records.new(
record_type: MajorRecord::RECORD_TYPES.key?(data["record_type"]) ? data["record_type"] : "repair",
title: data["title"].to_s.presence || "—", description: data["description"].to_s, date_performed: date(data["date_performed"]),
performed_by: data["performed_by"].to_s, component_id: @map[:component][data["component_id"].to_s],
form_337_document_id: @map[:document][data["form_337_document_id"].to_s], stc_number: data["stc_number"].to_s,
stc_holder: data["stc_holder"].to_s, stc_document_id: @map[:document][data["stc_document_id"].to_s],
logbook_entry_id: @map[:logbook][data["logbook_entry_id"].to_s], aircraft_hours: num(data["aircraft_hours"]),
notes: data["notes"].to_s, has_ica: data["has_ica"] == true, ica_notes: data["ica_notes"]
)
r.save!(validate: false)
tick("major_records")
end
end
def import_notes
m("notes").each do |data|
at = time(data["added_timestamp"]) || Time.current
n = @aircraft.notes.new(text: data["text"].to_s.presence || "—", public: data["public"] == true, created_at: at,
edited_at: time(data["edited_at"]))
n.text = "#{n.text}\n\n— #{data['added_by_display']}" if data["added_by_display"].present?
n.save!(validate: false)
tick("notes")
end
end
def import_oil_analyses
m("oil_analysis_reports").each do |data|
o = @aircraft.oil_analyses.new(
component_id: @map[:component][data["component_id"].to_s], sample_date: date(data["sample_date"]) || Date.new(1900, 1, 1),
analysis_date: date(data["analysis_date"]), lab: data["lab"].presence, sample_number: data["lab_number"].presence,
oil_type: data["oil_type"].presence, oil_hours: num(data["oil_hours"]), engine_hours: num(data["engine_hours"]),
filter_hours: num(data["filter_hours"]), oil_added: num(data["oil_added_quarts"]),
elements: data["elements_ppm"].is_a?(Hash) ? data["elements_ppm"] : {},
element_averages: data["element_averages"].is_a?(Hash) ? data["element_averages"] : {},
oil_properties: data["oil_properties"].is_a?(Hash) ? data["oil_properties"] : {},
lab_comments: data["lab_comments"].presence, notes: data["notes"].presence,
status: OilAnalysis::STATUSES.key?(data["status"]) ? data["status"] : nil,
excluded_from_averages: data["excluded_from_averages"] == true
)
o.save!(validate: false)
attach(o.report, data["report"], "oil analysis #{o.sample_date}") if data["report"]
tick("oil_analyses")
end
end
def import_flight_logs
m("flight_logs").each do |data|
f = @aircraft.flight_logs.new(
date: date(data["date"]) || Date.new(1900, 1, 1), tach_time: num(data["tach_time"]) || 0, tach_out: num(data["tach_out"]),
tach_in: num(data["tach_in"]), hobbs_time: num(data["hobbs_time"]), hobbs_out: num(data["hobbs_out"]),
hobbs_in: num(data["hobbs_in"]), departure_location: data["departure_location"].to_s,
destination_location: data["destination_location"].to_s, route: data["route"].to_s, oil_added: num(data["oil_added"]),
oil_added_type: data["oil_added_type"].to_s, oil_level_after: num(data["oil_level_after"]), fuel_added: num(data["fuel_added"]),
fuel_added_type: data["fuel_added_type"].to_s, fuel_level_after: num(data["fuel_level_after"]), notes: data["notes"].to_s,
created_at: time(data["created_at"]) || Time.current
)
f.save!(validate: false)
@map[:flight_log][data["id"].to_s] = f.id
attach(f.track_log, data["track_log"], "flight #{f.date} track log") if data["track_log"] && !data["_missing"]
attach(f.engine_data, data["engine_data"], "flight #{f.date} engine data") if data["engine_data"]
tick("flight_logs")
end
(@consumable_flight_links || {}).each do |record_id, archive_flight_id|
flight_id = @map[:flight_log][archive_flight_id] or next
ConsumableRecord.where(id: record_id).update_all(flight_log_id: flight_id)
end
end
# ---- files ----
# Copy one archive file into storage and attach it. Problems with a single
# file become warnings; the record is kept without it.
def attach(target, archive_path, what)
return false if archive_path.blank?
blob = upload(archive_path.to_s)
target.attach(blob)
true
rescue Error => e
warn("#{what.upcase_first}: #{e.message}")
false
end
def upload(archive_path)
raise Error, "unsafe file path #{archive_path}" unless self.class.safe_name?(archive_path)
entry = @zip.find_entry(archive_path) or raise Error, "file #{File.basename(archive_path)} isn't in the archive"
raise Error, "#{File.basename(archive_path)} is too large" if entry.size > FILE_MAX_BYTES
ext = AircraftArchive.extension(archive_path)
raise Error, "#{File.basename(archive_path)} isn't an allowed file type" unless EXTENSIONS.include?(ext)
Tempfile.create([ "sam-import", ".#{ext}" ], binmode: true) do |tmp|
entry.get_input_stream { |input| IO.copy_stream(input, tmp) }
tmp.rewind
head = tmp.read(16).to_s
raise Error, "#{File.basename(archive_path)} doesn't contain what its name says (.#{ext})" unless AircraftArchive.content_matches?(head, ext)
tmp.rewind
ActiveStorage::Blob.create_and_upload!(io: tmp, filename: File.basename(archive_path)).tap { @created_blobs << _1 }
end
end
# ---- helpers ----
def warn(message)
@warnings << message
nil
end
def components(ids) = Array(ids).filter_map { @map[:component][_1.to_s] }.then { Component.where(id: _1) }
def visibility(value) = DocumentCollection::VISIBILITIES.key?(value) ? value : nil
def date(value)
Date.iso8601(value.to_s[0, 10]) if value.present?
rescue Date::Error
nil
end
def time(value)
Time.zone.parse(value.to_s) if value.present?
rescue ArgumentError
nil
end
def num(value)
return nil if value.blank?
BigDecimal(value.to_s)
rescue ArgumentError
nil
end
# Undo a failed import: the aircraft (and its records, via dependent
# destroys), shared records only this import created, and uploaded files.
def clean_up
@aircraft&.destroy if @aircraft&.persisted?
@created_shared.reverse_each { |record| record.destroy if record.class.exists?(record.id) }
@created_blobs.each { |blob| blob.purge if ActiveStorage::Blob.exists?(blob.id) }
rescue StandardError => e
Rails.logger.error("[aircraft import] clean-up failed: #{e.class}: #{e.message}")
end
end
end
+91
View File
@@ -0,0 +1,91 @@
require "anthropic"
# Reads scanned logbook pages with Claude and returns the entries on them.
# One call handles one batch: several page images, or one PDF.
#
# LogbookTranscriber.new.transcribe([ { data: bytes, media_type: "image/jpeg" }, … ], carried: [...])
#
# `carried` are entries already read from page 0 of this batch (the last page of
# the previous one), so an entry continuing onto the next page is completed
# rather than duplicated.
class LogbookTranscriber
class Error < StandardError; end
MODEL = ENV.fetch("LOGBOOK_AI_MODEL", "claude-opus-5-5")
EFFORT = ENV.fetch("LOGBOOK_AI_EFFORT", "high") # handwriting rewards a careful read
MAX_TOKENS = 64_000
PROMPT = File.read(File.join(__dir__, "logbook_transcriber/prompt.txt")).freeze
SCHEMA = JSON.parse(File.read(File.join(__dir__, "logbook_transcriber/schema.json"))).freeze
FALLBACK_BETA = "server-side-fallback-2026-07-01"
IMAGE_TYPES = %w[image/jpeg image/png image/gif image/webp].freeze
MAX_IMAGE_BYTES = 5.megabytes # the API's per-image limit
MAX_PDF_BYTES = 30.megabytes # request limit is 32 MB
Result = Struct.new(:entries, :non_logbook_pages, :unparseable_pages, :model, keyword_init: true)
class << self
attr_writer :client # tests swap in a fake
def client = @client ||= Anthropic::Client.new
end
def initialize(client: self.class.client, model: MODEL, effort: EFFORT)
@client = client
@model = model
@effort = effort
end
def transcribe(pages, carried: [])
stream = @client.beta.messages.stream(
model: @model,
max_tokens: MAX_TOKENS,
system_: PROMPT,
output_config: { effort: @effort.to_sym, format_: { type: :json_schema, schema: SCHEMA } },
# If a safety classifier declines, the API retries on its default substitute model.
fallbacks: :default,
betas: [ FALLBACK_BETA ],
messages: [ { role: "user", content: content(pages, carried) } ]
)
message = stream.accumulated_message
case message.stop_reason
when :refusal then raise Error, "Claude declined to read these pages"
when :max_tokens then raise Error, "the transcription was too long to finish; try fewer pages per file"
end
text = message.content.select { _1.type == :text }.map(&:text).join
data = JSON.parse(text)
Result.new(entries: Array(data["entries"]), non_logbook_pages: Array(data["non_logbook_pages"]),
unparseable_pages: Array(data["unparseable_pages"]), model: message.model.to_s)
rescue JSON::ParserError
raise Error, "Claude's answer wasn't valid JSON"
rescue Anthropic::Errors::RateLimitError
raise Error, "the Anthropic API is rate limiting requests; try again in a few minutes"
rescue Anthropic::Errors::AuthenticationError, Anthropic::Errors::PermissionDeniedError
raise Error, "the Anthropic API key was rejected"
rescue Anthropic::Errors::APIStatusError => e
raise Error, "the Anthropic API returned an error (#{e.status})"
rescue Anthropic::Errors::APIConnectionError
raise Error, "couldn't reach the Anthropic API"
end
private
def content(pages, carried)
blocks = pages.each_with_index.flat_map do |page, i|
source = { type: "base64", media_type: page[:media_type], data: Base64.strict_encode64(page[:data]) }
if page[:media_type] == "application/pdf"
[ { type: "text", text: "This PDF holds the logbook pages; page indices count its pages from 0." },
{ type: "document", source: source } ]
else
[ { type: "text", text: "Page #{i}:" }, { type: "image", source: source } ]
end
end
if carried.any?
context = carried.map do |e|
"- #{e['date'] || 'undated'} (#{e['log_type']}, signed #{e['signoff_person'] || 'unsigned'}): #{e['text'].to_s.truncate(400)}"
end
blocks << { type: "text", text: "Entries already read from page 0 (it was the last page of the previous batch):\n#{context.join("\n")}" }
end
blocks << { type: "text", text: "Extract every logbook entry from these pages." }
end
end
@@ -0,0 +1,36 @@
You are an expert aviation maintenance logbook analyst with deep knowledge of FAA regulations,
aircraft maintenance records, and standard logbook formats.
You will get numbered pages of an aircraft logbook (page 0 is the first page in this request).
Extract every logbook entry visible.
Rules:
- A single entry may span two consecutive pages (look for "continued" or entries that cut off mid-sentence).
- Several separate entries may appear on one page (separated by dates, rules, or spacing).
- Some pages are not logbook entries: FAA Form 337 (major repair/alteration), yellow tags (8130-3),
weight and balance records, equipment lists, AD lists, or other administrative forms. List them in
non_logbook_pages instead of extracting entries from them.
- Dates: convert MM/DD/YYYY, MM/DD/YY, or written dates to YYYY-MM-DD. If the year is ambiguous, use the
surrounding entries. Use null only if the date is truly unreadable.
- Signoffs: include the mechanic's name and certificate number (e.g. "A&P 123456", IA number) or the repair
station number.
Entry types:
- MAINTENANCE: repairs, part replacements, AD compliance, alterations, STCs, 337s referenced
- INSPECTION: annual, 100-hour, phase checks, pre-buy, progressive, conditional inspections
- FLIGHT: test flights, ferry flights, return-to-service flights
- HOURS_UPDATE: an hours entry with no specific work described
- OTHER: administrative entries, certifications
Fields:
- log_type: AC (airframe), ENG (engine), PROP (propeller), or OTHER
- text: the entry text verbatim, or a close paraphrase where wording is unclear; write [?] for illegible words
- aircraft_hours: the tach/total time the entry records (a number), or null if none is written
- signoff_person / signoff_location: a string, or null
- page_start / page_end: page indices within this request
- confidence: high (clearly legible), medium (some guesswork), low (mostly illegible)
- unparseable_pages: pages too illegible to extract anything from
Overlapping pages: when the request says entries were already read from page 0, return each of those
entries exactly once, completed with any text that continues onto later pages, and do not return any
other entry that lies wholly on page 0.
@@ -0,0 +1,30 @@
{
"type": "object",
"properties": {
"entries": {
"type": "array",
"items": {
"type": "object",
"properties": {
"date": { "type": ["string", "null"], "description": "YYYY-MM-DD, or null if unreadable" },
"log_type": { "type": "string", "enum": ["AC", "ENG", "PROP", "OTHER"] },
"entry_type": { "type": "string", "enum": ["MAINTENANCE", "INSPECTION", "FLIGHT", "HOURS_UPDATE", "OTHER"] },
"text": { "type": "string" },
"aircraft_hours": { "type": ["number", "null"] },
"signoff_person": { "type": ["string", "null"] },
"signoff_location": { "type": ["string", "null"] },
"page_start": { "type": "integer" },
"page_end": { "type": "integer" },
"confidence": { "type": "string", "enum": ["high", "medium", "low"] }
},
"required": ["date", "log_type", "entry_type", "text", "aircraft_hours", "signoff_person", "signoff_location",
"page_start", "page_end", "confidence"],
"additionalProperties": false
}
},
"non_logbook_pages": { "type": "array", "items": { "type": "integer" } },
"unparseable_pages": { "type": "array", "items": { "type": "integer" } }
},
"required": ["entries", "non_logbook_pages", "unparseable_pages"],
"additionalProperties": false
}
+22 -186
View File
@@ -1,207 +1,43 @@
require "pdf-reader" require "pdf-reader"
# Reads an AvLab (Aviation Laboratories) oil analysis report PDF. A report # Reads an oil analysis lab report PDF into sample records. Supported labs:
# holds the current sample plus up to two previous ones, each with its dates, # - Aviation Laboratories (AvLab): the current sample plus up to two previous
# hours, lab verdict, comments and a ppm table (value, lab average beneath). # 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 # Every sample comes back as a hash of OilAnalysis attributes plus :current.
# 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.
class OilReportParser class OilReportParser
class Error < StandardError; end class Error < StandardError; end
LAB = "AvLab".freeze Report = Struct.new(:lab, :tail_number, :engine_serial, :engine_model, :oil_type, :samples, keyword_init: true)
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)
def self.from_pdf(io) def self.from_pdf(io)
reader = PDF::Reader.new(io) reader = PDF::Reader.new(io)
chars = reader.pages.each_with_index.flat_map do |page, index| receivers = reader.pages.map { |page| PDF::Reader::PageTextReceiver.new.tap { page.walk(_1) } }
receiver = PDF::Reader::PageTextReceiver.new text = receivers.flat_map { |r| r.runs(merge: true, skip_overlapping: false).map(&:text) }.join(" ")
page.walk(receiver)
# Keep overlapping glyphs: a real "0" under a hidden one would otherwise be dropped. if text.match?(/blackstone-labs\.com|BLACKSTONE LABORATORIES/i)
receiver.runs(merge: false, skip_overlapping: false).filter_map do |run| Blackstone.new(receivers.first.runs(merge: true, skip_overlapping: false)).parse
next if run.font_size >= COLOR_CODE_FONT_SIZE || run.text.strip.empty? elsif text.match?(/OIL ANALYSIS RESULTS IN PARTS PER MILLION|AVIATION LABORATORIES|avlab\.com/i)
Char.new(run.text, run.x, run.endx, run.y, index) Avlab.from_receivers(receivers)
else
raise Error, "Not a recognized lab report (AvLab and Blackstone PDFs are supported)"
end end
end
new(chars).parse
rescue PDF::Reader::MalformedPDFError, PDF::Reader::UnsupportedFeatureError => e rescue PDF::Reader::MalformedPDFError, PDF::Reader::UnsupportedFeatureError => e
raise Error, "Couldn't read the PDF (#{e.message})" raise Error, "Couldn't read the PDF (#{e.message})"
end end
def initialize(chars) # "1,903" → 1903, "<0.5" → 0.5, "5 qts" → 5, ".3" → 0.3; nil for "-", "N/A"…
@lines = build_lines(chars) 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 end
def parse # M/D/YYYY
unless @lines.any? { _1.text.include?("OIL ANALYSIS RESULTS IN PARTS PER MILLION") } def self.date(text)
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)
Date.strptime(text, "%m/%d/%Y") if text Date.strptime(text, "%m/%d/%Y") if text
rescue Date::Error rescue Date::Error
nil nil
end 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 end
+208
View File
@@ -0,0 +1,208 @@
class OilReportParser
# Aviation Laboratories (AvLab) report. Each sample block has its dates,
# hours, the lab's verdict, comments and a ppm table (value, lab average beneath).
#
# 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.
class Avlab
LAB = "AvLab".freeze
COLOR_CODE_FONT_SIZE = 8.5 # the hidden color codes are 9pt; real text is 7–8pt
WORD_GAP = 1.0 # glyphs closer than this are one word (spaces run 1.3pt+)
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
def self.from_receivers(receivers)
chars = receivers.each_with_index.flat_map do |receiver, index|
# 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
end
new(chars).parse
end
def initialize(chars)
@lines = build_lines(chars)
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 <= WORD_GAP
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 = comments(block)
elements, averages = results(block)
{
current: heading.words.first.text == "CURRENT",
sample_date: OilReportParser.date(fields[:sample_date]),
analysis_date: OilReportParser.date(fields[:analysis_date]),
sample_number: fields[:sample_number],
engine_hours: OilReportParser.number(fields[:engine_hours]),
oil_hours: OilReportParser.number(fields[:oil_hours]),
filter_hours: OilReportParser.number(fields[:filter_hours]),
oil_added: OilReportParser.number(fields[:oil_added]),
status: status(verdict),
lab_comments: [ (verdict unless status(verdict) == "normal"), comments ].compact_blank.join("\n").presence,
elements: elements,
element_averages: averages
}
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.
# Every label counts, including ones we don't keep (e.g. "Filter Wt. (mgs):"),
# so their values don't drift onto a neighbouring label.
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 text.end_with?(":")
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
# "Comments: …" and the lines that continue it at the same left margin.
def comments(block)
start = block.index { _1.words.first&.text == "Comments:" } or return nil
first = block[start]
margin = first.words.first.x
lines = [ first.words.drop(1) ]
block[(start + 1)..].each_with_index do |line, i|
previous = block[start + i]
break unless (line.words.first.x - margin).abs <= 3 && previous.y - line.y <= 14 && line.page == previous.page
lines << line.words
end
lines.flatten.map(&:text).join(" ").presence
end
# The ppm row under the element headings, and the (average) row under that.
def results(block)
elements, averages = {}, {}
header = block.find { |line| COLUMNS.keys.count { line.words.map(&:text).include?(_1) } >= 6 } or return [ elements, averages ]
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 do |line|
# 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?("(")
averages[element] = ppm(word.text.delete("()"))
else
elements[element] = ppm(word.text)
end
end
end
[ elements.compact, averages.compact ]
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?("<")
OilReportParser.number(text)
end
def status(verdict)
case verdict.upcase
when /ABNORMAL|HIGH|CRITICAL|SEVERE|PROBLEM/ then "action_required"
when /APPEARS NORMAL/ then "normal"
else "monitor"
end
end
end
end
@@ -0,0 +1,114 @@
class OilReportParser
# Blackstone Laboratories report: one page with a header (lab number, unit ID,
# oil type), the lab's comments, then a grid with one column per sample (newest
# on the left, dated across the top) and one row per element / oil property.
# Two extra columns hold averages: this unit's own (skipped) and the lab's
# universal averages for the engine type (kept as the comparison average).
#
# Numbers are right-aligned under their sample dates, so values are matched to
# a sample by their right edge.
class Blackstone
LAB = "Blackstone".freeze
ELEMENTS = %w[aluminum chromium iron copper lead tin molybdenum nickel manganese silver titanium
potassium boron silicon sodium calcium magnesium phosphorus zinc barium].freeze
PROPERTIES = {
/\ASUS Viscosity/ => "viscosity_sus_210f", /\AcSt Viscosity/ => "viscosity_cst_100c",
/\AFlashpoint/ => "flashpoint_f", /\AFuel %/ => "fuel_percent", /\AAntifreeze %/ => "antifreeze_percent",
/\AWater %/ => "water_percent", /\AInsolubles %/ => "insolubles_percent",
/\ATBN\z/ => "total_base_number", /\ATAN\z/ => "total_acid_number"
}.freeze
LABEL_MAX_X = 130 # row labels sit in the left margin
ROW_SLOP = 3.5 # a row's values can sit a couple of points off its label's baseline
COLUMN_SLOP = 8 # how far a value's right edge may be from its date's
DATE = %r{\A\d{1,2}/\d{1,2}/\d{4}\z}
# A US registration in the "unit ID" field; anything else (owners often put an
# engine name there) isn't treated as a tail number.
TAIL = /\AN[1-9][0-9A-Z]{0,4}\z/i
Run = Struct.new(:text, :x, :endx, :y)
def initialize(runs)
# The vertical side captions ("UNIT INFORMATION"…) are single letters at x≈43.
@runs = runs.filter_map { Run.new(_1.text.strip, _1.x, _1.endx, _1.y) if _1.x >= 48 && _1.text.strip.present? }
end
def parse
date_label = label("Sample Date") or raise Error, "No sample dates found in the Blackstone report"
dates = row(date_label).select { DATE.match?(_1.text) }.sort_by(&:x)
raise Error, "No sample dates found in the Blackstone report" if dates.empty?
@columns = dates.map(&:endx)
@universal_x = (@runs.find { _1.text == "UNIVERSAL" }&.x || 530) - 10
oil_hours, engine_hours, oil_added = [ "MI/HR on Oil", "MI/HR on Unit", "Make Up Oil Added" ].map { values(label(_1)) }
elements, universal = ELEMENTS.each_with_object([ {}, {} ]) do |el, (by_el, avg)|
by_el[el] = values(label(el.upcase))
avg[el] = universal_value(label(el.upcase))
end
properties = PROPERTIES.to_h { |pattern, key| [ key, values(label(pattern)) ] }
universal = universal.transform_values { OilReportParser.number(_1) }.compact
lab_number = after("LAB NUMBER:")
samples = dates.each_with_index.map do |date, i|
{
current: i.zero?,
sample_date: OilReportParser.date(date.text),
sample_number: (lab_number if i.zero?),
oil_hours: OilReportParser.number(oil_hours[i]),
engine_hours: OilReportParser.number(engine_hours[i]),
oil_added: OilReportParser.number(oil_added[i]),
status: nil, # Blackstone gives comments, not a rating
lab_comments: (comments if i.zero?),
elements: elements.transform_values { OilReportParser.number(_1[i]) }.compact,
element_averages: universal,
oil_properties: properties.transform_values { OilReportParser.number(_1[i]) }.compact
}
end.select { _1[:sample_date] }
unit = after("UNIT ID:")
Report.new(lab: LAB, tail_number: (unit if TAIL.match?(unit.to_s)), engine_serial: nil,
engine_model: after("MAKE/MODEL:"), oil_type: after("OIL TYPE & GRADE:"), samples: samples)
end
private
# The left-margin run that labels a row.
def label(match)
@runs.find { _1.x < LABEL_MAX_X && (match.is_a?(Regexp) ? match.match?(_1.text) : _1.text == match) }
end
# Runs on the same row as a label, to its right.
def row(label_run)
return [] unless label_run
@runs.select { (_1.y - label_run.y).abs <= ROW_SLOP && _1.x > label_run.endx }
end
# The row's value text for each sample column (nil where blank).
def values(label_run)
found = Array.new(@columns.size)
row(label_run).each do |run|
next if run.x >= @universal_x
i = @columns.each_index.min_by { (@columns[_1] - run.endx).abs }
found[i] ||= run.text if (@columns[i] - run.endx).abs <= COLUMN_SLOP
end
found
end
def universal_value(label_run) = row(label_run).find { _1.x >= @universal_x }&.text
# The value printed after a header label, e.g. "UNIT ID:" → "N12345".
def after(text)
label_run = @runs.find { _1.text == text } or return nil
@runs.select { (_1.y - label_run.y).abs <= 2 && _1.x > label_run.endx }.min_by(&:x)&.text
end
# The comment paragraph between the contact block and the results grid.
# It starts with who it's addressed to ("OWNER:"), which is dropped.
def comments
contact = @runs.select { _1.text.match?(/\A(PHONE|FAX|ALT PHONE|EMAIL):/) }
top = contact.map(&:y).min or return nil
grid = label("MI/HR on Oil")&.y || 0
lines = @runs.select { _1.y < top - 2 && _1.y > grid + 6 }.sort_by { [ -_1.y, _1.x ] }
lines.map(&:text).join(" ").sub(/\A[A-Z]+:\s*/, "").squeeze(" ").presence
end
end
end
+3
View File
@@ -1,7 +1,10 @@
<% content_for :title, "Fleet" %> <% content_for :title, "Fleet" %>
<div class="mb-5 flex items-center justify-between"> <div class="mb-5 flex items-center justify-between">
<h1 class="text-2xl font-bold tracking-tight">Fleet</h1> <h1 class="text-2xl font-bold tracking-tight">Fleet</h1>
<div class="flex gap-2">
<%= link_to "Import", new_aircraft_import_path, class: "btn-secondary", title: "Import an aircraft from a .sam.zip archive" %>
<%= link_to "+ Add Aircraft", new_aircraft_path, class: "btn-primary" %> <%= link_to "+ Add Aircraft", new_aircraft_path, class: "btn-primary" %>
</div>
</div> </div>
<% if @aircraft_list.empty? %> <% if @aircraft_list.empty? %>
+32
View File
@@ -0,0 +1,32 @@
<% content_for :title, "Import Aircraft" %>
<div class="mx-auto max-w-2xl space-y-5">
<h1 class="text-2xl font-bold tracking-tight">Import an aircraft</h1>
<section class="card card-body space-y-4">
<p class="text-sm text-slate-600">
Upload a <strong>.sam.zip</strong> archive exported from Simple Aircraft Manager (this app or the original).
Everything in it is added as a new aircraft that you own: components, logbook, squawks, inspections, ADs,
documents, oil and fuel, oil analyses, flights and their files.
</p>
<% if @preview %>
<div class="rounded-md bg-slate-50 px-3 py-2 text-sm ring-1 ring-slate-200">
Archive: <strong><%= @preview.tail_number %></strong> <%= [ @preview.make, @preview.model ].compact_blank.join(" ") %>
<% if @preview.exported_at %><span class="text-slate-500">· exported <%= fmt_date(@preview.exported_at.to_date) %></span><% end %>
</div>
<% end %>
<%= form_with url: aircraft_imports_path, multipart: true, class: "space-y-4" do |f| %>
<div>
<%= f.label :archive, "Archive (.sam.zip)", class: "label" %>
<%= f.file_field :archive, accept: ".zip,application/zip", required: true,
class: "block text-sm text-slate-600 file:mr-3 file:rounded-md file:border-0 file:bg-slate-100 file:px-3 file:py-1.5 file:text-sm file:font-medium hover:file:bg-slate-200" %>
<% if @conflict %><p class="hint mt-1">Choose the file again along with the new tail number.</p><% end %>
</div>
<div class="max-w-xs">
<%= f.label :tail_number, "Tail number", class: "label" %>
<%= f.text_field :tail_number, value: (params[:tail_number] unless @conflict), class: "input uppercase",
placeholder: @preview&.tail_number || "From the archive", required: @conflict.present? %>
<p class="hint mt-1">Leave blank to use the archive's. Needed when that tail number is already here.</p>
</div>
<div class="flex gap-2"><%= f.submit "Import", class: "btn-primary" %><%= link_to "Cancel", root_path, class: "btn-secondary" %></div>
<% end %>
</section>
</div>
+28
View File
@@ -0,0 +1,28 @@
<% content_for :title, "Aircraft Import" %>
<% if @import.in_progress? %><% content_for :head, tag.meta("http-equiv": "refresh", content: "2") %><% end %>
<div class="mx-auto max-w-2xl space-y-5">
<h1 class="text-2xl font-bold tracking-tight">Importing <%= @import.tail_number %></h1>
<section class="card card-body space-y-3 text-sm" id="import-status">
<p class="text-slate-500"><%= @import.filename %><%= " · from #{@import.source}" if @import.source.present? %></p>
<% if @import.in_progress? %>
<p class="flex items-center gap-2 font-medium"><span class="inline-block h-3 w-3 animate-pulse rounded-full bg-sky-500"></span>
<%= @import.status == "queued" ? "Waiting to start…" : "Importing…" %></p>
<% if @import.counts.any? %><p class="text-slate-600">So far: <%= @import.summary %>.</p><% end %>
<p class="hint">This page updates by itself. Large archives with many documents can take a few minutes.</p>
<% elsif @import.done? %>
<p class="font-medium text-emerald-700">Imported.</p>
<p class="text-slate-600"><%= @import.summary.presence&.upcase_first || "No records" %>.</p>
<% if @import.aircraft %><p><%= link_to "Open #{@import.aircraft.tail_number} →", aircraft_path(@import.aircraft), class: "btn-primary" %></p><% end %>
<% else %>
<p class="font-medium text-red-700">The import failed. Nothing was added.</p>
<p class="text-slate-600"><%= @import.error %></p>
<p><%= link_to "Try another file", new_aircraft_import_path, class: "btn-secondary" %></p>
<% end %>
<% if @import.warnings.any? %>
<div class="border-t border-slate-100 pt-3">
<p class="mb-1 text-xs font-semibold text-slate-500"><%= pluralize(@import.warnings.size, "note") %></p>
<ul class="list-disc space-y-0.5 pl-5 text-slate-600"><% @import.warnings.each do |w| %><li><%= w %></li><% end %></ul>
</div>
<% end %>
</section>
</div>
+10
View File
@@ -58,4 +58,14 @@
<%= f.submit "Create Share Link" %> <%= f.submit "Create Share Link" %>
<% end %> <% end %>
</section> </section>
<section class="card lg:col-span-2">
<div class="card-header">
<div>
<h2 class="card-title">Backup &amp; transfer</h2>
<p class="hint">Everything recorded for <%= @aircraft.tail_number %>, with its files, in one .sam.zip archive. Import it from the Fleet page here or into another Simple Aircraft Manager (including the original).</p>
</div>
<%= link_to "Download .sam.zip", aircraft_export_path(@aircraft), class: "btn-secondary", data: { turbo: false } %>
</div>
</section>
</div> </div>
+1 -1
View File
@@ -9,7 +9,7 @@
<%= f.select :log_type, LogbookEntry::LOG_TYPES.invert, { include_blank: "All logs", selected: @log_type }, class: "input w-32" %> <%= f.select :log_type, LogbookEntry::LOG_TYPES.invert, { include_blank: "All logs", selected: @log_type }, class: "input w-32" %>
<%= f.submit "Filter", class: "btn-secondary" %> <%= f.submit "Filter", class: "btn-secondary" %>
<% end %> <% end %>
<% if owner? %><%= link_to "+ New Entry", new_aircraft_logbook_entry_path(@aircraft), class: "btn-primary" %><% end %> <% if owner? %><%= link_to "Import scans", new_aircraft_logbook_import_path(@aircraft), class: "btn-secondary" %><%= link_to "+ New Entry", new_aircraft_logbook_entry_path(@aircraft), class: "btn-primary" %><% end %>
</div> </div>
</div> </div>
<% if @entries.any? %> <% if @entries.any? %>
+58
View File
@@ -0,0 +1,58 @@
<% content_for :title, "#{@aircraft.tail_number} Import Logbook Pages" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<% ai = LogbookImport.ai_available? %>
<%= form_with url: aircraft_logbook_imports_path(@aircraft), multipart: true, class: "card card-body max-w-3xl space-y-5" do |f| %>
<div>
<h2 class="card-title">Import scanned logbook pages</h2>
<p class="hint mt-1">
The pages are saved as a document, in file-name order (name them page-001, page-002…).
<% if ai %>Claude can also read them and add each entry to the logbook for you.<% else %>
To have entries transcribed automatically, set an <code>ANTHROPIC_API_KEY</code> for the app.<% end %>
</p>
</div>
<div>
<%= f.label :pages, "Scanned pages", class: "label" %>
<%= f.file_field :pages, multiple: true, required: true, accept: "image/jpeg,image/png,image/gif,image/webp,application/pdf",
class: "block text-sm text-slate-600 file:mr-3 file:rounded-md file:border-0 file:bg-slate-100 file:px-3 file:py-1.5 file:text-sm file:font-medium hover:file:bg-slate-200" %>
<p class="hint mt-1">JPEG, PNG, WebP or PDF. For transcription, images up to 5 MB each and PDFs up to 30 MB.</p>
</div>
<div class="grid gap-4 sm:grid-cols-2">
<div>
<%= f.label :collection_id, "Collection", class: "label" %>
<%= f.select :collection_id, [ [ "New collection…", "new" ] ] + @collections.map { [ _1.name, _1.id ] },
{ selected: params[:collection_id] || @collections.find { _1.name.match?(/log/i) }&.id || "new" }, class: "input" %>
</div>
<div>
<%= f.label :collection_name, "New collection name", class: "label" %>
<%= f.text_field :collection_name, value: params[:collection_name], placeholder: "Airframe Log #2", class: "input" %>
</div>
<div>
<%= f.label :document_name, "Document name", class: "label" %>
<%= f.text_field :document_name, value: params[:document_name], placeholder: "Same as the collection", class: "input" %>
</div>
<div>
<%= f.label :doc_type, "Document type", class: "label" %>
<%= f.select :doc_type, Document::TYPES.invert, { selected: params[:doc_type] || "LOG" }, class: "input" %>
</div>
</div>
<fieldset class="space-y-3 rounded-md p-3 ring-1 ring-slate-200 <%= 'opacity-60' unless ai %>">
<label class="flex items-start gap-2 text-sm">
<%= f.check_box :transcribe, { checked: ai, disabled: !ai, class: "mt-0.5" }, "1", "0" %>
<span><strong>Transcribe with Claude</strong><br>
<span class="hint">Each entry becomes a logbook entry linked to its page. Handwriting isn't always read perfectly: review the new entries against the scans.</span></span>
</label>
<div class="max-w-xs">
<%= f.label :log_type, "Log", class: "label" %>
<%= f.select :log_type, [ [ "Detect for each entry", "" ] ] + LogbookEntry::LOG_TYPES.invert.to_a, { selected: params[:log_type] }, class: "input", disabled: !ai %>
<p class="hint mt-1">Pick one when the whole book is a single log (e.g. an engine log).</p>
</div>
</fieldset>
<div class="flex gap-2">
<%= f.submit ai ? "Upload & transcribe" : "Upload pages", class: "btn-primary" %>
<%= link_to "Cancel", aircraft_logbook_entries_path(@aircraft), class: "btn-secondary" %>
</div>
<% end %>
+44
View File
@@ -0,0 +1,44 @@
<% content_for :title, "#{@aircraft.tail_number} Logbook Import" %>
<% if @import.in_progress? %><% content_for :head, tag.meta("http-equiv": "refresh", content: "3") %><% end %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<section class="card card-body max-w-3xl space-y-4 text-sm">
<div class="flex flex-wrap items-baseline justify-between gap-2">
<h2 class="card-title"><%= @import.document&.name || "Logbook pages" %></h2>
<span class="text-xs text-slate-500"><%= pluralize(@import.pages_total, "page") %><%= " · read by #{@import.model}" if @import.transcribe && @import.model %></span>
</div>
<% if @import.in_progress? %>
<div>
<div class="mb-1 flex justify-between text-xs text-slate-500">
<span><%= @import.transcribe ? "Claude is reading the pages…" : "Saving pages…" %></span>
<span class="tabular-nums"><%= @import.pages_done %> / <%= @import.pages_total %></span>
</div>
<div class="h-2 overflow-hidden rounded-full bg-slate-200" role="progressbar" aria-valuenow="<%= @import.progress_percent %>" aria-valuemin="0" aria-valuemax="100">
<div class="h-full rounded-full bg-sky-600 transition-all" style="width: <%= @import.progress_percent %>%"></div>
</div>
<p class="hint mt-2"><%= pluralize(@import.entries_created, "entry") %> so far. This page updates by itself; a long logbook can take several minutes.</p>
</div>
<% elsif @import.done? %>
<p class="font-medium text-emerald-700">
<%= @import.transcribe ? "Done: #{pluralize(@import.entries_created, 'logbook entry')} added." : "Pages saved." %>
</p>
<% if @import.transcribe && @import.entries_created.positive? %>
<p class="text-slate-600">Check the new entries against the scans: handwriting isn't always read perfectly.</p>
<% end %>
<% else %>
<p class="font-medium text-red-700">The import stopped: <%= @import.error %></p>
<% if @import.entries_created.positive? %><p class="text-slate-600"><%= pluralize(@import.entries_created, "entry") %> were added before it stopped.</p><% end %>
<% end %>
<div class="flex flex-wrap gap-2">
<%= link_to "Logbook", aircraft_logbook_entries_path(@aircraft), class: "btn-secondary btn-sm" %>
<% if @import.document %><%= link_to "View scans", aircraft_document_path(@aircraft, @import.document), class: "btn-secondary btn-sm" %><% end %>
</div>
<% if @import.warnings.any? %>
<div class="border-t border-slate-100 pt-3">
<p class="mb-1 text-xs font-semibold text-slate-500"><%= pluralize(@import.warnings.size, "note") %></p>
<ul class="list-disc space-y-0.5 pl-5 text-slate-600"><% @import.warnings.each do |w| %><li><%= w %></li><% end %></ul>
</div>
<% end %>
</section>
@@ -0,0 +1,9 @@
<%# One element's result and lab average. Locals: element, analysis %>
<% label = OilAnalysis.element_label(element) %>
<div class="grid grid-cols-[6rem_1fr_1fr] items-center gap-2">
<%= label_tag "oil_analysis_elements_#{element}", label, class: "text-sm font-medium text-slate-700" %>
<%= number_field_tag "oil_analysis[elements][#{element}]", analysis.value(element), id: "oil_analysis_elements_#{element}",
step: "any", min: 0, class: "input", placeholder: "Result" %>
<%= number_field_tag "oil_analysis[element_averages][#{element}]", analysis.average(element), id: "oil_analysis_element_averages_#{element}",
step: "any", min: 0, class: "input", placeholder: "Avg", "aria-label": "#{label} lab average" %>
</div>
+26 -8
View File
@@ -3,10 +3,11 @@
<div class="grid gap-4 sm:grid-cols-3"> <div class="grid gap-4 sm:grid-cols-3">
<%= f.field :sample_date, as: :date, required: true %> <%= f.field :sample_date, as: :date, required: true %>
<%= f.field :analysis_date, as: :date %> <%= f.field :analysis_date, as: :date %>
<%= f.field :status, as: :select, label: "Lab's verdict", collection: OilAnalysis::STATUSES.invert %> <%= f.field :status, as: :select, label: "Lab's verdict", collection: [ [ "Not rated", "" ] ] + OilAnalysis::STATUSES.invert.to_a %>
<%= f.field :component_id, as: :select, label: "Engine", collection: component_options(aircraft) %> <%= f.field :component_id, as: :select, label: "Engine", collection: component_options(aircraft) %>
<%= f.field :lab, placeholder: "AvLab, Blackstone…" %> <%= f.field :lab, placeholder: "AvLab, Blackstone…" %>
<%= f.field :sample_number, label: "Sample #" %> <%= f.field :sample_number, label: "Sample / lab #" %>
<%= f.field :oil_type, label: "Oil", placeholder: "Aeroshell W100…", wrapper: { class: "sm:col-span-2" } %>
</div> </div>
<fieldset class="grid gap-4 sm:grid-cols-4"> <fieldset class="grid gap-4 sm:grid-cols-4">
<legend class="mb-2 text-sm font-semibold">Hours</legend> <legend class="mb-2 text-sm font-semibold">Hours</legend>
@@ -15,19 +16,36 @@
<%= f.field :filter_hours, as: :number, step: 0.1, label: "On filter" %> <%= f.field :filter_hours, as: :number, step: 0.1, label: "On filter" %>
<%= f.field :oil_added, as: :number, step: 0.25, label: "Oil added (qt)" %> <%= f.field :oil_added, as: :number, step: 0.25, label: "Oil added (qt)" %>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend class="mb-2 text-sm font-semibold">Results (ppm)</legend> <legend class="mb-2 text-sm font-semibold">Results (ppm)</legend>
<p class="hint mb-3">Leave blank what the lab didn't test (N/A). Enter "&lt; 0.1" as 0. The average is the lab's figure for similar engines, usually in (parentheses) under the result.</p> <p class="hint mb-3">Leave blank what the lab didn't test (N/A). Enter "&lt; 0.1" as 0. The average is the lab's figure for similar engines, usually in (parentheses) under the result.</p>
<div class="grid gap-x-4 gap-y-3 sm:grid-cols-2 lg:grid-cols-3"> <div class="grid gap-x-4 gap-y-3 sm:grid-cols-2 lg:grid-cols-3">
<% OilAnalysis::ELEMENTS.each do |el, (label, _)| %> <%= render partial: "element_input", collection: OilAnalysis::COMMON_ELEMENTS, as: :element, locals: { analysis: analysis } %>
<div class="grid grid-cols-[6rem_1fr_1fr] items-center gap-2"> </div>
<%= f.label el, label, class: "text-sm font-medium text-slate-700" %> <% extra = OilAnalysis::ELEMENTS.keys - OilAnalysis::COMMON_ELEMENTS %>
<%= f.number_field el, step: 0.1, min: 0, class: "input", placeholder: "Result" %> <details class="mt-3" <%= "open" if extra.any? { analysis.value(_1) || analysis.average(_1) } %>>
<%= f.number_field :"#{el}_avg", step: 0.1, min: 0, class: "input", placeholder: "Avg", "aria-label": "#{label} lab average" %> <summary class="cursor-pointer text-sm text-sky-700">More elements (Blackstone and other labs)</summary>
<div class="mt-3 grid gap-x-4 gap-y-3 sm:grid-cols-2 lg:grid-cols-3">
<%= render partial: "element_input", collection: extra, as: :element, locals: { analysis: analysis } %>
</div>
</details>
</fieldset>
<details <%= "open" if analysis.oil_properties.any? %>>
<summary class="cursor-pointer text-sm font-semibold">Oil properties</summary>
<div class="mt-3 grid gap-x-4 gap-y-3 sm:grid-cols-3">
<% OilAnalysis::PROPERTIES.each do |key, (label, unit)| %>
<div>
<%= label_tag "oil_analysis_oil_properties_#{key}", [ label, (unit.presence && "(#{unit})") ].compact.join(" "), class: "label" %>
<%= number_field_tag "oil_analysis[oil_properties][#{key}]", analysis.property(key), id: "oil_analysis_oil_properties_#{key}", step: "any", min: 0, class: "input" %>
</div> </div>
<% end %> <% end %>
</div> </div>
</fieldset> </details>
<%= f.field :excluded_from_averages, as: :checkbox, label: "Leave out of trends",
hint: "For a sample that isn't representative, e.g. right after an overhaul or a top end." %>
<%= f.field :lab_comments, as: :textarea, rows: 2 %> <%= f.field :lab_comments, as: :textarea, rows: 2 %>
<%= f.field :notes, as: :textarea, rows: 2 %> <%= f.field :notes, as: :textarea, rows: 2 %>
<%= f.field :report, as: :file, label: "Lab report (PDF)", accept: ".pdf,application/pdf,image/*" %> <%= f.field :report, as: :file, label: "Lab report (PDF)", accept: ".pdf,application/pdf,image/*" %>
+11 -5
View File
@@ -6,7 +6,7 @@
<div class="card-header"> <div class="card-header">
<div> <div>
<h2 class="card-title">Import a lab report</h2> <h2 class="card-title">Import a lab report</h2>
<p class="hint">AvLab PDF reports: each sample on the report (including previous ones) is added unless it's already here.</p> <p class="hint">AvLab or Blackstone PDF reports: each sample on the report (including earlier ones) is added unless it's already here.</p>
</div> </div>
<%= render "import", aircraft: @aircraft %> <%= render "import", aircraft: @aircraft %>
</div> </div>
@@ -21,7 +21,7 @@
<% series = oil_trend_series(@analyses) %> <% series = oil_trend_series(@analyses) %>
<% if series.any? %> <% if series.any? %>
<%= line_chart series, ytitle: "ppm", height: "260px", curve: false, colors: OilAnalysesHelper::TREND_COLORS, legend: "bottom" %> <%= line_chart series, ytitle: "ppm", height: "260px", curve: false, colors: OilAnalysesHelper::TREND_COLORS, legend: "bottom" %>
<p class="hint">Lead is left out of the chart: it comes from 100LL fuel and reads in the thousands. Look for a steady trend rather than a single reading.</p> <p class="hint">Lead is left out of the chart: it comes from 100LL fuel and reads in the thousands. Look for a steady trend rather than a single reading.<%= " Samples marked “leave out of trends” aren't charted." if @analyses.any?(&:excluded_from_averages?) %></p>
<% else %> <% else %>
<%= empty_state @analyses.any? ? "Add another sample to see trends." : "No oil analyses yet. Add one from your lab report." %> <%= empty_state @analyses.any? ? "Add another sample to see trends." : "No oil analyses yet. Add one from your lab report." %>
<% end %> <% end %>
@@ -41,11 +41,16 @@
<tbody> <tbody>
<tr class="text-xs text-slate-500"><td>Engine / oil hrs</td> <tr class="text-xs text-slate-500"><td>Engine / oil hrs</td>
<% recent.each do |a| %><td class="text-right tabular-nums"><%= a.engine_hours ? hours(a.engine_hours) : "—" %> / <%= a.oil_hours ? hours(a.oil_hours) : "—" %></td><% end %></tr> <% recent.each do |a| %><td class="text-right tabular-nums"><%= a.engine_hours ? hours(a.engine_hours) : "—" %> / <%= a.oil_hours ? hours(a.oil_hours) : "—" %></td><% end %></tr>
<% OilAnalysis::ELEMENTS.each do |el, (label, source)| %> <% OilAnalysis.elements_in(recent).each do |el| %>
<% next if recent.none? { _1.value(el) } %> <% label, source = OilAnalysis::ELEMENTS.fetch(el) %>
<tr><td><span title="<%= source %>"><%= label %></span></td> <tr><td><span title="<%= source %>"><%= label %></span></td>
<% recent.each do |a| %><td class="text-right align-top"><%= ppm_cell(a, el) %></td><% end %></tr> <% recent.each do |a| %><td class="text-right align-top"><%= ppm_cell(a, el) %></td><% end %></tr>
<% end %> <% end %>
<% OilAnalysis::PROPERTIES.each_key do |key| %>
<% next if recent.none? { _1.property(key) } %>
<tr class="text-sm"><td class="text-slate-600"><%= OilAnalysis::PROPERTIES[key][0] %></td>
<% recent.each do |a| %><td class="text-right tabular-nums whitespace-nowrap text-slate-600"><%= oil_property(a, key) %></td><% end %></tr>
<% end %>
</tbody> </tbody>
</table> </table>
</div> </div>
@@ -61,7 +66,8 @@
<tbody> <tbody>
<% @analyses.each do |a| %> <% @analyses.each do |a| %>
<tr> <tr>
<td class="whitespace-nowrap"><%= link_to fmt_date(a.sample_date), aircraft_oil_analysis_path(@aircraft, a), class: "link" %></td> <td class="whitespace-nowrap"><%= link_to fmt_date(a.sample_date), aircraft_oil_analysis_path(@aircraft, a), class: "link" %>
<% if a.excluded_from_averages? %><div class="text-xs text-slate-500">left out of trends</div><% end %></td>
<td class="whitespace-nowrap"><%= oil_status_badge(a) %> <td class="whitespace-nowrap"><%= oil_status_badge(a) %>
<% if (flags = a.flagged_elements).any? %><span class="ml-1 text-xs text-red-700"><%= flags.map { OilAnalysis.element_label(_1) }.to_sentence %> above average</span><% end %></td> <% if (flags = a.flagged_elements).any? %><span class="ml-1 text-xs text-red-700"><%= flags.map { OilAnalysis.element_label(_1) }.to_sentence %> above average</span><% end %></td>
<td><%= a.component&.to_s || "—" %></td> <td><%= a.component&.to_s || "—" %></td>
+1 -1
View File
@@ -1,7 +1,7 @@
<% content_for :title, "Add Oil Analysis" %> <% content_for :title, "Add Oil Analysis" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %> <%= render "shared/aircraft_header", aircraft: @aircraft %>
<section class="card card-body mb-5 max-w-4xl"> <section class="card card-body mb-5 max-w-4xl">
<h2 class="mb-1 text-sm font-semibold">Have an AvLab PDF? Import it instead</h2> <h2 class="mb-1 text-sm font-semibold">Have an AvLab or Blackstone PDF? Import it instead</h2>
<p class="hint mb-3">Every sample on the report is filled in for you, and the PDF is attached to the newest one.</p> <p class="hint mb-3">Every sample on the report is filled in for you, and the PDF is attached to the newest one.</p>
<%= render "import", aircraft: @aircraft %> <%= render "import", aircraft: @aircraft %>
</section> </section>
+16 -1
View File
@@ -12,11 +12,26 @@
<dt class="text-slate-500">Oil hrs</dt><dd><%= @analysis.oil_hours ? hours(@analysis.oil_hours) : "—" %></dd> <dt class="text-slate-500">Oil hrs</dt><dd><%= @analysis.oil_hours ? hours(@analysis.oil_hours) : "—" %></dd>
<dt class="text-slate-500">Filter hrs</dt><dd><%= @analysis.filter_hours ? hours(@analysis.filter_hours) : "—" %></dd> <dt class="text-slate-500">Filter hrs</dt><dd><%= @analysis.filter_hours ? hours(@analysis.filter_hours) : "—" %></dd>
<dt class="text-slate-500">Oil added</dt><dd><%= @analysis.oil_added ? "#{number_with_precision(@analysis.oil_added, precision: 2, strip_insignificant_zeros: true)} qt" : "—" %></dd> <dt class="text-slate-500">Oil added</dt><dd><%= @analysis.oil_added ? "#{number_with_precision(@analysis.oil_added, precision: 2, strip_insignificant_zeros: true)} qt" : "—" %></dd>
<dt class="text-slate-500">Oil</dt><dd><%= @analysis.oil_type || "—" %></dd>
<dt class="text-slate-500">Lab</dt><dd><%= @analysis.lab || "—" %></dd> <dt class="text-slate-500">Lab</dt><dd><%= @analysis.lab || "—" %></dd>
<dt class="text-slate-500">Sample #</dt><dd><%= @analysis.sample_number || "—" %></dd> <dt class="text-slate-500">Sample #</dt><dd><%= @analysis.sample_number || "—" %></dd>
<dt class="text-slate-500">Analyzed</dt><dd><%= @analysis.analysis_date ? fmt_date(@analysis.analysis_date) : "—" %></dd> <dt class="text-slate-500">Analyzed</dt><dd><%= @analysis.analysis_date ? fmt_date(@analysis.analysis_date) : "—" %></dd>
<dt class="text-slate-500">Report</dt><dd><%= @analysis.report.attached? ? link_to(@analysis.report.filename, rails_blob_path(@analysis.report, disposition: :inline), class: "link", target: "_blank", rel: "noopener") : "—" %></dd> <dt class="text-slate-500">Report</dt><dd><%= @analysis.report.attached? ? link_to(@analysis.report.filename, rails_blob_path(@analysis.report, disposition: :inline), class: "link", target: "_blank", rel: "noopener") : "—" %></dd>
</dl> </dl>
<% if @analysis.excluded_from_averages? %>
<p class="border-t border-slate-100 px-4 py-2 text-xs text-slate-500">Left out of trend charts.</p>
<% end %>
<% if @analysis.oil_properties.any? %>
<div class="border-t border-slate-100 px-4 py-3 text-sm">
<div class="mb-1 text-xs font-semibold text-slate-500">Oil properties</div>
<dl class="grid grid-cols-2 gap-x-4 gap-y-1">
<% OilAnalysis::PROPERTIES.each_key do |key| %>
<% next unless @analysis.property(key) %>
<dt class="text-slate-500"><%= OilAnalysis::PROPERTIES[key][0] %></dt><dd class="tabular-nums"><%= oil_property(@analysis, key) %></dd>
<% end %>
</dl>
</div>
<% end %>
<% if @analysis.lab_comments.present? %> <% if @analysis.lab_comments.present? %>
<div class="border-t border-slate-100 px-4 py-3 text-sm"><div class="mb-1 text-xs font-semibold text-slate-500">Lab comments</div><%= simple_format @analysis.lab_comments %></div> <div class="border-t border-slate-100 px-4 py-3 text-sm"><div class="mb-1 text-xs font-semibold text-slate-500">Lab comments</div><%= simple_format @analysis.lab_comments %></div>
<% end %> <% end %>
@@ -40,7 +55,7 @@
<thead><tr><th>Element</th><th class="text-right">This sample</th><th class="text-right">Lab average</th><% if @previous %><th class="text-right">Previous</th><th class="text-right">Change</th><% end %><th>Usual source</th></tr></thead> <thead><tr><th>Element</th><th class="text-right">This sample</th><th class="text-right">Lab average</th><% if @previous %><th class="text-right">Previous</th><th class="text-right">Change</th><% end %><th>Usual source</th></tr></thead>
<tbody> <tbody>
<% OilAnalysis::ELEMENTS.each do |el, (label, source)| %> <% OilAnalysis::ELEMENTS.each do |el, (label, source)| %>
<% next unless @analysis.value(el) || @analysis.average(el) %> <% next unless @analysis.value(el) %>
<tr class="<%= "bg-red-50" if @analysis.flagged?(el) %>"> <tr class="<%= "bg-red-50" if @analysis.flagged?(el) %>">
<td><%= label %></td> <td><%= label %></td>
<td class="text-right tabular-nums <%= "font-semibold text-red-700" if @analysis.flagged?(el) %>"><%= ppm(@analysis.value(el)) %></td> <td class="text-right tabular-nums <%= "font-semibold text-red-700" if @analysis.flagged?(el) %>"><%= ppm(@analysis.value(el)) %></td>
+3
View File
@@ -9,6 +9,7 @@ Rails.application.routes.draw do
post :map_airport post :map_airport
end end
resources :aircraft_imports, path: "imports", only: %i[new create show]
resources :aircraft do resources :aircraft do
member do member do
patch :hours, action: :update_hours patch :hours, action: :update_hours
@@ -40,6 +41,7 @@ Rails.application.routes.draw do
resources :oil_analyses, path: "oil_analysis" do resources :oil_analyses, path: "oil_analysis" do
collection { post :import } collection { post :import }
end end
resources :logbook_imports, path: "logbook/import", only: %i[new create show]
resources :logbook_entries resources :logbook_entries
resources :documents resources :documents
resources :document_collections, except: %i[index show] resources :document_collections, except: %i[index show]
@@ -56,6 +58,7 @@ Rails.application.routes.draw do
resources :major_records resources :major_records
resources :aircraft_roles, path: "access", only: %i[index create update destroy] resources :aircraft_roles, path: "access", only: %i[index create update destroy]
resources :share_tokens, only: %i[create destroy] resources :share_tokens, only: %i[create destroy]
resource :export, controller: "aircraft_exports", only: :show
resources :events, only: :index resources :events, only: :index
end end
@@ -0,0 +1,58 @@
# Oil analysis results go from one column per AvLab element to per-element
# hashes, so labs that test more elements (Blackstone reports 20) fit. Adds oil
# type, oil properties (viscosity, flash point…) and "exclude from averages",
# and switches to the original app's statuses (normal / monitor / action required).
class MoveOilAnalysisResultsToJson < ActiveRecord::Migration[8.1]
ELEMENTS = %w[iron copper nickel chromium silver magnesium aluminum lead silicon titanium tin molybdenum].freeze
STATUS_UP = { "elevated" => "monitor", "high" => "action_required" }.freeze
class Row < ActiveRecord::Base
self.table_name = "oil_analyses"
end
def up
add_column :oil_analyses, :elements, :json, null: false, default: {}
add_column :oil_analyses, :element_averages, :json, null: false, default: {}
add_column :oil_analyses, :oil_properties, :json, null: false, default: {}
add_column :oil_analyses, :oil_type, :string
add_column :oil_analyses, :excluded_from_averages, :boolean, null: false, default: false
change_column_null :oil_analyses, :status, true
change_column_default :oil_analyses, :status, from: "normal", to: nil
Row.reset_column_information
Row.find_each do |row|
row.update_columns(
elements: ELEMENTS.to_h { [ _1, row[_1]&.to_f ] }.compact,
element_averages: ELEMENTS.to_h { [ _1, row["#{_1}_avg"]&.to_f ] }.compact,
status: STATUS_UP.fetch(row.status, row.status)
)
end
ELEMENTS.each do |el|
remove_column :oil_analyses, el
remove_column :oil_analyses, :"#{el}_avg"
end
end
def down
ELEMENTS.each do |el|
add_column :oil_analyses, el, :decimal, precision: 8, scale: 1
add_column :oil_analyses, :"#{el}_avg", :decimal, precision: 8, scale: 1
end
Row.reset_column_information
Row.find_each do |row|
values = ELEMENTS.to_h { [ _1, row.elements[_1] ] }
averages = ELEMENTS.to_h { [ "#{_1}_avg", row.element_averages[_1] ] }
row.update_columns(values.merge(averages).merge(status: STATUS_UP.invert.fetch(row.status, row.status || "normal")))
end
change_column_default :oil_analyses, :status, from: nil, to: "normal"
change_column_null :oil_analyses, :status, false
remove_column :oil_analyses, :elements
remove_column :oil_analyses, :element_averages
remove_column :oil_analyses, :oil_properties
remove_column :oil_analyses, :oil_type
remove_column :oil_analyses, :excluded_from_averages
end
end
@@ -0,0 +1,19 @@
# One upload of an aircraft archive (.sam.zip) and the background job importing it.
class CreateAircraftImports < ActiveRecord::Migration[8.1]
def change
create_table :aircraft_imports do |t|
t.references :user, foreign_key: true
t.references :aircraft, foreign_key: { on_delete: :nullify }
t.string :status, null: false, default: "queued" # queued, running, done, failed
t.string :filename
t.string :tail_number # the tail the aircraft will be created with
t.string :source # where the archive came from (manifest source_instance)
t.json :counts, null: false, default: {}
t.json :warnings, null: false, default: []
t.text :error
t.datetime :started_at
t.datetime :finished_at
t.timestamps
end
end
end
@@ -0,0 +1,23 @@
# Scanned logbook pages uploaded as a document, optionally transcribed into
# logbook entries by Claude (LogbookTranscriptionJob).
class CreateLogbookImports < ActiveRecord::Migration[8.1]
def change
create_table :logbook_imports do |t|
t.references :aircraft, null: false, foreign_key: true
t.references :user, foreign_key: { on_delete: :nullify }
t.references :document, foreign_key: { on_delete: :nullify }
t.string :status, null: false, default: "queued" # queued, running, done, failed
t.boolean :transcribe, null: false, default: true
t.string :log_type # force every entry to this log; nil = Claude decides
t.string :model
t.integer :pages_total, null: false, default: 0
t.integer :pages_done, null: false, default: 0
t.integer :entries_created, null: false, default: 0
t.json :warnings, null: false, default: []
t.text :error
t.datetime :started_at
t.datetime :finished_at
t.timestamps
end
end
end
Generated
+52 -26
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do ActiveRecord::Schema[8.1].define(version: 2026_11_06_180800) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -140,6 +140,24 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do
t.index ["user_id"], name: "index_aircraft_events_on_user_id" t.index ["user_id"], name: "index_aircraft_events_on_user_id"
end end
create_table "aircraft_imports", force: :cascade do |t|
t.integer "user_id"
t.integer "aircraft_id"
t.string "status", default: "queued", null: false
t.string "filename"
t.string "tail_number"
t.string "source"
t.json "counts", default: {}, null: false
t.json "warnings", default: [], null: false
t.text "error"
t.datetime "started_at"
t.datetime "finished_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["aircraft_id"], name: "index_aircraft_imports_on_aircraft_id"
t.index ["user_id"], name: "index_aircraft_imports_on_user_id"
end
create_table "aircraft_inspection_types", id: false, force: :cascade do |t| create_table "aircraft_inspection_types", id: false, force: :cascade do |t|
t.integer "aircraft_id", null: false t.integer "aircraft_id", null: false
t.integer "inspection_type_id", null: false t.integer "inspection_type_id", null: false
@@ -397,6 +415,28 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do
t.index ["document_id"], name: "index_logbook_entries_on_document_id" t.index ["document_id"], name: "index_logbook_entries_on_document_id"
end end
create_table "logbook_imports", force: :cascade do |t|
t.integer "aircraft_id", null: false
t.integer "user_id"
t.integer "document_id"
t.string "status", default: "queued", null: false
t.boolean "transcribe", default: true, null: false
t.string "log_type"
t.string "model"
t.integer "pages_total", default: 0, null: false
t.integer "pages_done", default: 0, null: false
t.integer "entries_created", default: 0, null: false
t.json "warnings", default: [], null: false
t.text "error"
t.datetime "started_at"
t.datetime "finished_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["aircraft_id"], name: "index_logbook_imports_on_aircraft_id"
t.index ["document_id"], name: "index_logbook_imports_on_document_id"
t.index ["user_id"], name: "index_logbook_imports_on_user_id"
end
create_table "major_records", force: :cascade do |t| create_table "major_records", force: :cascade do |t|
t.integer "aircraft_id", null: false t.integer "aircraft_id", null: false
t.integer "component_id" t.integer "component_id"
@@ -434,35 +474,16 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do
t.decimal "oil_hours", precision: 6, scale: 1 t.decimal "oil_hours", precision: 6, scale: 1
t.decimal "filter_hours", precision: 6, scale: 1 t.decimal "filter_hours", precision: 6, scale: 1
t.decimal "oil_added", precision: 6, scale: 2 t.decimal "oil_added", precision: 6, scale: 2
t.string "status", default: "normal", null: false t.string "status"
t.text "lab_comments" t.text "lab_comments"
t.text "notes" t.text "notes"
t.decimal "iron", precision: 8, scale: 1
t.decimal "iron_avg", precision: 8, scale: 1
t.decimal "copper", precision: 8, scale: 1
t.decimal "copper_avg", precision: 8, scale: 1
t.decimal "nickel", precision: 8, scale: 1
t.decimal "nickel_avg", precision: 8, scale: 1
t.decimal "chromium", precision: 8, scale: 1
t.decimal "chromium_avg", precision: 8, scale: 1
t.decimal "silver", precision: 8, scale: 1
t.decimal "silver_avg", precision: 8, scale: 1
t.decimal "magnesium", precision: 8, scale: 1
t.decimal "magnesium_avg", precision: 8, scale: 1
t.decimal "aluminum", precision: 8, scale: 1
t.decimal "aluminum_avg", precision: 8, scale: 1
t.decimal "lead", precision: 8, scale: 1
t.decimal "lead_avg", precision: 8, scale: 1
t.decimal "silicon", precision: 8, scale: 1
t.decimal "silicon_avg", precision: 8, scale: 1
t.decimal "titanium", precision: 8, scale: 1
t.decimal "titanium_avg", precision: 8, scale: 1
t.decimal "tin", precision: 8, scale: 1
t.decimal "tin_avg", precision: 8, scale: 1
t.decimal "molybdenum", precision: 8, scale: 1
t.decimal "molybdenum_avg", precision: 8, scale: 1
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.json "elements", default: {}, null: false
t.json "element_averages", default: {}, null: false
t.json "oil_properties", default: {}, null: false
t.string "oil_type"
t.boolean "excluded_from_averages", default: false, null: false
t.index ["aircraft_id", "sample_date"], name: "index_oil_analyses_on_aircraft_id_and_sample_date" t.index ["aircraft_id", "sample_date"], name: "index_oil_analyses_on_aircraft_id_and_sample_date"
t.index ["aircraft_id"], name: "index_oil_analyses_on_aircraft_id" t.index ["aircraft_id"], name: "index_oil_analyses_on_aircraft_id"
t.index ["component_id"], name: "index_oil_analyses_on_component_id" t.index ["component_id"], name: "index_oil_analyses_on_component_id"
@@ -530,6 +551,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do
add_foreign_key "air_trail_syncs", "flight_logs" add_foreign_key "air_trail_syncs", "flight_logs"
add_foreign_key "aircraft_events", "aircraft" add_foreign_key "aircraft_events", "aircraft"
add_foreign_key "aircraft_events", "users", on_delete: :nullify add_foreign_key "aircraft_events", "users", on_delete: :nullify
add_foreign_key "aircraft_imports", "aircraft", on_delete: :nullify
add_foreign_key "aircraft_imports", "users"
add_foreign_key "aircraft_notes", "aircraft" add_foreign_key "aircraft_notes", "aircraft"
add_foreign_key "aircraft_notes", "users", on_delete: :nullify add_foreign_key "aircraft_notes", "users", on_delete: :nullify
add_foreign_key "aircraft_roles", "aircraft" add_foreign_key "aircraft_roles", "aircraft"
@@ -552,6 +575,9 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_06_170000) do
add_foreign_key "inspection_records", "logbook_entries", on_delete: :nullify add_foreign_key "inspection_records", "logbook_entries", on_delete: :nullify
add_foreign_key "logbook_entries", "aircraft" add_foreign_key "logbook_entries", "aircraft"
add_foreign_key "logbook_entries", "documents", on_delete: :nullify add_foreign_key "logbook_entries", "documents", on_delete: :nullify
add_foreign_key "logbook_imports", "aircraft"
add_foreign_key "logbook_imports", "documents", on_delete: :nullify
add_foreign_key "logbook_imports", "users", on_delete: :nullify
add_foreign_key "major_records", "aircraft" add_foreign_key "major_records", "aircraft"
add_foreign_key "major_records", "components", on_delete: :nullify add_foreign_key "major_records", "components", on_delete: :nullify
add_foreign_key "major_records", "documents", column: "form_337_document_id", on_delete: :nullify add_foreign_key "major_records", "documents", column: "form_337_document_id", on_delete: :nullify
+28
View File
@@ -0,0 +1,28 @@
# Command-line export/import of aircraft archives (.sam.zip), e.g. for backups:
# bin/rails "sam:export[N12345]" → n12345-2026-11-06.sam.zip
# bin/rails "sam:export[N12345,/backups/n12345.sam.zip]"
# bin/rails "sam:import[/backups/n12345.sam.zip,owner@example.com]"
# bin/rails "sam:import[/backups/n12345.sam.zip,owner@example.com,N54321]" (new tail number)
namespace :sam do
desc "Export an aircraft to a .sam.zip archive"
task :export, %i[tail_number path] => :environment do |_, args|
aircraft = Aircraft.find_by("UPPER(tail_number) = ?", args[:tail_number].to_s.upcase) or abort "No aircraft #{args[:tail_number]}"
exporter = AircraftArchive::Exporter.new(aircraft)
path = exporter.write_to(args[:path].presence || exporter.filename)
puts "Wrote #{path} (#{ActiveSupport::NumberHelper.number_to_human_size(File.size(path))})"
warn "#{exporter.missing} attached file(s) were missing from storage and left out." if exporter.missing.positive?
end
desc "Import a .sam.zip archive as a new aircraft owned by the given user"
task :import, %i[path owner_email tail_number] => :environment do |_, args|
owner = User.find_by(email_address: args[:owner_email].to_s.strip.downcase) or abort "No user #{args[:owner_email]}"
preview = AircraftArchive::Importer.check(args[:path])
tail = args[:tail_number].presence&.upcase || preview.tail_number
abort "An aircraft #{tail} already exists; pass a different tail number." if Aircraft.where("UPPER(tail_number) = ?", tail.upcase).exists?
result = AircraftArchive::Importer.new(args[:path], owner: owner, tail_number: tail).run
puts "Imported #{result[:aircraft].tail_number}: #{result[:counts].map { |k, n| "#{n} #{k.tr('_', ' ')}" }.join(', ')}"
result[:warnings].each { warn " note: #{_1}" }
rescue AircraftArchive::Error => e
abort "Can't import: #{e.message}"
end
end
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,93 @@
require "test_helper"
class AircraftArchiveFlowTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
setup do
@owner = create_user("owner@example.com")
@pilot = create_user("pilot@example.com")
@a = create_aircraft(owner: @owner, pilot: @pilot, tail_number: "N12345", make: "Cessna")
engine = create_component(@a)
@a.squawks.create!(priority: 1, issue_reported: "Cracked fairing", component: engine,
attachments: [ { io: StringIO.new("see photo"), filename: "notes.txt", content_type: "text/plain" } ])
@a.logbook_entries.create!(date: Date.new(2026, 1, 5), text: "Oil changed")
end
def download_export
get aircraft_export_path(@a)
assert_response :success
assert_equal "application/zip", response.media_type
path = File.join(Dir.mktmpdir, "export.sam.zip")
File.binwrite(path, response.body)
path
end
test "owner downloads an export; pilots and outsiders can't" do
sign_in @pilot
get aircraft_export_path(@a)
assert_redirected_to aircraft_path(@a)
sign_in create_user
get aircraft_export_path(@a)
assert_response :not_found
sign_in @owner
get aircraft_aircraft_roles_path(@a)
assert_select "a[href='#{aircraft_export_path(@a)}']", text: "Download .sam.zip"
path = download_export
assert_match(/attachment; filename="n12345-\d{4}-\d\d-\d\d\.sam\.zip"/, response.headers["Content-Disposition"])
assert_equal "N12345", Zip::File.open(path) { JSON.parse(_1.read("manifest.json")) }["aircraft"]["tail_number"]
assert_equal "Aircraft exported (.sam.zip)", @a.events.last.event_name
end
test "importing an archive: tail clash asks for a new tail, then the job builds the aircraft" do
sign_in @owner
path = download_export
archive = -> { Rack::Test::UploadedFile.new(path, "application/zip", original_filename: "n12345.sam.zip") }
sign_in @pilot
get root_path
assert_select "a[href='#{new_aircraft_import_path}']"
post aircraft_imports_path, params: { archive: archive.() }
assert_response :unprocessable_entity
assert_includes response.body, "An aircraft N12345 already exists"
assert_equal 0, AircraftImport.count
assert_enqueued_jobs 1, only: AircraftImportJob do
post aircraft_imports_path, params: { archive: archive.(), tail_number: "n54321" }
end
import = AircraftImport.last
assert_redirected_to aircraft_import_path(import)
assert_equal [ "N54321", @pilot, "queued" ], [ import.tail_number, import.user, import.status ]
follow_redirect!
assert_includes response.body, "Waiting to start"
assert_select "meta[http-equiv=refresh]"
perform_enqueued_jobs(only: AircraftImportJob)
import.reload
assert_equal "done", import.status, import.error
copy = import.aircraft
assert_equal [ "N54321", "Cessna" ], [ copy.tail_number, copy.make ]
assert_equal [ "owner" ], copy.roles.where(user: @pilot).pluck(:role) # the importer owns the copy
assert_equal "notes.txt", copy.squawks.sole.attachments.sole.filename.to_s
assert_equal 1, import.counts["logbook_entries"]
get aircraft_import_path(import)
assert_includes response.body, "Imported."
assert_select "meta[http-equiv=refresh]", 0
assert_select "a[href='#{aircraft_path(copy)}']"
perform_enqueued_jobs # purge the staged archive
refute import.reload.archive.attached?
sign_in create_user
get aircraft_import_path(import)
assert_response :not_found # only the importer (or an admin) sees it
end
test "a bad file is refused up front" do
sign_in @owner
post aircraft_imports_path, params: { archive: fixture_file_upload("flights.csv", "application/zip") }
assert_redirected_to new_aircraft_import_path
assert_match "That archive can't be imported: Not a valid .sam.zip archive", flash[:alert]
end
end
@@ -0,0 +1,152 @@
require "test_helper"
class LogbookImportFlowTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
# Stands in for Anthropic::Client: records each request and answers from a queue.
class FakeClaude
Message = Struct.new(:stop_reason, :content, :model)
Text = Struct.new(:type, :text)
attr_reader :requests
def initialize(*answers)
@answers = answers
@requests = []
end
def beta = self
def messages = self
def stream(**params)
@requests << params
answer = @answers.shift or raise "unexpected request"
message = answer.is_a?(Symbol) ? Message.new(answer, [], "claude-opus-5-5") : Message.new(:end_turn, [ Text.new(:text, answer.to_json) ], "claude-opus-5-5")
Struct.new(:accumulated_message).new(message)
end
end
JPEG = "\xFF\xD8\xFF\xE0fake-jpeg".b
def entry(text, page_start, page_end = page_start, **extra)
{ date: "2026-01-05", log_type: "ENG", entry_type: "MAINTENANCE", text: text, aircraft_hours: 1990.5,
signoff_person: "J. Mechanic A&P 1234567", signoff_location: "KOQU", page_start: page_start, page_end: page_end,
confidence: "high" }.merge(extra)
end
def answer(*entries, forms: []) = { entries: entries, non_logbook_pages: forms, unparseable_pages: [] }
def page(name) = Rack::Test::UploadedFile.new(StringIO.new(JPEG), "image/jpeg", original_filename: name)
setup do
@owner = create_user("owner@example.com")
@pilot = create_user("pilot@example.com")
@a = create_aircraft(owner: @owner, pilot: @pilot)
@previous_key = ENV["ANTHROPIC_API_KEY"]
ENV["ANTHROPIC_API_KEY"] = "sk-ant-test"
LogbookTranscriptionJob.batch_pages = 2
end
teardown do
ENV["ANTHROPIC_API_KEY"] = @previous_key
LogbookTranscriber.client = nil
LogbookTranscriptionJob.batch_pages = 6
end
test "pages are saved in name order and transcribed in overlapping batches" do
claude = FakeClaude.new(
# pages 1–2: A on page 1; B starts on page 2 and may continue
answer(entry("Changed oil", 0), entry("Annual inspection, found", 1, 1, entry_type: "INSPECTION")),
# pages 2–3: B completed from the carried copy, C on page 3 (no readable date, hard to read), page 3 also has a form
answer(entry("Annual inspection, found airworthy", 0, 1, entry_type: "INSPECTION"), entry("Replaced [?] gasket", 1, date: nil, confidence: "low"))
)
LogbookTranscriber.client = claude
sign_in @owner
get aircraft_logbook_entries_path(@a)
assert_select "a[href='#{new_aircraft_logbook_import_path(@a)}']", text: "Import scans"
get new_aircraft_logbook_import_path(@a)
assert_select "input[type=checkbox][name=transcribe]:not([disabled])"
post aircraft_logbook_imports_path(@a), params: { pages: [ page("page-10.jpg"), page("page-2.jpg"), page("page-1.jpg") ],
collection_id: "new", collection_name: "Engine Log #1", transcribe: "1", log_type: "" }
import = @a.logbook_imports.last
assert_redirected_to aircraft_logbook_import_path(@a, import)
document = import.document
assert_equal %w[page-1.jpg page-2.jpg page-10.jpg], document.pages.sort_by(&:id).map { _1.filename.to_s } # natural order
assert_equal "Engine Log #1", document.document_collection.name
assert_equal "LOG", document.doc_type
follow_redirect!
assert_select "meta[http-equiv=refresh]"
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import.reload
assert_equal [ "done", 3, 3, 3 ], [ import.status, import.pages_total, import.pages_done, import.entries_created ], import.error
first, second = claude.requests
assert_equal "claude-opus-5-5", first[:model]
assert_equal :default, first[:fallbacks]
assert_equal [ "server-side-fallback-2026-07-01" ], first[:betas]
assert_equal :json_schema, first[:output_config][:format_][:type]
assert_equal 2, first[:messages][0][:content].count { _1[:type] == "image" }
assert_match "Entries already read from page 0", second[:messages][0][:content].map { _1[:text] }.compact.join
assert_match "Annual inspection, found", second[:messages][0][:content].map { _1[:text] }.compact.join
entries = @a.logbook_entries.order(:id)
assert_equal [ "Changed oil", "Annual inspection, found airworthy", "[date unreadable] Replaced [?] gasket" ], entries.map(&:text)
assert_equal [ 1, 2, 3 ], entries.map(&:page_number)
assert entries.all? { _1.document == document && _1.log_type == "ENG" }
assert_equal [ "MAINTENANCE", "INSPECTION", "MAINTENANCE" ], entries.map(&:entry_type)
assert_equal 1990.5, entries.first.aircraft_hours_at_entry.to_f
assert_equal Date.new(2026, 1, 5), entries.last.date # borrowed from the entry before it
assert_equal 2, import.warnings.size
assert_match "had no readable date", import.warnings.first
assert_match "hard to read", import.warnings.last
get aircraft_logbook_import_path(@a, import)
assert_includes response.body, "Done: 3 logbook entries added."
assert_select "meta[http-equiv=refresh]", 0
assert_equal "Logbook pages transcribed (3 entries)", @a.events.where(category: "logbook").last.event_name
end
test "a PDF goes up whole, the log type can be forced, and a refusal becomes a note" do
LogbookTranscriber.client = FakeClaude.new(answer(entry("Prop overhauled", 0), entry("Prop balanced", 2, log_type: "AC"), forms: [ 1 ]), :refusal)
sign_in @owner
pdf = Rack::Test::UploadedFile.new(StringIO.new("%PDF-1.4 fake"), "application/pdf", original_filename: "prop-log.pdf")
post aircraft_logbook_imports_path(@a), params: { pages: [ page("z-loose-page.jpg"), pdf ], transcribe: "1", log_type: "PROP",
collection_id: "new", document_name: "Prop log" }
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import = @a.logbook_imports.last
assert_equal "done", import.status
request = LogbookTranscriber.client.requests.first
assert_equal [ "text", "document", "text" ], request[:messages][0][:content].map { _1[:type] }
assert_equal %w[PROP PROP], @a.logbook_entries.pluck(:log_type)
assert_equal [ 1, 1 ], @a.logbook_entries.pluck(:page_number) # both from the PDF, page 1 of the document
assert_match "Not logbook entries (forms, tags…), so nothing was transcribed from: prop-log.pdf page 2", import.warnings.first
assert_equal "Page 2 wasn't transcribed: Claude declined to read these pages.", import.warnings.last
end
test "without an API key, pages are only uploaded" do
ENV["ANTHROPIC_API_KEY"] = nil
LogbookTranscriber.client = FakeClaude.new # any request would raise
sign_in @owner
get new_aircraft_logbook_import_path(@a)
assert_select "input[type=checkbox][name=transcribe][disabled]"
assert_includes response.body, "ANTHROPIC_API_KEY"
post aircraft_logbook_imports_path(@a), params: { pages: [ page("p1.jpg") ], transcribe: "1", collection_id: "new" }
perform_enqueued_jobs(only: LogbookTranscriptionJob)
import = @a.logbook_imports.last
assert_equal [ "done", false, 0 ], [ import.status, import.transcribe, import.entries_created ]
assert_equal 1, import.document.pages.count
end
test "rejects files that aren't scans, and pilots can't import" do
sign_in @owner
post aircraft_logbook_imports_path(@a), params: { pages: [ fixture_file_upload("engine_data/cgr30p.csv", "application/x-msdownload") ], collection_id: "new" }
assert_response :unprocessable_entity
assert_equal 0, @a.documents.count
sign_in @pilot
get new_aircraft_logbook_import_path(@a)
assert_redirected_to aircraft_path(@a)
end
end
+54 -11
View File
@@ -8,10 +8,11 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
@engine = create_component(@aircraft, hours_since_overhaul: 120.5) @engine = create_component(@aircraft, hours_since_overhaul: 120.5)
end end
def results(**overrides) def results(elements: {}, **overrides)
{ sample_date: "2026-08-01", status: "normal", component_id: @engine.id, lab: "Example Lab", { sample_date: "2026-08-01", status: "normal", component_id: @engine.id, lab: "Example Lab",
engine_hours: 120.5, oil_hours: 40, iron: 30, iron_avg: 35, copper: 6, copper_avg: 9, engine_hours: 120.5, oil_hours: 40,
lead: 2500, lead_avg: 2400, silicon: 22, silicon_avg: 12 }.merge(overrides) elements: { iron: 30, copper: 6, lead: 2500, silicon: 22 }.merge(elements),
element_averages: { iron: 35, copper: 9, lead: 2400, silicon: 12 } }.merge(overrides)
end end
test "pilot adds samples; trends, flags and comparison render" do test "pilot adds samples; trends, flags and comparison render" do
@@ -20,7 +21,7 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
assert_includes response.body, 'value="120.5"' # engine hours default from the engine assert_includes response.body, 'value="120.5"' # engine hours default from the engine
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-03-01", iron: 25, silicon: 10) } post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-03-01", elements: { iron: 25, silicon: 10 }) }
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(report: fixture_file_upload("engine_data/cgr30p.csv", "application/pdf")) } post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(report: fixture_file_upload("engine_data/cgr30p.csv", "application/pdf")) }
latest = @aircraft.oil_analyses.recent_first.first latest = @aircraft.oil_analyses.recent_first.first
assert_redirected_to aircraft_oil_analysis_path(@aircraft, latest) assert_redirected_to aircraft_oil_analysis_path(@aircraft, latest)
@@ -42,19 +43,44 @@ class OilAnalysisFlowTest < ActionDispatch::IntegrationTest
test "only owners edit and delete" do test "only owners edit and delete" do
analysis = @aircraft.oil_analyses.create!(results.except(:component_id)) analysis = @aircraft.oil_analyses.create!(results.except(:component_id))
sign_in @pilot sign_in @pilot
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "high" } } patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "action_required" } }
assert_equal "normal", analysis.reload.status assert_equal "normal", analysis.reload.status
sign_in @owner sign_in @owner
get edit_aircraft_oil_analysis_path(@aircraft, analysis) get edit_aircraft_oil_analysis_path(@aircraft, analysis)
assert_response :success assert_response :success
patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "elevated", iron: "" } } assert_select "input[name='oil_analysis[elements][iron]'][value='30.0']"
assert_equal "elevated", analysis.reload.status assert_select "input[name='oil_analysis[elements][zinc]']" # the extra elements are there too
assert_nil analysis.iron patch aircraft_oil_analysis_path(@aircraft, analysis), params: { oil_analysis: { status: "monitor", elements: { iron: "", copper: "6", lead: "2500", silicon: "22", zinc: "4" } } }
analysis.reload
assert_equal "monitor", analysis.status
assert_nil analysis.value("iron")
assert_equal 4.0, analysis.value("zinc")
delete aircraft_oil_analysis_path(@aircraft, analysis) delete aircraft_oil_analysis_path(@aircraft, analysis)
assert_equal 0, @aircraft.oil_analyses.count assert_equal 0, @aircraft.oil_analyses.count
end end
test "oil type, properties and leaving a sample out of trends" do
sign_in @owner
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-03-01", elements: { iron: 25 }) }
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(sample_date: "2026-05-01", elements: { iron: 90 }, excluded_from_averages: "1") }
post aircraft_oil_analyses_path(@aircraft), params: { oil_analysis: results(oil_type: "Aeroshell W100 Plus", status: "",
oil_properties: { flashpoint_f: "445", fuel_percent: "0.5" }) }
latest = @aircraft.oil_analyses.recent_first.first
assert_nil latest.status
get aircraft_oil_analysis_path(@aircraft, latest)
assert_includes response.body, "Aeroshell W100 Plus"
assert_includes response.body, "445 °F"
assert_includes response.body, "Not rated"
get aircraft_oil_analyses_path(@aircraft)
assert_includes response.body, "left out of trends"
iron = response.body[/"name":"Iron","data":\[(.*?)\]\]/, 1]
assert_includes iron, "25.0"
refute_includes iron, "90.0" # the excluded sample isn't charted
end
test "an engine from another aircraft is ignored" do test "an engine from another aircraft is ignored" do
other = create_component(create_aircraft) other = create_component(create_aircraft)
sign_in @owner sign_in @owner
@@ -85,8 +111,8 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
assert analyses.all? { _1.component == @engine && _1.lab == "AvLab" } # matched by engine serial assert analyses.all? { _1.component == @engine && _1.lab == "AvLab" } # matched by engine serial
assert analyses.first.report.attached? assert analyses.first.report.attached?
refute analyses.last.report.attached? refute analyses.last.report.attached?
assert_equal 44.0, analyses.first.iron.to_f assert_equal 44.0, analyses.first.value("iron")
assert_equal 13.7, analyses.first.silicon_avg.to_f assert_equal 13.7, analyses.first.average("silicon")
assert_equal 3, @aircraft.events.where(category: "oil_analysis").count assert_equal 3, @aircraft.events.where(category: "oil_analysis").count
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report } post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
@@ -98,7 +124,7 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
end end
test "attaches the PDF to a matching sample entered by hand" do test "attaches the PDF to a matching sample entered by hand" do
manual = @aircraft.oil_analyses.create!(sample_date: Date.new(2026, 8, 20), iron: 44) manual = @aircraft.oil_analyses.create!(sample_date: Date.new(2026, 8, 20), elements: { iron: 44 })
sign_in @owner sign_in @owner
post import_aircraft_oil_analyses_path(@aircraft), params: { report: report } post import_aircraft_oil_analyses_path(@aircraft), params: { report: report }
assert_equal 3, @aircraft.oil_analyses.count assert_equal 3, @aircraft.oil_analyses.count
@@ -116,4 +142,21 @@ class OilAnalysisImportTest < ActionDispatch::IntegrationTest
assert_match(/Couldn't import that report/, flash[:alert]) assert_match(/Couldn't import that report/, flash[:alert])
assert_equal 0, @aircraft.oil_analyses.count assert_equal 0, @aircraft.oil_analyses.count
end end
test "imports a Blackstone report: 20 elements, properties, oil type, unrated" do
sign_in @owner
post import_aircraft_oil_analyses_path(@aircraft), params: { report: fixture_file_upload("oil_analysis/blackstone_report.pdf", "application/pdf") }
assert_equal "Imported 5 samples from the Blackstone report.", flash[:notice]
latest = @aircraft.oil_analyses.recent_first.first
assert_equal [ Date.new(2024, 1, 25), "Blackstone", "Phillips XC (A/C) 20W/50", nil, @in_service ],
[ latest.sample_date, latest.lab, latest.oil_type, latest.status, latest.component ] # no engine S/N: the engine in service
assert_equal 20, latest.elements.size
assert_equal 445.0, latest.property("flashpoint_f")
assert latest.report.attached?
follow_redirect!
assert_select "td", text: "Zinc"
assert_includes response.body, "Viscosity @ 100°C"
assert_match(/Iron, Copper, .* above average/, response.body) # 2019: iron 818 ppm vs the 35 ppm universal average
end
end end
+1 -1
View File
@@ -24,7 +24,7 @@ class PagesTest < ActionDispatch::IntegrationTest
@major = @a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.current) @major = @a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.current)
@flight = @a.flight_logs.create!(date: Date.current, tach_time: 1.2) @flight = @a.flight_logs.create!(date: Date.current, tach_time: 1.2)
@cr = @a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 1000) @cr = @a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 1000)
@oa = @a.oil_analyses.create!(sample_date: Date.current, iron: 30, iron_avg: 35) @oa = @a.oil_analyses.create!(sample_date: Date.current, elements: { iron: 30 }, element_averages: { iron: 35 })
@token = @a.share_tokens.create!(privilege: "maintenance", label: "Mechanic") @token = @a.share_tokens.create!(privilege: "maintenance", label: "Mechanic")
@a.log_event!("aircraft", "Created", user: @owner) @a.log_event!("aircraft", "Created", user: @owner)
sign_in @owner sign_in @owner
+23
View File
@@ -0,0 +1,23 @@
require "test_helper"
class ComponentTest < ActiveSupport::TestCase
test "deleting a component, or its aircraft, works when records point at it" do
a = create_aircraft
engine = create_component(a)
ad = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Check")
compliance = a.ad_compliances.create!(airworthiness_directive: ad, date_complied: Date.current, compliance_notes: "Done",
permanent: true, component: engine)
annual = InspectionType.create!(name: "Annual")
record = a.inspection_records.create!(inspection_type: annual, date: Date.current, component: engine)
engine.destroy!
assert_nil compliance.reload.component_id
assert_nil record.reload.component_id
other = create_component(a)
a.ad_compliances.create!(airworthiness_directive: ad, date_complied: Date.current, compliance_notes: "Done", permanent: true, component: other)
a.inspection_records.create!(inspection_type: annual, date: Date.current, component: other)
a.destroy!
assert_not Aircraft.exists?(a.id)
end
end
+25 -3
View File
@@ -6,7 +6,8 @@ class OilAnalysisTest < ActiveSupport::TestCase
def analysis(**attrs) = @a.oil_analyses.new({ sample_date: Date.current }.merge(attrs)) def analysis(**attrs) = @a.oil_analyses.new({ sample_date: Date.current }.merge(attrs))
test "flags readings well above the lab average" do test "flags readings well above the lab average" do
a = analysis(iron: 60, iron_avg: 35, copper: 10, copper_avg: 20, silicon: 20, silicon_avg: 13.3, tin: 5) a = analysis(elements: { iron: 60, copper: 10, silicon: 20, tin: 5 },
element_averages: { iron: 35, copper: 20, silicon: 13.3 })
assert a.flagged?("iron") assert a.flagged?("iron")
refute a.flagged?("copper") refute a.flagged?("copper")
assert a.flagged?("silicon") # exactly 1.5× assert a.flagged?("silicon") # exactly 1.5×
@@ -14,13 +15,34 @@ class OilAnalysisTest < ActiveSupport::TestCase
assert_equal %w[iron silicon], a.flagged_elements assert_equal %w[iron silicon], a.flagged_elements
end end
test "validates status, non-negative results, and engine on this aircraft" do test "results from a form: blanks dropped, numbers kept, unknown keys ignored" do
a = analysis(elements: { "iron" => "30.5", "copper" => "", "unobtainium" => "9" },
oil_properties: { "flashpoint_f" => "445", "water_percent" => "0" })
assert a.valid?
assert_equal({ "iron" => 30.5 }, a.elements)
assert_equal({ "flashpoint_f" => 445.0, "water_percent" => 0.0 }, a.oil_properties)
a.save!
assert_equal({ "iron" => 30.5 }, a.reload.elements)
end
test "validates status, non-negative numeric results, and engine on this aircraft" do
assert analysis.valid? assert analysis.valid?
assert analysis(status: "").valid? # not rated
assert analysis(status: "monitor").valid?
refute analysis(status: "bad").valid? refute analysis(status: "bad").valid?
refute analysis(iron: -1).valid? a = analysis(elements: { iron: "-1", copper: "lots" })
refute a.valid?
assert_match "Iron and Copper", a.errors[:elements].first
refute analysis(oil_properties: { fuel_percent: "x" }).valid?
other_engine = create_component(create_aircraft) other_engine = create_component(create_aircraft)
a = analysis(component: other_engine) a = analysis(component: other_engine)
refute a.valid? refute a.valid?
assert_includes a.errors[:component], "must be on this aircraft" assert_includes a.errors[:component], "must be on this aircraft"
end end
test "elements_in lists elements any sample has, in display order" do
a = analysis(elements: { lead: 900, iron: 30 })
b = analysis(elements: { zinc: 4 })
assert_equal %w[iron lead zinc], OilAnalysis.elements_in([ a, b ])
end
end end
+213
View File
@@ -0,0 +1,213 @@
require "test_helper"
class AircraftArchiveTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
PNG = "\x89PNG\r\n\x1A\n".b + "rest-of-image".b
setup do
@owner = create_user("owner@example.com")
@dir = Dir.mktmpdir
end
teardown { FileUtils.rm_rf(@dir) }
def upload(name, body, type) = { io: StringIO.new(body), filename: name, content_type: type }
# An aircraft with something in every section.
def full_aircraft
a = create_aircraft(owner: @owner, tail_number: "N12345", make: "Cessna", model: "150", icao_type: "C150", tach_time: 2000)
a.picture.attach(upload("plane.png", PNG, "image/png"))
engine = create_component(a, serial_number: "900001", component_type: component_type("Engine"))
oil = create_component(a, parent_component: engine, component_type: component_type("Oil"), replacement_critical: true, replacement_hours: 50)
collection = a.document_collections.create!(name: "Logbooks", visibility: "maintenance")
doc = a.documents.create!(name: "Engine log", document_collection: collection, doc_type: "LOG")
doc.pages.attach(upload("page1.pdf", "%PDF-1.4 page one", "application/pdf"), upload("page2.png", PNG, "image/png"))
entry = a.logbook_entries.create!(date: Date.new(2026, 1, 5), text: "Oil changed", log_type: "ENG", document: doc, aircraft_hours_at_entry: 1990)
a.squawks.create!(priority: 1, issue_reported: "Cracked fairing", component: engine, reported_by: @owner,
attachments: [ upload("crack.png", PNG, "image/png"), upload("notes.txt", "see photo", "text/plain") ])
a.squawks.create!(priority: 3, issue_reported: "Old one", resolved: true, resolved_at: 1.day.ago)
annual = InspectionType.create!(name: "Annual", recurring: true, recurring_months: 12)
annual.aircraft << a
record = a.inspection_records.create!(inspection_type: annual, date: Date.new(2026, 2, 1), aircraft_hours: 1995, logbook_entry: entry)
ad = AirworthinessDirective.create!(name: "2020-01-01", short_description: "Check muffler", recurring: true, recurring_hours: 100, docket_number: "FAA-2020-1")
ad.components << engine
a.ad_compliances.create!(airworthiness_directive: ad, date_complied: Date.new(2026, 2, 1), next_due_at_time: 2095, compliance_notes: "Inspected",
component: engine, inspection_record: record)
flight = a.flight_logs.create!(date: Date.new(2025, 7, 14), tach_time: 1.2, departure_location: "KOQU", destination_location: "KBID",
user: @owner, track_log: { io: file_fixture("tracks/flight.gpx").open, filename: "flight.gpx" },
engine_data: { io: file_fixture("engine_data/cgr30p.csv").open, filename: "cgr30p.csv" })
a.consumable_records.create!(record_type: "oil", date: Date.new(2026, 1, 5), quantity_added: 1, flight_hours: 1990, flight_log: flight)
a.major_records.create!(record_type: "alteration", title: "LED lights", date_performed: Date.new(2025, 5, 1), form_337_document: doc)
a.notes.create!(text: "Hangar 4", public: true, user: @owner)
a.oil_analyses.create!(sample_date: Date.new(2026, 1, 5), lab: "Blackstone", sample_number: "S1", component: engine, oil_type: "W100",
elements: { iron: 30, zinc: 4 }, element_averages: { iron: 35 }, oil_properties: { flashpoint_f: 445 },
excluded_from_averages: true, report: upload("report.pdf", "%PDF-1.4 lab", "application/pdf"))
a
end
def export(aircraft)
path = File.join(@dir, "export.sam.zip")
AircraftArchive::Exporter.new(aircraft, source: "sam.example.com").write_to(path)
path
end
def manifest(path) = Zip::File.open(path) { JSON.parse(_1.read("manifest.json")) }
test "export writes a manifest the original app accepts, plus every file" do
path = export(full_aircraft)
data = manifest(path)
assert_equal AircraftArchive::TOP_LEVEL_KEYS.sort, data.keys.sort # the original rejects unknown or missing top-level keys
assert_equal 2, data["schema_version"]
assert_equal "N12345", data["aircraft"]["tail_number"]
assert_equal 2, data["document_images"].size
squawk = data["squawks"].find { _1["issue_reported"] == "Cracked fairing" }
assert_equal 2, squawk["attachments"].size
assert_equal squawk["attachments"].first, squawk["attachment"] # what the original reads
oil = data["oil_analysis_reports"].first
assert_equal({ "iron" => 30.0, "zinc" => 4.0 }, oil["elements_ppm"])
assert_equal "S1", oil["lab_number"]
files = Zip::File.open(path) { |zip| zip.entries.map(&:name) - [ "manifest.json" ] }
assert_equal 8, files.size # picture, 2 document pages, 2 squawk files, track log, engine data, oil report
assert files.all? { _1.start_with?("attachments/") }
assert files.any? { _1.end_with?("/crack.png") }, "original filenames are kept"
end
test "round trip: importing an export recreates the aircraft with new IDs" do
original = full_aircraft
path = export(original)
other = create_user("other@example.com")
result = AircraftArchive::Importer.new(path, owner: other, tail_number: "N54321").run
a = result[:aircraft]
assert_equal [ "N54321", "Cessna", "150", "C150", 2000 ], [ a.tail_number, a.make, a.model, a.icao_type, a.tach_time.to_i ]
refute_equal original.id, a.id
assert a.picture.attached?
assert_equal [ "owner" ], a.roles.where(user: other).pluck(:role)
assert_equal 2, a.components.count
oil = a.components.find_by(component_type: component_type("Oil"))
assert_equal a.components.find_by(serial_number: "900001"), oil.parent_component
doc = a.documents.sole
assert_equal %w[page1.pdf page2.png], doc.pages.map { _1.filename.to_s }
assert_equal "%PDF-1.4 page one", doc.pages.first.download
assert_equal doc, a.logbook_entries.sole.document
squawk = a.squawks.find_by(issue_reported: "Cracked fairing")
assert_equal %w[crack.png notes.txt], squawk.attachments.map { _1.filename.to_s }.sort
assert_equal "IN-USE", squawk.component.status
assert a.squawks.find_by(issue_reported: "Old one").resolved?
assert_includes a.inspection_types.pluck(:name), "Annual"
assert_equal 1, InspectionType.where(name: "Annual").count, "existing inspection types are reused"
compliance = a.ad_compliances.sole
assert_equal [ "2020-01-01", 2095 ], [ compliance.airworthiness_directive.name, compliance.next_due_at_time.to_i ]
assert_equal a.inspection_records.sole, compliance.inspection_record
assert_equal 1, AirworthinessDirective.where(name: "2020-01-01").count
assert_includes compliance.airworthiness_directive.components, a.components.find_by(serial_number: "900001")
flight = a.flight_logs.sole
assert flight.track_log.attached?
assert flight.engine_data.attached?
assert_equal flight, a.consumable_records.sole.flight_log
assert_equal doc, a.major_records.sole.form_337_document
assert_match "Hangar 4", a.notes.sole.text
analysis = a.oil_analyses.sole
assert_equal [ "Blackstone", "S1", "W100", true ], [ analysis.lab, analysis.sample_number, analysis.oil_type, analysis.excluded_from_averages ]
assert_equal({ "iron" => 30.0, "zinc" => 4.0 }, analysis.elements)
assert_equal({ "iron" => 35.0 }, analysis.element_averages)
assert analysis.report.attached?
assert_equal "aircraft", a.events.last.category
assert_empty result[:warnings]
end
test "imports an archive from the original app (UUIDs, its field names, links we don't keep)" do
path = File.join(@dir, "original.sam.zip")
ids = Hash.new { |h, k| h[k] = SecureRandom.uuid }
data = AircraftArchive::TOP_LEVEL_KEYS.to_h { [ _1, [] ] }.merge(
"schema_version" => 2, "exported_at" => "2026-10-01T12:00:00+00:00", "source_instance" => "sam.example.org",
"aircraft" => { "id" => ids[:a], "tail_number" => "N0000X", "make" => "Piper", "model" => "PA-28", "status" => "AVAILABLE",
"tach_time" => "1234.5", "tach_time_offset" => "0.0", "hobbs_time" => "0.0", "hobbs_time_offset" => "0.0", "picture" => nil },
"component_types" => [ { "id" => ids[:ct], "name" => "engine", "consumable" => false } ],
"components" => [ { "id" => ids[:c], "aircraft_id" => ids[:a], "parent_component_id" => nil, "component_type_id" => ids[:ct],
"manufacturer" => "Lycoming", "model" => "O-360", "status" => "IN-USE", "date_in_service" => "2020-01-01",
"hours_in_service" => "500.0", "hours_since_overhaul" => "500.0", "tbo_hours" => 2000, "tbo_critical" => true } ],
"logbook_entries" => [ { "id" => ids[:le], "aircraft_id" => ids[:a], "log_type" => "ENG", "date" => "2026-01-01", "text" => "Annual",
"components" => [ ids[:c] ], "component_hours" => { ids[:c] => 500.0 }, "entry_type" => "INSPECTION" } ],
"squawks" => [ { "id" => ids[:s], "aircraft_id" => ids[:a], "priority" => 0, "issue_reported" => "Mag drop",
"attachment" => "attachments/health/squawks/abc123.png", "resolved" => false, "logbook_entries" => [] } ],
"oil_analysis_reports" => [ { "id" => ids[:o], "aircraft_id" => ids[:a], "component_id" => ids[:c], "sample_date" => "2026-01-01",
"lab" => "AVLab", "lab_number" => "", "oil_hours" => "40.0", "elements_ppm" => { "iron" => 12.5 },
"oil_properties" => nil, "status" => "monitor", "excluded_from_averages" => false } ],
"flight_logs" => [ { "id" => ids[:f], "aircraft_id" => ids[:a], "date" => "2026-02-01", "tach_time" => "1.3",
"track_log" => "attachments/track_logs/xyz.kml" } ],
"features" => [ { "feature" => "airworthiness_enforcement", "enabled" => true }, { "feature" => "oil_analysis", "enabled" => true } ]
)
Zip::OutputStream.open(path) do |zip|
zip.put_next_entry("manifest.json")
zip.write(data.to_json)
zip.put_next_entry("attachments/health/squawks/abc123.png")
zip.write(PNG)
zip.put_next_entry("attachments/track_logs/xyz.kml")
zip.write(file_fixture("tracks/flight.kml").read)
end
engine_type = component_type("Engine")
preview = AircraftArchive::Importer.check(path)
assert_equal [ "N0000X", "sam.example.org", 1 ], [ preview.tail_number, preview.source, preview.counts["components"] ]
result = AircraftArchive::Importer.new(path, owner: @owner).run
a = result[:aircraft]
assert_equal [ "N0000X", "Piper", 1234.5 ], [ a.tail_number, a.make, a.tach_time.to_f ]
assert_equal engine_type, a.components.sole.component_type, "matched case-insensitively"
assert_equal 2000, a.components.sole.tbo_hours
assert_equal "INSPECTION", a.logbook_entries.sole.entry_type
squawk = a.squawks.sole
assert_equal [ 0, "abc123.png" ], [ squawk.priority, squawk.attachments.sole.filename.to_s ]
analysis = a.oil_analyses.sole
assert_equal [ "monitor", 12.5, nil, a.components.sole ], [ analysis.status, analysis.value("iron"), analysis.sample_number, analysis.component ]
assert a.flight_logs.sole.track_log.attached?
assert_equal [ "1 logbook entry was linked to components; those links aren't kept here." ], result[:warnings]
end
test "a file whose contents don't match its name is skipped with a note" do
path = export(full_aircraft)
data = manifest(path)
fake = data["squawks"].find { _1["attachments"].any? }["attachments"].first
Zip::File.open(path) { |zip| zip.get_output_stream(fake) { _1.write("MZ not a png") } }
result = AircraftArchive::Importer.new(path, owner: @owner, tail_number: "N777").run
assert_equal 1, result[:aircraft].squawks.find_by(issue_reported: "Cracked fairing").attachments.count
assert_match "doesn't contain what its name says (.png)", result[:warnings].sole
end
test "rejects unsafe or unrecognized archives before importing anything" do
write = lambda do |entries|
path = File.join(@dir, "#{SecureRandom.hex(4)}.zip")
Zip::OutputStream.open(path) { |zip| entries.each { |name, body| zip.put_next_entry(name); zip.write(body) } }
path
end
good = AircraftArchive::TOP_LEVEL_KEYS.to_h { [ _1, [] ] }.merge("schema_version" => 2, "aircraft" => { "tail_number" => "N1" })
{
write.({ "notes.txt" => "hi" }) => "no manifest.json",
write.({ "manifest.json" => good.merge("schema_version" => 3).to_json }) => "Unsupported archive version 3",
write.({ "manifest.json" => good.except("squawks").to_json }) => "missing squawks",
write.({ "manifest.json" => good.to_json, "../../evil.txt" => "x" }) => "unsafe path",
write.({ "manifest.json" => good.merge("aircraft" => {}).to_json }) => "no aircraft tail number",
write.({ "manifest.json" => "{not json" }) => "couldn't be read",
File.join(@dir, "missing.zip").tap { File.write(_1, "not a zip") } => "Not a valid .sam.zip"
}.each do |path, message|
error = assert_raises(AircraftArchive::Error) { AircraftArchive::Importer.check(path) }
assert_match message, error.message
end
assert_equal 0, Aircraft.where(tail_number: "N1").count
end
test "a failure part-way removes everything the import created" do
path = export(full_aircraft)
before = [ Aircraft.count, Component.count, ActiveStorage::Blob.count ]
importer = AircraftArchive::Importer.new(path, owner: @owner, tail_number: "N999")
importer.define_singleton_method(:import_notes) { raise "disk on fire" }
assert_raises(RuntimeError) { importer.run }
assert_equal before[0, 2], [ Aircraft.count, Component.count ]
assert_equal 0, Aircraft.where(tail_number: "N999").count
perform_enqueued_jobs
assert_equal before[2], ActiveStorage::Blob.count
end
end
+32
View File
@@ -0,0 +1,32 @@
require "test_helper"
class LogbookTranscriberTest < ActiveSupport::TestCase
Message = Struct.new(:stop_reason, :content, :model)
Text = Struct.new(:type, :text)
def client_returning(message)
Class.new do
define_method(:beta) { self }
define_method(:messages) { self }
define_method(:stream) { |**| Struct.new(:accumulated_message).new(message) }
end.new
end
def transcribe(message) = LogbookTranscriber.new(client: client_returning(message)).transcribe([ { data: "x", media_type: "image/png" } ])
test "reads the structured answer" do
json = { entries: [ { "text" => "Annual" } ], non_logbook_pages: [ 2 ], unparseable_pages: [] }.to_json
result = transcribe(Message.new(:end_turn, [ Text.new(:thinking, ""), Text.new(:text, json) ], "claude-opus-5-5"))
assert_equal [ { "text" => "Annual" } ], result.entries
assert_equal [ 2 ], result.non_logbook_pages
end
test "turns refusals, truncation and bad JSON into clear errors" do
{ refusal: "declined", max_tokens: "too long to finish" }.each do |reason, message|
error = assert_raises(LogbookTranscriber::Error) { transcribe(Message.new(reason, [], "m")) }
assert_match message, error.message
end
error = assert_raises(LogbookTranscriber::Error) { transcribe(Message.new(:end_turn, [ Text.new(:text, "{nope") ], "m")) }
assert_match "wasn't valid JSON", error.message
end
end
+91 -22
View File
@@ -1,43 +1,112 @@
require "test_helper" require "test_helper"
class OilReportParserTest < ActiveSupport::TestCase class OilReportParserTest < ActiveSupport::TestCase
def parse(name) = OilReportParser.from_pdf(file_fixture("oil_analysis/#{name}").open)
# A real AvLab report rebuilt with glyph positions intact (including the hidden # A real AvLab report rebuilt with glyph positions intact (including the hidden
# color-code layer that overlaps the results) and a dummy header. # color-code layer that overlaps the results) and a dummy header.
def report = @report ||= OilReportParser.from_pdf(file_fixture("oil_analysis/avlab_report.pdf").open) def avlab = @avlab ||= parse("avlab_report.pdf")
test "reads the header" do # Anonymized sample reports from the original Simple Aircraft Manager project's
assert_equal "AvLab", report.lab # test suite (public domain): an older AvLab layout and a Blackstone report.
assert_equal "N12345", report.tail_number def older_avlab = @older_avlab ||= parse("avlab_older_report.pdf")
assert_equal "900001", report.engine_serial def blackstone = @blackstone ||= parse("blackstone_report.pdf")
assert_equal "CONT. 200", report.engine_model
test "AvLab: reads the header" do
assert_equal "AvLab", avlab.lab
assert_equal "N12345", avlab.tail_number
assert_equal "900001", avlab.engine_serial
assert_equal "CONT. 200", avlab.engine_model
end end
test "reads the current sample and both previous ones" do test "AvLab: reads the current sample and both previous ones" do
assert_equal [ true, false, false ], report.samples.map { _1[:current] } assert_equal [ true, false, false ], avlab.samples.map { _1[:current] }
current = report.samples.first current = avlab.samples.first
assert_equal Date.new(2026, 8, 20), current[:sample_date] assert_equal Date.new(2026, 8, 20), current[:sample_date]
assert_equal Date.new(2026, 9, 3), current[:analysis_date] assert_equal Date.new(2026, 9, 3), current[:analysis_date]
assert_equal "P26", current[:sample_number] assert_equal "P26", current[:sample_number]
assert_equal [ 94.7, 43.7, 43.7, 1 ], current.values_at(:engine_hours, :oil_hours, :filter_hours, :oil_added).map(&:to_f) assert_equal [ 94.7, 43.7, 43.7, 1 ], current.values_at(:engine_hours, :oil_hours, :filter_hours, :oil_added).map(&:to_f)
assert_equal "normal", current[:status] assert_equal "normal", current[:status]
assert_match(/ALL OIL VALUES SEEM FINE/, current[:lab_comments]) assert_equal "ALL OIL VALUES SEEM FINE. WE WILL CONTINUE TO MONITOR THIS ENGINE WITH YOUR NEXT SAMPLE.", current[:lab_comments]
end end
test "reads results and lab averages through the color-code layer" do test "AvLab: reads results and lab averages through the color-code layer" do
current, previous1, previous2 = report.samples current, previous1, previous2 = avlab.samples.map { |s| [ s[:elements].transform_values(&:to_f), s[:element_averages].transform_values(&:to_f) ] }
assert_equal({ iron: 44.0, copper: 8.1, nickel: 2.5, chromium: 3.9, aluminum: 6.7, lead: 2650.0, silicon: 25.3, tin: 1.6 }, assert_equal({ "iron" => 44.0, "copper" => 8.1, "nickel" => 2.5, "chromium" => 3.9, "aluminum" => 6.7, "lead" => 2650.0, "silicon" => 25.3, "tin" => 1.6 },
current.slice(:iron, :copper, :nickel, :chromium, :aluminum, :lead, :silicon, :tin).transform_values(&:to_f)) current[0].slice("iron", "copper", "nickel", "chromium", "aluminum", "lead", "silicon", "tin"))
assert_equal({ iron_avg: 40.5, lead_avg: 2654.0, tin_avg: 3.2 }, current.slice(:iron_avg, :lead_avg, :tin_avg).transform_values(&:to_f)) assert_equal({ "iron" => 40.5, "lead" => 2654.0, "tin" => 3.2 }, current[1].slice("iron", "lead", "tin"))
assert_nil current[:silver] # N/A refute current[0].key?("silver") # N/A
assert_nil current[:silver_avg] refute current[1].key?("silver")
assert_equal 0.3, previous1[:nickel].to_f # a real "0" hidden under a color-code "0" assert_equal 0.3, previous1[0]["nickel"] # a real "0" hidden under a color-code "0"
assert_equal 0, previous1[:tin] # "< 0.1" assert_equal 0, previous1[0]["tin"] # "< 0.1"
assert_equal 2024.0, previous2[:lead].to_f assert_equal 2024.0, previous2[0]["lead"]
assert_equal 58.7, previous2[:iron].to_f # not overwritten by "Cylinder Type: steel" assert_equal 58.7, previous2[0]["iron"] # not overwritten by "Cylinder Type: steel"
end
test "AvLab, older layout: unused labels don't steal values; comments span lines" do
current, previous = older_avlab.samples
assert_nil current[:oil_added] # the "Filter Wt. (mgs): 121" beside it isn't oil added
assert_equal "normal", current[:status]
assert_match(/\AALL METALLIC DEBRIS .* WITH YOUR NEXT SAMPLE\.\z/, current[:lab_comments])
assert_equal "monitor", previous[:status] # "** SEE LAB COMMENTS **"
assert_match "CLOSEST MATCH AMS# 6274, WITH THE LARGEST PIECE", previous[:lab_comments]
assert_equal [ 518.1, 43, 5 ], previous.values_at(:engine_hours, :oil_hours, :oil_added).map(&:to_f)
assert_equal({ "aluminum" => 6.8, "chromium" => 4.1, "copper" => 13.0, "iron" => 30.2, "lead" => 1510.0, "nickel" => 0.5, "silicon" => 3.7, "tin" => 0.0 },
previous[:elements].transform_values(&:to_f).sort.to_h)
end
test "Blackstone: header, five samples newest first, no rating" do
assert_equal "Blackstone", blackstone.lab
assert_equal "Phillips XC (A/C) 20W/50", blackstone.oil_type
assert_equal "Generic Engine", blackstone.engine_model
assert_nil blackstone.tail_number # "N0000X" isn't a real registration
assert_equal %w[2024-01-25 2022-03-31 2021-08-12 2020-06-08 2019-12-30], blackstone.samples.map { _1[:sample_date].iso8601 }
assert_equal [ true, false, false, false, false ], blackstone.samples.map { _1[:current] }
assert blackstone.samples.all? { _1[:status].nil? }
current = blackstone.samples.first
assert_equal "S000000", current[:sample_number]
assert_match(/\ALorem ipsum dolor sit amet.* id est laborum\.\z/, current[:lab_comments])
assert_nil blackstone.samples.second[:lab_comments]
end
test "Blackstone: hours, all 20 elements, universal averages and oil properties per column" do
current, second, _, fourth, oldest = blackstone.samples
assert_equal [ 43, 518, 5 ], current.values_at(:oil_hours, :engine_hours, :oil_added).map(&:to_f)
assert_equal 1903, fourth[:engine_hours].to_i # "1,903"
assert_nil blackstone.samples.third[:oil_added]
assert_equal 20, current[:elements].size
assert_equal({ "iron" => 36.0, "lead" => 1841.0, "aluminum" => 7.0, "zinc" => 4.0, "barium" => 0.0 },
current[:elements].slice("iron", "lead", "aluminum", "zinc", "barium").transform_values(&:to_f))
assert_equal 807.0, second[:elements]["lead"].to_f # skips the unit/location average column (1458)
assert_equal 818.0, oldest[:elements]["iron"].to_f
assert_equal({ "iron" => 35.0, "lead" => 2354.0, "phosphorus" => 415.0 },
current[:element_averages].slice("iron", "lead", "phosphorus").transform_values(&:to_f))
assert_equal({ "viscosity_sus_210f" => 95.3, "viscosity_cst_100c" => 19.24, "flashpoint_f" => 445.0, "fuel_percent" => 0.5, "water_percent" => 0.0, "insolubles_percent" => 0.3 },
current[:oil_properties].transform_values(&:to_f))
assert_equal 490.0, oldest[:oil_properties]["flashpoint_f"].to_f
end end
test "rejects other PDFs and non-PDFs" do test "rejects other PDFs and non-PDFs" do
assert_raises(OilReportParser::Error) { OilReportParser.from_pdf(StringIO.new("not a pdf")) } assert_raises(OilReportParser::Error) { OilReportParser.from_pdf(StringIO.new("not a pdf")) }
assert_raises(OilReportParser::Error) { OilReportParser.new([]).parse } assert_raises(OilReportParser::Error) { OilReportParser::Avlab.new([]).parse }
error = assert_raises(OilReportParser::Error) { parse_pdf_text("Just some other document") }
assert_match "AvLab and Blackstone", error.message
end
private
# A minimal one-page PDF with the given text.
def parse_pdf_text(text)
objects = [ "<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",
nil, "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>" ]
stream = "BT /F1 12 Tf 72 720 Td (#{text}) Tj ET"
objects[3] = "<< /Length #{stream.bytesize} >>\nstream\n#{stream}\nendstream"
pdf = +"%PDF-1.4\n"
offsets = objects.each_with_index.map { |body, i| pdf.bytesize.tap { pdf << "#{i + 1} 0 obj\n#{body}\nendobj\n" } }
xref = pdf.bytesize
pdf << "xref\n0 #{objects.size + 1}\n0000000000 65535 f \n" << offsets.map { format("%010d 00000 n \n", _1) }.join
pdf << "trailer\n<< /Size #{objects.size + 1} /Root 1 0 R >>\nstartxref\n#{xref}\n%%EOF\n"
OilReportParser.from_pdf(StringIO.new(pdf))
end end
end end