Skip to content

Commit

Permalink
k线接口,查询优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Queenie authored and Queenie committed Oct 29, 2019
1 parent cf4322f commit a43690d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
76 changes: 29 additions & 47 deletions api/v1/kLine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ type chart struct {
}

func V1GetK(context echo.Context) error {
var market Market
mainDB := utils.MainDbBegin()
defer mainDB.DbRollback()
if mainDB.Where("code = ?", context.QueryParam("market")).First(&market).RecordNotFound() {
market, err := FindMarketByCode(context.QueryParam("market"))
if err != nil {
return utils.BuildError("1021")
}
limit := 100
Expand All @@ -41,73 +39,57 @@ func V1GetK(context echo.Context) error {
limit = 10000
}
}
period := 1
periodstr, _ := strconv.Atoi(context.QueryParam("period"))
periods := []int{1, 5, 15, 30, 60, 120, 240, 360, 720, 1440, 4320, 10080}
periodBool := false
for _, per := range periods {
if periodstr == per {
periodBool = true
period = per
}
}
if !periodBool {
period, _ := strconv.ParseInt(context.QueryParam("period"), 10, 64)
if period == 0 {
return utils.BuildError("1053")
}

kRedis := utils.GetRedisConn("k")
defer kRedis.Close()

var timestamp int
var timestamp int64
if context.QueryParam("timestamp") == "" {
timestamp = int(time.Now().Unix())
timestamp = time.Now().Unix()
} else {
timestamp, _ = strconv.Atoi(context.QueryParam("timestamp"))
timestamp, _ = strconv.ParseInt(context.QueryParam("timestamp"), 10, 64)
}
minTimestamp := timestamp - period*60*limit

values, _ := redis.Values(
kRedis.Do(
"ZREVRANGEBYSCORE",
market.KLineRedisKey(period),
timestamp,
minTimestamp,
),
)

var result []interface{}
for _, value := range values {
var item []string
json.Unmarshal(value.([]byte), &item)
result = append(result, item)
kRedis := utils.GetRedisConn("data")
defer kRedis.Close()
values, _ := redis.Values(kRedis.Do("ZREVRANGEBYSCORE", market.KLineRedisKey(period), timestamp, 0, "limit", 0, limit, "withscores"))
var line []KLine
var k KLine
for i, value := range values {
if i%2 == 0 {
json.Unmarshal(value.([]byte), &k)
} else {
k.Timestamp, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
line = append(line, k)
}
}
response := utils.SuccessResponse
response.Body = result
response.Body = line
return context.JSON(http.StatusOK, response)
}

func V1GetChart(context echo.Context) error {
var market Market
mainDB := utils.MainDbBegin()
defer mainDB.DbRollback()
if mainDB.Where("code = ?", context.QueryParam("market")).First(&market).RecordNotFound() {
market, err := FindMarketByCode(context.QueryParam("market"))
if err != nil {
return utils.BuildError("1021")
}
kRedis := utils.GetRedisConn("k")
defer kRedis.Close()
limit := 100
timestamp := int(time.Now().Unix())
timestamp := time.Now().Unix()

var c chart
periods := []int{1, 5, 15, 30, 60, 120, 240, 360, 720, 1440, 4320, 10080}
periods := []int64{1, 5, 15, 30, 60, 120, 240, 360, 720, 1440, 4320, 10080}
for _, period := range periods {
minTimestamp := timestamp - period*60*limit

values, _ := redis.Values(
kRedis.Do(
"ZREVRANGEBYSCORE",
market.KLineRedisKey(period),
timestamp,
minTimestamp,
0,
"limit",
0,
limit,
),
)
var items []interface{}
Expand Down
4 changes: 4 additions & 0 deletions initializers/locales/error_code.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
success_code:
'1000': Success.
error_code:
'1021': 没有匹配的market。

'1053': 无效的 period.

'3020': No record can be found.
'3021': 暂不开放用户注册。
'3022': 下单失败。
Expand Down
4 changes: 4 additions & 0 deletions initializers/locales/error_code.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
success_code:
'1000': 成功。
error_code:
'1021': 没有匹配的market。

'1053': 无效的 period.

'3020': 找不到记录。
'3021': 暂不开放用户注册。
'3022': 下单失败。
Expand Down
12 changes: 11 additions & 1 deletion models/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func FindMarketById(id int) (Market, error) {
return market, fmt.Errorf("No market can be found.")
}

func FindMarketByCode(code string) (Market, error) {
for _, market := range Markets {
if market.Code == code {
return market, nil
}
}
var market Market
return market, fmt.Errorf("No market can be found.")
}

func (market *Market) AfterCreate(db *gorm.DB) {
tickerRedis := utils.GetRedisConn("ticker")
defer tickerRedis.Close()
Expand Down Expand Up @@ -100,7 +110,7 @@ func (market *Market) LatestTradesRedisKey() string {
func (market *Market) TickerRedisKey() string {
return "goDCE:ticker:" + market.Code
}
func (market *Market) KLineRedisKey(period int) string {
func (market *Market) KLineRedisKey(period int64) string {
return fmt.Sprintf("goDCE:k:%v:%v", market.Id, period)
}

Expand Down

0 comments on commit a43690d

Please sign in to comment.