generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRD-684 price filler & tradelogs query server
- Loading branch information
1 parent
1b4e334
commit e45261e
Showing
10 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/KyberNetwork/go-binance/v2" | ||
libapp "github.com/KyberNetwork/tradelogs/v2/pkg/app" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/price_filler" | ||
storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types" | ||
zxotcStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxotc" | ||
"github.com/KyberNetwork/tradinglib/pkg/dbutil" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/urfave/cli" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
app := libapp.NewApp() | ||
app.Name = "trade logs crawler service" | ||
app.Action = run | ||
app.Flags = append(app.Flags, libapp.PostgresSQLFlags("tradelogs_v2")...) | ||
app.Flags = append(app.Flags, libapp.PriceFillerFlags()...) | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
|
||
func run(c *cli.Context) error { | ||
logger, _, flush, err := libapp.NewLogger(c) | ||
if err != nil { | ||
return fmt.Errorf("new logger: %w", err) | ||
} | ||
|
||
defer flush() | ||
|
||
zap.ReplaceGlobals(logger) | ||
l := logger.Sugar() | ||
l.Infow("Starting price filler service") | ||
|
||
db, err := initDB(c) | ||
l.Infow("init db successfully") | ||
if err != nil { | ||
return fmt.Errorf("cannot init DB: %w", err) | ||
} | ||
|
||
// trade log storages | ||
s := []storageTypes.Storage{ | ||
zxotcStorage.New(l, db), | ||
} | ||
|
||
binanceClient := binance.NewClient(c.String(libapp.BinanceAPIKeyFlag.Name), c.String(libapp.BinanceSecretKeyFlag.Name)) | ||
priceFiller, err := pricefiller.NewPriceFiller(l, binanceClient, s) | ||
if err != nil { | ||
l.Errorw("Error while init price filler") | ||
return err | ||
} | ||
priceFiller.Run() | ||
return nil | ||
} | ||
|
||
func initDB(c *cli.Context) (*sqlx.DB, error) { | ||
db, err := libapp.NewDB(map[string]interface{}{ | ||
"host": c.String(libapp.PostgresHost.Name), | ||
"port": c.Int(libapp.PostgresPort.Name), | ||
"user": c.String(libapp.PostgresUser.Name), | ||
"password": c.String(libapp.PostgresPassword.Name), | ||
"dbname": c.String(libapp.PostgresDatabase.Name), | ||
"sslmode": "disable", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = dbutil.RunMigrationUp(db.DB, c.String(libapp.PostgresMigrationPath.Name), | ||
c.String(libapp.PostgresDatabase.Name)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/KyberNetwork/tradelogs/v2/internal/server" | ||
"log" | ||
"os" | ||
|
||
libapp "github.com/KyberNetwork/tradelogs/v2/pkg/app" | ||
storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types" | ||
zxotcStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxotc" | ||
"github.com/KyberNetwork/tradinglib/pkg/dbutil" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/urfave/cli" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
app := libapp.NewApp() | ||
app.Name = "trade logs crawler service" | ||
app.Action = run | ||
app.Flags = append(app.Flags, libapp.PostgresSQLFlags("tradelogs_v2")...) | ||
app.Flags = append(app.Flags, libapp.HTTPServerFlags()...) | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
|
||
func run(c *cli.Context) error { | ||
logger, _, flush, err := libapp.NewLogger(c) | ||
if err != nil { | ||
return fmt.Errorf("new logger: %w", err) | ||
} | ||
|
||
defer flush() | ||
|
||
zap.ReplaceGlobals(logger) | ||
l := logger.Sugar() | ||
l.Infow("Starting trade logs server") | ||
|
||
db, err := initDB(c) | ||
l.Infow("init db successfully") | ||
if err != nil { | ||
return fmt.Errorf("cannot init DB: %w", err) | ||
} | ||
|
||
// trade log storages | ||
storage := []storageTypes.Storage{ | ||
zxotcStorage.New(l, db), | ||
} | ||
|
||
s := server.NewTradeLogs(l, storage, c.String(libapp.HTTPTradeLogsServerFlag.Name)) | ||
return s.Run() | ||
} | ||
|
||
func initDB(c *cli.Context) (*sqlx.DB, error) { | ||
db, err := libapp.NewDB(map[string]interface{}{ | ||
"host": c.String(libapp.PostgresHost.Name), | ||
"port": c.Int(libapp.PostgresPort.Name), | ||
"user": c.String(libapp.PostgresUser.Name), | ||
"password": c.String(libapp.PostgresPassword.Name), | ||
"dbname": c.String(libapp.PostgresDatabase.Name), | ||
"sslmode": "disable", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = dbutil.RunMigrationUp(db.DB, c.String(libapp.PostgresMigrationPath.Name), | ||
c.String(libapp.PostgresDatabase.Name)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types" | ||
"github.com/gin-contrib/pprof" | ||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
maxTimeRange = uint64(7 * 24 * time.Hour.Milliseconds()) | ||
) | ||
|
||
type TradeLogs struct { | ||
r *gin.Engine | ||
bindAddr string | ||
l *zap.SugaredLogger | ||
storage []storageTypes.Storage | ||
} | ||
|
||
func NewTradeLogs(l *zap.SugaredLogger, s []storageTypes.Storage, bindAddr string) *TradeLogs { | ||
engine := gin.New() | ||
engine.Use(gin.Recovery()) | ||
|
||
server := &TradeLogs{ | ||
r: engine, | ||
bindAddr: bindAddr, | ||
l: l, | ||
storage: s, | ||
} | ||
|
||
gin.SetMode(gin.ReleaseMode) | ||
server.register() | ||
|
||
return server | ||
} | ||
|
||
// Run runs server. | ||
func (s *TradeLogs) Run() error { | ||
if err := s.r.Run(s.bindAddr); err != nil { | ||
return fmt.Errorf("run server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *TradeLogs) register() { | ||
pprof.Register(s.r, "/debug") | ||
s.r.GET("/tradelogs", s.getTradeLogs) | ||
} | ||
|
||
func (s *TradeLogs) getTradeLogs(c *gin.Context) { | ||
var ( | ||
query storageTypes.TradeLogsQuery | ||
err = c.ShouldBind(&query) | ||
) | ||
|
||
if err != nil { | ||
responseErr(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
if query.ToTime < query.FromTime { | ||
responseErr(c, http.StatusBadRequest, fmt.Errorf("to_time cannot smaller than from_time")) | ||
return | ||
} | ||
|
||
if query.ToTime-query.FromTime > maxTimeRange { | ||
responseErr(c, http.StatusBadRequest, fmt.Errorf("max time range: %v", maxTimeRange)) | ||
return | ||
} | ||
|
||
var data []storageTypes.TradeLog | ||
for _, storage := range s.storage { | ||
tradeLogs, err := storage.Get(query) | ||
if err != nil { | ||
responseErr(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
data = append(data, tradeLogs...) | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"data": data, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.