Refactor validator interface
This commit is contained in:
parent
de2ea4ed9d
commit
17beb9b88c
@ -44,10 +44,10 @@ func main() {
|
||||
a.Start(30 * time.Second)
|
||||
|
||||
v := validator{}
|
||||
v.db = db
|
||||
v.logger = logger
|
||||
v.validate("ETH-USD", "BTC-USD")
|
||||
v.stop()
|
||||
v.Database = db
|
||||
v.Logger = logger
|
||||
v.Products = []string{"ETH-USD", "BTC-USD"}
|
||||
v.Start(30 * time.Second)
|
||||
|
||||
t := make(chan bool)
|
||||
<-t
|
||||
|
@ -8,33 +8,34 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ByTradeId []tacitus.Trade
|
||||
type byTradeId []tacitus.Trade
|
||||
|
||||
func (t ByTradeId) Len() int { return len(t) }
|
||||
func (t ByTradeId) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
||||
func (t ByTradeId) Less(i, j int) bool { return t[i].TradeId > t[j].TradeId }
|
||||
func (t byTradeId) Len() int { return len(t) }
|
||||
func (t byTradeId) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
||||
func (t byTradeId) Less(i, j int) bool { return t[i].TradeId > t[j].TradeId }
|
||||
|
||||
type validator struct {
|
||||
db tacitus.DatabaseClientService
|
||||
logger tacitus.Logger
|
||||
Database tacitus.DatabaseClientService
|
||||
Logger tacitus.Logger
|
||||
Products []string
|
||||
|
||||
ticker *time.Ticker
|
||||
done chan bool
|
||||
}
|
||||
|
||||
func (v *validator) stop() {
|
||||
func (v *validator) Stop() {
|
||||
if v.ticker == nil || v.done == nil {
|
||||
v.ticker.Stop()
|
||||
v.done <- true
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) validate(products ...string) {
|
||||
v.ticker = time.NewTicker(1 * time.Hour)
|
||||
func (v *validator) Start(frequency time.Duration) {
|
||||
v.ticker = time.NewTicker(frequency)
|
||||
v.done = make(chan bool)
|
||||
|
||||
go func() {
|
||||
v.emitProducts(products...)
|
||||
v.emitProducts()
|
||||
|
||||
var done bool
|
||||
|
||||
@ -43,7 +44,7 @@ func (v *validator) validate(products ...string) {
|
||||
case <-v.done:
|
||||
done = true
|
||||
case <-v.ticker.C:
|
||||
v.emitProducts(products...)
|
||||
v.emitProducts()
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,16 +53,16 @@ func (v *validator) validate(products ...string) {
|
||||
}()
|
||||
}
|
||||
|
||||
func (v *validator) emitProducts(products ...string) {
|
||||
for _, p := range products {
|
||||
func (v *validator) emitProducts() {
|
||||
for _, p := range v.Products {
|
||||
v.validateProduct(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) validateProduct(product string) {
|
||||
v.logger.Info("validator: %v", product)
|
||||
v.Logger.Info("validator: %v", product)
|
||||
|
||||
conf, _ := v.db.ConfirmationService().Confirmation(product)
|
||||
conf, _ := v.Database.ConfirmationService().Confirmation(product)
|
||||
|
||||
groups, last_id := v.findMissingGroups(product, conf.LastTradeId)
|
||||
v.getMissingTrades(product, groups)
|
||||
@ -69,12 +70,12 @@ func (v *validator) validateProduct(product string) {
|
||||
conf.LastTradeId = last_id
|
||||
conf.Product = product
|
||||
if conf.Id == 0 {
|
||||
v.db.ConfirmationService().CreateConfirmation(conf)
|
||||
v.Database.ConfirmationService().CreateConfirmation(conf)
|
||||
} else {
|
||||
v.db.ConfirmationService().UpdateConfirmation(conf)
|
||||
v.Database.ConfirmationService().UpdateConfirmation(conf)
|
||||
}
|
||||
|
||||
v.logger.Info("validator: DONE %v", product)
|
||||
v.Logger.Info("validator: DONE %v", product)
|
||||
}
|
||||
|
||||
func (v *validator) getMissingTrades(product string, groups [][]int) {
|
||||
@ -82,30 +83,30 @@ func (v *validator) getMissingTrades(product string, groups [][]int) {
|
||||
|
||||
for _, group := range groups {
|
||||
total := 1 + group[1] - group[0]
|
||||
v.logger.Info("validator: retrieving %v missing trade(s): %v", total, group)
|
||||
v.Logger.Info("validator: retrieving %v missing trade(s): %v", total, group)
|
||||
|
||||
for i := group[1]; i >= group[0]; i-- {
|
||||
ts, _ := c.GetTradesBefore(product, i+1)
|
||||
sort.Sort(ByTradeId(ts))
|
||||
sort.Sort(byTradeId(ts))
|
||||
|
||||
for _, t := range ts {
|
||||
if t.TradeId < group[0] && t.TradeId > group[1] {
|
||||
i = ts[len(ts)-1].TradeId
|
||||
} else {
|
||||
v.db.TradeService().CreateTrade(t)
|
||||
v.Database.TradeService().CreateTrade(t)
|
||||
i = t.TradeId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
v.logger.Info("validator: DONE")
|
||||
v.Logger.Info("validator: DONE")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validator) findMissingGroups(product string, starting int) ([][]int, int) {
|
||||
results, err := v.db.TradeService().TradesAfterAll(product, starting)
|
||||
results, err := v.Database.TradeService().TradesAfterAll(product, starting)
|
||||
if err != nil {
|
||||
v.logger.Info("Error getting all trades: %v", err)
|
||||
v.Logger.Info("Error getting all trades: %v", err)
|
||||
}
|
||||
|
||||
var trade tacitus.Trade
|
||||
|
Reference in New Issue
Block a user