···5566 alias Sower.Repo
77 alias Sower.Orchestration.Agent
88+ alias Sower.Orchestration.Deployment
89 alias Sower.Orchestration.DeploymentPubSub
9101011 import Ecto.Query, warn: false
···2324 """
2425 def list_agents do
2526 Repo.all(Agent)
2727+ end
2828+2929+ @doc """
3030+ Returns the list of agents with their latest deployment preloaded.
3131+3232+ ## Examples
3333+3434+ iex> list_agents_with_latest_deployment()
3535+ [%Agent{latest_deployment: %Deployment{} | nil}, ...]
3636+3737+ """
3838+ def list_agents_with_latest_deployment do
3939+ latest_deployment_query =
4040+ from(d in Deployment,
4141+ where: d.agent_id == parent_as(:agent).id,
4242+ order_by: [desc: d.inserted_at],
4343+ limit: 1
4444+ )
4545+4646+ from(a in Agent,
4747+ as: :agent,
4848+ left_lateral_join: d in subquery(latest_deployment_query),
4949+ on: true,
5050+ select: %{a | latest_deployment: d}
5151+ )
5252+ |> Repo.all()
2653 end
27542855 def get_agent(
···277304 end
278305279306 alias Sower.Orchestration.Subscription
280280- alias Sower.Orchestration.Deployment
281307282308 @doc """
283309 List deployments for a specific agent, ordered by most recent first.
···599625 |> Subscription.changeset(attrs)
600626 end
601627602602- alias Sower.Orchestration.Deployment
603628 alias Sower.Seed
604629605630 def match_seed(%Subscription{} = subscription) do
+2
apps/sower/lib/sower/orchestration/agent.ex
···1515 has_many :deployments, Sower.Orchestration.Deployment
1616 has_many :agent_seed_generations, Sower.Orchestration.AgentSeedGeneration
17171818+ field :latest_deployment, :any, virtual: true
1919+1820 timestamps()
1921 end
2022