Flights tab: flight log grouped by day (route chain, tach/Hobbs/fuel/oil totals), expand for the legs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-11 18:44:11 -05:00
1 parent 0db17b3ba6
commit 7dbf180f76
5 files changed
+142 -23

No files matched your search

@@ -7,6 +7,7 @@ class FlightLogsController < ApplicationController
def index def index
@flight_logs = @aircraft.flight_logs.recent_first.includes(:user).with_attached_track_log.with_attached_engine_data @flight_logs = @aircraft.flight_logs.recent_first.includes(:user).with_attached_track_log.with_attached_engine_data
@days = FlightDay.group(@flight_logs)
@stats = FlightStats.new(@aircraft) @stats = FlightStats.new(@aircraft)
end end
+50
View File
@@ -0,0 +1,50 @@
# One day of flying on the Flights tab: its legs in the order flown, the
# route as one chain (KOQU → KBID → KOQU), and the day's totals.
class FlightDay
attr_reader :date, :flights
def self.group(flight_logs)
flight_logs.group_by(&:date).map { |date, flights| new(date, flights) }.sort_by(&:date).reverse
end
def initialize(date, flights)
@date = date
# Order flown: by tach out where known, then by when they were logged.
@flights = flights.sort_by { [ _1.tach_out || 0, _1.created_at || Time.at(0) ] }
end
# The day's airports chained leg to leg; a gap (a leg that doesn't start
# where the last one ended, or a missing code) starts a new chain: "A → B · C → D".
def route
chains = []
current = []
flights.each do |f|
from = f.departure_location.presence
to = f.destination_location.presence
if from && current.any? && from != current.last
chains << current
current = []
end
current << from if from && current.empty?
current << to if to
end
chains << current if current.any?
chains.map { _1.join(" → ") }.join(" · ")
end
def tach_time = sum(:tach_time)
def hobbs_time = sum(:hobbs_time)
def fuel_added = sum(:fuel_added)
def oil_added = sum(:oil_added)
def pilots = flights.filter_map { _1.user&.display_name }.uniq
def track_log? = flights.any? { _1.track_log.attached? }
def engine_data? = flights.any? { _1.engine_data.attached? }
private
# nil when no leg recorded it, so "—" can be shown instead of 0.
def sum(attr)
values = flights.filter_map(&attr)
values.sum if values.any?
end
end
+46 -22
View File
@@ -4,28 +4,52 @@
<section class="card"> <section class="card">
<div class="card-header"><h2 class="card-title">Flight Log</h2><div class="flex gap-2"><%= link_to "Export for AirTrail", new_aircraft_air_trail_export_path(@aircraft), class: "btn-secondary" %><% if owner? %><%= link_to "Standardize airport codes", aircraft_airport_codes_path(@aircraft), class: "btn-secondary" %><%= 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> <div class="card-header"><h2 class="card-title">Flight Log</h2><div class="flex gap-2"><%= link_to "Export for AirTrail", new_aircraft_air_trail_export_path(@aircraft), class: "btn-secondary" %><% if owner? %><%= link_to "Standardize airport codes", aircraft_airport_codes_path(@aircraft), class: "btn-secondary" %><%= 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? %> <% if @flight_logs.any? %>
<div class="overflow-x-auto"> <% blank = tag.span("—", class: "text-slate-300") %>
<table class="table"> <% qty = ->(v) { v ? number_with_precision(v, precision: 2, strip_insignificant_zeros: true) : blank } %>
<thead><tr><th>Date</th><th>Route</th><th class="text-right">Tach</th><th class="text-right">Hobbs</th><th class="text-right">Oil (qt)</th><th class="text-right">Fuel (gal)</th><th class="pl-6">Pilot</th></tr></thead> <div class="hidden grid-cols-[9.5rem_1fr_repeat(4,5rem)_1.25rem] gap-3 border-b border-slate-200 bg-slate-50 px-4 py-2 text-xs font-semibold uppercase tracking-wide text-slate-500 sm:grid">
<tbody> <span>Date</span><span>Route</span><span class="text-right">Tach</span><span class="text-right">Hobbs</span><span class="text-right">Fuel (gal)</span><span class="text-right">Oil (qt)</span><span></span>
<% blank = tag.span("—", class: "text-slate-300") %> </div>
<% @flight_logs.each do |fl| %> <div class="divide-y divide-slate-100">
<tr> <% @days.each do |day| %>
<td class="whitespace-nowrap"><%= link_to fl.date, aircraft_flight_log_path(@aircraft, fl), class: "link" %></td> <details class="group flight-day">
<td class="whitespace-nowrap"> <summary class="flex cursor-pointer list-none flex-wrap items-baseline gap-x-3 gap-y-1 px-4 py-3 hover:bg-slate-50 sm:grid sm:grid-cols-[9.5rem_1fr_repeat(4,5rem)_1.25rem] [&::-webkit-details-marker]:hidden">
<%= fl.route_summary.presence || blank %> <span class="font-medium whitespace-nowrap"><%= day.date.strftime("%a %b %-d, %Y") %><span class="block text-xs font-normal text-slate-500"><%= pluralize(day.flights.size, "flight") %></span></span>
<% if fl.track_log.attached? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %> <span class="min-w-0 basis-full sm:basis-auto">
<% if fl.engine_data.attached? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %> <%= day.route.presence || blank %>
</td> <% if day.track_log? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
<td class="text-right tabular-nums"><%= fl.tach_time ? hours(fl.tach_time) : blank %></td> <% if day.engine_data? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
<td class="text-right tabular-nums"><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %></td> <% if day.pilots.any? %><span class="block text-xs text-slate-500"><%= day.pilots.to_sentence %></span><% end %>
<td class="text-right tabular-nums"><%= fl.oil_added ? number_with_precision(fl.oil_added, precision: 2, strip_insignificant_zeros: true) : blank %></td> </span>
<td class="text-right tabular-nums"><%= fl.fuel_added ? number_with_precision(fl.fuel_added, precision: 2, strip_insignificant_zeros: true) : blank %></td> <span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Tach </span><%= day.tach_time ? hours(day.tach_time) : blank %></span>
<td class="pl-6 text-slate-600"><%= fl.user&.display_name || blank %></td> <span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Hobbs </span><%= day.hobbs_time ? hours(day.hobbs_time) : blank %></span>
</tr> <span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Fuel </span><%= qty.(day.fuel_added) %></span>
<% end %> <span class="text-sm tabular-nums sm:text-right"><span class="text-xs text-slate-500 sm:hidden">Oil </span><%= qty.(day.oil_added) %></span>
</tbody> <span aria-hidden="true" class="ml-auto text-slate-400 transition group-open:rotate-90">›</span>
</table> </summary>
<div class="overflow-x-auto bg-slate-50/60 px-2 pb-3 sm:px-4">
<table class="table">
<thead><tr><th>Leg</th><th>Route</th><th class="text-right">Tach</th><th class="text-right">Hobbs</th><th class="text-right">Fuel (gal)</th><th class="text-right">Oil (qt)</th><th class="pl-6">Pilot</th></tr></thead>
<tbody>
<% day.flights.each_with_index do |fl, i| %>
<tr>
<td class="whitespace-nowrap" data-label=""><%= link_to "Leg #{i + 1}", aircraft_flight_log_path(@aircraft, fl), class: "link" %></td>
<td class="whitespace-nowrap" data-label="Route">
<%= fl.route_summary.presence || blank %>
<% if fl.track_log.attached? %><span class="ml-1" title="GPS track attached" aria-label="GPS track attached">📍</span><% end %>
<% if fl.engine_data.attached? %><span class="ml-1" title="Engine data attached" aria-label="Engine data attached">📈</span><% end %>
</td>
<td class="text-right tabular-nums" data-label="Tach"><%= fl.tach_time ? hours(fl.tach_time) : blank %></td>
<td class="text-right tabular-nums" data-label="Hobbs"><%= fl.hobbs_time ? hours(fl.hobbs_time) : blank %></td>
<td class="text-right tabular-nums" data-label="Fuel"><%= qty.(fl.fuel_added) %></td>
<td class="text-right tabular-nums" data-label="Oil"><%= qty.(fl.oil_added) %></td>
<td class="pl-6 text-slate-600" data-label="Pilot"><%= fl.user&.display_name || blank %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</details>
<% end %>
</div> </div>
<% else %> <% else %>
<%= empty_state "No flights logged yet." %> <%= empty_state "No flights logged yet." %>
+1 -1
View File
@@ -19,7 +19,7 @@ If **Enforce airworthiness** is on, neither works while the aircraft is grounded
## The Flights tab ## The Flights tab
The **Flights** tab lists flights with totals and hours by month. From there: The **Flights** tab shows totals and hours by month, then the flight log grouped by day: each day shows the whole route flown (KOQU → KBID → KOQU; a "·" marks where the aircraft moved without a logged leg) and that day's tach and Hobbs time, fuel and oil. Click a day to see its legs, and a leg to open the flight. From there:
- **Import from spreadsheet** (owners): historical flights from `.xlsx` or `.csv` (Date, From, To, Tach, Hobbs, Oil Added, Fuel Added). A preview flags problems such as a tach going backwards and skips rows already imported. It only adds history: component hours don't change unless you choose to advance the meters. - **Import from spreadsheet** (owners): historical flights from `.xlsx` or `.csv` (Date, From, To, Tach, Hobbs, Oil Added, Fuel Added). A preview flags problems such as a tach going backwards and skips rows already imported. It only adds history: component hours don't change unless you choose to advance the meters.
- **Export for AirTrail**: a file to import into AirTrail. For automatic syncing, see [Your profile](profile.md). - **Export for AirTrail**: a file to import into AirTrail. For automatic syncing, see [Your profile](profile.md).
+44
View File
@@ -0,0 +1,44 @@
require "test_helper"
# Flights tab grouped by day: the day's route, totals, and its legs.
class FlightDaysTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user("owner@example.com")
@a = create_aircraft(owner: @owner, tail_number: "N12345")
end
def leg(date, from, to, tach_out, tach_in, fuel: nil, oil: nil)
@a.flight_logs.create!(date: date, departure_location: from, destination_location: to, tach_out: tach_out, tach_in: tach_in,
fuel_added: fuel, oil_added: oil, user: @owner)
end
test "legs are grouped by day with the route chained and totals added up" do
day = Date.new(2026, 11, 7)
leg(day, "KBID", "KOQU", 101.0, 101.6, fuel: 10) # logged first but flown second
leg(day, "KOQU", "KBID", 100.0, 100.7, oil: 1)
leg(day, "1B9", "KOQU", 102.0, 102.4, fuel: 5.5) # repositioned in between: a gap
leg(day - 1, "KOQU", "KOQU", 99.0, 99.5)
days = FlightDay.group(@a.flight_logs.to_a)
assert_equal [ day, day - 1 ], days.map(&:date)
d = days.first
assert_equal "KOQU → KBID → KOQU · 1B9 → KOQU", d.route
assert_in_delta 1.7, d.tach_time, 0.001
assert_in_delta 15.5, d.fuel_added, 0.001
assert_equal 1, d.oil_added
assert_nil days.last.fuel_added # nothing recorded: shown as "—"
sign_in @owner
get aircraft_flight_logs_path(@a)
assert_select "details.flight-day", 2
assert_select "details.flight-day summary", text: /Sat Nov 7, 2026.*3 flights.*KOQU → KBID → KOQU · 1B9 → KOQU/m
assert_select "details.flight-day:first-of-type table tbody tr", 3
assert_select "details.flight-day:first-of-type a", text: "Leg 1"
end
test "legs with missing airports still show" do
leg(Date.new(2026, 11, 8), nil, "KOQU", 1, 1.5)
leg(Date.new(2026, 11, 8), nil, nil, 2, 2.5)
assert_equal "KOQU", FlightDay.group(@a.flight_logs.to_a).first.route
end
end