Skip to content

Commit

Permalink
✨ Add appeal count reset
Browse files Browse the repository at this point in the history
  • Loading branch information
foysalit committed Sep 20, 2024
1 parent 73fd1f8 commit 268065e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions automod/countstore/countstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type CountStore interface {
// TODO: batch increment method
GetCountDistinct(ctx context.Context, name, bucket, period string) (int, error)
IncrementDistinct(ctx context.Context, name, bucket, val string) error
Reset(ctx context.Context, name, val string) error
}

func periodBucket(name, val, period string) string {
Expand Down
6 changes: 6 additions & 0 deletions automod/countstore/countstore_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (s MemCountStore) Increment(ctx context.Context, name, val string) error {
return nil
}

func (s *MemCountStore) Reset(ctx context.Context, name, val string) error {
key := periodBucket(name, val, PeriodTotal)
s.Counts.Delete(key)
return nil
}

func (s MemCountStore) IncrementPeriod(ctx context.Context, name, val, period string) error {
k := periodBucket(name, val, period)
s.Counts.Compute(k, func(oldVal int, _ bool) (int, bool) {
Expand Down
11 changes: 11 additions & 0 deletions automod/countstore/countstore_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ func (s *RedisCountStore) Increment(ctx context.Context, name, val string) error
return err
}

func (s *RedisCountStore) Reset(ctx context.Context, name, val string) error {
var key string

// increment multiple counters in a single redis round-trip
multi := s.Client.Pipeline()
key = redisCountPrefix + periodBucket(name, val, PeriodHour)
multi.Del(ctx, key)
_, err := multi.Exec(ctx)
return err
}

// Variant of Increment() which only acts on a single specified time period. The intended us of this variant is to control the total number of counters persisted, by using a relatively short time period, for which the counters will expire.
func (s *RedisCountStore) IncrementPeriod(ctx context.Context, name, val, period string) error {

Expand Down
3 changes: 3 additions & 0 deletions automod/engine/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func (c *BaseContext) GetAccountMeta(did syntax.DID) *AccountMeta {
func (c *BaseContext) Increment(name, val string) {
c.effects.Increment(name, val)
}
func (c *BaseContext) ResetCounter(name, val string) {
c.effects.ResetCounter(name, val)
}

func (c *BaseContext) IncrementDistinct(name, bucket, val string) {
c.effects.IncrementDistinct(name, bucket, val)
Expand Down
12 changes: 12 additions & 0 deletions automod/engine/effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Effects struct {
mu sync.Mutex
// List of counters which should be incremented as part of processing this event. These are collected during rule execution and persisted in bulk at the end.
CounterIncrements []CounterRef
// List of counters which should be reset as part of processing this event. These are collected during rule execution and persisted in bulk at the end.
CounterResets []CounterRef
// Similar to "CounterIncrements", but for "distinct" style counters
CounterDistinctIncrements []CounterDistinctRef // TODO: better variable names
// Label values which should be applied to the overall account, as a result of rule execution.
Expand Down Expand Up @@ -76,6 +78,16 @@ func (e *Effects) Increment(name, val string) {
e.CounterIncrements = append(e.CounterIncrements, CounterRef{Name: name, Val: val})
}

// Enqueues the named counter to be reset at the end of all rule processing.
//
// "name" is the counter namespace.
// "val" is the specific counter with that namespace.
func (e *Effects) ResetCounter(name, val string) {
e.mu.Lock()
defer e.mu.Unlock()
e.CounterResets = append(e.CounterResets, CounterRef{Name: name, Val: val})
}

// Enqueues the named counter to be incremented at the end of all rule processing. Will only increment the indicated time period bucket.
func (e *Effects) IncrementPeriod(name, val string, period string) {
e.mu.Lock()
Expand Down
6 changes: 6 additions & 0 deletions automod/engine/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func (eng *Engine) persistCounters(ctx context.Context, eff *Effects) error {
return err
}
}
for _, ref := range eff.CounterResets {
err := eng.Counters.Reset(ctx, ref.Name, ref.Val)
if err != nil {
return err
}
}
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions automod/rules/resolve_appeal_on_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ func MarkAppealOzoneEventRule(c *automod.OzoneEventContext) error {
}

if isAppealEvent {
c.Increment("appealed", counterKey)
c.Increment("appeal", counterKey)
} else {
// @TODO: We should reset the appeal counter here but there doesn't seem to be an api for it
// c.Reset("appealed", counterKey)
c.ResetCounter("appeal", counterKey)
}

return nil
Expand Down

0 comments on commit 268065e

Please sign in to comment.