From 970f80b15558b7acb5dba93ae087a5781906b67c Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 17 Jul 2022 19:40:40 -0500 Subject: [PATCH] Debounce analytics service --- .../IndexerSearch/ReleaseAnalyticsService.cs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core/IndexerSearch/ReleaseAnalyticsService.cs b/src/NzbDrone.Core/IndexerSearch/ReleaseAnalyticsService.cs index a195e1125..1bf364ffe 100644 --- a/src/NzbDrone.Core/IndexerSearch/ReleaseAnalyticsService.cs +++ b/src/NzbDrone.Core/IndexerSearch/ReleaseAnalyticsService.cs @@ -1,13 +1,17 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using NLog; using NzbDrone.Common.Cloud; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Common.Serializer; +using NzbDrone.Common.TPL; using NzbDrone.Core.Analytics; using NzbDrone.Core.Indexers.Events; using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.IndexerSearch { @@ -16,26 +20,48 @@ namespace NzbDrone.Core.IndexerSearch private readonly IHttpClient _httpClient; private readonly IHttpRequestBuilderFactory _requestBuilder; private readonly IAnalyticsService _analyticsService; + private readonly Debouncer _debouncer; private readonly Logger _logger; + private readonly List _pendingUpdates; public ReleaseAnalyticsService(IHttpClient httpClient, IProwlarrCloudRequestBuilder requestBuilder, IAnalyticsService analyticsService, Logger logger) { + _debouncer = new Debouncer(SendReleases, TimeSpan.FromMinutes(10)); _analyticsService = analyticsService; _requestBuilder = requestBuilder.Releases; _httpClient = httpClient; _logger = logger; + + _pendingUpdates = new List(); } 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(); request.Method = HttpMethod.Post; request.Headers.ContentType = "application/json"; 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, Categories = x.Categories?.Where(c => c.Id < 10000).Select(c => c.Id) ?? new List(),