AD baseline: record DRS imports and the models they covered; weekly search starts from the last import; reminders and audit check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-11 20:56:20 -05:00
1 parent ba0455b149
commit 8bea2abf75
13 files changed
+169 -4

No files matched your search

+3
View File
@@ -29,6 +29,9 @@ class AdImportsController < ApplicationController
# Only AD numbers present in the uploaded file; values are checked against AdImport::ACTIONS.
decisions = params.fetch(:decisions, {}).permit(*import.rows.map(&:number)).to_h
result = import.import!(decisions: decisions, user: current_user)
@aircraft.drs_imports.create!(user: current_user, filename: params[:filename].to_s.first(200), ad_count: import.rows.size,
tracked: result[:tracked], not_applicable: result[:not_applicable],
models_covered: DrsImport.covered(@aircraft, import.rows.flat_map(&:models)))
discard_upload(token)
redirect_to aircraft_airworthiness_directives_path(@aircraft),
notice: "Imported #{result[:tracked]} ADs to track and #{result[:not_applicable]} marked N/A; " \
+15 -2
View File
@@ -10,16 +10,29 @@ class ReminderMailer < ApplicationMailer
def self.audit_due_for?(user) = user.admin? && AuditRun.due_soon?
def self.anything_for?(user) = items_for(user).any? || expiring_certificates_for(user).any? || audit_due_for?(user)
# Owned aircraft whose AD baseline needs a (new) DRS import: none yet, a
# year old, or a model never covered.
def self.drs_due_for(user)
Aircraft.joins(:roles).where(aircraft_roles: { user_id: user.id, role: "owner" }).ordered
.select { DrsImport.renewal_due?(_1) || DrsImport.missing_models(_1).any? }
end
# The DRS reminder rides along with any other news; on its own it's sent
# on the first Monday of the month only, not every week.
def self.anything_for?(user, today: Date.current)
items_for(user).any? || expiring_certificates_for(user).any? || audit_due_for?(user) ||
(today.day <= 7 && drs_due_for(user).any?)
end
def weekly(user)
@user = user
@items = self.class.items_for(user)
@certificates = self.class.expiring_certificates_for(user)
@audit_due = self.class.audit_due_for?(user) && AuditRun.next_due
@drs_due = self.class.drs_due_for(user)
overdue = @items.sum { |_, issues, _| issues.count { _1.severity.in?(%w[RED LIMITED]) } }
subject = overdue.positive? ? "#{overdue} #{'item'.pluralize(overdue)} overdue" : "Maintenance coming due"
subject = (@certificates.any? ? "Certificate expiring" : "Records audit due") if @items.empty?
subject = (@certificates.any? ? "Certificate expiring" : (@audit_due ? "Records audit due" : "Refresh the AD list from DRS")) if @items.empty?
mail subject: [ subject, @items.map { _1.first.tail_number }.to_sentence.presence ].compact.join(": "), to: user.email_address
end
end
+1
View File
@@ -21,6 +21,7 @@ class Aircraft < ApplicationRecord
has_many :documents, dependent: :destroy
has_many :inspection_records, dependent: :destroy
has_many :ad_compliances, dependent: :destroy
has_many :drs_imports, dependent: :delete_all
has_many :major_records, dependent: :destroy
has_many :flight_logs, dependent: :destroy
has_many :consumable_records, dependent: :destroy
+37
View File
@@ -0,0 +1,37 @@
# One import of ADs from an FAA DRS export, and which of the aircraft's
# models it covered. Together they're the AD baseline; the weekly
# Federal Register search covers what's published after.
class DrsImport < ApplicationRecord
belongs_to :aircraft
belongs_to :user, optional: true
RENEW_AFTER = 12.months
scope :latest_first, -> { order(created_at: :desc, id: :desc) }
def self.normalize(model) = model.to_s.upcase.gsub(/[\s\-]/, "")
# The aircraft's own models worth a DRS search: airframe, engines, propellers.
def self.models_for(aircraft)
([ aircraft.model ] + aircraft.components.in_use.includes(:component_type)
.select { _1.component_type&.name.to_s.match?(AdWatch::ENGINE_OR_PROP) }.map(&:model))
.map { _1.to_s.strip }.compact_blank.uniq
end
# Which of the aircraft's models appear in the file's model lists ("O-200A" matches "O-200-A").
def self.covered(aircraft, file_models)
listed = file_models.map { normalize(_1) }.to_set
models_for(aircraft).select { listed.include?(normalize(_1)) }
end
# Models never covered by an import.
def self.missing_models(aircraft)
done = aircraft.drs_imports.flat_map { Array(_1.models_covered) }.map { normalize(_1) }.to_set
models_for(aircraft).reject { done.include?(normalize(_1)) }
end
def self.renewal_due?(aircraft)
latest = aircraft.drs_imports.latest_first.first
latest.nil? ? AirworthinessDirective.applicable_to(aircraft).exists? || aircraft.components.any? : latest.created_at < RENEW_AFTER.ago
end
end
+10 -1
View File
@@ -92,13 +92,22 @@ class AdWatch
# Returns the new AdAlerts.
def check!
since = (@aircraft.ad_watch_checked_at || FIRST_LOOKBACK.ago).to_date - 7 # overlap; duplicates are skipped
since = start_date - 7 # overlap; duplicates are skipped
known = AirworthinessDirective.pluck(:name).to_set
created = terms.flat_map { |term| search(term.words, since).filter_map { record(_1, term, known) } }
@aircraft.update_column(:ad_watch_checked_at, Time.current)
created
end
# Where the search picks up: the last check; before the first one, the
# last DRS import (the baseline, less 60 days for ADs that hadn't reached
# DRS yet), else two years back.
def start_date
return @aircraft.ad_watch_checked_at.to_date if @aircraft.ad_watch_checked_at
imported = @aircraft.drs_imports.maximum(:created_at)
imported ? (imported - 60.days).to_date : FIRST_LOOKBACK.ago.to_date
end
private
def record(doc, term, known)
+7
View File
@@ -29,6 +29,7 @@ class RecordsAudit
backup.present? && backup.ok && backup.created_at > 2.days.ago,
backup ? "#{backup.created_at.to_date}: #{backup.message}" : "Never run"),
*offsite_items,
drs_item,
Item.new("procedures", "The procedures document is adopted and its details filled in", true,
revision.present? && @policy.complete?,
revision ? "Revision #{revision.number}, adopted #{revision.adopted_at.to_date}" : "Not adopted yet"),
@@ -68,6 +69,12 @@ class RecordsAudit
private
def drs_item
due = Aircraft.ordered.select { DrsImport.renewal_due?(_1) || DrsImport.missing_models(_1).any? }
Item.new("drs", "Each aircraft's AD list was refreshed from FAA DRS in the last 12 months, for all its models", true, due.empty?,
due.any? ? "Needs a DRS import: #{due.map(&:tail_number).join(', ')}" : "Up to date")
end
def offsite_items
return [] unless OffsiteConnection.connected?
run = OffsiteRun.latest_backup
@@ -51,6 +51,27 @@
<% end %>
</section>
<% last_drs = @aircraft.drs_imports.latest_first.first %>
<% missing = DrsImport.missing_models(@aircraft) %>
<section class="card mt-5" id="ad-baseline">
<div class="card-header">
<div>
<h2 class="card-title">AD baseline from FAA DRS</h2>
<p class="text-xs text-slate-500">The full list comes from DRS imports; the weekly search below adds what's published after.</p>
</div>
<% if owner? %><%= link_to "Import from FAA DRS", new_aircraft_ad_import_path(@aircraft), class: "btn-secondary btn-sm" %><% end %>
</div>
<div class="card-body space-y-2 text-sm">
<% if last_drs %>
<p>Last import <strong><%= last_drs.created_at.to_date %></strong> by <%= last_drs.user&.display_name || "—" %>: <%= last_drs.ad_count %> ADs<%= ", covering #{Array(last_drs.models_covered).to_sentence}" if Array(last_drs.models_covered).any? %>.</p>
<% if last_drs.created_at < DrsImport::RENEW_AFTER.ago %><p class="text-amber-800">Over a year old: import again to pick up revised ADs and anything the weekly search missed.</p><% end %>
<% else %>
<p class="text-amber-800">No DRS import yet. Export the ADs for each model from drs.faa.gov and import them here.</p>
<% end %>
<% if missing.any? %><p class="text-amber-800">Never imported for: <strong><%= missing.join(", ") %></strong>. Search DRS for each and import the file.</p><% end %>
</div>
</section>
<section class="card mt-5" id="new-ads">
<div class="card-header">
<div>
@@ -18,6 +18,8 @@
<% @certificates.each do |c| %><li><strong><%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>:</strong> <%= c.kind_label %> <%= c.number %></li><% end %>
</ul>
<% end %>
<% if @drs_due.any? %><p><strong>Refresh the AD list from FAA DRS</strong> (new ADs are found weekly, but revisions and anything missed come from a fresh import):</p>
<ul><% @drs_due.each do |a| %><li><%= link_to a.tail_number, aircraft_airworthiness_directives_url(a) %>: <%= (m = DrsImport.missing_models(a)).any? ? "never imported for #{m.to_sentence}" : "last import #{a.drs_imports.latest_first.first&.created_at&.to_date || 'never'}" %></li><% end %></ul><% end %>
<% if @audit_due %><p><strong>Records audit due <%= @audit_due %></strong> (AC 120-78B quality control): <%= link_to "start it", new_admin_audit_url %>.</p><% end %>
<p style="color: #64748b; font-size: 12px; margin-top: 20px;">
Based on the records in Simple Aircraft Manager. Turn these emails off on your <%= link_to "profile", edit_profile_url %>.
@@ -16,6 +16,13 @@ Your certificates:
- <%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>: <%= c.kind_label %> <%= c.number %>
<% end %>
<% end %>
<% if @drs_due.any? %>
AD list from FAA DRS needs a refresh:
<% @drs_due.each do |a| %>
- <%= a.tail_number %>: <%= (m = DrsImport.missing_models(a)).any? ? "never imported for #{m.to_sentence}" : "last import #{a.drs_imports.latest_first.first&.created_at&.to_date || 'never'}" %> (<%= aircraft_airworthiness_directives_url(a) %>)
<% end %>
<% end %>
<% if @audit_due %>
Records audit due <%= @audit_due %>: <%= new_admin_audit_url %>
@@ -0,0 +1,16 @@
class CreateDrsImports < ActiveRecord::Migration[8.1]
def change
# Each FAA DRS import: when, by whom, and which of the aircraft's models
# the file covered (the baseline the weekly new-AD search builds on).
create_table :drs_imports do |t|
t.references :aircraft, null: false, foreign_key: { on_delete: :cascade }
t.references :user, foreign_key: { on_delete: :nullify }
t.string :filename
t.json :models_covered
t.integer :ad_count, null: false, default: 0
t.integer :tracked, null: false, default: 0
t.integer :not_applicable, null: false, default: 0
t.timestamps
end
end
end
Generated
+17 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2026_11_09_010000) do
ActiveRecord::Schema[8.1].define(version: 2026_11_11_010000) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -407,6 +407,20 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_09_010000) do
t.index ["document_collection_id"], name: "index_documents_on_document_collection_id"
end
create_table "drs_imports", force: :cascade do |t|
t.integer "aircraft_id", null: false
t.integer "user_id"
t.string "filename"
t.json "models_covered"
t.integer "ad_count", default: 0, null: false
t.integer "tracked", default: 0, null: false
t.integer "not_applicable", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["aircraft_id"], name: "index_drs_imports_on_aircraft_id"
t.index ["user_id"], name: "index_drs_imports_on_user_id"
end
create_table "entry_signatures", force: :cascade do |t|
t.integer "logbook_entry_id", null: false
t.integer "user_id"
@@ -854,6 +868,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_09_010000) do
add_foreign_key "document_collections", "aircraft"
add_foreign_key "documents", "aircraft"
add_foreign_key "documents", "document_collections"
add_foreign_key "drs_imports", "aircraft", on_delete: :cascade
add_foreign_key "drs_imports", "users", on_delete: :nullify
add_foreign_key "entry_signatures", "logbook_entries"
add_foreign_key "entry_signatures", "users", on_delete: :nullify
add_foreign_key "flight_logs", "aircraft"
+4
View File
@@ -8,6 +8,10 @@ The **ADs** tab lists the ADs and bulletins that apply to the aircraft or its co
- **Link existing**: ADs are shared between aircraft; link one already entered for another aircraft.
- **Import from FAA DRS**: export the ADs for the aircraft, engine and propeller from the FAA's Dynamic Regulatory System, preferably as `.xlsx` so each AD keeps its DRS link, and upload it. The preview suggests **Track**, **Not applicable** or **Skip** for each AD and flags superseded ADs, model mismatches, appliance and STC-only ADs, and recurring actions. Nothing is duplicated.
## The AD baseline
The full list of ADs comes from DRS imports; the weekly search (below) adds what's published afterwards. The **AD baseline** panel on the ADs tab shows the last import, which of the aircraft's models it covered, and any model (airframe, engine or propeller) that was never imported. Import again at least once a year to pick up revised ADs: the weekly email and the records audit remind you when it's due.
## Newly published ADs
The app searches the Federal Register weekly for new ADs mentioning the aircraft make and model, the engine and propeller models (and their families, e.g. O-200 for an O-200A), and every other installed component by its maker, type and model, such as magnetos, the ELT and radios. ADs often name a series rather than the exact model, so "Bendix" + "magneto" is searched as well as the model. You can add your own terms (an STC, a part that isn't a component); they're searched as well. Tracking an AD found through a component makes it apply to that component. **Check now** runs it straight away. For each match, **Track** it or **Dismiss** it. Matches are only *possibly* applicable: check the AD's applicability and serial number ranges first.
+29
View File
@@ -27,6 +27,8 @@ class AdImportFlowTest < ActionDispatch::IntegrationTest
}
assert_redirected_to aircraft_airworthiness_directives_path(@aircraft)
assert_equal %w[2008-22-18 2011-10-09], @aircraft.applicable_ads.order(:name).pluck(:name)
drs = @aircraft.drs_imports.sole # the AD baseline is recorded
assert_equal [ [ "150L" ], @owner, "AD Final Rules.csv" ], [ drs.models_covered, drs.user, drs.filename ]
follow_redirect!
assert_select "span", text: "N/A"
# CSV exports carry no links, so the AD number isn't a link
@@ -51,4 +53,31 @@ class AdImportFlowTest < ActionDispatch::IntegrationTest
assert_response :redirect
assert_equal 0, @aircraft.applicable_ads.count
end
test "the AD baseline: missing models, renewal, where the weekly search starts, reminders and the audit" do
create_component(@aircraft, component_type: component_type("Engine"), model: "O-200A")
assert_equal %w[150L O-200A], DrsImport.missing_models(@aircraft)
assert DrsImport.renewal_due?(@aircraft)
assert_equal 2.years.ago.to_date, AdWatch.new(@aircraft).start_date
travel_to Time.zone.local(2026, 11, 2) do
@aircraft.drs_imports.create!(user: @owner, models_covered: [ "150L" ])
@aircraft.drs_imports.create!(user: @owner, models_covered: [ "O-200-A" ]) # matches O-200A
end
assert_empty DrsImport.missing_models(@aircraft)
travel_to Time.zone.local(2026, 11, 20) do
refute DrsImport.renewal_due?(@aircraft)
assert_equal Date.new(2026, 9, 3), AdWatch.new(@aircraft).start_date # last import less 60 days
sign_in @owner
get aircraft_airworthiness_directives_path(@aircraft)
assert_includes response.body, "Last import <strong>2026-11-02</strong>"
end
travel_to Time.zone.local(2027, 12, 1) do # over a year: the first Monday-of-month email and the audit flag it
assert DrsImport.renewal_due?(@aircraft)
assert ReminderMailer.anything_for?(@owner, today: Date.new(2027, 12, 6))
refute ReminderMailer.anything_for?(@owner, today: Date.new(2027, 12, 13))
assert_includes ReminderMailer.weekly(@owner).text_part.body.to_s, "needs a refresh"
refute RecordsAudit.new.auto_items.find { _1.key == "drs" }.passed
end
end
end