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

@ -129,6 +129,12 @@
<input id="sender" class="form-control" @bind="@_smtpsender" /> <input id="sender" class="form-control" @bind="@_smtpsender" />
</div> </div>
</div> </div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="retention" HelpText="Number of days of notifications to retain" ResourceKey="Retention">Retention (Days): </Label>
<div class="col-sm-9">
<input id="retention" class="form-control" @bind="@_retention" />
</div>
</div>
<button type="button" class="btn btn-secondary" @onclick="SendEmail">@Localizer["Smtp.TestConfig"]</button> <button type="button" class="btn btn-secondary" @onclick="SendEmail">@Localizer["Smtp.TestConfig"]</button>
<br /><br /> <br /><br />
</div> </div>
@ -261,6 +267,7 @@
private string _smtpusername = string.Empty; private string _smtpusername = string.Empty;
private string _smtppassword = string.Empty; private string _smtppassword = string.Empty;
private string _smtpsender = string.Empty; private string _smtpsender = string.Empty;
private string _retention = string.Empty;
private string _pwaisenabled; private string _pwaisenabled;
private int _pwaappiconfileid = -1; private int _pwaappiconfileid = -1;
private FileManager _pwaappiconfilemanager; private FileManager _pwaappiconfilemanager;
@ -330,6 +337,7 @@
_smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty); _smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty);
_smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty); _smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty);
_smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty); _smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty);
_retention = SettingService.GetSetting(settings, "NotificationRetention", "30");
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{ {
@ -479,12 +487,13 @@
site = await SiteService.UpdateSiteAsync(site); site = await SiteService.UpdateSiteAsync(site);
var settings = await SettingService.GetSiteSettingsAsync(site.SiteId); var settings = await SettingService.GetSiteSettingsAsync(site.SiteId);
SettingService.SetSetting(settings, "SMTPHost", _smtphost, true); settings = SettingService.SetSetting(settings, "SMTPHost", _smtphost, true);
SettingService.SetSetting(settings, "SMTPPort", _smtpport, true); settings = SettingService.SetSetting(settings, "SMTPPort", _smtpport, true);
SettingService.SetSetting(settings, "SMTPSSL", _smtpssl, true); settings = SettingService.SetSetting(settings, "SMTPSSL", _smtpssl, true);
SettingService.SetSetting(settings, "SMTPUsername", _smtpusername, true); settings = SettingService.SetSetting(settings, "SMTPUsername", _smtpusername, true);
SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true); settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true);
SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true); settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true);
settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true);
await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId); await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId);
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))

View File

@ -31,6 +31,7 @@ namespace Oqtane.Infrastructure
var settingRepository = provider.GetRequiredService<ISettingRepository>(); var settingRepository = provider.GetRequiredService<ISettingRepository>();
var logRepository = provider.GetRequiredService<ILogRepository>(); var logRepository = provider.GetRequiredService<ILogRepository>();
var visitorRepository = provider.GetRequiredService<IVisitorRepository>(); var visitorRepository = provider.GetRequiredService<IVisitorRepository>();
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
// iterate through sites for current tenant // iterate through sites for current tenant
List<Site> sites = siteRepository.GetSites().ToList(); List<Site> sites = siteRepository.GetSites().ToList();
@ -51,7 +52,7 @@ namespace Oqtane.Infrastructure
} }
try try
{ {
count = logRepository.DeleteLogs(retention); count = logRepository.DeleteLogs(site.SiteId, retention);
log += count.ToString() + " Events Purged<br />"; log += count.ToString() + " Events Purged<br />";
} }
catch (Exception ex) catch (Exception ex)
@ -69,7 +70,7 @@ namespace Oqtane.Infrastructure
} }
try try
{ {
count = visitorRepository.DeleteVisitors(retention); count = visitorRepository.DeleteVisitors(site.SiteId, retention);
log += count.ToString() + " Visitors Purged<br />"; log += count.ToString() + " Visitors Purged<br />";
} }
catch (Exception ex) catch (Exception ex)
@ -77,6 +78,22 @@ namespace Oqtane.Infrastructure
log += $"Error Purging Visitors - {ex.Message}<br />"; log += $"Error Purging Visitors - {ex.Message}<br />";
} }
} }
// purge notifications
retention = 30; // 30 days
if (settings.ContainsKey("NotificationRetention") && !string.IsNullOrEmpty(settings["NotificationRetention"]))
{
retention = int.Parse(settings["NotificationRetention"]);
}
try
{
count = notificationRepository.DeleteNotifications(site.SiteId, retention);
log += count.ToString() + " Notifications Purged<br />";
}
catch (Exception ex)
{
log += $"Error Purging Notifications - {ex.Message}<br />";
}
} }
return log; return log;

View File

@ -8,6 +8,6 @@ namespace Oqtane.Repository
IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows); IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows);
Log GetLog(int logId); Log GetLog(int logId);
void AddLog(Log log); 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);
Notification GetNotification(int notificationId, bool tracking); Notification GetNotification(int notificationId, bool tracking);
void DeleteNotification(int notificationId); 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 visitorId);
Visitor GetVisitor(int siteId, string IPAddress); Visitor GetVisitor(int siteId, string IPAddress);
void DeleteVisitor(int visitorId); 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(); _db.SaveChanges();
} }
public int DeleteLogs(int age) public int DeleteLogs(int siteId, int age)
{ {
// delete logs in batches of 100 records // delete logs in batches of 100 records
int count = 0; int count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age); 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(); .OrderBy(item => item.LogDate).Take(100).ToList();
while (logs.Count > 0) while (logs.Count > 0)
{ {
count += logs.Count; count += logs.Count;
_db.Log.RemoveRange(logs); _db.Log.RemoveRange(logs);
_db.SaveChanges(); _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(); .OrderBy(item => item.LogDate).Take(100).ToList();
} }
return count; return count;

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -68,6 +69,25 @@ namespace Oqtane.Repository
_db.Notification.Remove(notification); _db.Notification.Remove(notification);
_db.SaveChanges(); _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(); _db.SaveChanges();
} }
public int DeleteVisitors(int age) public int DeleteVisitors(int siteId, int age)
{ {
// delete visitors in batches of 100 records // delete visitors in batches of 100 records
int count = 0; int count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age); 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(); .OrderBy(item => item.VisitedOn).Take(100).ToList();
while (visitors.Count > 0) while (visitors.Count > 0)
{ {
count += visitors.Count; count += visitors.Count;
_db.Visitor.RemoveRange(visitors); _db.Visitor.RemoveRange(visitors);
_db.SaveChanges(); _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(); .OrderBy(item => item.VisitedOn).Take(100).ToList();
} }
return count; return count;