Track oil analysis reports; import AvLab PDFs

New Oil Analysis tab: one record per oil sample with dates, hours, the lab's
verdict and comments, ppm results for 12 wear metals with the lab's averages,
and the report PDF. Shows wear-metal trends, a side-by-side history table,
and flags readings 1.5x or more above the lab average.

AvLab report PDFs can be imported: every sample on the report (current and
previous) is added unless already recorded, the engine is matched by serial
number, and reports for another tail number are refused. The parser reads
positioned text and drops the hidden color-code layer that overlaps results.

Adds the pdf-reader gem. Test fixture is a rebuilt report with a dummy header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-06 06:12:51 -05:00
1 parent 6c6e7af8e6
commit d519aa9aae
24 files changed
+3456 -2

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
<%= form_with model: [aircraft, analysis], class: "card card-body space-y-5 max-w-4xl" do |f| %>
<%= f.errors_summary %>
<div class="grid gap-4 sm:grid-cols-3">
<%= f.field :sample_date, as: :date, required: true %>
<%= f.field :analysis_date, as: :date %>
<%= f.field :status, as: :select, label: "Lab's verdict", collection: OilAnalysis::STATUSES.invert %>
<%= f.field :component_id, as: :select, label: "Engine", collection: component_options(aircraft) %>
<%= f.field :lab, placeholder: "AvLab, Blackstone…" %>
<%= f.field :sample_number, label: "Sample #" %>
</div>
<fieldset class="grid gap-4 sm:grid-cols-4">
<legend class="mb-2 text-sm font-semibold">Hours</legend>
<%= f.field :engine_hours, as: :number, step: 0.1, label: "Engine (TSN/TSO)" %>
<%= f.field :oil_hours, as: :number, step: 0.1, label: "On oil" %>
<%= 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)" %>
</fieldset>
<fieldset>
<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>
<div class="grid gap-x-4 gap-y-3 sm:grid-cols-2 lg:grid-cols-3">
<% OilAnalysis::ELEMENTS.each do |el, (label, _)| %>
<div class="grid grid-cols-[6rem_1fr_1fr] items-center gap-2">
<%= f.label el, label, class: "text-sm font-medium text-slate-700" %>
<%= f.number_field el, step: 0.1, min: 0, class: "input", placeholder: "Result" %>
<%= f.number_field :"#{el}_avg", step: 0.1, min: 0, class: "input", placeholder: "Avg", "aria-label": "#{label} lab average" %>
</div>
<% end %>
</div>
</fieldset>
<%= f.field :lab_comments, 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/*" %>
<% if analysis.report.attached? %><p class="hint">Current: <%= analysis.report.filename %>. Choosing a file replaces it.</p><% end %>
<div class="flex gap-2"><%= f.submit "Save" %><%= link_to "Cancel", analysis.persisted? ? aircraft_oil_analysis_path(aircraft, analysis) : aircraft_oil_analyses_path(aircraft), class: "btn-secondary" %></div>
<% end %>
+6
View File
@@ -0,0 +1,6 @@
<%# Upload a lab report PDF to create the sample records. Locals: aircraft %>
<%= form_with url: import_aircraft_oil_analyses_path(aircraft), multipart: true, class: "flex flex-wrap items-center gap-2" do |f| %>
<%= f.file_field :report, accept: ".pdf,application/pdf", required: true, "aria-label": "Lab report 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" %>
<%= f.submit "Import report", class: "btn-secondary btn-sm" %>
<% end %>
+3
View File
@@ -0,0 +1,3 @@
<% content_for :title, "Edit Oil Analysis" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<%= render "form", aircraft: @aircraft, analysis: @analysis %>
+81
View File
@@ -0,0 +1,81 @@
<% content_for :title, "#{@aircraft.tail_number} Oil Analysis" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<% recent = @analyses.first(OilAnalysesController::COMPARE_SAMPLES) %>
<div class="space-y-5">
<section class="card">
<div class="card-header">
<div>
<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>
</div>
<%= render "import", aircraft: @aircraft %>
</div>
</section>
<section class="card">
<div class="card-header">
<h2 class="card-title">Wear metal trends</h2>
<%= link_to "+ Add Oil Analysis", new_aircraft_oil_analysis_path(@aircraft), class: "btn-secondary btn-sm" %>
</div>
<div class="card-body">
<% series = oil_trend_series(@analyses) %>
<% if series.any? %>
<%= 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>
<% else %>
<%= empty_state @analyses.any? ? "Add another sample to see trends." : "No oil analyses yet. Add one from your lab report." %>
<% end %>
</div>
</section>
<% if recent.any?(&:any_results?) %>
<section class="card">
<div class="card-header"><h2 class="card-title">Results (ppm)</h2>
<span class="text-xs text-slate-500">Lab average for similar engines in (parentheses); <span class="font-semibold text-red-700">red</span> is <%= OilAnalysis::FLAG_RATIO %>× or more above it.</span></div>
<div class="overflow-x-auto">
<table class="table">
<thead>
<tr><th>Element</th>
<% recent.each do |a| %><th class="text-right whitespace-nowrap"><%= link_to fmt_date(a.sample_date), aircraft_oil_analysis_path(@aircraft, a), class: "link" %></th><% end %></tr>
</thead>
<tbody>
<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>
<% OilAnalysis::ELEMENTS.each do |el, (label, source)| %>
<% next if recent.none? { _1.value(el) } %>
<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>
<% end %>
</tbody>
</table>
</div>
</section>
<% end %>
<section class="card">
<div class="card-header"><h2 class="card-title">Samples</h2></div>
<% if @analyses.any? %>
<div class="overflow-x-auto">
<table class="table">
<thead><tr><th>Sampled</th><th>Result</th><th>Engine</th><th class="text-right">Engine hrs</th><th class="text-right">Oil hrs</th><th>Lab</th><th>Report</th></tr></thead>
<tbody>
<% @analyses.each do |a| %>
<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"><%= 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>
<td><%= a.component&.to_s || "—" %></td>
<td class="text-right tabular-nums"><%= a.engine_hours ? hours(a.engine_hours) : "—" %></td>
<td class="text-right tabular-nums"><%= a.oil_hours ? hours(a.oil_hours) : "—" %></td>
<td><%= [ a.lab, a.sample_number ].compact.join(" · ").presence || "—" %></td>
<td><%= a.report.attached? ? link_to("PDF", rails_blob_path(a.report, disposition: :inline), class: "link", target: "_blank", rel: "noopener") : "—" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<%= empty_state "No samples recorded." %>
<% end %>
</section>
</div>
+8
View File
@@ -0,0 +1,8 @@
<% content_for :title, "Add Oil Analysis" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<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>
<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 %>
</section>
<%= render "form", aircraft: @aircraft, analysis: @analysis %>
+62
View File
@@ -0,0 +1,62 @@
<% content_for :title, "Oil Analysis #{fmt_date(@analysis.sample_date)}" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<div class="grid gap-5 lg:grid-cols-3">
<section class="card lg:col-span-1">
<div class="card-header">
<h2 class="card-title">Sample <%= fmt_date(@analysis.sample_date) %></h2>
<%= oil_status_badge(@analysis) %>
</div>
<dl class="card-body grid grid-cols-2 gap-x-4 gap-y-2 text-sm">
<dt class="text-slate-500">Engine</dt><dd><%= @analysis.component ? link_to(@analysis.component, aircraft_component_path(@aircraft, @analysis.component), class: "link") : "—" %></dd>
<dt class="text-slate-500">Engine hrs (TSN/TSO)</dt><dd><%= @analysis.engine_hours ? hours(@analysis.engine_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">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">Lab</dt><dd><%= @analysis.lab || "—" %></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">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>
<% 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>
<% end %>
<% if @analysis.notes.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">Notes</div><%= simple_format @analysis.notes %></div>
<% end %>
<% if owner? %>
<div class="flex gap-2 border-t border-slate-100 px-4 py-3">
<%= link_to "Edit", edit_aircraft_oil_analysis_path(@aircraft, @analysis), class: "btn-secondary btn-sm" %>
<%= delete_button "Delete", aircraft_oil_analysis_path(@aircraft, @analysis), confirm: "Delete this oil analysis?" %>
</div>
<% end %>
</section>
<section class="card lg:col-span-2">
<div class="card-header"><h2 class="card-title">Results (ppm)</h2>
<span class="text-xs text-slate-500"><%= @previous ? "Compared with #{fmt_date(@previous.sample_date)}" : "First sample for this engine" %></span></div>
<% if @analysis.any_results? %>
<div class="overflow-x-auto">
<table class="table">
<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>
<% OilAnalysis::ELEMENTS.each do |el, (label, source)| %>
<% next unless @analysis.value(el) || @analysis.average(el) %>
<tr class="<%= "bg-red-50" if @analysis.flagged?(el) %>">
<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 text-slate-500"><%= ppm(@analysis.average(el)) %></td>
<% if @previous %>
<td class="text-right tabular-nums"><%= ppm(@previous.value(el)) %></td>
<td class="text-right tabular-nums"><%= ppm_change(@analysis, @previous, el) %></td>
<% end %>
<td class="text-xs text-slate-500"><%= source %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<%= empty_state "No results entered." %>
<% end %>
</section>
</div>