Skip to content

Commit

Permalink
add random utilization data for mem and cpu
Browse files Browse the repository at this point in the history
Co-authored-by: Johnny Horvi <[email protected]>
  • Loading branch information
frodesundby and jhrv committed Mar 13, 2024
1 parent 5f2ad0a commit 38be623
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cmd/setup_local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 38be623

Please sign in to comment.