mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 07:26:50 +00:00
* modernise: Run new gopls modernise tool against codebase
* Address shazbert's nits
* apichecker, gctcli: Simplify HTML scraping functions and improve depth limit handling
* refactor: Create minSyncInterval const and update order book limit handling for binance and binanceUS
* refactor: Various slice usage improvements and rename TODO
* tranches: Revert deleteByID changes due to performance decrease
Shazbert was a F1 driver in a past lifetime 🏎️
* tranches: Simply retrieve copy
Thanks to shazbert
* documentation: Sort contributors list by contributions
* tranches: Remove deadcode in deleteByID
86 lines
2.2 KiB
Go
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 {
|
|
tempEvent := modelSQLite.AuditEvent{
|
|
Type: msgtype,
|
|
Identifier: id,
|
|
Message: message,
|
|
}
|
|
err = tempEvent.Insert(ctx, tx, boil.Blacklist("created_at"))
|
|
} else {
|
|
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) (any, 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)
|
|
}
|