mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Add spreadsheet flight import; fix oil/fuel consumption charts
Flight import (owners, Flights tab → Import from spreadsheet): - Upload .xlsx or .csv, preview, then confirm. Columns are matched by header name (Date, From, To, Tach, Hobbs, Oil, Fuel, Flight Time). - Each flight's time is derived from the previous row's meter reading; rows are kept in file order so typos (tach going backwards) are flagged and skipped while the next good row absorbs the time. - Unchanged tach imports as a 0.0 hr entry so fuel/oil isn't lost; "Full" fuel is warned about; re-imports skip existing rows. - History only: component hours are untouched unless the owner opts in to advancing the aircraft's meters to the last reading. - Upload redirects to a GET preview (Turbo ignores 200 renders after a form POST, which made the button appear to do nothing). Consumption charts: - Replace per-fill math (qty ÷ hours since last add), which spiked to 80 gal/hr on partial fills and top-offs, with a rolling rate (total added ÷ hours over a trailing 10 h fuel / 25 h oil window). - Headline shows recent (last ~50/100 hrs) and lifetime rates; straight chart lines instead of splines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
38df19c331
commit
4d3dc7150a
21 files changed
+820
-75
No files matched your search
@@ -46,8 +46,8 @@
|
||||
<div class="card-header"><h2 class="card-title">Consumption</h2>
|
||||
<%= link_to "Oil & fuel →", aircraft_consumable_records_path(@aircraft), class: "link text-sm" %></div>
|
||||
<div class="card-body grid grid-cols-2 gap-4 text-center">
|
||||
<div><div class="text-2xl font-semibold"><%= @oil.average || "—" %></div><div class="text-xs text-slate-500">Avg oil, hrs/qt</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @fuel.average || "—" %></div><div class="text-xs text-slate-500">Avg fuel burn, gal/hr</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @oil.recent || "—" %></div><div class="text-xs text-slate-500">Oil, hrs/qt (last ~100 hrs)</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @fuel.recent || "—" %></div><div class="text-xs text-slate-500">Fuel burn, gal/hr (last ~50 hrs)</div></div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<%= f.field :flight_hours, as: :number, step: 0.1, label: "Tach at time of record" %>
|
||||
</div>
|
||||
<%= f.field :notes, as: :textarea, rows: 2 %>
|
||||
<%= f.field :excluded_from_averages, as: :checkbox, label: "Exclude from averages", hint: "Use for oil changes or partial fills that would skew the trend." %>
|
||||
<%= f.field :excluded_from_averages, as: :checkbox, label: "Exclude from averages", hint: "Use for oil changes: the quantity added is ignored in consumption rates." %>
|
||||
<div class="flex gap-2"><%= f.submit "Save" %><%= link_to "Cancel", aircraft_consumable_records_path(aircraft), class: "btn-secondary" %></div>
|
||||
<% end %>
|
||||
<% if record.persisted? %><div class="mt-4"><%= delete_button "Delete record", aircraft_consumable_record_path(aircraft, record) %></div><% end %>
|
||||
@@ -8,15 +8,16 @@
|
||||
<%= link_to "+ Add #{stats.oil? ? 'Oil' : 'Fuel'}", new_aircraft_consumable_record_path(@aircraft, type: stats.record_type), class: "btn-secondary btn-sm" %>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="mb-3 flex gap-6 text-sm">
|
||||
<div><span class="text-2xl font-semibold"><%= stats.average || "—" %></span> <span class="text-slate-500"><%= stats.metric_label %> avg (last 20)</span></div>
|
||||
<div><span class="text-lg"><%= stats.latest || "—" %></span> <span class="text-slate-500">latest</span></div>
|
||||
<div class="mb-3 flex flex-wrap gap-x-6 gap-y-1 text-sm">
|
||||
<div><span class="text-2xl font-semibold"><%= stats.recent || "—" %></span> <span class="text-slate-500"><%= stats.metric_label %>, last ~<%= stats.recent_window_hours %> hrs</span></div>
|
||||
<div><span class="text-lg"><%= stats.lifetime || "—" %></span> <span class="text-slate-500">lifetime</span></div>
|
||||
</div>
|
||||
<% if stats.points.size >= 2 %>
|
||||
<%= line_chart stats.chart_data, xtitle: "Tach hours", ytitle: stats.metric_label, height: "220px",
|
||||
<%= line_chart stats.chart_data, xtitle: "Tach hours", ytitle: stats.metric_label, height: "220px", curve: false, points: false,
|
||||
library: { scales: { x: { type: "linear" } } }, colors: [stats.oil? ? "#b45309" : "#0369a1"] %>
|
||||
<p class="hint">Rolling <%= stats.chart_window_hours %>-hour window: total <%= stats.oil? ? "oil" : "fuel" %> added ÷ hours flown, so partial fills average out.<%= " Higher is better." if stats.oil? %></p>
|
||||
<% else %>
|
||||
<%= empty_state "Add at least three records to see a trend." %>
|
||||
<%= empty_state "Not enough history yet (need more than #{stats.chart_window_hours} tach hours of records)." %>
|
||||
<% end %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<% content_for :title, "Import Flights" %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<div class="grid gap-5 lg:grid-cols-3">
|
||||
<%= form_with url: aircraft_flight_import_path(@aircraft), multipart: true, class: "card card-body space-y-4 lg:col-span-2" do |f| %>
|
||||
<h2 class="card-title">Import flights from a spreadsheet</h2>
|
||||
<div>
|
||||
<%= f.label :file, "Spreadsheet (.xlsx or .csv)", class: "label" %>
|
||||
<%= f.file_field :file, accept: ".xlsx,.csv", 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" %>
|
||||
</div>
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<%= f.label :starting_tach, "Tach before the first flight (optional)", class: "label" %>
|
||||
<%= f.number_field :starting_tach, step: 0.1, class: "input" %>
|
||||
<p class="hint">E.g. the tach at purchase. Without it, the first row's time comes from a Flight Time column, if you have one.</p>
|
||||
</div>
|
||||
<div>
|
||||
<%= f.label :starting_hobbs, "Hobbs before the first flight (optional)", class: "label" %>
|
||||
<%= f.number_field :starting_hobbs, step: 0.1, class: "input" %>
|
||||
</div>
|
||||
</div>
|
||||
<%= f.submit "Upload & Preview", class: "btn-primary" %>
|
||||
<p class="hint">Nothing is saved until you confirm on the next page.</p>
|
||||
<% end %>
|
||||
|
||||
<section class="card card-body text-sm text-slate-700 space-y-2">
|
||||
<h3 class="font-semibold">Expected layout</h3>
|
||||
<p>One row per flight, with the meter readings <em>at the end</em> of the flight. Columns are matched by header name, in any order:</p>
|
||||
<ul class="list-disc pl-5 text-xs">
|
||||
<li><strong>Date</strong> (required)</li>
|
||||
<li><strong>Tach</strong> / Tach Time (required)</li>
|
||||
<li>From, To</li>
|
||||
<li>Hobbs / Hobbs Time</li>
|
||||
<li>Oil Added (qt), Fuel Added (gal); “N” or blank = none</li>
|
||||
<li>Flight Time (used only for the first row)</li>
|
||||
</ul>
|
||||
<p class="text-xs">Each flight's time is the difference from the row above. Rows are kept in file order.</p>
|
||||
<p class="text-xs">Importing records history only. It doesn't add time to components.</p>
|
||||
</section>
|
||||
</div>
|
||||
@@ -0,0 +1,84 @@
|
||||
<% content_for :title, "Import Preview" %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<% rows = @import.rows %>
|
||||
<% problems = rows.select { _1.status.in?(%i[error warning]) } %>
|
||||
<% last = @import.last_reading %>
|
||||
|
||||
<section class="card mb-5">
|
||||
<div class="card-header">
|
||||
<h2 class="card-title">Preview: <%= @filename %></h2>
|
||||
<span class="text-xs text-slate-500">Columns found: <%= @import.columns.keys.map { _1.to_s.humanize }.join(", ") %></span>
|
||||
</div>
|
||||
<div class="card-body grid grid-cols-2 gap-4 text-center sm:grid-cols-5">
|
||||
<div><div class="text-2xl font-semibold"><%= @import.importable.size %></div><div class="text-xs text-slate-500">flights to import</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= hours(@import.total_tach_hours) %></div><div class="text-xs text-slate-500">tach hours</div></div>
|
||||
<div><div class="text-2xl font-semibold text-amber-700"><%= @import.count(:warning) %></div><div class="text-xs text-slate-500">warnings (imported)</div></div>
|
||||
<div><div class="text-2xl font-semibold text-red-700"><%= @import.count(:error) %></div><div class="text-xs text-slate-500">errors (skipped)</div></div>
|
||||
<div><div class="text-2xl font-semibold text-slate-500"><%= @import.count(:duplicate) %></div><div class="text-xs text-slate-500">already imported</div></div>
|
||||
</div>
|
||||
|
||||
<% if @import.importable.any? %>
|
||||
<%= form_with url: confirm_aircraft_flight_import_path(@aircraft), class: "space-y-3 border-t border-slate-100 p-4",
|
||||
data: { turbo_confirm: "Import #{@import.importable.size} flights?" } do |f| %>
|
||||
<%= hidden_field_tag :token, @token %>
|
||||
<%= hidden_field_tag :filename, @filename %>
|
||||
<%= hidden_field_tag :starting_tach, @starting_tach %>
|
||||
<%= hidden_field_tag :starting_hobbs, @starting_hobbs %>
|
||||
<% if last && last.tach_in > @aircraft.tach_time %>
|
||||
<label class="flex items-start gap-2 text-sm">
|
||||
<%= check_box_tag :update_aircraft_hours, "1", true, class: "mt-0.5 h-4 w-4" %>
|
||||
<span>Also update <%= @aircraft.tail_number %>'s tach from <%= hours(@aircraft.tach_time) %> to <strong><%= hours(last.tach_in) %></strong>
|
||||
<%= "and hobbs to #{hours(last.hobbs_in)}" if last.hobbs_in && last.hobbs_in >= @aircraft.hobbs_time %>.
|
||||
<span class="block text-xs text-slate-500">Works like a normal hours update: the difference (<%= hours(last.tach_in - @aircraft.tach_time) %> hrs) is added to in-service components. Leave unchecked if component times are already current.</span></span>
|
||||
</label>
|
||||
<% elsif last %>
|
||||
<p class="text-xs text-slate-500">The aircraft's tach (<%= hours(@aircraft.tach_time) %>) is already at or past the last reading (<%= hours(last.tach_in) %>), so its hours won't change.</p>
|
||||
<% end %>
|
||||
<div class="flex gap-2">
|
||||
<%= f.submit "Import #{@import.importable.size} Flights", class: "btn-primary" %>
|
||||
<%= link_to "Upload a different file", new_aircraft_flight_import_path(@aircraft), class: "btn-secondary" %>
|
||||
</div>
|
||||
<% if @import.count(:error) > 0 %><p class="hint">Rows with errors are skipped. The next good row absorbs their time, so total hours stay correct. To include them, fix the spreadsheet and upload it again. Rows already imported are skipped automatically.</p><% end %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<p class="border-t border-slate-100 p-4 text-sm">Nothing to import. <%= link_to "Upload a different file", new_aircraft_flight_import_path(@aircraft), class: "link" %></p>
|
||||
<% end %>
|
||||
</section>
|
||||
|
||||
<% if problems.any? %>
|
||||
<section class="card mb-5">
|
||||
<div class="card-header"><h2 class="card-title">Rows needing attention</h2></div>
|
||||
<table class="table">
|
||||
<thead><tr><th>Row</th><th>Date</th><th>Route</th><th class="text-right">Tach</th><th>Issue</th></tr></thead>
|
||||
<tbody>
|
||||
<% problems.each do |r| %>
|
||||
<tr>
|
||||
<td><%= r.line %></td><td class="whitespace-nowrap"><%= r.date || "—" %></td>
|
||||
<td><%= [r.from, r.to].compact.join(" → ") %></td><td class="text-right"><%= r.tach_in %></td>
|
||||
<td><%= tag.span(r.status == :error ? "Skipped" : "Warning", class: r.status == :error ? "badge-red" : "badge-orange") %> <span class="text-xs"><%= r.messages.join("; ") %></span></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
<details class="card">
|
||||
<summary class="card-header cursor-pointer"><span class="card-title">All <%= rows.size %> rows</span></summary>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table">
|
||||
<thead><tr><th>Row</th><th>Date</th><th>From</th><th>To</th><th class="text-right">Tach</th><th class="text-right">Tach hrs</th><th class="text-right">Hobbs</th><th class="text-right">Hobbs hrs</th><th class="text-right">Oil</th><th class="text-right">Fuel</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
<% rows.each do |r| %>
|
||||
<tr class="<%= 'opacity-50' unless r.importable? %>">
|
||||
<td><%= r.line %></td><td class="whitespace-nowrap"><%= r.date %></td><td><%= r.from %></td><td><%= r.to %></td>
|
||||
<td class="text-right tabular-nums"><%= r.tach_in %></td><td class="text-right tabular-nums"><%= hours(r.tach_time) if r.tach_time %></td>
|
||||
<td class="text-right tabular-nums"><%= r.hobbs_in %></td><td class="text-right tabular-nums"><%= hours(r.hobbs_time) if r.hobbs_time %></td>
|
||||
<td class="text-right"><%= r.oil %></td><td class="text-right"><%= r.fuel %></td>
|
||||
<td><%= { ok: "", warning: tag.span("warning", class: "badge-orange"), error: tag.span("skipped", class: "badge-red"), duplicate: tag.span("duplicate", class: "badge-gray") }[r.status] %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</details>
|
||||
@@ -1,7 +1,7 @@
|
||||
<% content_for :title, "#{@aircraft.tail_number} Flights" %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title">Flight Log</h2><%= link_to "+ Log Flight", new_aircraft_flight_log_path(@aircraft), class: "btn-primary" %></div>
|
||||
<div class="card-header"><h2 class="card-title">Flight Log</h2><div class="flex gap-2"><% if owner? %><%= link_to "Import from spreadsheet", new_aircraft_flight_import_path(@aircraft), class: "btn-secondary" %><% end %><%= link_to "+ Log Flight", new_aircraft_flight_log_path(@aircraft), class: "btn-primary" %></div></div>
|
||||
<% if @flight_logs.any? %>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table">
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title">Consumption</h2></div>
|
||||
<div class="card-body grid grid-cols-2 gap-4 text-center">
|
||||
<div><div class="text-2xl font-semibold"><%= @oil.average || "—" %></div><div class="text-xs text-slate-500">Oil hrs/qt</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @fuel.average || "—" %></div><div class="text-xs text-slate-500">Fuel gal/hr</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @oil.recent || "—" %></div><div class="text-xs text-slate-500">Oil hrs/qt (recent)</div></div>
|
||||
<div><div class="text-2xl font-semibold"><%= @fuel.recent || "—" %></div><div class="text-xs text-slate-500">Fuel gal/hr (recent)</div></div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user