From a800016d2e9bc858563f0681f18c3d99efe18cd6 Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Tue, 10 Oct 2017 17:22:00 -0700 Subject: [PATCH] Add confirmation type --- config/postgres.sql | 3 ++- confirmation.go | 9 +++++---- ops/validator.go | 7 ++++--- postgres/confirmation_service.go | 33 ++++++++++++++++---------------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/config/postgres.sql b/config/postgres.sql index 7f679c8..6360fd0 100644 --- a/config/postgres.sql +++ b/config/postgres.sql @@ -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, diff --git a/confirmation.go b/confirmation.go index e71072e..bab1281 100644 --- a/confirmation.go +++ b/confirmation.go @@ -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) } diff --git a/ops/validator.go b/ops/validator.go index 221fce5..ac38970 100644 --- a/ops/validator.go +++ b/ops/validator.go @@ -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 { diff --git a/postgres/confirmation_service.go b/postgres/confirmation_service.go index 294d1b5..946c04e 100644 --- a/postgres/confirmation_service.go +++ b/postgres/confirmation_service.go @@ -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