Skip to content

Commit

Permalink
Fix: ticker空值填充
Browse files Browse the repository at this point in the history
  • Loading branch information
leon authored and leon committed Sep 22, 2020
1 parent c24408d commit 95928b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
19 changes: 19 additions & 0 deletions initializers/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"encoding/json"
"fmt"

"github.com/gomodule/redigo/redis"

"github.com/oldfritter/goDCE/config"
. "github.com/oldfritter/goDCE/models"
"github.com/oldfritter/goDCE/utils"
)

func LoadLatestTickers() {
Expand All @@ -32,4 +35,20 @@ func LoadLatestTickers() {
}
return
}()

go func() {
tickerRedis := utils.GetRedisConn("ticker")
defer tickerRedis.Close()
for i, market := range AllMarkets {
jsonStr, err := redis.Bytes(tickerRedis.Do("GET", market.TickerRedisKey()))
if err != nil {
continue
}
var ticker Ticker
json.Unmarshal(jsonStr, &ticker)
if AllMarkets[i].Ticker == nil {
AllMarkets[i].Ticker = ticker.TickerAspect
}
}
}()
}
5 changes: 3 additions & 2 deletions models/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"

"github.com/jinzhu/gorm"
"github.com/oldfritter/goDCE/utils"
"github.com/shopspring/decimal"

"github.com/oldfritter/goDCE/utils"
)

type OrderCurrency struct {
Expand All @@ -32,7 +33,7 @@ type Market struct {
Tradable bool `json:"tradable"`

// 暂存数据
Ticker TickerAspect `sql:"-" json:"ticker"`
Ticker *TickerAspect `sql:"-" json:"ticker"`
LatestKLines map[int]KLine `sql:"-" json:"-"`

// 撮合相关属性
Expand Down
8 changes: 4 additions & 4 deletions models/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type TickerAspect struct {
}

type Ticker struct {
MarketId int `json:"market_id"`
At int64 `json:"at"`
Name string `json:"name"`
TickerAspect TickerAspect `json:"ticker"`
MarketId int `json:"market_id"`
At int64 `json:"at"`
Name string `json:"name"`
TickerAspect *TickerAspect `json:"ticker"`
}
20 changes: 11 additions & 9 deletions workers/sneakerWorkers/tickerWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"time"

"github.com/gomodule/redigo/redis"
sneaker "github.com/oldfritter/sneaker-go/v3"
"github.com/streadway/amqp"

"github.com/oldfritter/goDCE/config"
. "github.com/oldfritter/goDCE/models"
"github.com/oldfritter/goDCE/utils"
sneaker "github.com/oldfritter/sneaker-go/v3"
"github.com/streadway/amqp"
)

func InitializeTickerWorker() {
Expand Down Expand Up @@ -40,9 +41,7 @@ func buildTicker(marketId int) {
if err != nil {
fmt.Println("error:", err)
}
dataRedis := utils.GetRedisConn("data")
defer dataRedis.Close()
ticker := refreshTicker(dataRedis, &market)
ticker := refreshTicker(&market)
t, err := json.Marshal(ticker)
if err != nil {
fmt.Println("error:", err)
Expand All @@ -51,16 +50,19 @@ func buildTicker(marketId int) {
if err != nil {
fmt.Println("{ error:", err, "}")
}
dataRedis.Do("SET", market.TickerRedisKey(), string(t))

tickerRedis := utils.GetRedisConn("ticker")
defer tickerRedis.Close()
tickerRedis.Do("SET", market.TickerRedisKey(), string(t))
}

func refreshTicker(dataRedis redis.Conn, market *Market) (ticker Ticker) {
func refreshTicker(market *Market) (ticker Ticker) {
klineRedis := utils.GetRedisConn("kline")
defer klineRedis.Close()
now := time.Now()
ticker.MarketId = (*market).Id
ticker.At = now.Unix()
ticker.Name = (*market).Name
kJsons, _ := redis.Values(dataRedis.Do("ZRANGEBYSCORE", (*market).KLineRedisKey(1), now.Add(-time.Hour*24).Unix(), now.Unix()))
kJsons, _ := redis.Values(klineRedis.Do("ZRANGEBYSCORE", (*market).KLineRedisKey(1), now.Add(-time.Hour*24).Unix(), now.Unix()))
var k KLine
for i, kJson := range kJsons {
json.Unmarshal(kJson.([]byte), &k)
Expand Down

0 comments on commit 95928b0

Please sign in to comment.