Skip to content

Commit

Permalink
Merge pull request #21 from nais/sqlinstances_refactor
Browse files Browse the repository at this point in the history
Sqlinstances refactor
  • Loading branch information
christeredvartsen authored Apr 10, 2024
2 parents af6b30e + 8449f0a commit 9afd5c7
Show file tree
Hide file tree
Showing 19 changed files with 947 additions and 420 deletions.
5 changes: 0 additions & 5 deletions data/k8s/superprod/devteam/sqlinstance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ metadata:
team: nais
name: contests
namespace: nais-system
ownerReferences:
- apiVersion: nais.io/v1
kind: Application
name: contests
uid: 6957e4dd-2997-46b3-8f6f-b6d1e0606b1d
spec:
databaseVersion: POSTGRES_14
instanceType: CLOUD_SQL_INSTANCE
Expand Down
5 changes: 0 additions & 5 deletions data/k8s/superprod/devteam/sqlinstance_update_failed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ metadata:
team: nais
name: contests-failing
namespace: nais-system
ownerReferences:
- apiVersion: nais.io/v1
kind: Application
name: contests-failing
uid: 6957e4dd-2997-46b3-8f6f-b6d1e0606b1d
spec:
databaseVersion: POSTGRES_14
instanceType: CLOUD_SQL_INSTANCE
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {

userSyncRuns := usersync.NewRunsHandler(cfg.UserSync.RunsToPersist)
resourceUsageClient := resourceusage.NewClient(cfg.K8s.AllClusterNames(), db, log)
sqlinstanceMetrics, err := sqlinstance.NewMetrics(ctx, log)
sqlInstanceClient, err := sqlinstance.NewClient(ctx, k8sClient.Informers(), log)
if err != nil {
return err
return fmt.Errorf("create sql instance client: %w", err)
}

resolver := graph.NewResolver(
Expand All @@ -180,7 +180,7 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
userSyncRuns,
pubsubTopic,
log,
sqlinstanceMetrics,
sqlInstanceClient,
)

graphHandler, err := graph.NewHandler(gengql.Config{
Expand Down
16 changes: 16 additions & 0 deletions internal/database/cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"time"

"github.com/jackc/pgx/v5/pgtype"
"github.com/nais/api/internal/database/gensql"
Expand All @@ -17,6 +18,7 @@ type CostRepo interface {
MonthlyCostForApp(ctx context.Context, teamSlug slug.Slug, app string, environment string) ([]*gensql.MonthlyCostForAppRow, error)
MonthlyCostForTeam(ctx context.Context, teamSlug slug.Slug) ([]*gensql.MonthlyCostForTeamRow, error)
CostForSqlInstance(ctx context.Context, fromDate, toDate pgtype.Date, teamSlug slug.Slug, appName, environment string) (float32, error)
CurrentSqlInstancesCostForTeam(ctx context.Context, teamSlug slug.Slug) (float32, error)
}

var _ CostRepo = (*database)(nil)
Expand Down Expand Up @@ -82,3 +84,17 @@ func (d *database) CostForSqlInstance(ctx context.Context, fromDate, toDate pgty

return cost, nil
}

func (d *database) CurrentSqlInstancesCostForTeam(ctx context.Context, teamSlug slug.Slug) (float32, error) {
now := time.Now()
var from, to pgtype.Date

_ = to.Scan(now)
_ = from.Scan(now.AddDate(0, 0, -32)) // we don't have cost for today or yesterday

return d.querier.CurrentSqlInstancesCostForTeam(ctx, gensql.CurrentSqlInstancesCostForTeamParams{
FromDate: from,
ToDate: to,
TeamSlug: teamSlug,
})
}
25 changes: 25 additions & 0 deletions internal/database/gensql/cost.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions internal/database/gensql/mock_querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/database/gensql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions internal/database/mock_database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion internal/database/queries/cost.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ WHERE
AND app = @app_name
AND date >= @from_date
AND date <= @to_date
AND environment = @environment::text;
AND environment = @environment::text;

-- name: CurrentSqlInstancesCostForTeam :one
SELECT
COALESCE(SUM(daily_cost), 0)::real
FROM
cost
WHERE
team_slug = @team_slug
AND cost_type = 'Cloud SQL'
AND date >= @from_date
AND date <= @to_date;

Loading

0 comments on commit 9afd5c7

Please sign in to comment.