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 %>