Skip to content

Commit

Permalink
Merge pull request #72 from skamphuis/feature/#25-clean-logtable
Browse files Browse the repository at this point in the history
Implemented #25 log cleanup
  • Loading branch information
skamphuis committed May 1, 2020
2 parents b55e073 + 99a7ef3 commit 9b7b5bd
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 11 deletions.
2 changes: 2 additions & 0 deletions 40Fingers.SeoRedirect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="Components\SeoRedirectModule.cs" />
<Compile Include="Components\RedirectConfig.cs" />
<Compile Include="Components\RedirectController.cs" />
<Compile Include="Components\TaskCleaner.cs" />
<Compile Include="EditForce404.ascx.cs">
<DependentUpon>EditForce404.ascx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
Expand Down Expand Up @@ -227,6 +228,7 @@
<Content Include="_Installation\40Fingers.SeoRedirect\02.01.01\40Fingers.SeoRedirect.02.01.01.00.Install.zip" />
<Content Include="Sql\02.01.02.SqlDataProvider" />
<Content Include="Sql\02.03.00.SqlDataProvider" />
<Content Include="Sql\02.05.00.SqlDataProvider" />
</ItemGroup>
<ItemGroup>
<Folder Include="css\" />
Expand Down
7 changes: 6 additions & 1 deletion 40Fingers.SeoRedirect.dnn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<dotnetnuke type="Package" version="5.0">
<packages><package name="40Fingers.SeoRedirect" type="Module" version="02.04.03">
<packages><package name="40Fingers.SeoRedirect" type="Module" version="02.05.00">
<friendlyName>40Fingers SeoRedirect</friendlyName>
<description>This 40FINGERS module will allow you to monitor and manage (redirect) the 404's on your DNN site for SEO.</description>
<owner>
Expand Down Expand Up @@ -106,6 +106,11 @@
<name>02.03.00.SqlDataProvider</name>
<version>02.03.00</version>
</script>
<script type="Install">
<path>SQL</path>
<name>02.05.00.SqlDataProvider</name>
<version>02.05.00</version>
</script>
</scripts>
</component>
</components>
Expand Down
60 changes: 60 additions & 0 deletions Components/Common.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
Expand All @@ -10,6 +12,7 @@
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.Scheduling;

namespace FortyFingers.SeoRedirect.Components
{
Expand Down Expand Up @@ -173,5 +176,62 @@ public static void Handle404Exception(HttpResponse response, PortalSettings port
}
}

public static ScheduleItem GetScheduleItem(string fullTypeName)
{
ScheduleItem retval = null;

var provider = SchedulingProvider.Instance();

var list = new List<ScheduleItem>();
list.AddRange(provider.GetSchedule().OfType<ScheduleItem>());

if (list.Any(s => s.TypeFullName == fullTypeName))
retval = list.First(s => s.TypeFullName == fullTypeName);

return retval;
}
public static bool IsScheduleItemEnabled(string fullTypeName)
{
var item = GetScheduleItem(fullTypeName);
return item != null && item.Enabled;
}

public static ScheduleItem EnableScheduleItem(string fullTypeName)
{
var provider = SchedulingProvider.Instance();
var item = GetScheduleItem(fullTypeName);

if (item == null)
{
item = new ScheduleItem();
item.CatchUpEnabled = false;
item.FriendlyName = Constants.CleanerTaskName;
item.RetainHistoryNum = 50;
item.RetryTimeLapse = -1;
item.RetryTimeLapseMeasurement = "d";
item.TimeLapse = 1;
item.TimeLapseMeasurement = "d";
item.TypeFullName = fullTypeName;

item.ScheduleID = provider.AddSchedule(item);
}

item.Enabled = true;
provider.UpdateSchedule(item);

return item;
}
public static void DisableScheduleItem(string fullTypeName)
{
var provider = SchedulingProvider.Instance();
var item = GetScheduleItem(fullTypeName);

if (item != null)
{
item.Enabled = false;
provider.UpdateSchedule(item);
}
}

}
}
21 changes: 21 additions & 0 deletions Components/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ public int NoOfEntries
get { return GetSettingInt("NoOfEntries", true); }
set { ModuleCtrl.UpdateModuleSetting(ModuleId, "NoOfEntries", value.ToString()); }
}
public bool CleanerEnabled
{
get => PortalController.GetPortalSettingAsBoolean("40F_SEO_CleanerEnabled", Ps.PortalId, false);
set
{
PortalController.UpdatePortalSetting(Ps.PortalId, "40F_SEO_CleanerEnabled", value.ToString());
if(value) Common.EnableScheduleItem(Constants.CleanerTaskTypeName);
else Common.DisableScheduleItem(Constants.CleanerTaskTypeName);
}
}

public int MaxAgeDays
{
get => PortalController.GetPortalSettingAsInteger("40F_SEO_MaxAgeDays", Ps.PortalId, 90);
set => PortalController.UpdatePortalSetting(Ps.PortalId, "40F_SEO_MaxAgeDays", value.ToString());
}
public int MaxEntries
{
get => PortalController.GetPortalSettingAsInteger("40F_SEO_MaxEntries", Ps.PortalId, 10000);
set => PortalController.UpdatePortalSetting(Ps.PortalId, "40F_SEO_MaxEntries", value.ToString());
}

}
}
4 changes: 4 additions & 0 deletions Components/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ public class Constants
public const string MODULE_LOGGER_NAME = "40Fingers.SeoRedirect";

public const string Force404TabSetting = "40F_SEO_Force404";

public const string CleanerTaskName = "40F Seo Cleaner";
public const string CleanerTaskTypeName = "FortyFingers.SeoRedirect.Components.TaskCleaner, 40Fingers.DNN.Modules.SeoRedirect";

}
}
1 change: 1 addition & 0 deletions Components/Data/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public abstract void AddRedirectLog(int portalId, string requestedUrl, DateTime

public abstract IDataReader GetTopUnhandledUrls(int portalId, DateTime startDate, int maxUrls);
public abstract void SetHandledUrl(string url, DateTime handledOn, string handledBy);
public abstract void CleanupRedirectLog(int portalId, int maxAgeDays, int maxEntries);
}
}
5 changes: 5 additions & 0 deletions Components/Data/SqlDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,10 @@ public override void SetHandledUrl(string url, DateTime handledOn, string handle
{
SqlHelper.ExecuteNonQuery(ConnectionString, GetObjectName("SetHandledUrl"), url, handledOn, handledBy);
}

public override void CleanupRedirectLog(int portalId, int maxAgeDays, int maxEntries)
{
SqlHelper.ExecuteNonQuery(ConnectionString, GetObjectName("CleanRedirectLog"), portalId, maxAgeDays, maxEntries);
}
}
}
5 changes: 5 additions & 0 deletions Components/RedirectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ internal static void AddRedirectLog(int portalId, string incoming, string target

}

internal static void CleanRedirectLog(int portalId, int maxAgeDays, int maxEntries)
{

}

public static List<RedirectLogUrl> GetTopUnhandledUrls(int portalId, int maxDays, int maxUrls)
{
var startdate = DateTime.Today.AddDays(-1 * maxDays);
Expand Down
50 changes: 50 additions & 0 deletions Components/TaskCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Scheduling;
using FortyFingers.SeoRedirect.Components.Data;

namespace FortyFingers.SeoRedirect.Components
{
public class TaskCleaner : SchedulerClient
{
public TaskCleaner(ScheduleHistoryItem objScheduleHistoryItem)
{
ScheduleHistoryItem = objScheduleHistoryItem;
}
public override void DoWork()
{
try
{
ScheduleHistoryItem.AddLogNote(String.Format("Cleaner started<br />", ThreadID));
Progressing();

var portals = new PortalController().GetPortals();

foreach (PortalInfo portal in portals)
{
if (PortalController.GetPortalSettingAsBoolean("40F_SEO_CleanerEnabled", portal.PortalID, false))
{
var maxAgeDays = PortalController.GetPortalSettingAsInteger("40F_SEO_MaxAgeDays", portal.PortalID, Null.NullInteger);
var maxEntries = PortalController.GetPortalSettingAsInteger("40F_SEO_MaxEntries", portal.PortalID, Null.NullInteger);
DataProvider.Instance().CleanupRedirectLog(portal.PortalID, maxAgeDays, maxEntries);
ScheduleHistoryItem.AddLogNote($"Portal {portal.PortalName} done.<br />");
}
}
ScheduleHistoryItem.AddLogNote("Finished.<br />");
ScheduleHistoryItem.Succeeded = true;
}
catch (Exception e)
{
ScheduleHistoryItem.Succeeded = false;
ScheduleHistoryItem.AddLogNote($"Exception occurred: {e.Message}<br />");
ScheduleHistoryItem.AddLogNote($"Export failed.<br />");
DotNetNuke.Services.Exceptions.Exceptions.LogException(e);
Errored(ref e);
}
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("02.04.03.00")]
[assembly: AssemblyFileVersion("02.04.03.00")]
[assembly: AssemblyVersion("02.05.00.00")]
[assembly: AssemblyFileVersion("02.05.00.00")]
20 changes: 20 additions & 0 deletions Settings.ascx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,25 @@
<asp:RangeValidator runat="server" ID="ValNoE" ControlToValidate="NoOfEntries" MinimumValue="0" MaximumValue="1000" ErrorMessage="Please enter a number between 0 and 1000" Type="Integer" />
</td>
</tr>
<tr>
<td class="SubHead" width="150" valign="top"><dnn:label id="plEnableCleaner" controlname="EnableCleaner" runat="server" Text="Enable cleaner" suffix=":" /></td>
<td valign="top">
<asp:CheckBox ID="EnableCleaner" runat="server" CssClass="NormalTextBox" Width="60"></asp:CheckBox>
</td>
</tr>
<tr>
<td class="SubHead" width="150" valign="top"><dnn:label id="plMaxAgeDays" controlname="MaxAgeDays" runat="server" Text="Max age in days" suffix=":" /></td>
<td valign="top">
<asp:TextBox ID="MaxAgeDays" runat="server" CssClass="NormalTextBox" Width="60"></asp:TextBox>
<asp:RangeValidator runat="server" ID="ValMaxAge" ControlToValidate="MaxAgeDays" MinimumValue="0" MaximumValue="1000" ErrorMessage="Please enter a number between 0 and 1000" Type="Integer" />
</td>
</tr>
<tr>
<td class="SubHead" width="150" valign="top"><dnn:label id="plMaxEntries" controlname="MaxEntries" runat="server" Text="Max number of entries" suffix=":" /></td>
<td valign="top">
<asp:TextBox ID="MaxEntries" runat="server" CssClass="NormalTextBox" Width="60"></asp:TextBox>
<asp:RangeValidator runat="server" ID="ValMaxEntries" ControlToValidate="MaxEntries" MinimumValue="0" MaximumValue="1000" ErrorMessage="Please enter a number between 0 and 1000" Type="Integer" />
</td>
</tr>
</table>

6 changes: 6 additions & 0 deletions Settings.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ protected void Page_Load(object sender, EventArgs e)
public override void LoadSettings()
{
NoOfEntries.Text = Config.NoOfEntries.ToString();
EnableCleaner.Checked = Config.CleanerEnabled;
MaxAgeDays.Text = Config.MaxAgeDays.ToString();
MaxEntries.Text = Config.MaxEntries.ToString();
}
/// <summary>
/// updates module settings
/// </summary>
public override void UpdateSettings()
{
Config.NoOfEntries = int.Parse(NoOfEntries.Text);
Config.CleanerEnabled = EnableCleaner.Checked;
Config.MaxEntries = int.Parse(MaxEntries.Text);
Config.MaxAgeDays = int.Parse(MaxAgeDays.Text);
}


Expand Down
Loading

0 comments on commit 9b7b5bd

Please sign in to comment.