diff --git a/cmd/watcher/main.go b/cmd/watcher/main.go index 6830ca9..75c8c4c 100644 --- a/cmd/watcher/main.go +++ b/cmd/watcher/main.go @@ -1,27 +1,87 @@ package main import ( + "github.com/kcotugno/tacitus" "github.com/kcotugno/tacitus/gdax" "github.com/kcotugno/tacitus/gdax/websocket" "github.com/kcotugno/tacitus/ops" "github.com/kcotugno/tacitus/osutil" "github.com/kcotugno/tacitus/postgres" + "flag" + "fmt" + "os" + "os/signal" "time" ) +type Parameters struct { + Products []string + + Host string + Port int + User string + Name string + Password string +} + +type StringList []string + +func parseFlags() Parameters { + var p Parameters + p.Products = make([]string, 0) + + host := flag.String("host", "localhost", "Database host") + port := flag.Int("port", 5432, "Database port") + user := flag.String("user", "gdax", "Database user") + db := flag.String("name", "gdax", "Database name") + password := flag.String("password", `""`, "Database password") + + products := make(StringList, 0) + flag.Var(&products, "product", "Product to watch. This option can "+ + "be specified multiple time") + + flag.Parse() + + for _, v := range products { + if !tacitus.ValidProduct(v) { + fmt.Printf("error: %v is not a valid product", v) + os.Exit(128) + } + } + + p.Products = []string(products) + p.Host = *host + p.Port = *port + p.User = *user + p.Name = *db + p.Password = *password + + return p +} + func main() { + params := parseFlags() + if len(params.Products) == 0 { + flag.PrintDefaults() + os.Exit(1) + } + logger := osutil.NewLogger() db := postgres.NewClient() - db.Name = "gdax" - db.User = "gdax" + db.Name = params.Name + db.User = params.User + db.Password = params.Password + db.Host = params.Host + db.Port = params.Port db.SetLogger(logger) err := db.Open() if err != nil { logger.Info("Error openning database connection: %v", err) return } + defer db.Close() ws := websocket.NewClient() ls := &gdax.ListenerService{} @@ -32,30 +92,49 @@ func main() { r.Database = db r.Listener = ls r.SetLogger(logger) - if err := r.Record("ETH-USD", "BTC-USD"); err != nil { + if err := r.Record(params.Products...); err != nil { logger.Info("Error: %v", err) return } + defer ls.Close() + a := ops.Aggregator{} a.Database = db a.Logger = logger - a.Products = []string{"ETH-USD", "BTC-USD"} + a.Products = params.Products a.Start(30 * time.Second) v := ops.Validator{} v.Database = db v.Logger = logger - v.Products = []string{"ETH-USD", "BTC-USD"} + v.Products = params.Products v.Start(30 * time.Second) av := ops.AggregationValidator{} av.Database = db av.Logger = logger - av.Products = []string{"ETH-USD", "BTC-USD"} + av.Products = params.Products av.Intervals = []time.Duration{30 * time.Second} av.Start(30 * time.Second) - t := make(chan bool) - <-t + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt) + + var done bool + for !done { + select { + case <- c: + done = true + } + } +} + +func (l *StringList) String() string { + return "List of strings" +} + +func (l *StringList) Set(value string) error { + *l = append(*l, value) + return nil } diff --git a/product.go b/product.go new file mode 100644 index 0000000..f1fbca9 --- /dev/null +++ b/product.go @@ -0,0 +1,37 @@ +package tacitus + +import ( + "strings" +) + +type Product string + +const ( + BtcUsd = "BTC-USD" + BtcEur = "BTC-EUR" + BtcGbp = "BTC-GBP" + + BchUsd = "BCH-USD" + + EthUsd = "ETH-USD" + EthBtc = "ETH-BTC" + EthEur = "ETH-EUR" + + LtcUsd = "LTC-USD" + LtcBtc = "LTC-BTC" + LtcEur = "LTC-EUR" +) + +var products = [...]string{BtcUsd, BtcEur, BtcGbp, BchUsd, EthUsd, EthBtc, + EthEur, LtcUsd, LtcBtc, LtcEur} + +func ValidProduct(prod string) bool { + prod = strings.ToUpper(prod) + for _, p := range products { + if p == prod { + return true + } + } + + return false +} diff --git a/trade.go b/trade.go index fb95611..f422031 100644 --- a/trade.go +++ b/trade.go @@ -3,12 +3,9 @@ package tacitus import ( "github.com/shopspring/decimal" - "strings" "time" ) -var PRODUCTS = [...]string{"BTC-USD", "ETH-USD"} - type Trade struct { Id int `json:"-"` TradeId int `json:"trade_id,"` @@ -35,14 +32,3 @@ type TradeService interface { TradesAfterAll(product string, id int) (Results, error) TradesWhereResults(sql string, params ...interface{}) (Results, error) } - -func ValidProduct(prod string) bool { - prod = strings.ToUpper(prod) - for _, p := range PRODUCTS { - if p == prod { - return true - } - } - - return false -}