Add confirmation type
This commit is contained in:
parent
8da4fef6cc
commit
a800016d2e
@ -17,7 +17,8 @@ CREATE INDEX timestamp_index ON trades (timestamp);
|
||||
CREATE TABLE confirmations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
product CHAR(7) NOT NULL,
|
||||
last_trade_id INTEGER NOT NULL DEFAULT 0);
|
||||
type CHAR(1) NOT NULL,
|
||||
last_id INTEGER NOT NULL DEFAULT 0);
|
||||
|
||||
CREATE TABLE aggregations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
|
@ -1,13 +1,14 @@
|
||||
package tacitus
|
||||
|
||||
type Confirmation struct {
|
||||
Id int `json:"id"`
|
||||
Product string `json:"product"`
|
||||
LastTradeId int `json:"last_trade_id"`
|
||||
Id int `json:"id"`
|
||||
Product string `json:"product"`
|
||||
Type string `json:"type"`
|
||||
LastId int `json:"last_id"`
|
||||
}
|
||||
|
||||
type ConfirmationService interface {
|
||||
Confirmation(product string) (Confirmation, error)
|
||||
Confirmation(product, c_type string) (Confirmation, error)
|
||||
CreateConfirmation(c Confirmation) (Confirmation, error)
|
||||
UpdateConfirmation(c Confirmation) (Confirmation, error)
|
||||
}
|
||||
|
@ -62,13 +62,14 @@ func (v *Validator) emitProducts() {
|
||||
func (v *Validator) validateProduct(product string) {
|
||||
v.Logger.Info("validator: %v", product)
|
||||
|
||||
conf, _ := v.Database.ConfirmationService().Confirmation(product)
|
||||
conf, _ := v.Database.ConfirmationService().Confirmation(product, "t")
|
||||
|
||||
groups, last_id := v.findMissingGroups(product, conf.LastTradeId)
|
||||
groups, last_id := v.findMissingGroups(product, conf.LastId)
|
||||
v.getMissingTrades(product, groups)
|
||||
|
||||
conf.LastTradeId = last_id
|
||||
conf.Product = product
|
||||
conf.Type = "t"
|
||||
conf.LastId = last_id
|
||||
if conf.Id == 0 {
|
||||
v.Database.ConfirmationService().CreateConfirmation(conf)
|
||||
} else {
|
||||
|
@ -5,27 +5,28 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
conf_columns = `product, last_trade_id`
|
||||
conf_columns = `product, type, last_id`
|
||||
conf_insert = `INSERT INTO confirmations (` + conf_columns +
|
||||
`) VALUES ($1, $2) RETURNING id, ` + conf_columns + `;`
|
||||
`) VALUES ($1, $2, $3) RETURNING id, ` + conf_columns + `;`
|
||||
conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations ` +
|
||||
`WHERE product = $1;`
|
||||
conf_update = `UPDATE confirmations SET last_trade_id = $1 WHERE product = $2;`
|
||||
`WHERE product = $1 AND type = $2;`
|
||||
conf_update = `UPDATE confirmations SET product = $1, type = $2, last_id = $3 ` +
|
||||
`WHERE id = $4;`
|
||||
)
|
||||
|
||||
type ConfirmationService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (s *ConfirmationService) Confirmation(product string) (tacitus.Confirmation, error) {
|
||||
func (s *ConfirmationService) Confirmation(product, c_type string) (tacitus.Confirmation, error) {
|
||||
var conf tacitus.Confirmation
|
||||
|
||||
s.client.logQuery(conf_find_product, product)
|
||||
s.client.logQuery(conf_find_product, product, c_type)
|
||||
|
||||
row := s.client.db.QueryRow(conf_find_product, product)
|
||||
err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
|
||||
row := s.client.db.QueryRow(conf_find_product, product, c_type)
|
||||
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
|
||||
if err != nil {
|
||||
s.client.logError(conf_find_product, err, product)
|
||||
s.client.logError(conf_find_product, err, product, c_type)
|
||||
|
||||
return conf, err
|
||||
}
|
||||
@ -36,12 +37,12 @@ func (s *ConfirmationService) Confirmation(product string) (tacitus.Confirmation
|
||||
func (s *ConfirmationService) CreateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
||||
var conf tacitus.Confirmation
|
||||
|
||||
s.client.logQuery(conf_insert, c.Product, c.LastTradeId)
|
||||
s.client.logQuery(conf_insert, c.Product, c.Type, c.LastId)
|
||||
|
||||
row := s.client.db.QueryRow(conf_insert, c.Product, c.LastTradeId)
|
||||
err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
|
||||
row := s.client.db.QueryRow(conf_insert, c.Product, c.Type, c.LastId)
|
||||
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
|
||||
if err != nil {
|
||||
s.client.logError(conf_insert, err, c.Product, c.LastTradeId)
|
||||
s.client.logError(conf_insert, err, c.Product, c.Type, c.LastId)
|
||||
|
||||
return conf, err
|
||||
}
|
||||
@ -50,11 +51,11 @@ func (s *ConfirmationService) CreateConfirmation(c tacitus.Confirmation) (tacitu
|
||||
}
|
||||
|
||||
func (s *ConfirmationService) UpdateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
||||
s.client.logQuery(conf_update, c.LastTradeId, c.Product)
|
||||
s.client.logQuery(conf_update, c.Product, c.Type, c.LastId, c.Id)
|
||||
|
||||
_, err := s.client.db.Exec(conf_update, c.LastTradeId, c.Product)
|
||||
_, err := s.client.db.Exec(conf_update, c.Product, c.Type, c.LastId, c.Id)
|
||||
if err != nil {
|
||||
s.client.logError(conf_update, err, c.LastTradeId, c.Product)
|
||||
s.client.logError(conf_update, err, c.Product, c.Type, c.LastId, c.Id)
|
||||
}
|
||||
|
||||
return c, err
|
||||
|
Reference in New Issue
Block a user