Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3b667443c2 | ||
![]() |
7fbc4041a3 | ||
![]() |
dfb682c027 | ||
![]() |
62a37d7342 |
@ -11,3 +11,5 @@ go mod tidy
|
|||||||
go mod vendor
|
go mod vendor
|
||||||
|
|
||||||
加入内网gitea库
|
加入内网gitea库
|
||||||
|
|
||||||
|
|
||||||
|
6
core.go
6
core.go
@ -699,9 +699,9 @@ func (cr *Core) GetExpiration(per string) (time.Duration, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
exp, err := cr.PeriodToMinutes(per)
|
exp, err := cr.PeriodToMinutes(per)
|
||||||
length, _ := cr.Cfg.Config.Get("sortedSet").Get("length").String()
|
length, _ := cr.Cfg.Config.Get("sortedSet").Get("length").Int64()
|
||||||
ln := ToInt64(length)
|
logrus.Info("length of sortedSet: ", length)
|
||||||
dur := time.Duration(exp*ln) * time.Minute
|
dur := time.Duration(exp*length) * time.Minute
|
||||||
return dur, err
|
return dur, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
maX.go
7
maX.go
@ -136,13 +136,12 @@ func (mxl *MaXList) RPush(sm *MaX) (Sample, error) {
|
|||||||
mxl.List = append(mxl.List, sm)
|
mxl.List = append(mxl.List, sm)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 冒泡排序
|
// 冒泡排序
|
||||||
func (mxl *MaXList) RecursiveBubbleS(length int, ctype string) error {
|
func (mxl *MaXList) RecursiveBubbleS(length int, ctype string) error {
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
return nil
|
return errors.New("length is zero")
|
||||||
}
|
}
|
||||||
realLength := len(mxl.List)
|
realLength := len(mxl.List)
|
||||||
//FIXME:在对这个List进行排序时,List中途长度变了,就会报错:
|
//FIXME:在对这个List进行排序时,List中途长度变了,就会报错:
|
||||||
@ -166,6 +165,6 @@ func (mxl *MaXList) RecursiveBubbleS(length int, ctype string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
length--
|
length--
|
||||||
mxl.RecursiveBubbleS(length, ctype)
|
err := mxl.RecursiveBubbleS(length, ctype)
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
91
plate.go
91
plate.go
@ -2,6 +2,7 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,23 +21,84 @@ func (pl *Plate) Init(instId string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO 从redis里读出来已经存储的plate,如果不存在就创建一个新的
|
// TODO 从redis里读出来已经存储的plate,如果不存在就创建一个新的
|
||||||
|
// LoadPlate 加载或创建指定Instrument的Plate
|
||||||
|
// 1. 尝试从Redis加载已存储的Plate
|
||||||
|
// 2. 如果Redis中不存在,则初始化一个新的Plate
|
||||||
|
// 3. 根据配置创建所有需要的Coaster
|
||||||
|
// 参数:
|
||||||
|
// - cr: 核心上下文对象
|
||||||
|
// - instId: Instrument ID
|
||||||
|
//
|
||||||
|
// 返回值:
|
||||||
|
// - *Plate: 加载或新建的Plate对象
|
||||||
|
// - error: 操作过程中发生的错误
|
||||||
func LoadPlate(cr *Core, instId string) (*Plate, error) {
|
func LoadPlate(cr *Core, instId string) (*Plate, error) {
|
||||||
|
// 初始化一个空的Plate对象
|
||||||
pl := Plate{}
|
pl := Plate{}
|
||||||
|
|
||||||
|
// 构造Redis中存储Plate数据的key,格式为"instId|plate"
|
||||||
plateName := instId + "|plate"
|
plateName := instId + "|plate"
|
||||||
_, err := cr.RedisLocalCli.Exists().Result()
|
|
||||||
if err == nil {
|
// 尝试从Redis获取Plate数据
|
||||||
str, _ := cr.RedisLocalCli.Get(plateName).Result()
|
str, err := cr.RedisLocalCli.Get(plateName).Result()
|
||||||
json.Unmarshal([]byte(str), &pl)
|
|
||||||
} else {
|
// 如果Redis中存在数据且没有错误
|
||||||
pl.Init(instId)
|
if err == nil && str != "" {
|
||||||
prs := cr.Cfg.Config.Get("candleDimentions").MustArray()
|
// 将JSON字符串反序列化为Plate对象
|
||||||
for _, v := range prs {
|
if err := json.Unmarshal([]byte(str), &pl); err != nil {
|
||||||
pl.MakeCoaster(cr, v.(string))
|
// 反序列化失败时初始化一个新的Plate
|
||||||
|
// logrus.Warnf("failed to unmarshal plate data from redis, init new plate: %v", err)
|
||||||
|
pl.Init(instId)
|
||||||
|
}
|
||||||
|
// 返回从Redis加载的Plate对象
|
||||||
|
return &pl, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redis不可用或数据不存在时,初始化一个新的Plate
|
||||||
|
pl.Init(instId)
|
||||||
|
|
||||||
|
// 从配置中获取candleDimentions配置项
|
||||||
|
prs := cr.Cfg.Config.Get("candleDimentions")
|
||||||
|
|
||||||
|
// 检查配置项是否存在
|
||||||
|
if prs == nil || prs.Interface() == nil {
|
||||||
|
return nil, fmt.Errorf("candleDimentions config not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将配置项转换为数组
|
||||||
|
periods := prs.MustArray()
|
||||||
|
|
||||||
|
// 遍历所有周期配置
|
||||||
|
for _, v := range periods {
|
||||||
|
// 将interface{}类型转换为string
|
||||||
|
period, ok := v.(string)
|
||||||
|
// 检查周期字符串是否有效
|
||||||
|
if !ok || period == "" {
|
||||||
|
continue // 跳过无效的周期配置
|
||||||
|
}
|
||||||
|
// 为每个周期创建Coaster
|
||||||
|
if err := pl.MakeCoaster(cr, period); err != nil {
|
||||||
|
// 如果创建失败,返回错误
|
||||||
|
return nil, fmt.Errorf("failed to create coaster for period %s: %v", period, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 将新创建的Plate保存到Redis(可选操作)
|
||||||
|
if err := savePlateToRedis(cr, plateName, &pl); err != nil {
|
||||||
|
// 保存失败时记录警告日志
|
||||||
|
// logrus.Warnf("failed to save plate to redis: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回创建好的Plate对象
|
||||||
return &pl, nil
|
return &pl, nil
|
||||||
}
|
}
|
||||||
|
func savePlateToRedis(cr *Core, key string, pl *Plate) error {
|
||||||
|
data, err := json.Marshal(pl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return cr.RedisLocalCli.Set(key, data, 0).Err()
|
||||||
|
}
|
||||||
func (pl *Plate) SetToKey(cr *Core) error {
|
func (pl *Plate) SetToKey(cr *Core) error {
|
||||||
js, _ := json.Marshal(*pl)
|
js, _ := json.Marshal(*pl)
|
||||||
plateName := pl.InstID + "|plate"
|
plateName := pl.InstID + "|plate"
|
||||||
@ -51,19 +113,22 @@ func (pl *Plate) MakeCoaster(cr *Core, period string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cdl.RecursiveBubbleS(len(cdl.List), "asc")
|
err = cdl.RecursiveBubbleS(len(cdl.List), "asc")
|
||||||
setName7 := "ma7|" + setName
|
setName7 := "ma7|" + setName
|
||||||
setName30 := "ma30|" + setName
|
setName30 := "ma30|" + setName
|
||||||
mxl7, err := cr.GetRangeMaXSortedSet(setName7, pl.Count, lastTime)
|
mxl7, err := cr.GetRangeMaXSortedSet(setName7, pl.Count, lastTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mxl7.RecursiveBubbleS(len(mxl7.List), "asc")
|
err = mxl7.RecursiveBubbleS(len(mxl7.List), "asc")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
mxl30, err := cr.GetRangeMaXSortedSet(setName30, pl.Count, lastTime)
|
mxl30, err := cr.GetRangeMaXSortedSet(setName30, pl.Count, lastTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mxl30.RecursiveBubbleS(len(mxl30.List), "asc")
|
err = mxl30.RecursiveBubbleS(len(mxl30.List), "asc")
|
||||||
coaster := Coaster{
|
coaster := Coaster{
|
||||||
InstID: pl.InstID,
|
InstID: pl.InstID,
|
||||||
Period: period,
|
Period: period,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user