Skip to content

Commit

Permalink
[db] Use UnixMilli() instead of Unix() for createdAt / updatedAt
Browse files Browse the repository at this point in the history
The increased precision avoids the need of time.Sleep() in tests
  • Loading branch information
scthi committed Nov 16, 2023
1 parent 0130d2b commit 3a9038d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
28 changes: 14 additions & 14 deletions pkg/hub/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *client) SavePlugins(ctx context.Context, cluster string, plugins []plug
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, p := range plugins {
p.ID = fmt.Sprintf("/cluster/%s/type/%s/name/%s", cluster, p.Type, p.Name)
Expand Down Expand Up @@ -203,7 +203,7 @@ func (c *client) SaveNamespaces(ctx context.Context, cluster string, namespaces
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, n := range namespaces {
namespace := Namespace{
Expand Down Expand Up @@ -236,14 +236,14 @@ func (c *client) SaveCRDs(ctx context.Context, crds []kubernetes.CRD) error {

var models []mongo.WriteModel
updatedAtTime := time.Now()
updatedAt := updatedAtTime.Unix()
updatedAt := updatedAtTime.UnixMilli()

for _, crd := range crds {
crd.UpdatedAt = updatedAt
models = append(models, mongo.NewReplaceOneModel().SetFilter(bson.D{{Key: "_id", Value: crd.ID}}).SetReplacement(crd).SetUpsert(true))
}

err := c.save(ctx, "crds", models, "", updatedAtTime.Add(time.Duration(-72*time.Hour)).Unix())
err := c.save(ctx, "crds", models, "", updatedAtTime.Add(time.Duration(-72*time.Hour)).UnixMilli())
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
Expand All @@ -263,7 +263,7 @@ func (c *client) SaveApplications(ctx context.Context, cluster string, applicati
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, a := range applications {
a.UpdatedAt = updatedAt
Expand All @@ -285,7 +285,7 @@ func (c *client) SaveApplication(ctx context.Context, application *applicationv1
defer span.End()

upsert := true
application.UpdatedAt = time.Now().Unix()
application.UpdatedAt = time.Now().UnixMilli()

_, err := c.coll(ctx, "applications").ReplaceOne(ctx, bson.D{{Key: "_id", Value: application.ID}}, application, &options.ReplaceOptions{Upsert: &upsert})
if err != nil {
Expand All @@ -307,7 +307,7 @@ func (c *client) SaveDashboards(ctx context.Context, cluster string, dashboards
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, d := range dashboards {
d.UpdatedAt = updatedAt
Expand All @@ -334,7 +334,7 @@ func (c *client) SaveTeams(ctx context.Context, cluster string, teams []teamv1.T
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, t := range teams {
t.UpdatedAt = updatedAt
Expand All @@ -356,7 +356,7 @@ func (c *client) SaveTeam(ctx context.Context, team *teamv1.TeamSpec) error {
defer span.End()

upsert := true
team.UpdatedAt = time.Now().Unix()
team.UpdatedAt = time.Now().UnixMilli()

_, err := c.coll(ctx, "teams").ReplaceOne(ctx, bson.D{{Key: "_id", Value: team.ID}}, team, &options.ReplaceOptions{Upsert: &upsert})
if err != nil {
Expand All @@ -378,7 +378,7 @@ func (c *client) SaveUsers(ctx context.Context, cluster string, users []userv1.U
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, u := range users {
u.UpdatedAt = updatedAt
Expand All @@ -400,7 +400,7 @@ func (c *client) SaveUser(ctx context.Context, user *userv1.UserSpec) error {
defer span.End()

upsert := true
user.UpdatedAt = time.Now().Unix()
user.UpdatedAt = time.Now().UnixMilli()

_, err := c.coll(ctx, "users").ReplaceOne(ctx, bson.D{{Key: "_id", Value: user.ID}}, user, &options.ReplaceOptions{Upsert: &upsert})
if err != nil {
Expand All @@ -418,7 +418,7 @@ func (c *client) SaveTags(ctx context.Context, applications []applicationv1.Appl

var models []mongo.WriteModel
updatedAtTime := time.Now()
updatedAt := updatedAtTime.Unix()
updatedAt := updatedAtTime.UnixMilli()

for _, a := range applications {
for _, t := range a.Tags {
Expand All @@ -436,7 +436,7 @@ func (c *client) SaveTags(ctx context.Context, applications []applicationv1.Appl
return nil
}

err := c.save(ctx, "tags", models, "", updatedAtTime.Add(time.Duration(-72*time.Hour)).Unix())
err := c.save(ctx, "tags", models, "", updatedAtTime.Add(time.Duration(-72*time.Hour)).UnixMilli())
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
Expand All @@ -452,7 +452,7 @@ func (c *client) SaveTopology(ctx context.Context, cluster string, applications
defer span.End()

var models []mongo.WriteModel
updatedAt := time.Now().Unix()
updatedAt := time.Now().UnixMilli()

for _, a := range applications {
for _, dependency := range a.Topology.Dependencies {
Expand Down
23 changes: 1 addition & 22 deletions pkg/hub/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/kobsio/kobs/pkg/cluster/kubernetes"
applicationv1 "github.com/kobsio/kobs/pkg/cluster/kubernetes/apis/application/v1"
Expand Down Expand Up @@ -80,8 +79,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedPlugins1))

time.Sleep(1 * time.Second)

err = c.SavePlugins(ctx(t), "test-cluster", plugins[0:1])
require.NoError(t, err)

Expand All @@ -99,8 +96,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedNamespaces1))

time.Sleep(1 * time.Second)

namespaces2 := []string{"default"}
err = c.SaveNamespaces(ctx(t), "test-cluster", namespaces2)
require.NoError(t, err)
Expand All @@ -123,8 +118,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedCRDs1))

time.Sleep(1 * time.Second)

err = c.SaveCRDs(ctx(t), crds1[0:1])
require.NoError(t, err)

Expand Down Expand Up @@ -153,8 +146,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedApplications1))

time.Sleep(1 * time.Second)

err = c.SaveApplications(ctx(t), "test-cluster", applications[0:1])
require.NoError(t, err)

Expand All @@ -181,8 +172,6 @@ func TestDB(t *testing.T) {
require.Equal(t, application.Namespace, storedApplication1.Namespace)
require.Equal(t, application.Name, storedApplication1.Name)

time.Sleep(1 * time.Second)

application.Name = "application2"
err = c.SaveApplication(ctx(t), &application)
require.NoError(t, err)
Expand Down Expand Up @@ -219,8 +208,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, len(storedDashboards2))

time.Sleep(1 * time.Second)

err = c.SaveDashboards(ctx(t), "test-cluster", dashboards[0:1])
require.NoError(t, err)

Expand Down Expand Up @@ -249,8 +236,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedTeams1))

time.Sleep(1 * time.Second)

err = c.SaveTeams(ctx(t), "test-cluster", teams[0:1])
require.NoError(t, err)

Expand All @@ -277,8 +262,6 @@ func TestDB(t *testing.T) {
require.Equal(t, team.Namespace, storedTeam1.Namespace)
require.Equal(t, team.Name, storedTeam1.Name)

time.Sleep(1 * time.Second)

team.Name = "team2"
err = c.SaveTeam(ctx(t), &team)
require.NoError(t, err)
Expand Down Expand Up @@ -311,8 +294,6 @@ func TestDB(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(storedUsers1))

time.Sleep(1 * time.Second)

err = c.SaveUsers(ctx(t), "test-cluster", users[0:1])
require.NoError(t, err)

Expand All @@ -339,8 +320,6 @@ func TestDB(t *testing.T) {
require.Equal(t, user.Namespace, storedUser1.Namespace)
require.Equal(t, user.Name, storedUser1.Name)

time.Sleep(1 * time.Second)

user.Name = "user2"
err = c.SaveUser(ctx(t), &user)
require.NoError(t, err)
Expand Down Expand Up @@ -452,9 +431,9 @@ func TestDB(t *testing.T) {
{ID: "cluster/test-cluster1/namespace/default/application1", Cluster: "test-cluster1", Namespace: "default", Name: "application1", Teams: []string{"team1"}, Tags: []string{"monitoring", "observability"}, Topology: applicationv1.Topology{External: false}},
{ID: "cluster/test-cluster1/namespace/default/application2", Cluster: "test-cluster1", Namespace: "default", Name: "application2", Teams: []string{"team1", "team2"}, Tags: []string{"monitoring", "observability"}, Topology: applicationv1.Topology{External: false}},
{ID: "cluster/test-cluster1/namespace/default/application3", Cluster: "test-cluster1", Namespace: "default", Name: "application3", Teams: []string{"team3"}, Tags: []string{}, Topology: applicationv1.Topology{External: true}},
{ID: "cluster/test-cluster1/namespace/default/application4", Cluster: "test-cluster2", Namespace: "default", Name: "application4", Teams: []string{"team1", "team2", "team3"}, Tags: []string{"monitoring", "observability"}, Topology: applicationv1.Topology{External: false}},
}
applications2 := []applicationv1.ApplicationSpec{
{ID: "cluster/test-cluster2/namespace/default/application4", Cluster: "test-cluster2", Namespace: "default", Name: "application4", Teams: []string{"team1", "team2", "team3"}, Tags: []string{"monitoring", "observability"}, Topology: applicationv1.Topology{External: false}},
{ID: "cluster/test-cluster2/namespace/default/application5", Cluster: "test-cluster2", Namespace: "default", Name: "application5", Teams: []string{}, Tags: []string{"monitoring", "observability"}, Topology: applicationv1.Topology{External: false}},
{ID: "cluster/test-cluster2/namespace/kube-system/application6", Cluster: "test-cluster2", Namespace: "kube-system", Name: "application6", Teams: []string{"team1"}, Tags: []string{"core", "provider"}, Topology: applicationv1.Topology{External: false}},
{ID: "cluster/test-cluster2/namespace/kube-system/application7", Cluster: "test-cluster2", Namespace: "kube-system", Name: "application7", Teams: []string{}, Tags: []string{"core", "provider"}, Topology: applicationv1.Topology{External: false}},
Expand Down

0 comments on commit 3a9038d

Please sign in to comment.