Skip to content

Commit

Permalink
有问题,需要改
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Oct 29, 2024
1 parent 6270b2c commit fcdc7a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
34 changes: 17 additions & 17 deletions pkg/accumulate/accumulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -40,19 +40,19 @@ 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
}
}
}
}

for key, value := range a.TargetFields {
for key, value := range a.Fields {

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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit fcdc7a5

Please sign in to comment.