mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Add flying stats to the Flights tab; drop the DRS search link setting
Flights tab: - Summary cards for the past 30 days, this year, past 12 months and since the first flight (flight hours, flights, tach, fuel). - Flight hours by month (last 24 months) with the past-year monthly average, totals (flights, flight/tach time, average and longest flight, fuel, gal/hr, airports visited, most visited) and a by-year table. - Flight time is Hobbs time where recorded, otherwise tach time. Removed the per-aircraft DRS search link and its "Open in DRS" button: each AD's DRS link already comes from the Excel import. A guarded migration drops the column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b75fd7cf62
commit
0c4803d717
15 files changed
+221
-23
No files matched your search
@@ -68,7 +68,7 @@ class AircraftController < ApplicationController
|
||||
params.require(:aircraft).permit(
|
||||
:tail_number, :make, :model, :serial_number, :description, :purchased, :status,
|
||||
:tach_time, :tach_time_offset, :hobbs_time, :hobbs_time_offset,
|
||||
:airworthiness_enforcement, :picture, :drs_search_url
|
||||
:airworthiness_enforcement, :picture
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -4,6 +4,7 @@ class FlightLogsController < ApplicationController
|
||||
|
||||
def index
|
||||
@flight_logs = @aircraft.flight_logs.recent_first.includes(:user)
|
||||
@stats = FlightStats.new(@aircraft)
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
@@ -28,8 +28,6 @@ class Aircraft < ApplicationRecord
|
||||
|
||||
validates :tail_number, presence: true
|
||||
validates :status, inclusion: { in: STATUSES.keys }
|
||||
validates :drs_search_url, format: { with: %r{\Ahttps://drs\.faa\.gov/\S*\z}, message: "must be a https://drs.faa.gov/ link" }, allow_blank: true
|
||||
normalizes :drs_search_url, with: ->(url) { url.strip.sub(/#.*\z/, "").presence }
|
||||
validate :picture_is_image
|
||||
|
||||
scope :ordered, -> { order(:tail_number) }
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
# Flying metrics for the Flights tab: totals, recent periods, fuel, airports
|
||||
# and a monthly/yearly breakdown.
|
||||
#
|
||||
# "Flight time" is Hobbs time where the flight has it, otherwise tach time
|
||||
# (e.g. flights logged before a Hobbs was installed).
|
||||
class FlightStats
|
||||
Period = Struct.new(:label, :flights, :tach, :flight, :fuel, keyword_init: true)
|
||||
|
||||
attr_reader :today
|
||||
|
||||
def initialize(aircraft, today: Date.current)
|
||||
@aircraft = aircraft
|
||||
@today = today
|
||||
@flights = aircraft.flight_logs.order(:date, :created_at).to_a
|
||||
@fuel = aircraft.consumable_records.where(record_type: "fuel").pluck(:date, :quantity_added)
|
||||
end
|
||||
|
||||
def any? = @flights.any?
|
||||
def count = @flights.size
|
||||
def first_date = @flights.first&.date
|
||||
|
||||
def total_tach = sum(@flights) { _1.tach_time }
|
||||
def total_flight = sum(@flights) { flight_hours(_1) }
|
||||
def total_fuel = @fuel.sum { _2.to_f }.round(1)
|
||||
|
||||
def average_flight = count.zero? ? nil : (total_flight / count).round(1)
|
||||
def shortest_flight = @flights.map { flight_hours(_1) }.min&.round(1)
|
||||
def longest_flight = @flights.map { flight_hours(_1) }.max&.round(1)
|
||||
|
||||
# Gallons per tach hour over all fuel records.
|
||||
def gph = ConsumptionStats.new(@aircraft, "fuel").lifetime
|
||||
|
||||
def airports
|
||||
@airports ||= @flights.flat_map { [ _1.departure_location, _1.destination_location ] }
|
||||
.filter_map { _1.to_s.strip.upcase.presence }
|
||||
end
|
||||
|
||||
def airports_visited = airports.uniq.size
|
||||
|
||||
# Most-used airports (departures + arrivals), e.g. [["KOQU", 412], ...]
|
||||
def top_airports(limit = 5) = airports.tally.sort_by { [ -_2, _1 ] }.first(limit)
|
||||
|
||||
def periods
|
||||
[
|
||||
period("Past 30 days", today - 29..today),
|
||||
period("Past 12 months", (today << 12) + 1..today),
|
||||
period("This year (#{today.year})", Date.new(today.year)..today),
|
||||
period("Last year (#{today.year - 1})", Date.new(today.year - 1)..Date.new(today.year - 1, 12, 31)),
|
||||
period("Since purchase", (first_date || today)..today)
|
||||
]
|
||||
end
|
||||
|
||||
def monthly_average_past_year
|
||||
(period("", (today << 12) + 1..today).flight / 12.0).round(1)
|
||||
end
|
||||
|
||||
# { "Nov 2024" => 12.3, ... } for the last `months` months, including empty ones.
|
||||
def flight_hours_by_month(months = 24)
|
||||
start = (today << (months - 1)).beginning_of_month
|
||||
totals = @flights.select { _1.date >= start }.group_by { _1.date.beginning_of_month }
|
||||
.transform_values { |fs| sum(fs) { flight_hours(_1) } }
|
||||
(0...months).to_h do |i|
|
||||
month = start >> i
|
||||
[ month.strftime("%b %Y"), totals.fetch(month, 0.0) ]
|
||||
end
|
||||
end
|
||||
|
||||
def by_year
|
||||
years = (@flights.map { _1.date.year } + @fuel.map { _1[0].year }).uniq.sort.reverse
|
||||
years.map { |y| period(y.to_s, Date.new(y)..Date.new(y, 12, 31)) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def flight_hours(f) = (f.hobbs_time || f.tach_time).to_f
|
||||
|
||||
def sum(items, &block) = items.sum { block.(_1).to_f }.round(1)
|
||||
|
||||
def period(label, range)
|
||||
flights = @flights.select { range.cover?(_1.date) }
|
||||
Period.new(label: label, flights: flights.size,
|
||||
tach: sum(flights) { _1.tach_time }, flight: sum(flights) { flight_hours(_1) },
|
||||
fuel: @fuel.select { range.cover?(_1[0]) }.sum { _2.to_f }.round(1))
|
||||
end
|
||||
end
|
||||
@@ -14,11 +14,10 @@
|
||||
<section class="card card-body space-y-2 text-sm text-slate-700">
|
||||
<h3 class="font-semibold">Getting the file</h3>
|
||||
<ol class="list-decimal space-y-1 pl-4 text-xs">
|
||||
<li>On <a href="<%= @aircraft.drs_search_url.presence || "https://drs.faa.gov" %>" class="link" target="_blank" rel="noopener noreferrer"><%= @aircraft.drs_search_url.present? ? "your saved DRS search" : "drs.faa.gov" %></a>, search Airworthiness Directives for this aircraft's make and model (add the engine and propeller too).</li>
|
||||
<li>On <a href="https://drs.faa.gov" class="link" target="_blank" rel="noopener noreferrer">drs.faa.gov</a>, search Airworthiness Directives for this aircraft's make and model (add the engine and propeller too).</li>
|
||||
<li>Save the results as <strong>Excel</strong>. The Excel file links each AD number to its DRS page; the CSV doesn't, so a CSV import won't have links.</li>
|
||||
<li>Upload it here.</li>
|
||||
</ol>
|
||||
<% unless @aircraft.drs_search_url.present? %><p class="text-xs">Tip: save the DRS search link on the aircraft (Edit aircraft) for a one-click “Open in DRS” button.</p><% end %>
|
||||
<p class="text-xs">ADs already tracked are matched by number, so importing the Excel file after a CSV import just adds the links.</p>
|
||||
</section>
|
||||
</div>
|
||||
@@ -20,8 +20,6 @@
|
||||
<% unless aircraft.new_record? %>
|
||||
<p class="hint">To change hours use “Update Hours” on the overview, so component times stay in sync.</p>
|
||||
<% end %>
|
||||
<%= f.field :drs_search_url, label: "FAA DRS AD search link", placeholder: "https://drs.faa.gov/browse/ADFRAWD/doctypeDetails?…",
|
||||
hint: "Run the AD search for this aircraft on drs.faa.gov and paste the page link. The ADs tab then gets a one-click “Open in DRS” button." %>
|
||||
<%= f.field :picture, as: :file, accept: "image/*" %>
|
||||
<%= f.field :airworthiness_enforcement, as: :checkbox, label: "Enforce airworthiness",
|
||||
hint: "Block flight logs and hours updates while the aircraft is red." %>
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
<%= f.submit "Link existing", class: "btn-secondary" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% if @aircraft.drs_search_url.present? %><%= link_to "Open in DRS ↗", @aircraft.drs_search_url, class: "btn-secondary", target: "_blank", rel: "noopener noreferrer" %><% end %>
|
||||
<%= link_to "Import from FAA DRS", new_aircraft_ad_import_path(@aircraft), class: "btn-secondary" %>
|
||||
<%= link_to "+ New AD", new_aircraft_airworthiness_directive_path(@aircraft), class: "btn-primary" %>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<%# Flying metrics. "Flight time" = Hobbs where recorded, else tach. %>
|
||||
<% periods = stats.periods %>
|
||||
<div class="mb-5 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<% [ [ "Past 30 days", periods[0] ], [ "This year", periods[2] ], [ "Past 12 months", periods[1] ], [ "Since #{fmt_date(stats.first_date)}", periods[4] ] ].each do |title, p| %>
|
||||
<section class="card card-body">
|
||||
<div class="text-xs font-medium uppercase tracking-wide text-slate-500"><%= title %></div>
|
||||
<div class="mt-1 text-2xl font-semibold tabular-nums"><%= hours(p.flight) %> <span class="text-sm font-normal text-slate-500">hrs</span></div>
|
||||
<div class="mt-1 text-xs text-slate-600"><%= pluralize(p.flights, "flight") %> · <%= hours(p.tach) %> tach · <%= number_with_delimiter(p.fuel) %> gal</div>
|
||||
</section>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="mb-5 grid gap-5 lg:grid-cols-3">
|
||||
<section class="card lg:col-span-2">
|
||||
<div class="card-header"><h2 class="card-title">Flight hours by month</h2>
|
||||
<span class="text-xs text-slate-500">Avg <%= hours(stats.monthly_average_past_year) %> hrs/month over the past year</span></div>
|
||||
<div class="card-body">
|
||||
<%= column_chart stats.flight_hours_by_month, height: "220px", colors: [ "#0369a1" ], ytitle: "hrs", library: { plugins: { legend: { display: false } } } %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title">Totals</h2></div>
|
||||
<dl class="card-body grid grid-cols-2 gap-x-3 gap-y-2 text-sm">
|
||||
<dt class="text-slate-500">Flights logged</dt><dd class="text-right tabular-nums"><%= number_with_delimiter(stats.count) %></dd>
|
||||
<dt class="text-slate-500">Flight time</dt><dd class="text-right tabular-nums"><%= hours(stats.total_flight) %> hrs</dd>
|
||||
<dt class="text-slate-500">Tach time</dt><dd class="text-right tabular-nums"><%= hours(stats.total_tach) %> hrs</dd>
|
||||
<dt class="text-slate-500">Average flight</dt><dd class="text-right tabular-nums"><%= hours(stats.average_flight) %> hrs</dd>
|
||||
<dt class="text-slate-500">Longest flight</dt><dd class="text-right tabular-nums"><%= hours(stats.longest_flight) %> hrs</dd>
|
||||
<dt class="text-slate-500">Fuel</dt><dd class="text-right tabular-nums"><%= number_with_delimiter(stats.total_fuel) %> gal</dd>
|
||||
<dt class="text-slate-500">Fuel burn</dt><dd class="text-right tabular-nums"><%= stats.gph || "—" %> gal/hr</dd>
|
||||
<dt class="text-slate-500">Airports visited</dt><dd class="text-right tabular-nums"><%= stats.airports_visited %></dd>
|
||||
</dl>
|
||||
<% if stats.top_airports.any? %>
|
||||
<div class="border-t border-slate-100 px-4 py-3 text-xs text-slate-600">
|
||||
<span class="font-medium text-slate-700">Most visited:</span>
|
||||
<%= stats.top_airports.map { |code, n| "#{code} (#{n})" }.join(" · ") %>
|
||||
</div>
|
||||
<% end %>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<details class="card mb-5">
|
||||
<summary class="card-header cursor-pointer"><span class="card-title">By year</span></summary>
|
||||
<table class="table">
|
||||
<thead><tr><th>Year</th><th class="text-right">Flights</th><th class="text-right">Flight hrs</th><th class="text-right">Tach hrs</th><th class="text-right">Fuel (gal)</th><th class="text-right">Avg flight</th></tr></thead>
|
||||
<tbody>
|
||||
<% stats.by_year.each do |y| %>
|
||||
<tr>
|
||||
<td><%= y.label %></td>
|
||||
<td class="text-right tabular-nums"><%= y.flights %></td>
|
||||
<td class="text-right tabular-nums"><%= hours(y.flight) %></td>
|
||||
<td class="text-right tabular-nums"><%= hours(y.tach) %></td>
|
||||
<td class="text-right tabular-nums"><%= number_with_delimiter(y.fuel) %></td>
|
||||
<td class="text-right tabular-nums"><%= y.flights.positive? ? hours(y.flight / y.flights) : "—" %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="px-4 pb-3 hint">Flight time is Hobbs time where recorded, otherwise tach time.</p>
|
||||
</details>
|
||||
@@ -1,5 +1,6 @@
|
||||
<% content_for :title, "#{@aircraft.tail_number} Flights" %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<% if @stats.any? %><%= render "flight_logs/stats", stats: @stats %><% end %>
|
||||
<section class="card">
|
||||
<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? %>
|
||||
|
||||
Reference in new issue
Block a user