delete orphan nodes (fix #778) (#794)

* delete orphan nodes (for #778 and partially #781)

* and do it as well for decisions
This commit is contained in:
Thibault "bui" Koechlin 2021-05-17 11:45:01 +02:00 committed by GitHub
parent 98277f5bb7
commit f881510f79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -575,11 +575,36 @@ func (c *Client) DeleteAlertWithFilter(filter map[string][]string) (int, error)
return len(alertsToDelete), nil
}
func (c *Client) FlushOrphans() {
/* While it has only been linked to some very corner-case bug : https://github.com/crowdsecurity/crowdsec/issues/778 */
/* We want to take care of orphaned events for which the parent alert/decision has been deleted */
events_count, err := c.Ent.Event.Delete().Where(event.Not(event.HasOwner())).Exec(c.CTX)
if err != nil {
c.Log.Warningf("error while deleting orphan events : %s", err)
return
}
if events_count > 0 {
c.Log.Infof("%d deleted orphan events", events_count)
}
events_count, err = c.Ent.Decision.Delete().Where(decision.Not(decision.HasOwner())).Exec(c.CTX)
if err != nil {
c.Log.Warningf("error while deleting orphan decisions : %s", err)
return
}
if events_count > 0 {
c.Log.Infof("%d deleted orphan decisions", events_count)
}
}
func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
var deletedByAge int
var deletedByNbItem int
var totalAlerts int
var err error
c.FlushOrphans()
totalAlerts, err = c.TotalAlerts()
if err != nil {
c.Log.Warningf("FlushAlerts (max items count) : %s", err)