mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Blackstone oil analysis, .sam.zip export/import, AI logbook transcription
- Oil analysis: Blackstone PDF import (20 elements, oil properties, universal averages), results stored per element, oil type, "leave out of trends", the original app's statuses; AvLab fixes (multi-line comments, unrelated labels no longer steal values). Existing records migrate. - Aircraft archives in the original app's .sam.zip format (schema v2): export from Access & Sharing, background import from the Fleet page, rake tasks. Archives move both ways between this app and the original. - Scanned logbook pages (images or PDFs) upload as a document; with ANTHROPIC_API_KEY set, Claude transcribes them into logbook entries. - Fix: deleting a component or aircraft failed when AD compliances or inspection records pointed at a component. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d068e1a63
commit
1c6c2f88c0
54 files changed
+2794
-286
No files matched your search
@@ -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
|
||||
@@ -31,8 +31,8 @@ class OilAnalysesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# Create records from a lab report PDF. AvLab reports include the two previous
|
||||
# samples too, so samples already on file are skipped rather than duplicated.
|
||||
# Create records from a lab report PDF (AvLab or Blackstone). Reports repeat
|
||||
# earlier samples, so samples already on file are skipped rather than duplicated.
|
||||
def import
|
||||
file = params[:report]
|
||||
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
|
||||
report.samples.each do |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]
|
||||
end
|
||||
end
|
||||
@@ -108,9 +108,10 @@ class OilAnalysesController < ApplicationController
|
||||
|
||||
def analysis_params
|
||||
params.require(:oil_analysis).permit(
|
||||
:component_id, :sample_date, :analysis_date, :lab, :sample_number, :engine_hours, :oil_hours,
|
||||
:filter_hours, :oil_added, :status, :lab_comments, :notes, :report,
|
||||
*OilAnalysis::ELEMENTS.keys.flat_map { [ _1, :"#{_1}_avg" ] }
|
||||
:component_id, :sample_date, :analysis_date, :lab, :sample_number, :oil_type, :engine_hours, :oil_hours,
|
||||
:filter_hours, :oil_added, :status, :lab_comments, :notes, :report, :excluded_from_averages,
|
||||
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) }
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user