Files
gocryptotrader/database/repository/audit/audit.go
Adrian Gallagher 9a4eb9de84 CI: Fix golangci-lint linter issues, add prealloc linter and bump version depends for Go 1.18 (#915)
* Bump CI versions

* Specifically set go version as 1.17.x bumps it to 1.18

* Another

* Adjust AppVeyor

* Part 1 of linter issues

* Part 2

* Fix various linters and improvements

* Part 3

* Finishing touches

* Tests and EqualFold

* Fix nitterinos plus bonus requester jobs bump for exchanges with large number of tests

* Fix nitterinos and bump golangci-lint timeout for AppVeyor

* Address nits, ensure all books are returned on err due to syncer regression

* Fix the wiggins

* Fix duplication

* Fix nitterinos
2022-04-20 13:45:15 +10:00

86 lines
2.2 KiB
Go

package audit
import (
"context"
"time"
"github.com/thrasher-corp/gocryptotrader/database"
modelPSQL "github.com/thrasher-corp/gocryptotrader/database/models/postgres"
modelSQLite "github.com/thrasher-corp/gocryptotrader/database/models/sqlite3"
"github.com/thrasher-corp/gocryptotrader/database/repository"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/sqlboiler/boil"
"github.com/thrasher-corp/sqlboiler/queries/qm"
)
// Event inserts a new audit event to database
func Event(id, msgtype, message string) {
if database.DB.SQL == nil {
return
}
ctx := context.TODO()
ctx = boil.SkipTimestamps(ctx)
tx, err := database.DB.SQL.BeginTx(ctx, nil)
if err != nil {
log.Errorf(log.Global, "Event transaction begin failed: %v", err)
return
}
if repository.GetSQLDialect() == database.DBSQLite3 {
var tempEvent = modelSQLite.AuditEvent{
Type: msgtype,
Identifier: id,
Message: message,
}
err = tempEvent.Insert(ctx, tx, boil.Blacklist("created_at"))
} else {
var tempEvent = modelPSQL.AuditEvent{
Type: msgtype,
Identifier: id,
Message: message,
}
err = tempEvent.Insert(ctx, tx, boil.Blacklist("created_at"))
}
if err != nil {
log.Errorf(log.Global, "Event insert failed: %v", err)
err = tx.Rollback()
if err != nil {
log.Errorf(log.Global, "Event Transaction rollback failed: %v", err)
}
return
}
err = tx.Commit()
if err != nil {
log.Errorf(log.Global, "Event Transaction commit failed: %v", err)
return
}
}
// GetEvent () returns list of order events matching query
func GetEvent(startTime, endTime time.Time, order string, limit int) (interface{}, error) {
if database.DB.SQL == nil {
return nil, database.ErrDatabaseSupportDisabled
}
query := qm.Where("created_at BETWEEN ? AND ?", startTime, endTime)
orderByQueryString := "id"
if order == "desc" {
orderByQueryString += " desc"
}
orderByQuery := qm.OrderBy(orderByQueryString)
limitQuery := qm.Limit(limit)
ctx := context.TODO()
if repository.GetSQLDialect() == database.DBSQLite3 {
return modelSQLite.AuditEvents(query, orderByQuery, limitQuery).All(ctx, database.DB.SQL)
}
return modelPSQL.AuditEvents(query, orderByQuery, limitQuery).All(ctx, database.DB.SQL)
}