Skip to content

Commit

Permalink
Rolling back changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Dec 8, 2023
1 parent 01cccd0 commit 55efbbb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 74 deletions.
75 changes: 33 additions & 42 deletions api/pkg/di/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import (
type Container struct {
projectID string
db *gorm.DB
yugaByteDB *gorm.DB
dedicatedDB *gorm.DB
version string
app *fiber.App
eventDispatcher *services.EventDispatcher
Expand Down Expand Up @@ -209,47 +209,38 @@ func (container *Container) GormLogger() gormLogger.Interface {
)
}

// YugaByteDB creates an instance of gorm.DB if it has not been created already
func (container *Container) YugaByteDB() (db *gorm.DB) {
// DedicatedDB creates an instance of gorm.DB if it has not been created already
func (container *Container) DedicatedDB() (db *gorm.DB) {
container.logger.Debug(fmt.Sprintf("creating %T", db))
if container.yugaByteDB != nil {
return container.yugaByteDB
if container.dedicatedDB != nil {
return container.dedicatedDB
}
//
//config := &gorm.Config{}
//if isLocal() {
// config = &gorm.Config{Logger: container.GormLogger()}
//}
//
//db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL_YUGABYTE")), config)
//if err != nil {
// container.logger.Fatal(err)
//}
//
//if err = db.Use(tracing.NewPlugin()); err != nil {
// container.logger.Fatal(stacktrace.Propagate(err, "cannot use GORM tracing plugin"))
//}
//
//sql, err := db.DB()
//if err != nil {
// container.logger.Fatal(err)
//}
//
//sql.SetMaxOpenConns(7)
//sql.SetMaxIdleConns(3)
//sql.SetConnMaxLifetime(time.Minute * 10)
//
//container.logger.Debug(fmt.Sprintf("Running migrations for yugabyte [%T]", db))
//if err = db.AutoMigrate(&entities.Heartbeat{}); err != nil {
// container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Heartbeat{})))
//}
//
//if err = db.AutoMigrate(&entities.HeartbeatMonitor{}); err != nil {
// container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.HeartbeatMonitor{})))
//}
//
//container.yugaByteDB = db
return container.yugaByteDB

config := &gorm.Config{}
if isLocal() {
config = &gorm.Config{Logger: container.GormLogger()}
}

db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL_DEDICATED")), config)
if err != nil {
container.logger.Fatal(err)
}

if err = db.Use(tracing.NewPlugin()); err != nil {
container.logger.Fatal(stacktrace.Propagate(err, "cannot use GORM tracing plugin"))
}

container.logger.Debug(fmt.Sprintf("Running migrations for yugabyte [%T]", db))
if err = db.AutoMigrate(&entities.Heartbeat{}); err != nil {
container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Heartbeat{})))
}

if err = db.AutoMigrate(&entities.HeartbeatMonitor{}); err != nil {
container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.HeartbeatMonitor{})))
}

container.dedicatedDB = db
return container.dedicatedDB
}

// DB creates an instance of gorm.DB if it has not been created already
Expand Down Expand Up @@ -706,7 +697,7 @@ func (container *Container) HeartbeatMonitorRepository() (repository repositorie
return repositories.NewGormHeartbeatMonitorRepository(
container.Logger(),
container.Tracer(),
container.YugaByteDB(),
container.DedicatedDB(),
)
}

Expand Down Expand Up @@ -1303,7 +1294,7 @@ func (container *Container) HeartbeatRepository() repositories.HeartbeatReposito
return repositories.NewGormHeartbeatRepository(
container.Logger(),
container.Tracer(),
container.YugaByteDB(),
container.DedicatedDB(),
)
}

Expand Down
24 changes: 6 additions & 18 deletions api/pkg/repositories/gorm_heartbeat_monitor_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func (repository *gormHeartbeatMonitorRepository) UpdateQueueID(ctx context.Cont
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

err := repository.db.
Expand All @@ -50,9 +48,7 @@ func (repository *gormHeartbeatMonitorRepository) Delete(ctx context.Context, us
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

err := repository.db.WithContext(ctx).
Expand Down Expand Up @@ -85,9 +81,7 @@ func (repository *gormHeartbeatMonitorRepository) Index(ctx context.Context, use
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return nil, stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

query := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("owner = ?", owner)
Expand All @@ -105,9 +99,7 @@ func (repository *gormHeartbeatMonitorRepository) Store(ctx context.Context, hea
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

if err := repository.db.WithContext(ctx).Create(heartbeatMonitor).Error; err != nil {
Expand All @@ -123,9 +115,7 @@ func (repository *gormHeartbeatMonitorRepository) Load(ctx context.Context, user
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return nil, stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

phone := new(entities.HeartbeatMonitor)
Expand All @@ -152,9 +142,7 @@ func (repository *gormHeartbeatMonitorRepository) Exists(ctx context.Context, us
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return false, stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

var exists bool
Expand Down
13 changes: 3 additions & 10 deletions api/pkg/repositories/gorm_heartbeat_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repositories
import (
"context"
"fmt"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -37,9 +36,7 @@ func (repository *gormHeartbeatRepository) Last(ctx context.Context, userID enti
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return nil, stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

heartbeat := new(entities.Heartbeat)
Expand All @@ -66,9 +63,7 @@ func (repository *gormHeartbeatRepository) Index(ctx context.Context, userID ent
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return nil, stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

query := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("owner = ?", owner)
Expand All @@ -91,9 +86,7 @@ func (repository *gormHeartbeatRepository) Store(ctx context.Context, heartbeat
ctx, span := repository.tracer.Start(ctx)
defer span.End()

return stacktrace.NewError("not implemented")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()

if err := repository.db.WithContext(ctx).Create(heartbeat).Error; err != nil {
Expand Down
8 changes: 7 additions & 1 deletion api/pkg/repositories/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package repositories

import "github.com/palantir/stacktrace"
import (
"time"

"github.com/palantir/stacktrace"
)

// IndexParams parameters for indexing a database table
type IndexParams struct {
Expand All @@ -12,4 +16,6 @@ type IndexParams struct {
const (
// ErrCodeNotFound is thrown when an entity does not exist in storage
ErrCodeNotFound = stacktrace.ErrorCode(1000)

dbOperationDuration = 1 * time.Second
)
4 changes: 1 addition & 3 deletions web/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ export const actions = {
},
})

try {
await context.dispatch('getHeartbeat')
} catch (e) {}
await context.dispatch('getHeartbeat')
context.commit('setThreads', response.data.data)
},

Expand Down

0 comments on commit 55efbbb

Please sign in to comment.