This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/cmd/watcher/validator.go

81 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"github.com/kcotugno/tacitus"
"time"
)
type validator struct {
db tacitus.DatabaseClientService
logger tacitus.Logger
ticker *time.Ticker
done chan bool
}
func (v *validator) stop() {
2017-09-27 23:27:05 -07:00
if v.ticker == nil || v.done == nil {
v.ticker.Stop()
v.done <- true
}
}
func (v *validator) validate(products ...string) {
v.ticker = time.NewTicker(10 * time.Minute)
v.done = make(chan bool)
go func() {
v.emit_products(products...)
var done bool
for !done {
select {
case <-v.done:
done = true
case <-v.ticker.C:
v.emit_products(products...)
}
}
v.ticker = nil
v.done = nil
}()
}
2017-09-28 21:41:33 -07:00
func (v *validator) emitProducts(products ...string) {
for _, p := range products {
go v.validate_product(p)
}
}
2017-09-28 21:41:33 -07:00
func (v *validator) validateProduct(product string) {
v.logger.Info("Staring validation of %v", product)
results, err := v.db.TradeService().TradesAfterAll(product, 0)
if err != nil {
v.logger.Info("Error getting all trades: %v", err)
}
var trade tacitus.Trade
current := 0
missing := [][]int{}
for results.Next() {
trade = results.Value().(tacitus.Trade)
if trade.TradeId != current+1 {
missing = append(missing, []int{current + 1, trade.TradeId - 1})
}
current = trade.TradeId
}
for _, i := range missing {
v.logger.Info("%v", i)
}
v.logger.Info("DONE: %v", product)
}