Debounce analytics service

pull/1070/head
Qstick 2 years ago
parent b8dd8b1880
commit 970f80b155

@ -1,13 +1,17 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using NLog; using NLog;
using NzbDrone.Common.Cloud; using NzbDrone.Common.Cloud;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Common.TPL;
using NzbDrone.Core.Analytics; using NzbDrone.Core.Analytics;
using NzbDrone.Core.Indexers.Events; using NzbDrone.Core.Indexers.Events;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.IndexerSearch namespace NzbDrone.Core.IndexerSearch
{ {
@ -16,26 +20,48 @@ namespace NzbDrone.Core.IndexerSearch
private readonly IHttpClient _httpClient; private readonly IHttpClient _httpClient;
private readonly IHttpRequestBuilderFactory _requestBuilder; private readonly IHttpRequestBuilderFactory _requestBuilder;
private readonly IAnalyticsService _analyticsService; private readonly IAnalyticsService _analyticsService;
private readonly Debouncer _debouncer;
private readonly Logger _logger; private readonly Logger _logger;
private readonly List<ReleaseInfo> _pendingUpdates;
public ReleaseAnalyticsService(IHttpClient httpClient, IProwlarrCloudRequestBuilder requestBuilder, IAnalyticsService analyticsService, Logger logger) public ReleaseAnalyticsService(IHttpClient httpClient, IProwlarrCloudRequestBuilder requestBuilder, IAnalyticsService analyticsService, Logger logger)
{ {
_debouncer = new Debouncer(SendReleases, TimeSpan.FromMinutes(10));
_analyticsService = analyticsService; _analyticsService = analyticsService;
_requestBuilder = requestBuilder.Releases; _requestBuilder = requestBuilder.Releases;
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_pendingUpdates = new List<ReleaseInfo>();
} }
public void HandleAsync(IndexerQueryEvent message) public void HandleAsync(IndexerQueryEvent message)
{ {
if (_analyticsService.IsEnabled && message.QueryResult?.Releases != null) if (message.QueryResult?.Releases != null)
{
lock (_pendingUpdates)
{
_pendingUpdates.AddRange(message.QueryResult.Releases.Where(r => r.Title.IsNotNullOrWhiteSpace()));
}
_debouncer.Execute();
}
}
public void SendReleases()
{
lock (_pendingUpdates)
{ {
var pendingUpdates = _pendingUpdates.ToArray();
_pendingUpdates.Clear();
var request = _requestBuilder.Create().Resource("release/push").Build(); var request = _requestBuilder.Create().Resource("release/push").Build();
request.Method = HttpMethod.Post; request.Method = HttpMethod.Post;
request.Headers.ContentType = "application/json"; request.Headers.ContentType = "application/json";
request.SuppressHttpError = true; request.SuppressHttpError = true;
request.LogHttpError = false;
var body = message.QueryResult.Releases.Select(x => new var body = pendingUpdates.DistinctBy(r => r.Title).Select(x => new
{ {
Title = x.Title, Title = x.Title,
Categories = x.Categories?.Where(c => c.Id < 10000).Select(c => c.Id) ?? new List<int>(), Categories = x.Categories?.Where(c => c.Id < 10000).Select(c => c.Id) ?? new List<int>(),

Loading…
Cancel
Save