New: Custom Filters for Stats

This commit is contained in:
Qstick
2023-09-03 10:56:43 -05:00
parent c873b3ffac
commit b608e38454
12 changed files with 214 additions and 97 deletions
@@ -46,7 +46,7 @@ namespace NzbDrone.Core.Test.IndexerStatsTests
.Setup(o => o.Between(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
.Returns<DateTime, DateTime>((s, f) => history);
var statistics = Subject.IndexerStatistics(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow);
var statistics = Subject.IndexerStatistics(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow, new List<int> { 5 });
statistics.IndexerStatistics.Count.Should().Be(1);
statistics.IndexerStatistics.First().AverageResponseTime.Should().Be(0);
@@ -8,7 +8,7 @@ namespace NzbDrone.Core.IndexerStats
{
public interface IIndexerStatisticsService
{
CombinedStatistics IndexerStatistics(DateTime start, DateTime end);
CombinedStatistics IndexerStatistics(DateTime start, DateTime end, List<int> indexerIds);
}
public class IndexerStatisticsService : IIndexerStatisticsService
@@ -22,13 +22,15 @@ namespace NzbDrone.Core.IndexerStats
_indexerFactory = indexerFactory;
}
public CombinedStatistics IndexerStatistics(DateTime start, DateTime end)
public CombinedStatistics IndexerStatistics(DateTime start, DateTime end, List<int> indexerIds)
{
var history = _historyService.Between(start, end);
var groupedByIndexer = history.GroupBy(h => h.IndexerId);
var groupedByUserAgent = history.GroupBy(h => h.Data.GetValueOrDefault("source") ?? "");
var groupedByHost = history.GroupBy(h => h.Data.GetValueOrDefault("host") ?? "");
var filteredHistory = history.Where(h => indexerIds.Contains(h.IndexerId));
var groupedByIndexer = filteredHistory.GroupBy(h => h.IndexerId);
var groupedByUserAgent = filteredHistory.GroupBy(h => h.Data.GetValueOrDefault("source") ?? "");
var groupedByHost = filteredHistory.GroupBy(h => h.Data.GetValueOrDefault("host") ?? "");
var indexerStatsList = new List<IndexerStatistics>();
var userAgentStatsList = new List<UserAgentStatistics>();
@@ -1,6 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.IndexerStats;
using NzbDrone.Core.Tags;
using Prowlarr.Http;
namespace Prowlarr.Api.V1.Indexers
@@ -9,20 +14,41 @@ namespace Prowlarr.Api.V1.Indexers
public class IndexerStatsController : Controller
{
private readonly IIndexerStatisticsService _indexerStatisticsService;
private readonly IIndexerFactory _indexerFactory;
private readonly ITagService _tagService;
public IndexerStatsController(IIndexerStatisticsService indexerStatisticsService)
public IndexerStatsController(IIndexerStatisticsService indexerStatisticsService, IIndexerFactory indexerFactory, ITagService tagService)
{
_indexerStatisticsService = indexerStatisticsService;
_indexerFactory = indexerFactory;
_tagService = tagService;
}
[HttpGet]
[Produces("application/json")]
public IndexerStatsResource GetAll(DateTime? startDate, DateTime? endDate)
public IndexerStatsResource GetAll(DateTime? startDate, DateTime? endDate, string indexers, string tags)
{
var statsStartDate = startDate ?? DateTime.MinValue;
var statsEndDate = endDate ?? DateTime.Now;
var parsedIndexers = new List<int>();
var parsedTags = new List<int>();
var indexerIds = _indexerFactory.All().Select(i => i.Id).ToList();
var indexerStats = _indexerStatisticsService.IndexerStatistics(statsStartDate, statsEndDate);
if (tags.IsNotNullOrWhiteSpace())
{
parsedTags.AddRange(tags.Split(',').Select(_tagService.GetTag).Select(t => t.Id));
indexerIds = indexerIds.Intersect(parsedTags.SelectMany(t => _indexerFactory.AllForTag(t).Select(i => i.Id))).ToList();
}
if (indexers.IsNotNullOrWhiteSpace())
{
parsedIndexers.AddRange(indexers.Split(',').Select(x => Convert.ToInt32(x)));
indexerIds = indexerIds.Intersect(parsedIndexers).ToList();
}
var indexerStats = _indexerStatisticsService.IndexerStatistics(statsStartDate, statsEndDate, indexerIds);
var indexerResource = new IndexerStatsResource
{