diff --git a/pkg/accumulate/accumulate.go b/pkg/accumulate/accumulate.go index 6b60242..3ff4bef 100644 --- a/pkg/accumulate/accumulate.go +++ b/pkg/accumulate/accumulate.go @@ -16,19 +16,19 @@ type Field struct { } type Result struct { - Target string - MetaFields map[string]any - TargetFields map[string]any + Target string + Meta map[string]any + Values map[string]any } // Accumulation 累积器,空间换时间,主要用于统计 type Accumulation struct { - Target string `json:"target"` - MetaFields map[string]any `json:"meta_fields"` - TargetFields map[string]any `json:"target_fields"` + Target string `json:"target"` + Meta map[string]any `json:"meta"` + Fields map[string]any `json:"fields"` _target gval.Evaluable - _filter map[string]gval.Evaluable + _meta map[string]gval.Evaluable _fields []*Field } @@ -40,11 +40,11 @@ func (a *Accumulation) Init() (err error) { } } - a._filter = make(map[string]gval.Evaluable) - for key, value := range a.MetaFields { + 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._filter[key], err = calc.New(expr) + a._meta[key], err = calc.New(expr) if err != nil { return err } @@ -52,7 +52,7 @@ func (a *Accumulation) Init() (err error) { } } - for key, value := range a.TargetFields { + for key, value := range a.Fields { f := &Field{Key: key, Value: value} @@ -94,19 +94,19 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) { } //过滤器 - ret.MetaFields = make(map[string]any) - for key, value := range a._filter { + ret.Meta = make(map[string]any) + for key, value := range a._meta { if value != nil { - ret.MetaFields[key], err = a._target(context.Background(), args) + ret.Meta[key], err = a._target(context.Background(), args) if err != nil { return } } else { - ret.MetaFields[key] = a.MetaFields[key] + ret.Meta[key] = a.Meta[key] } } - ret.TargetFields = make(map[string]any) + ret.Values = make(map[string]any) for _, f := range a._fields { key := f.Key @@ -130,7 +130,7 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) { } } - ret.TargetFields[key] = val + ret.Values[key] = val } return &ret, nil diff --git a/table/table.go b/table/table.go index 45464ff..7ff9817 100644 --- a/table/table.go +++ b/table/table.go @@ -156,8 +156,8 @@ func (t *Table) Insert(doc any) (id string, err error) { continue } - if len(ret.TargetFields) > 0 { - _, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$inc": ret.TargetFields}, true) + if len(ret.Values) > 0 { + _, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$inc": ret.Values}, true) if err != nil { log.Error(err) } @@ -302,8 +302,8 @@ func (t *Table) Delete(id string) error { log.Error(err) continue } - if len(ret.TargetFields) > 0 { - _, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$dec": ret.TargetFields}, false) + if len(ret.Values) > 0 { + _, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$dec": ret.Values}, false) if err != nil { log.Error(err) } @@ -408,8 +408,8 @@ func (t *Table) Update(id string, update any) error { log.Error(err) continue } - if len(ret.TargetFields) > 0 { - _, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$dec": ret.TargetFields}, true) + if len(ret.Values) > 0 { + _, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$dec": ret.Values}, true) if err != nil { log.Error(err) } @@ -427,8 +427,8 @@ func (t *Table) Update(id string, update any) error { log.Error(err) continue } - if len(ret.TargetFields) > 0 { - _, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$inc": ret.TargetFields}, true) + if len(ret.Values) > 0 { + _, err = mongodb.UpdateMany(ret.Target, ret.Meta, bson.M{"$inc": ret.Values}, true) if err != nil { log.Error(err) }