Enhanced Purge Job to include retention policy for Notifications

This commit is contained in:
Shaun Walker
2022-02-24 12:37:06 -05:00
parent 82fef82c4f
commit 0fba385b9e
8 changed files with 63 additions and 16 deletions

View File

@ -8,6 +8,6 @@ namespace Oqtane.Repository
IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows);
Log GetLog(int logId);
void AddLog(Log log);
int DeleteLogs(int age);
int DeleteLogs(int siteId, int age);
}
}

View File

@ -11,5 +11,6 @@ namespace Oqtane.Repository
Notification GetNotification(int notificationId);
Notification GetNotification(int notificationId, bool tracking);
void DeleteNotification(int notificationId);
int DeleteNotifications(int siteId, int age);
}
}

View File

@ -12,6 +12,6 @@ namespace Oqtane.Repository
Visitor GetVisitor(int visitorId);
Visitor GetVisitor(int siteId, string IPAddress);
void DeleteVisitor(int visitorId);
int DeleteVisitors(int age);
int DeleteVisitors(int siteId, int age);
}
}

View File

@ -49,19 +49,19 @@ namespace Oqtane.Repository
_db.SaveChanges();
}
public int DeleteLogs(int age)
public int DeleteLogs(int siteId, int age)
{
// delete logs in batches of 100 records
int count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age);
var logs = _db.Log.Where(item => item.Level != "Error" && item.LogDate < purgedate)
var logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
.OrderBy(item => item.LogDate).Take(100).ToList();
while (logs.Count > 0)
{
count += logs.Count;
_db.Log.RemoveRange(logs);
_db.SaveChanges();
logs = _db.Log.Where(item => item.Level != "Error" && item.LogDate < purgedate)
logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
.OrderBy(item => item.LogDate).Take(100).ToList();
}
return count;

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
@ -68,6 +69,25 @@ namespace Oqtane.Repository
_db.Notification.Remove(notification);
_db.SaveChanges();
}
public int DeleteNotifications(int siteId, int age)
{
// delete notifications in batches of 100 records
int count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age);
var notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
while (notifications.Count > 0)
{
count += notifications.Count;
_db.Notification.RemoveRange(notifications);
_db.SaveChanges();
notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
}
return count;
}
}
}

View File

@ -53,19 +53,19 @@ namespace Oqtane.Repository
_db.SaveChanges();
}
public int DeleteVisitors(int age)
public int DeleteVisitors(int siteId, int age)
{
// delete visitors in batches of 100 records
int count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age);
var visitors = _db.Visitor.Where(item => item.Visits <= 1 && item.VisitedOn < purgedate)
var visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
.OrderBy(item => item.VisitedOn).Take(100).ToList();
while (visitors.Count > 0)
{
count += visitors.Count;
_db.Visitor.RemoveRange(visitors);
_db.SaveChanges();
visitors = _db.Visitor.Where(item => item.Visits < 2 && item.VisitedOn < purgedate)
visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
.OrderBy(item => item.VisitedOn).Take(100).ToList();
}
return count;