From 2ff2ce2cb36cb6bc2b81cc4ccde7fe41540964a0 Mon Sep 17 00:00:00 2001 From: Wes Date: Mon, 8 Apr 2024 10:34:52 -0700 Subject: [PATCH] fix: add query for getting deployments with replicas --- backend/controller/console.go | 2 +- backend/controller/dal/dal.go | 20 ++++++++++++ backend/controller/sql/models.go | 3 ++ backend/controller/sql/querier.go | 1 + backend/controller/sql/queries.sql | 7 +++++ backend/controller/sql/queries.sql.go | 44 +++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) diff --git a/backend/controller/console.go b/backend/controller/console.go index eeb63b49f..0f2371345 100644 --- a/backend/controller/console.go +++ b/backend/controller/console.go @@ -41,7 +41,7 @@ func (*ConsoleService) Ping(context.Context, *connect.Request[ftlv1.PingRequest] } func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pbconsole.GetModulesRequest]) (*connect.Response[pbconsole.GetModulesResponse], error) { - deployments, err := c.dal.GetActiveDeployments(ctx) + deployments, err := c.dal.GetDeploymentsWithMinReplicas(ctx) if err != nil { return nil, err } diff --git a/backend/controller/dal/dal.go b/backend/controller/dal/dal.go index cf8273c13..54da43554 100644 --- a/backend/controller/dal/dal.go +++ b/backend/controller/dal/dal.go @@ -741,6 +741,26 @@ func (d *DAL) GetActiveDeployments(ctx context.Context) ([]Deployment, error) { }) } +func (d *DAL) GetDeploymentsWithMinReplicas(ctx context.Context) ([]Deployment, error) { + rows, err := d.db.GetDeploymentsWithMinReplicas(ctx) + if err != nil { + if isNotFound(err) { + return nil, nil + } + return nil, translatePGError(err) + } + return slices.MapErr(rows, func(in sql.GetDeploymentsWithMinReplicasRow) (Deployment, error) { + return Deployment{ + Key: in.Deployment.Key, + Module: in.ModuleName, + Language: in.Language, + MinReplicas: int(in.Deployment.MinReplicas), + Schema: in.Deployment.Schema, + CreatedAt: in.Deployment.CreatedAt, + }, nil + }) +} + func (d *DAL) GetActiveDeploymentSchemas(ctx context.Context) ([]*schema.Module, error) { rows, err := d.db.GetActiveDeploymentSchemas(ctx) if err != nil { diff --git a/backend/controller/sql/models.go b/backend/controller/sql/models.go index 9ea1530af..17f1b107f 100644 --- a/backend/controller/sql/models.go +++ b/backend/controller/sql/models.go @@ -271,6 +271,7 @@ type Runner struct { type Topic struct { ID int64 + Key interface{} CreatedAt time.Time ModuleID int64 Name string @@ -286,6 +287,7 @@ type TopicEvent struct { type TopicSubscriber struct { ID int64 + Key interface{} CreatedAt time.Time TopicSubscriptionsID int64 DeploymentID int64 @@ -294,6 +296,7 @@ type TopicSubscriber struct { type TopicSubscription struct { ID int64 + Key interface{} CreatedAt time.Time TopicID int64 Name string diff --git a/backend/controller/sql/querier.go b/backend/controller/sql/querier.go index 7cf643bef..ca09f303d 100644 --- a/backend/controller/sql/querier.go +++ b/backend/controller/sql/querier.go @@ -37,6 +37,7 @@ type Querier interface { GetDeploymentsNeedingReconciliation(ctx context.Context) ([]GetDeploymentsNeedingReconciliationRow, error) // Get all deployments that have artefacts matching the given digests. GetDeploymentsWithArtefacts(ctx context.Context, digests [][]byte, schema []byte, count int64) ([]GetDeploymentsWithArtefactsRow, error) + GetDeploymentsWithMinReplicas(ctx context.Context) ([]GetDeploymentsWithMinReplicasRow, error) GetExistingDeploymentForModule(ctx context.Context, name string) (GetExistingDeploymentForModuleRow, error) GetIdleRunners(ctx context.Context, labels []byte, limit int64) ([]Runner, error) // Get the runner endpoints corresponding to the given ingress route. diff --git a/backend/controller/sql/queries.sql b/backend/controller/sql/queries.sql index 08059d80a..4e0a32bb6 100644 --- a/backend/controller/sql/queries.sql +++ b/backend/controller/sql/queries.sql @@ -152,6 +152,13 @@ WHERE min_replicas > 0 AND r.state = 'assigned' GROUP BY d.id, m.name, m.language HAVING COUNT(r.id) > 0; +-- name: GetDeploymentsWithMinReplicas :many +SELECT sqlc.embed(d), m.name AS module_name, m.language +FROM deployments d + INNER JOIN modules m on d.module_id = m.id +WHERE min_replicas > 0 +ORDER BY d.key; + -- name: GetActiveDeploymentSchemas :many SELECT key, schema FROM deployments WHERE min_replicas > 0; diff --git a/backend/controller/sql/queries.sql.go b/backend/controller/sql/queries.sql.go index 0d8b5de9a..fd0226eef 100644 --- a/backend/controller/sql/queries.sql.go +++ b/backend/controller/sql/queries.sql.go @@ -594,6 +594,50 @@ func (q *Queries) GetDeploymentsWithArtefacts(ctx context.Context, digests [][]b return items, nil } +const getDeploymentsWithMinReplicas = `-- name: GetDeploymentsWithMinReplicas :many +SELECT d.id, d.created_at, d.module_id, d.key, d.schema, d.labels, d.min_replicas, m.name AS module_name, m.language +FROM deployments d + INNER JOIN modules m on d.module_id = m.id +WHERE min_replicas > 0 +ORDER BY d.key +` + +type GetDeploymentsWithMinReplicasRow struct { + Deployment Deployment + ModuleName string + Language string +} + +func (q *Queries) GetDeploymentsWithMinReplicas(ctx context.Context) ([]GetDeploymentsWithMinReplicasRow, error) { + rows, err := q.db.Query(ctx, getDeploymentsWithMinReplicas) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetDeploymentsWithMinReplicasRow + for rows.Next() { + var i GetDeploymentsWithMinReplicasRow + if err := rows.Scan( + &i.Deployment.ID, + &i.Deployment.CreatedAt, + &i.Deployment.ModuleID, + &i.Deployment.Key, + &i.Deployment.Schema, + &i.Deployment.Labels, + &i.Deployment.MinReplicas, + &i.ModuleName, + &i.Language, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getExistingDeploymentForModule = `-- name: GetExistingDeploymentForModule :one SELECT d.id, created_at, module_id, key, schema, labels, min_replicas, m.id, language, name FROM deployments d