diff --git a/lib/towerops/monitoring/check_query.ex b/lib/towerops/monitoring/check_query.ex new file mode 100644 index 00000000..d5fa2c0f --- /dev/null +++ b/lib/towerops/monitoring/check_query.ex @@ -0,0 +1,36 @@ +defmodule Towerops.Monitoring.CheckQuery do + @moduledoc """ + Composable query fragments for `Towerops.Monitoring.Check`. + """ + import Ecto.Query + + alias Towerops.Monitoring.Check + + @doc "Starting point for a Check query." + def base, do: Check + + @doc "Filter to a single organization." + def for_organization(query \\ base(), organization_id) do + where(query, [c], c.organization_id == ^organization_id) + end + + @doc "Filter to a single device." + def for_device(query \\ base(), device_id) do + where(query, [c], c.device_id == ^device_id) + end + + @doc "Filter by check_type." + def of_type(query \\ base(), check_type) do + where(query, [c], c.check_type == ^check_type) + end + + @doc "Filter by enabled flag." + def with_enabled(query \\ base(), enabled) do + where(query, [c], c.enabled == ^enabled) + end + + @doc "Default ordering by check name." + def order_by_name(query \\ base()) do + order_by(query, [c], asc: c.name) + end +end diff --git a/lib/towerops/sites/site_query.ex b/lib/towerops/sites/site_query.ex new file mode 100644 index 00000000..56223e88 --- /dev/null +++ b/lib/towerops/sites/site_query.ex @@ -0,0 +1,26 @@ +defmodule Towerops.Sites.SiteQuery do + @moduledoc """ + Composable query fragments for `Towerops.Sites.Site`. + """ + import Ecto.Query + + alias Towerops.Sites.Site + + @doc "Starting point for a Site query." + def base, do: Site + + @doc "Filter to a single organization." + def for_organization(query \\ base(), organization_id) do + where(query, [s], s.organization_id == ^organization_id) + end + + @doc "Filter to root sites (no parent)." + def roots(query \\ base()) do + where(query, [s], is_nil(s.parent_site_id)) + end + + @doc "Default display ordering by display_order then name." + def order_by_display(query \\ base()) do + order_by(query, [s], asc: s.display_order, asc: s.name) + end +end