Skip to content

Commit

Permalink
fix(db): Fix json marshaling and deploy time in overview cache (#1772)
Browse files Browse the repository at this point in the history
- Use protojson for marshaling api objects
- Fix Deploy time in overview cache
- ForceRecalculate should not write anything when overview cache is
already empty
  • Loading branch information
AminSlk authored Jul 17, 2024
1 parent e61ce42 commit 32d4aab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
10 changes: 6 additions & 4 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/freiheit-com/kuberpult/pkg/valid"
"google.golang.org/protobuf/encoding/protojson"

"github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/event"
Expand Down Expand Up @@ -1706,10 +1707,11 @@ func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deploymen

span.SetTag("query", insertQuery)
nullVersion := NewNullInt(deployment.Version)
createdTime := time.Now().UTC()
_, err = tx.Exec(
insertQuery,
previousEslVersion+1,
time.Now().UTC(),
createdTime,
nullVersion,
deployment.App,
deployment.Env,
Expand All @@ -1719,7 +1721,7 @@ func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deploymen
if err != nil {
return fmt.Errorf("could not write deployment into DB. Error: %w\n", err)
}
err = h.UpdateOverviewDeployment(ctx, tx, deployment)
err = h.UpdateOverviewDeployment(ctx, tx, deployment, createdTime)
if err != nil {
return fmt.Errorf("could not update overview table. Error: %w\n", err)
}
Expand Down Expand Up @@ -4406,7 +4408,7 @@ func (h *DBHandler) ReadLatestOverviewCache(ctx context.Context, transaction *sq
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "",
}
err = json.Unmarshal([]byte(row.Json), result)
err = protojson.Unmarshal([]byte(row.Json), result)
if err != nil {
return nil, err
}
Expand All @@ -4429,7 +4431,7 @@ func (h *DBHandler) WriteOverviewCache(ctx context.Context, transaction *sql.Tx,
"INSERT INTO overview_cache (timestamp, Json) VALUES (?, ?);",
)
span.SetTag("query", insertQuery)
jsonResponse, err := json.Marshal(overviewResponse)
jsonResponse, err := protojson.Marshal(overviewResponse)
if err != nil {
return fmt.Errorf("could not marshal overview json data: %w", err)
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/db/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"context"
"database/sql"
"fmt"
"regexp"
"time"

"github.com/freiheit-com/kuberpult/pkg/api/v1"
"google.golang.org/protobuf/types/known/timestamppb"
"regexp"
)

func (h *DBHandler) UpdateOverviewTeamLock(ctx context.Context, transaction *sql.Tx, teamLock TeamLock) error {
Expand Down Expand Up @@ -96,7 +98,7 @@ func (h *DBHandler) UpdateOverviewEnvironmentLock(ctx context.Context, transacti
return nil
}

func (h *DBHandler) UpdateOverviewDeployment(ctx context.Context, transaction *sql.Tx, deployment Deployment) error {
func (h *DBHandler) UpdateOverviewDeployment(ctx context.Context, transaction *sql.Tx, deployment Deployment, createdTime time.Time) error {
latestOverview, err := h.ReadLatestOverviewCache(ctx, transaction)
if err != nil {
return err
Expand All @@ -118,7 +120,7 @@ func (h *DBHandler) UpdateOverviewDeployment(ctx context.Context, transaction *s
appInEnv.Version = uint64(*deployment.Version)
}
appInEnv.DeploymentMetaData.DeployAuthor = deployment.Metadata.DeployedByEmail
appInEnv.DeploymentMetaData.DeployTime = fmt.Sprintf("%d", deployment.Created.Unix())
appInEnv.DeploymentMetaData.DeployTime = fmt.Sprintf("%d", createdTime.Unix())
app := getApplicationByName(latestOverview.Applications, deployment.App)
app.Warnings = CalculateWarnings(ctx, app.Name, latestOverview.EnvironmentGroups)

Expand Down Expand Up @@ -235,14 +237,21 @@ func (h *DBHandler) UpdateOverviewRelease(ctx context.Context, transaction *sql.
}

func (h *DBHandler) ForceOverviewRecalculation(ctx context.Context, transaction *sql.Tx) error {
latestOverview, err := h.ReadLatestOverviewCache(ctx, transaction)
if err != nil {
return err
}
if h.IsOverviewEmpty(latestOverview) {
return nil
}
emptyOverview := &api.GetOverviewResponse{
Applications: map[string]*api.Application{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "",
Branch: "",
ManifestRepoUrl: "",
}
err := h.WriteOverviewCache(ctx, transaction, emptyOverview)
err = h.WriteOverviewCache(ctx, transaction, emptyOverview)
if err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/db/overview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func makeTestStartingOverview() *api.GetOverviewResponse {
},
},
Team: "team-123",
Warnings: []*api.Warning{
{
WarningType: &api.Warning_UnusualDeploymentOrder{
UnusualDeploymentOrder: &api.UnusualDeploymentOrder{
UpstreamEnvironment: "staging",
ThisVersion: 12,
ThisEnvironment: "development",
},
},
},
},
},
},
GitRevision: "0",
Expand Down Expand Up @@ -515,7 +526,7 @@ func TestUpdateOverviewDeployment(t *testing.T) {
if err != nil {
return err
}
err = dbHandler.UpdateOverviewDeployment(ctx, transaction, tc.NewDeployment)
err = dbHandler.UpdateOverviewDeployment(ctx, transaction, tc.NewDeployment, tc.NewDeployment.Created)
if err != nil {
if diff := cmp.Diff(tc.ExpectedError, err, cmpopts.EquateErrors()); diff != "" {
return fmt.Errorf("mismatch between errors (-want +got):\n%s", diff)
Expand All @@ -532,7 +543,6 @@ func TestUpdateOverviewDeployment(t *testing.T) {
}
return nil
})

if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 32d4aab

Please sign in to comment.