diff --git a/cmd/webapp/main.go b/cmd/webapp/main.go index 0e47ef7..47e3048 100644 --- a/cmd/webapp/main.go +++ b/cmd/webapp/main.go @@ -4,13 +4,17 @@ import ( "log" "tacitus/http" "tacitus/postgres" + "tacitus/osutil" ) func main() { + logger := osutil.NewLogger() + logger.Utc = true s := http.NewServer() db := postgres.NewClient() db.Name = "gdax" db.User = "gdax" + db.SetLogger(logger) if err := db.Open(); err != nil { log.Panic(err) } diff --git a/http/trade_handler.go b/http/trade_handler.go index d78c9a4..3844f9b 100644 --- a/http/trade_handler.go +++ b/http/trade_handler.go @@ -20,7 +20,7 @@ type tradesResponse struct { func (t *TradeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { paths := strings.Split(r.URL.Path, "/") - if len(paths) < 4 && !tacitus.ValidProduct(paths[3]) { + if len(paths) < 3 || !tacitus.ValidProduct(paths[3]) { errorResp(w, tacitus.Error("Invalid product")) return } diff --git a/osutil/logger_service.go b/osutil/logger_service.go index cec0c24..7cf26e5 100644 --- a/osutil/logger_service.go +++ b/osutil/logger_service.go @@ -20,7 +20,7 @@ type LoggerService struct { } func NewLogger() *LoggerService { - l := Logger{} + l := LoggerService{} l.DateFormat = time.RFC3339Nano l.Stderr = true @@ -68,7 +68,7 @@ func (l *LoggerService) Info(format string, params ...interface{}) { } } -func (l * Logger) formattedTime() string { +func (l *LoggerService) formattedTime() string { t := time.Now() if l.Utc { diff --git a/postgres/client.go b/postgres/client.go index 3f72b6d..903bcba 100644 --- a/postgres/client.go +++ b/postgres/client.go @@ -19,6 +19,7 @@ type Client struct { tradeService TradeService + logger tacitus.Logger db *sql.DB } @@ -30,6 +31,11 @@ func NewClient() *Client { return &c } +func (c *Client) SetLogger(logger tacitus.Logger) { + c.logger = logger + c.tradeService.Logger = logger +} + func (c *Client) Open() error { buf := bytes.Buffer{} tmpl, _ := template.New("conn").Parse(connStr) @@ -37,17 +43,32 @@ func (c *Client) Open() error { db, err := sql.Open("postgres", buf.String()) if err != nil { + c.log(`Failed to connect to postgres. params=%v`, buf.String()) return err } if err = db.Ping(); err != nil { + c.log(`Failed to connect to postgres. params=%v`, buf.String()) return err } + c.log(`Conntected to postgres. params="%v"`, buf.String()) c.db = db return nil } +func (c *Client) Close() error { + err := c.db.Close() + c.log("Closed postgres connection") + return err +} + func (c *Client) TradeService() tacitus.TradeService { return &c.tradeService } + +func (c *Client) log(format string, params ...interface{}) { + if c.logger != nil { + c.logger.Info(format, params...) + } +} diff --git a/postgres/trade_service.go b/postgres/trade_service.go index 5549038..a6b8810 100644 --- a/postgres/trade_service.go +++ b/postgres/trade_service.go @@ -28,15 +28,18 @@ const ( ) type TradeService struct { + Logger tacitus.Logger client *Client } func (t *TradeService) Trade(id int) (*tacitus.Trade, error) { var tr tacitus.Trade + t.logQuery(trade_find, id) row := t.client.db.QueryRow(trade_find, id) if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy, &tr.Sell, &tr.Timestamp); err != nil { + t.logError(trade_find, err, id) return nil, err } @@ -47,10 +50,13 @@ func (t *TradeService) Trade(id int) (*tacitus.Trade, error) { func (t *TradeService) TradeByTradeId(id int, prod string) (*tacitus.Trade, error) { var tr tacitus.Trade + t.logQuery(trade_find_trade_id, id, prod) row := t.client.db.QueryRow(trade_find_trade_id, id, prod) if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy, &tr.Sell, &tr.Timestamp); err != nil { + t.logError(trade_find_trade_id, err, id, prod) + return nil, err } @@ -59,9 +65,13 @@ func (t *TradeService) TradeByTradeId(id int, prod string) (*tacitus.Trade, erro func (t *TradeService) CreateTrade(trade *tacitus.Trade) error { var id int - res := t.client.db.QueryRow(trade_insert, trade.TradeId, trade.Product, - trade.Price, trade.Size, trade.Buy, trade.Sell, trade.Timestamp) + params := []interface{}{trade.TradeId, trade.Product, trade.Price, + trade.Size, trade.Buy, trade.Sell, trade.Timestamp} + + t.logQuery(trade_insert, params...) + res := t.client.db.QueryRow(trade_insert, params...) if err := res.Scan(&id); err != nil { + t.logError(trade_insert, err, params...) return err } @@ -71,14 +81,22 @@ func (t *TradeService) CreateTrade(trade *tacitus.Trade) error { } func (t *TradeService) DeleteTrade(id int) { - t.client.db.Exec(trade_delete, id) + t.logQuery(trade_delete, id) + + _, err := t.client.db.Exec(trade_delete, id) + if err != nil{ + t.logError(trade_delete, err, id) + } } func (t *TradeService) TradesInDateRange(product string, start, end time.Time) ([]tacitus.Trade, error) { + t.logQuery(trade_in_date_range, product, start, end) rows, err := t.client.db.Query(trade_in_date_range, product, start, end) if err != nil { + t.logError(trade_in_date_range, err, product, start, end) + return nil, err } @@ -86,8 +104,11 @@ func (t *TradeService) TradesInDateRange(product string, start, } func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) { + t.logQuery(trade_first, product, limit) rows, err := t.client.db.Query(trade_first, product, limit) if err != nil { + t.logError(trade_first, err, product, limit) + return nil, err } @@ -95,8 +116,11 @@ func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, } func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) { + t.logQuery(trade_last, product, limit) rows, err := t.client.db.Query(trade_last, product, limit) if err != nil { + t.logError(trade_last, err, product, limit) + return nil, err } @@ -104,8 +128,11 @@ func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, e } func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) { + t.logQuery(trade_after, product, id, limit) rows, err := t.client.db.Query(trade_after, product, id, limit) if err != nil { + t.logError(trade_after, err, product, id, limit) + return nil, err } @@ -114,8 +141,11 @@ func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Tra } func (t *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) { + t.logQuery(trade_before, product, id, limit) rows, err := t.client.db.Query(trade_before, product, id, limit) if err != nil { + t.logError(trade_before, err, product, id, limit) + return nil, err } @@ -123,6 +153,20 @@ func (t *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Tr } +func (t *TradeService) logQuery(sql string, params ...interface{}) { + t.log(`SQL query="%v" params="%v"`, sql, params) +} + +func (t *TradeService) logError(sql string, err error, params ...interface{}) { + t.log(`SQL FAIL query="%v" params="%v" error="%v"`, sql, params, err) +} + +func (t *TradeService) log(format string, params ...interface{}) { + if t.Logger != nil { + t.Logger.Info(format, params...) + } +} + func deserializeTrades(rows *sql.Rows) ([]tacitus.Trade, error) { defer rows.Close()