diff --git a/cmd/setup_local/main.go b/cmd/setup_local/main.go index 46391a3e2..39b4f8607 100644 --- a/cmd/setup_local/main.go +++ b/cmd/setup_local/main.go @@ -16,6 +16,7 @@ import ( "github.com/jackc/pgx/v5/pgtype" "github.com/nais/api/internal/database" "github.com/nais/api/internal/database/gensql" + "github.com/nais/api/internal/graph/model" "github.com/nais/api/internal/logger" "github.com/nais/api/internal/slug" "github.com/nais/api/internal/usersync" @@ -28,6 +29,11 @@ import ( "google.golang.org/grpc/status" ) +type ( + // utilizationMapForEnv is a map of team -> app -> time.Time -> *model.ResourceUtilization + utilizationMapForEnv map[slug.Slug]map[string]map[time.Time]*model.ResourceUtilization +) + type seedConfig struct { DatabaseURL string `env:"DATABASE_URL,default=postgres://api:api@localhost:3002/api?sslmode=disable"` Domain string `env:"TENANT_DOMAIN,default=example.com"` @@ -233,6 +239,11 @@ func run(ctx context.Context, cfg *seedConfig, log logrus.FieldLogger) error { return err } } + dbtx.ResourceUtilizationUpsert(ctx, generateUtilizationData("dev", "devteam", "devapp", time.Now().Add(-24*time.Hour*7), time.Now())).Exec(func(i int, err error) { + if err != nil { + log.Errorf("error updating resource utilization for team %s: %v", devteam.Slug, err) + } + }) err = seedVulnerabilities(ctx, *cfg, dbtx, devteam, log) if err != nil { @@ -283,6 +294,37 @@ func run(ctx context.Context, cfg *seedConfig, log logrus.FieldLogger) error { return nil } +func generateUtilizationData(env, team, app string, start, end time.Time) []gensql.ResourceUtilizationUpsertParams { + ret := make([]gensql.ResourceUtilizationUpsertParams, 0) + current := start + for current.Before(end) { + + pgTs := &pgtype.Timestamptz{} + _ = pgTs.Scan(current) + + ret = append(ret, gensql.ResourceUtilizationUpsertParams{ + Timestamp: *pgTs, + Environment: env, + TeamSlug: slug.Slug(team), + App: app, + ResourceType: gensql.ResourceTypeCpu, + Usage: rand.Float64() * 100, + Request: 50.0, + }) + ret = append(ret, gensql.ResourceUtilizationUpsertParams{ + Timestamp: *pgTs, + Environment: env, + TeamSlug: slug.Slug(team), + App: app, + ResourceType: gensql.ResourceTypeMemory, + Usage: rand.Float64() * 10, + Request: 5.0, + }) + current = current.Add(time.Hour) + } + return ret +} + func seedVulnerabilities(ctx context.Context, cfg seedConfig, dbtx database.Database, team *database.Team, log logrus.FieldLogger) error { numbOfErrors := 0 for j := 0; j < *cfg.VulnSeed.NumVulnAppsForTeam; j++ {