Skip to content

Commit

Permalink
优化累加器结构
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Nov 1, 2024
1 parent 42c17e7 commit dda0d92
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 61 deletions.
110 changes: 53 additions & 57 deletions pkg/accumulate/accumulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,44 @@ import (
)

type Field struct {
Key string
Value any
Key string `json:"key"`
Value any `json:"value"`

_key gval.Evaluable
_value gval.Evaluable
}

func (f *Field) Compile() (err error) {
if expr, has := strings.CutPrefix(f.Key, "="); has {
f._key, err = calc.New(expr)
if err != nil {
return err
}
}
if value, ok := f.Value.(string); ok {
if expr, has := strings.CutPrefix(value, "="); has {
f._value, err = calc.New(expr)
if err != nil {
return err
}
}
}
return nil
}

type Result struct {
Target string
Meta map[string]any
Values map[string]any
Target string `json:"target"`
Filter map[string]any `json:"filter"`
Values map[string]any `json:"values"`
}

// Accumulation 累积器,空间换时间,主要用于统计
type Accumulation struct {
Target string `json:"target"`
Meta map[string]any `json:"meta"`
Fields map[string]any `json:"fields"`
Target string `json:"target"`
Filter []*Field `json:"filter"`
Values []*Field `json:"values"`

_target gval.Evaluable
_meta map[string]gval.Evaluable
_fields []*Field
}

func (a *Accumulation) Init() (err error) {
Expand All @@ -40,41 +56,18 @@ func (a *Accumulation) Init() (err error) {
}
}

a._meta = make(map[string]gval.Evaluable)
for key, value := range a.Meta {
if val, ok := value.(string); ok {
if expr, has := strings.CutPrefix(val, "="); has {
a._meta[key], err = calc.New(expr)
if err != nil {
return err
}
}
for _, f := range a.Filter {
err = f.Compile()
if err != nil {
return err
}
}

for key, value := range a.Fields {

f := &Field{Key: key, Value: value}

//键
if expr, has := strings.CutPrefix(key, "="); has {
f._key, err = calc.New(expr)
if err != nil {
return err
}
}

//值
if val, ok := value.(string); ok {
if expr, has := strings.CutPrefix(val, "="); has {
f._value, err = calc.New(expr)
if err != nil {
return err
}
}
for _, f := range a.Values {
err = f.Compile()
if err != nil {
return err
}

a._fields = append(a._fields, f)
}

return nil
Expand All @@ -94,22 +87,31 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) {
}

//过滤器
ret.Meta = make(map[string]any)
for key, value := range a.Meta {
if val, has := a._meta[key]; has {
ret.Meta[key], err = val(context.Background(), args)
ret.Filter = make(map[string]any)
for _, f := range a.Filter {

key := f.Key
if f._key != nil {
key, err = f._key.EvalString(context.Background(), args)
if err != nil {
return
}
}

value := f.Value
if f._value != nil {
value, err = f._key(context.Background(), args)
if err != nil {
return
}
} else {
ret.Meta[key] = value
}
ret.Filter[key] = value
}

//值
ret.Values = make(map[string]any)
for _, f := range a.Values {

for _, f := range a._fields {
key := f.Key
if f._key != nil {
key, err = f._key.EvalString(context.Background(), args)
Expand All @@ -118,20 +120,14 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) {
}
}

val := f.Value
value := f.Value
if f._value != nil {
val, err = f._value.EvalFloat64(context.Background(), args)
value, err = f._key(context.Background(), args)
if err != nil {
return
}

//路过0值
if val.(float64) == 0.0 {
continue
}
}

ret.Values[key] = val
ret.Filter[key] = value
}

return &ret, nil
Expand Down
6 changes: 6 additions & 0 deletions table/conf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package table

import (
"errors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"io"
Expand Down Expand Up @@ -49,6 +50,10 @@ func ApiConf(ctx *gin.Context) {
fn := filepath.Join(viper.GetString("data"), Path, tab, conf)
_, err := os.Stat(fn)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Fail(ctx, "不存在")
return
}
Error(ctx, err)
return
}
Expand All @@ -67,6 +72,7 @@ func ApiConfUpdate(ctx *gin.Context) {
Error(ctx, err)
return
}
defer file.Close()

_, err = io.Copy(file, ctx.Request.Body)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (t *Table) Insert(doc any) (id string, err error) {
}

if len(ret.Values) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$inc": ret.Values}, true)
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$inc": ret.Values}, true)
if err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (t *Table) Delete(id string) error {
continue
}
if len(ret.Values) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$dec": ret.Values}, false)
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$dec": ret.Values}, false)
if err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -392,7 +392,7 @@ func (t *Table) Update(id string, update any) error {
continue
}
if len(ret.Values) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$dec": ret.Values}, true)
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$dec": ret.Values}, true)
if err != nil {
log.Error(err)
}
Expand All @@ -411,7 +411,7 @@ func (t *Table) Update(id string, update any) error {
continue
}
if len(ret.Values) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$inc": ret.Values}, true)
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$inc": ret.Values}, true)
if err != nil {
log.Error(err)
}
Expand Down

0 comments on commit dda0d92

Please sign in to comment.