core/plate.go
zhangkun9038@dingtalk.com b9d1cb390c trace bug
2025-01-15 00:24:56 +08:00

79 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package core
import (
"encoding/json"
"time"
)
type Plate struct {
InstID string `json:"instId,string"`
Scale float64 `json:"scale,number"`
Count int `json:"count,number"`
CoasterMap map[string]Coaster `json:"coasterMap"`
}
func (pl *Plate) Init(instId string) {
pl.InstID = instId
pl.Count = 24
pl.Scale = float64(0.005)
pl.CoasterMap = make(map[string]Coaster)
}
// TODO 从redis里读出来已经存储的plate如果不存在就创建一个新的
func LoadPlate(cr *Core, instId string) (*Plate, error) {
pl := Plate{}
plateName := instId + "|plate"
_, err := cr.RedisLocalCli.Exists().Result()
if err == nil {
str, _ := cr.RedisLocalCli.Get(plateName).Result()
json.Unmarshal([]byte(str), &pl)
} else {
pl.Init(instId)
prs := cr.Cfg.Config.Get("candleDimentions").MustArray()
for _, v := range prs {
pl.MakeCoaster(cr, v.(string))
}
}
return &pl, nil
}
func (pl *Plate) SetToKey(cr *Core) error {
js, _ := json.Marshal(*pl)
plateName := pl.InstID + "|plate"
_, err := cr.RedisLocalCli.Set(plateName, string(js), 0).Result()
return err
}
func (pl *Plate) MakeCoaster(cr *Core, period string) error {
lastTime := time.Now()
setName := "candle" + period + "|" + pl.InstID + "|sortedSet"
cdl, err := cr.GetRangeCandleSortedSet(setName, pl.Count, lastTime)
if err != nil {
return err
}
cdl.RecursiveBubbleS(len(cdl.List), "asc")
setName7 := "ma7|" + setName
setName30 := "ma30|" + setName
mxl7, err := cr.GetRangeMaXSortedSet(setName7, pl.Count, lastTime)
if err != nil {
return err
}
mxl7.RecursiveBubbleS(len(mxl7.List), "asc")
mxl30, err := cr.GetRangeMaXSortedSet(setName30, pl.Count, lastTime)
if err != nil {
return err
}
mxl30.RecursiveBubbleS(len(mxl30.List), "asc")
coaster := Coaster{
InstID: pl.InstID,
Period: period,
Count: pl.Count,
Scale: pl.Scale,
CandleList: *cdl,
Ma7List: *mxl7,
Ma30List: *mxl30,
}
pl.CoasterMap["period"+period] = coaster
return err
}