Applies the 11 recommendations from the codebase optimization review. Module decomposition: - Extract Towerops.Topology.InferenceEngine: pulls device-role inference (capability rules, vendor matching, evidence merging) out of the topology context. Topology now delegates merge_confidence/2, infer_role_from_capabilities/1, and infer_device_role/1. - Extract Towerops.Accounts.TOTP: TOTP secret/QR generation, multi-device verification, recovery code lifecycle, and sudo MFA logic. Accounts delegates the public TOTP API for backwards compatibility. - Extract Towerops.Accounts.Sessions: browser session create/list/touch/ revoke/anonymize/expire. Accounts delegates the public session API. - Extract Towerops.Snmp.Queries: time-series read paths (sensor readings, interface stats, latest-per-id batches). - Extract Towerops.Snmp.Monitoring: time-series write paths (sensor/interface/processor reading inserts, batch inserts via Repo.insert_all). Snmp.ex shrinks from 2985 to 2742 lines. DRY refactoring: - Add per-schema Query modules (Devices.DeviceQuery, Alerts.AlertQuery) with composable for_organization/for_site/with_status/etc filters; refactor list_site_devices, count_organization_devices, count_site_devices, count_site_devices_down, count_organization_alerts to compose them. - Introduce create_discovered_checks/6 helper in Snmp; collapses 4 nearly identical Enum.reduce blocks for sensors/interfaces/processors/storage. - Unify MAC and ARP evidence collectors behind collect_lookup_evidence/3 (lldp/cdp were already factored). Pattern matching: - Replace cond block in infer_role_from_capabilities/1 with a declarative @role_rules table consulted via Enum.find_value (Elixir 1.19 forbids `in` with runtime lists in guards, so multi-clause functions weren't an option). - Replace inline `if since` in get_sensor_readings/2 with a maybe_filter_since pipe helper. Performance: - Replace Enum.each + Repo.insert per evidence row in upsert_link with a single Repo.insert_all batch (truncates :observed_at to seconds). Human-centric: - Decompose build_device_lookup/1 into fetch_lookup_devices, map_ips_to_ids, map_names_to_ids, map_macs_to_ids. - Adopt Towerops.Result.map/2 in Snmp.Client.do_get_multiple_sequential (kept narrow; `with` is preferred over Result.and_then for chained operations and is already used widely). CLAUDE.md updates: - Replace stale main→staging / production-branch deployment notes with the current flow: PRs deploy to Dokku staging, push to main runs the ExUnit gate then builds an image; argocd-image-updater rolls production. - Refresh data-model diagram and references from "Equipment" to "Device" (post equipment→devices rename migration). - Correct stale architecture facts: Hammer → in-tree Towerops.RateLimit, Rust NIF → C NIF (libnetsnmp), gitlab-registry → forgejo-registry, expand the Oban queue and cron lists, list actual implemented Ecto types. All 10,228 tests pass.
48 lines
1.3 KiB
Elixir
48 lines
1.3 KiB
Elixir
defmodule Towerops.Alerts.AlertQuery do
|
|
@moduledoc """
|
|
Composable query fragments for `Towerops.Alerts.Alert`.
|
|
|
|
## Example
|
|
|
|
AlertQuery.base()
|
|
|> AlertQuery.for_organization(org_id)
|
|
|> AlertQuery.unresolved()
|
|
|> Repo.all()
|
|
"""
|
|
import Ecto.Query
|
|
|
|
alias Towerops.Alerts.Alert
|
|
|
|
@doc "Starting point for an Alert query."
|
|
def base, do: Alert
|
|
|
|
@doc "Filter to a single organization."
|
|
def for_organization(query \\ base(), organization_id) do
|
|
where(query, [a], a.organization_id == ^organization_id)
|
|
end
|
|
|
|
@doc "Filter to a single device."
|
|
def for_device(query \\ base(), device_id) do
|
|
where(query, [a], a.device_id == ^device_id)
|
|
end
|
|
|
|
@doc "Filter by alert_type."
|
|
def of_type(query \\ base(), alert_type) do
|
|
where(query, [a], a.alert_type == ^alert_type)
|
|
end
|
|
|
|
@doc "Only unresolved alerts (resolved_at IS NULL)."
|
|
def unresolved(query \\ base()) do
|
|
where(query, [a], is_nil(a.resolved_at))
|
|
end
|
|
|
|
@doc "Only resolved alerts (resolved_at IS NOT NULL)."
|
|
def resolved(query \\ base()) do
|
|
where(query, [a], not is_nil(a.resolved_at))
|
|
end
|
|
|
|
@doc "Filter by severity (e.g. :critical, :warning, :info)."
|
|
def with_severity(query \\ base(), severity) do
|
|
where(query, [a], a.severity == ^severity)
|
|
end
|
|
end
|