Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore suggestions based on previously ignored #82

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/Geta.NotFoundHandler/Core/Suggestions/RequestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Geta.NotFoundHandler.Core.Redirects;
using Geta.NotFoundHandler.Data;
using Geta.NotFoundHandler.Infrastructure.Configuration;
using Geta.NotFoundHandler.Infrastructure.Processing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -15,28 +18,30 @@ public class RequestLogger : IRequestLogger
{
private readonly ILogger<RequestLogger> _logger;
private readonly ISuggestionRepository _suggestionRepository;
private readonly IRedirectsService _redirectsService;
private readonly NotFoundHandlerOptions _configuration;

public RequestLogger(
IOptions<NotFoundHandlerOptions> options,
ILogger<RequestLogger> logger,
ISuggestionRepository suggestionRepository)
ISuggestionRepository suggestionRepository,
IRedirectsService redirectsService)
{
_logger = logger;
_suggestionRepository = suggestionRepository;
_redirectsService = redirectsService;
_configuration = options.Value;
}

public void LogRequest(string oldUrl, string referer)
{
var bufferSize = _configuration.BufferSize;
if (!LogQueue.IsEmpty && LogQueue.Count >= bufferSize)
if (AllowLogOfRequests())
{
lock (LogQueue)
{
try
{
if (LogQueue.Count >= bufferSize)
if (AllowLogOfRequests())
{
LogRequests(LogQueue);
}
Expand All @@ -58,14 +63,15 @@ private void LogRequests(ConcurrentQueue<LogEvent> logEvents)
var threshold = _configuration.ThreshHold;
var start = logEvents.First().Requested;
var end = logEvents.Last().Requested;
var diff = (end - start).Seconds;
var diff = (int)Math.Floor((end - start).TotalSeconds);

if ((diff != 0 && bufferSize / diff <= threshold)
|| bufferSize == 0)
{
var ignored = _redirectsService.GetIgnored().Select(x => x.OldUrl);
while (!logEvents.IsEmpty)
{
if (logEvents.TryDequeue(out var logEvent))
if (logEvents.TryDequeue(out var logEvent) && !IgnoreSuggestion(logEvent, ignored))
{
_suggestionRepository.Save(logEvent.OldUrl, logEvent.Referer, logEvent.Requested);
}
Expand All @@ -80,6 +86,30 @@ private void LogRequests(ConcurrentQueue<LogEvent> logEvents)
}
}

private bool AllowLogOfRequests()
{
if (LogQueue.IsEmpty)
return false;

var bufferSize = _configuration.BufferSize;
var threshold = _configuration.ThreshHold;
if (threshold > 0 && bufferSize > 0 && (bufferSize / (DateTime.UtcNow - LogQueue.Last().Requested).TotalSeconds) <= threshold)
return true;

return LogQueue.Count >= bufferSize;
}

private static bool IgnoreSuggestion(LogEvent logEvent, IEnumerable<string> ignored)
{
if (logEvent.OldUrl.StartsWith("/EPiServer/", StringComparison.InvariantCultureIgnoreCase))
return true;

if (ignored.Any(x => logEvent.OldUrl.AsPathSpan().UrlPathMatch(x.AsPathSpan()) && logEvent.OldUrl.AsPathSpan().StartsWith(x.AsQuerySpan(), StringComparison.InvariantCultureIgnoreCase)))
return true;

return false;
}

private static ConcurrentQueue<LogEvent> LogQueue { get; } = new ConcurrentQueue<LogEvent>();
}
}
Loading