Files
gocryptotrader/database/repository/audit/audit.go
Andrew 0c76789b0d Database interface & auditing feature (#332)
* added audit manager

* Basic database DOA setup

* Added base config file

* added sqlite support and creation of schema

* added basic tests and config entry

* corrected issues of database is disabled

* fixed path for test

* WIP

* Added tests fixed config checking

* reverted files back to upstream

* reverted go.mod files

* no more test test test

* removed local testing details for psql

* hello

* added comments

* increased ping to 30 seconds

* renamed database table and added additional condition around test

* removed database test details

* goimport ran on all files

* WIP

* first attempt at migration

* fixes for migration system

* Migration system logger interface implemented

* fixes to print functions

* added write pooling pass

* gofmt :D

* formatted imports correctly

* removed old code

* added creation of migration

* gofmt

* :D Hello

*  🏎️

* maybe one day i will remember to revert go mod files

* checked err return condition correctly

* first changes for PR feedback

* code clean up

* protect Connected with RWmutex & event with mutex

* : D

* we can just pretend like it never happened

* MOved migrations back to source directory and added README

* readme formatting update

* Addd command line override for datadir

* use correct var when creating a migration and confirm folder is created

* Check if database version is newer than latest migration and also you know make migrations work.....

* uses filepath instead of manual path to use correct path seperator

* Add connection message and lower timeout

* Added support for sslmode for psql

* no longer force Close of database instead allow driver to maage

* Added closer func to test output

* sslmode added to example config
2019-08-20 16:35:06 +10:00

63 lines
1.3 KiB
Go

package audit
import (
"sync"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/models"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
// Repository that is required for each driver type to implement
type Repository interface {
AddEventTx(event []*models.AuditEvent)
}
var (
// Audit repository initialise copy of Audit Repository
Audit Repository
)
type eventPool struct {
events []*models.AuditEvent
eventMu sync.Mutex
}
var ep eventPool
// Event allows you to call audit.Event() as long as the audit repository package without the need to include each driver
func Event(msgType, identifier, message string) {
if database.Conn.SQL == nil {
return
}
if Audit == nil {
return
}
tempEvent := models.AuditEvent{
Type: msgType,
Identifier: identifier,
Message: message}
ep.poolEvents(&tempEvent)
}
func (e *eventPool) poolEvents(event *models.AuditEvent) {
e.eventMu.Lock()
defer e.eventMu.Unlock()
e.events = append(e.events, event)
database.Conn.Mu.RLock()
defer database.Conn.Mu.RUnlock()
if !database.Conn.Connected {
log.Warnln(log.DatabaseMgr, "connection to database interrupted pooling database writes")
return
}
Audit.AddEventTx(e.events)
e.events = nil
}