Skip to content

Commit

Permalink
revert - fix recent revert tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianZaremba committed Dec 11, 2024
1 parent 942dc68 commit 0c718a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cluebot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CREATE TABLE IF NOT EXISTS `last_revert`
`id` int(11) NOT NULL auto_increment,
`title` varchar(256) NOT NULL,
`user` varchar(256) NOT NULL,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`time` int NOT NULL,
PRIMARY KEY (`id`),
INDEX (`title`, `user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
6 changes: 4 additions & 2 deletions pkg/cbng/database/cluebot/cluebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (ci *CluebotInstance) GetLastRevertTime(l *logrus.Entry, ctx context.Contex
return revertTime
}

func (ci *CluebotInstance) SaveRevertTime(l *logrus.Entry, ctx context.Context, title, user string) int64 {
func (ci *CluebotInstance) SaveRevertTime(l *logrus.Entry, ctx context.Context, title, user string) error {
logger := l.WithFields(logrus.Fields{
"function": "database.cluebot.SaveRevertTime",
"args": map[string]interface{}{
Expand All @@ -195,6 +195,7 @@ func (ci *CluebotInstance) SaveRevertTime(l *logrus.Entry, ctx context.Context,
if err != nil {
logger.Infof("Error running query: %v", err)
span.SetStatus(codes.Error, err.Error())
return err
} else {
defer rows.Close()
if !rows.Next() {
Expand All @@ -203,9 +204,10 @@ func (ci *CluebotInstance) SaveRevertTime(l *logrus.Entry, ctx context.Context,
if err := rows.Scan(&revertTime); err != nil {
logger.Errorf("Error reading rows for query: %v", err)
span.SetStatus(codes.Error, err.Error())
return err
}
}
}

return revertTime
return nil
}
27 changes: 19 additions & 8 deletions pkg/cbng/processor/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func shouldRevert(l *logrus.Entry, parentCtx context.Context, configuration *con

// If we reverted this user/page before in the last 24 hours, don't
lastRevertTime := db.ClueBot.GetLastRevertTime(logger, ctx, change.Common.Title, change.User.Username)
if lastRevertTime != 0 && lastRevertTime < 86400 {
if lastRevertTime != 0 && lastRevertTime > time.Now().UTC().Unix()-86400 {
change.RevertReason = "Reverted before"
metrics.RevertStatus.With(prometheus.Labels{"state": "should_revert", "status": "failed", "meta": "recent_revert"}).Inc()
return false
Expand Down Expand Up @@ -276,6 +276,11 @@ func processSingleRevertChange(logger *logrus.Entry, parentCtx context.Context,
}
logger.Infof("Generated vandalism id %v", mysqlVandalismId)

// Log the revert time for later
if err := db.ClueBot.SaveRevertTime(logger, ctx, change.Common.Title, change.User.Username); err != nil {
logger.Warnf("Failed to save revert time: %v", err)
}

// Revert or not
if !shouldRevert(logger, ctx, configuration, db, change) {
metrics.EditStatus.With(prometheus.Labels{"state": "revert", "status": "skipped"}).Inc()
Expand All @@ -294,15 +299,21 @@ func processSingleRevertChange(logger *logrus.Entry, parentCtx context.Context,
r.SendRevert(fmt.Sprintf("%s (Reverted) (%s) (%d s)", change.FormatIrcRevert(), change.RevertReason, time.Now().Unix()-change.ReceivedTime.Unix()))
r.SendSpam(fmt.Sprintf("%s # %f # %s # Reverted", change.FormatIrcChange(), change.VandalismScore, change.RevertReason))
} else {
metrics.EditStatus.With(prometheus.Labels{"state": "revert", "status": "failed"}).Inc()
logger.Infof("Failed to revert")
revision := api.GetPage(logger, ctx, helpers.PageTitle(change.Common.Namespace, change.Common.Title))
if revision != nil && change.User.Username != revision.User {
change.RevertReason = fmt.Sprintf("Beaten by %s", revision.User)
db.ClueBot.MarkVandalismRevertBeaten(logger, ctx, mysqlVandalismId, change.Common.Title, change.GetDiffUrl(), revision.User)

r.SendRevert(fmt.Sprintf("%s (Not Reverted) (%s) (%d s)", change.FormatIrcRevert(), change.RevertReason, time.Now().Unix()-change.ReceivedTime.Unix()))
r.SendSpam(fmt.Sprintf("%s # %f # %s # Not Reverted", change.FormatIrcChange(), change.VandalismScore, change.RevertReason))
if revision != nil {
if change.User.Username == revision.User {
metrics.EditStatus.With(prometheus.Labels{"state": "revert", "status": "self_beaten"}).Inc()
} else {
metrics.EditStatus.With(prometheus.Labels{"state": "revert", "status": "beaten"}).Inc()
change.RevertReason = fmt.Sprintf("Beaten by %s", revision.User)
db.ClueBot.MarkVandalismRevertBeaten(logger, ctx, mysqlVandalismId, change.Common.Title, change.GetDiffUrl(), revision.User)

r.SendRevert(fmt.Sprintf("%s (Not Reverted) (%s) (%d s)", change.FormatIrcRevert(), change.RevertReason, time.Now().Unix()-change.ReceivedTime.Unix()))
r.SendSpam(fmt.Sprintf("%s # %f # %s # Not Reverted", change.FormatIrcChange(), change.VandalismScore, change.RevertReason))
}
} else {
metrics.EditStatus.With(prometheus.Labels{"state": "revert", "status": "failed"}).Inc()
}
}
return nil
Expand Down

0 comments on commit 0c718a2

Please sign in to comment.