Skip to content

Commit

Permalink
Improved respository and log packages (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Sep 17, 2024
1 parent 082aac5 commit 383beb8
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 160 deletions.
12 changes: 6 additions & 6 deletions src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,

accountCreated, err := controller.accountRepository.Create(&accountRequest)
if err != nil {
utils.Log(utils.LogLevelError, "Error creating account: %s", err.Error())
utils.LogError("Error creating account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Error creating account"}, http.StatusInternalServerError)
return
}
Expand All @@ -73,7 +73,7 @@ func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter,

account, err := controller.accountRepository.FetchByDomainIdEnvironment(domainId, enviroment)
if err != nil {
utils.Log(utils.LogLevelError, "Error fetching account: %s", err.Error())
utils.LogError("Error fetching account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Account not found"}, http.StatusNotFound)
return
}
Expand All @@ -86,7 +86,7 @@ func (controller *AccountController) FetchAllAccountsByDomainIdHandler(w http.Re

accounts := controller.accountRepository.FetchAllByDomainId(domainId)
if accounts == nil {
utils.Log(utils.LogLevelError, "Not found accounts for domain: %s", domainId)
utils.LogError("Not found accounts for domain: %s", domainId)
utils.ResponseJSON(w, ErrorResponse{Error: "Not found accounts for domain: " + domainId}, http.StatusNotFound)
return
}
Expand All @@ -98,7 +98,7 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
var accountRequest model.Account
err := json.NewDecoder(r.Body).Decode(&accountRequest)
if err != nil {
utils.Log(utils.LogLevelError, "Error updating account: %s", err.Error())
utils.LogError("Error updating account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Invalid request"}, http.StatusBadRequest)
return
}
Expand All @@ -110,7 +110,7 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,

accountUpdated, err := controller.accountRepository.Update(&accountRequest)
if err != nil {
utils.Log(utils.LogLevelError, "Error updating account: %s", err.Error())
utils.LogError("Error updating account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Error updating account"}, http.StatusInternalServerError)
return
}
Expand All @@ -124,7 +124,7 @@ func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter,

err := controller.accountRepository.DeleteByDomainIdEnvironment(domainId, enviroment)
if err != nil {
utils.Log(utils.LogLevelError, "Error deleting account: %s", err.Error())
utils.LogError("Error deleting account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Error deleting account: " + err.Error()}, http.StatusInternalServerError)
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestDeleteAccountHandler(t *testing.T) {

// Assert
assert.Equal(t, http.StatusInternalServerError, response.Code)
assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: not-found\"}", response.Body.String())
assert.Equal(t, "{\"error\":\"Error deleting account: account not found\"}", response.Body.String())
})
}

Expand Down
36 changes: 17 additions & 19 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type CoreHandler struct {
}

func NewCoreHandler(accountRepository repository.AccountRepository, apiService IAPIService, comparatorService IComparatorService) *CoreHandler {
timeWindow, unitWindow := getTimeWindow(config.GetEnv("HANDLER_WAITING_TIME"))
timeWindow, unitWindow := utils.GetTimeWindow(config.GetEnv("HANDLER_WAITING_TIME"))
waitingTime := time.Duration(timeWindow) * unitWindow

return &CoreHandler{
Expand Down Expand Up @@ -64,21 +64,21 @@ func (c *CoreHandler) InitCoreHandlerGoroutine() (int, error) {
}

func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitService) {
utils.Log(utils.LogLevelInfo, "[%s] Starting account handler", accountId)
utils.LogInfo("[%s] Starting account handler", accountId)

for {
// Refresh account settings
account, _ := c.accountRepository.FetchByAccountId(accountId)

if account == nil {
// Terminate the goroutine (account was deleted)
utils.Log(utils.LogLevelInfo, "[%s] Account was deleted, terminating account handler", accountId)
utils.LogInfo("[%s] Account was deleted, terminating account handler", accountId)
return
}

// Wait for account to be active
if !account.Settings.Active {
utils.Log(utils.LogLevelInfo, "[%s - %s (%s)] Account is not active, waiting for activation",
utils.LogInfo("[%s - %s (%s)] Account is not active, waiting for activation",
accountId, account.Domain.Name, account.Environment)

c.updateDomainStatus(*account, model.StatusPending, "Account was deactivated")
Expand All @@ -93,7 +93,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
repositoryData, err := gitService.GetRepositoryData(account.Environment)

if err != nil {
utils.Log(utils.LogLevelError, "[%s - %s (%s)] Failed to fetch repository data - %s",
utils.LogError("[%s - %s (%s)] Failed to fetch repository data - %s",
accountId, account.Domain.Name, account.Environment, err.Error())

c.updateDomainStatus(*account, model.StatusError, "Failed to fetch repository data - "+err.Error())
Expand All @@ -105,7 +105,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
snapshotVersionPayload, err := c.apiService.FetchSnapshotVersion(account.Domain.ID, account.Environment)

if err != nil {
utils.Log(utils.LogLevelError, "[%s - %s (%s)] Failed to fetch snapshot version - %s",
utils.LogError("[%s - %s (%s)] Failed to fetch snapshot version - %s",
accountId, account.Domain.Name, account.Environment, err.Error())

c.updateDomainStatus(*account, model.StatusError, "Failed to fetch snapshot version - "+err.Error())
Expand All @@ -119,13 +119,13 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
}

// Wait for the next cycle
timeWindow, unitWindow := getTimeWindow(account.Settings.Window)
timeWindow, unitWindow := utils.GetTimeWindow(account.Settings.Window)
time.Sleep(time.Duration(timeWindow) * unitWindow)
}
}

func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.RepositoryData, gitService IGitService) {
utils.Log(utils.LogLevelInfo, "[%s - %s (%s)] Syncing up", account.ID.Hex(), account.Domain.Name, account.Environment)
utils.LogInfo("[%s - %s (%s)] Syncing up", account.ID.Hex(), account.Domain.Name, account.Environment)

// Update account status: Out of sync
account.Domain.LastCommit = repositoryData.CommitHash
Expand All @@ -136,6 +136,9 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
diff, snapshotApi, err := c.checkForChanges(account, repositoryData.Content)

if err != nil {
utils.LogError("[%s - %s (%s)] Failed to check for changes - %s",
account.ID.Hex(), account.Domain.Name, account.Environment, err.Error())

c.updateDomainStatus(account, model.StatusError, "Failed to check for changes - "+err.Error())
return
}
Expand All @@ -144,7 +147,7 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
changeSource, account, err := c.applyChanges(snapshotApi, account, repositoryData, diff, gitService)

if err != nil {
utils.Log(utils.LogLevelError, "[%s - %s (%s)] Failed to apply changes [%s] - %s",
utils.LogError("[%s - %s (%s)] Failed to apply changes [%s] - %s",
account.ID.Hex(), account.Domain.Name, account.Environment, changeSource, err.Error())

c.updateDomainStatus(account, model.StatusError, "Failed to apply changes ["+changeSource+"] - "+err.Error())
Expand Down Expand Up @@ -181,7 +184,7 @@ func (c *CoreHandler) checkForChanges(account model.Account, content string) (mo

func (c *CoreHandler) applyChanges(snapshotApi model.Snapshot, account model.Account,
repositoryData *model.RepositoryData, diff model.DiffResult, gitService IGitService) (string, model.Account, error) {
utils.Log(utils.LogLevelDebug, "[%s - %s (%s)] SnapshotAPI version: %s - SnapshotRepo version: %s",
utils.LogDebug("[%s - %s (%s)] SnapshotAPI version: %s - SnapshotRepo version: %s",
account.ID.Hex(), account.Domain.Name, account.Environment, fmt.Sprint(snapshotApi.Domain.Version), fmt.Sprint(account.Domain.Version))

err := error(nil)
Expand All @@ -192,7 +195,7 @@ func (c *CoreHandler) applyChanges(snapshotApi model.Snapshot, account model.Acc
if c.isRepositoryOutSync(repositoryData, diff) {
account, err = c.applyChangesToRepository(account, snapshotApi, gitService)
} else {
utils.Log(utils.LogLevelInfo, "[%s - %s (%s)] Repository is up to date",
utils.LogInfo("[%s - %s (%s)] Repository is up to date",
account.ID.Hex(), account.Domain.Name, account.Environment)

account.Domain.Version = snapshotApi.Domain.Version
Expand All @@ -206,7 +209,7 @@ func (c *CoreHandler) applyChanges(snapshotApi model.Snapshot, account model.Acc
}

func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *model.RepositoryData, diff model.DiffResult) model.Account {
utils.Log(utils.LogLevelInfo, "[%s - %s (%s)] Pushing changes to API", account.ID.Hex(), account.Domain.Name, account.Environment)
utils.LogInfo("[%s - %s (%s)] Pushing changes to API", account.ID.Hex(), account.Domain.Name, account.Environment)

// Removed deleted if force prune is disabled
if !account.Settings.ForcePrune {
Expand All @@ -223,7 +226,7 @@ func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *m
}

func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot model.Snapshot, gitService IGitService) (model.Account, error) {
utils.Log(utils.LogLevelInfo, "[%s - %s (%s)] Pushing changes to repository", account.ID.Hex(), account.Domain.Name, account.Environment)
utils.LogInfo("[%s - %s (%s)] Pushing changes to repository", account.ID.Hex(), account.Domain.Name, account.Environment)

// Remove version from domain
snapshotContent := snapshot
Expand All @@ -245,7 +248,7 @@ func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot m
func (c *CoreHandler) isOutSync(account model.Account, lastCommit string, snapshotVersionPayload string) bool {
snapshotVersion := c.apiService.NewDataFromJson([]byte(snapshotVersionPayload)).Snapshot.Domain.Version

utils.Log(utils.LogLevelDebug, "[%s - %s (%s)] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d",
utils.LogDebug("[%s - %s (%s)] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d",
account.ID.Hex(), account.Domain.Name, account.Environment, account.Domain.LastCommit, account.Domain.Version, snapshotVersion)

return account.Domain.LastCommit == "" || // First sync
Expand All @@ -264,8 +267,3 @@ func (c *CoreHandler) updateDomainStatus(account model.Account, status string, m
account.Domain.LastDate = time.Now().Format(time.ANSIC)
c.accountRepository.Update(&account)
}

func getTimeWindow(window string) (int, time.Duration) {
duration, _ := time.ParseDuration(window)
return 1, duration
}
4 changes: 2 additions & 2 deletions src/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func InitDb() *mongo.Database {

err = client.Ping(context.Background(), nil)
if err != nil {
utils.Log(utils.LogLevelError, "Error connecting to MongoDB: %s", err.Error())
utils.LogError("Error connecting to MongoDB: %s", err.Error())
}

utils.Log(utils.LogLevelInfo, "Connected to MongoDB!")
utils.LogInfo("Connected to MongoDB!")
return client.Database(config.GetEnv("MONGO_DB"))
}
118 changes: 49 additions & 69 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,74 @@ func (repo *AccountRepositoryMongo) Create(account *model.Account) (*model.Accou
return account, nil
}

func (repo *AccountRepositoryMongo) FetchByAccountId(accountId string) (*model.Account, error) {
func (repo *AccountRepositoryMongo) Update(account *model.Account) (*model.Account, error) {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

var account model.Account
objectId, _ := primitive.ObjectIDFromHex(accountId)
filter := bson.M{"_id": objectId}
err := collection.FindOne(ctx, filter).Decode(&account)
filter := primitive.M{domainIdFilter: account.Domain.ID, environmentFilter: account.Environment}
update := getUpdateFields(account)

var updatedAccount model.Account
err := collection.FindOneAndUpdate(ctx, filter, update, options.FindOneAndUpdate().
SetReturnDocument(options.After)).
Decode(&updatedAccount)

if err != nil {
return nil, err
}

return &account, nil
return &updatedAccount, nil
}

func (repo *AccountRepositoryMongo) FetchByDomainIdEnvironment(domainId string, environment string) (*model.Account, error) {
collection, ctx, cancel := getDbContext(repo)
defer cancel()
func (repo *AccountRepositoryMongo) FetchByAccountId(accountId string) (*model.Account, error) {
objectId, _ := primitive.ObjectIDFromHex(accountId)
filter := bson.M{"_id": objectId}
return repo.fetchOne(filter)
}

var account model.Account
func (repo *AccountRepositoryMongo) FetchByDomainIdEnvironment(domainId string, environment string) (*model.Account, error) {
filter := primitive.M{domainIdFilter: domainId, environmentFilter: environment}
err := collection.FindOne(ctx, filter).Decode(&account)
if err != nil {
return nil, err
}

return &account, nil
return repo.fetchOne(filter)
}

func (repo *AccountRepositoryMongo) FetchAllByDomainId(domainId string) []model.Account {
filter := primitive.M{domainIdFilter: domainId}
return repo.fetchMany(filter)
}

func (repo *AccountRepositoryMongo) FetchAllAccounts() []model.Account {
filter := bson.M{}
return repo.fetchMany(filter)
}

func (repo *AccountRepositoryMongo) DeleteByAccountId(accountId string) error {
objectId, _ := primitive.ObjectIDFromHex(accountId)
filter := bson.M{"_id": objectId}
return repo.deleteOne(filter)
}

func (repo *AccountRepositoryMongo) DeleteByDomainIdEnvironment(domainId string, environment string) error {
filter := primitive.M{domainIdFilter: domainId, environmentFilter: environment}
return repo.deleteOne(filter)
}

func (repo *AccountRepositoryMongo) deleteOne(filter primitive.M) error {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

filter := primitive.M{domainIdFilter: domainId}
cursor, _ := collection.Find(ctx, filter)

var accounts []model.Account
for cursor.Next(ctx) {
var account model.Account
err := cursor.Decode(&account)
if err == nil {
accounts = append(accounts, account)
}
result, err := collection.DeleteOne(ctx, filter)
if result.DeletedCount == 0 {
return errors.New("account not found")
}

return accounts
return err
}

func (repo *AccountRepositoryMongo) FetchAllAccounts() []model.Account {
func (repo *AccountRepositoryMongo) fetchMany(filter primitive.M) []model.Account {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

cursor, _ := collection.Find(ctx, primitive.M{})
cursor, _ := collection.Find(ctx, filter)

var accounts []model.Account
for cursor.Next(ctx) {
Expand All @@ -115,52 +130,17 @@ func (repo *AccountRepositoryMongo) FetchAllAccounts() []model.Account {
return accounts
}

func (repo *AccountRepositoryMongo) Update(account *model.Account) (*model.Account, error) {
func (repo *AccountRepositoryMongo) fetchOne(filter primitive.M) (*model.Account, error) {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

filter := primitive.M{domainIdFilter: account.Domain.ID, environmentFilter: account.Environment}
update := getUpdateFields(account)

var updatedAccount model.Account
err := collection.FindOneAndUpdate(ctx, filter, update, options.FindOneAndUpdate().
SetReturnDocument(options.After)).
Decode(&updatedAccount)

var account model.Account
err := collection.FindOne(ctx, filter).Decode(&account)
if err != nil {
return nil, err
}

return &updatedAccount, nil
}

func (repo *AccountRepositoryMongo) DeleteByAccountId(accountId string) error {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

objectId, _ := primitive.ObjectIDFromHex(accountId)
filter := bson.M{"_id": objectId}
result, err := collection.DeleteOne(ctx, filter)

if result.DeletedCount == 0 {
return errors.New("Account not found for id: " + accountId)
}

return err
}

func (repo *AccountRepositoryMongo) DeleteByDomainIdEnvironment(domainId string, environment string) error {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

filter := primitive.M{domainIdFilter: domainId, environmentFilter: environment}
result, err := collection.DeleteOne(ctx, filter)

if result.DeletedCount == 0 {
return errors.New("Account not found for domain.id: " + domainId)
}

return err
return &account, nil
}

func getDbContext(repo *AccountRepositoryMongo) (*mongo.Collection, context.Context, context.CancelFunc) {
Expand All @@ -182,7 +162,7 @@ func registerAccountRepositoryValidators(db *mongo.Database) {

_, err := collection.Indexes().CreateOne(context.Background(), indexModel)
if err != nil {
utils.Log(utils.LogLevelError, "Error creating index for account (environment, domain.id): %s", err.Error())
utils.LogError("Error creating index for account (environment, domain.id): %s", err.Error())
}
}

Expand Down
Loading

0 comments on commit 383beb8

Please sign in to comment.