parent
fef81171ba
commit
5b22093c29
@ -0,0 +1,11 @@
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.IndexerStats
|
||||
{
|
||||
public class IndexerStatistics : ResultSet
|
||||
{
|
||||
public int IndexerId { get; set; }
|
||||
public int AverageResponseTime { get; set; }
|
||||
public int NumberOfQueries { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Indexers;
|
||||
|
||||
namespace NzbDrone.Core.IndexerStats
|
||||
{
|
||||
public interface IIndexerStatisticsRepository
|
||||
{
|
||||
List<IndexerStatistics> IndexerStatistics();
|
||||
}
|
||||
|
||||
public class IndexerStatisticsRepository : IIndexerStatisticsRepository
|
||||
{
|
||||
private const string _selectTemplate = "SELECT /**select**/ FROM History /**join**/ /**innerjoin**/ /**leftjoin**/ /**where**/ /**groupby**/ /**having**/ /**orderby**/";
|
||||
|
||||
private readonly IMainDatabase _database;
|
||||
|
||||
public IndexerStatisticsRepository(IMainDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public List<IndexerStatistics> IndexerStatistics()
|
||||
{
|
||||
var time = DateTime.UtcNow;
|
||||
return Query(Builder());
|
||||
}
|
||||
|
||||
public List<IndexerStatistics> IndexerStatistics(int indexerId)
|
||||
{
|
||||
var time = DateTime.UtcNow;
|
||||
return Query(Builder().Where<IndexerDefinition>(x => x.Id == indexerId));
|
||||
}
|
||||
|
||||
private List<IndexerStatistics> Query(SqlBuilder builder)
|
||||
{
|
||||
var sql = builder.AddTemplate(_selectTemplate).LogQuery();
|
||||
|
||||
using (var conn = _database.OpenConnection())
|
||||
{
|
||||
return conn.Query<IndexerStatistics>(sql.RawSql, sql.Parameters).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private SqlBuilder Builder() => new SqlBuilder()
|
||||
.Select(@"Indexers.Id AS IndexerId,
|
||||
COUNT(History.Id) AS NumberOfQueries,
|
||||
AVG(json_extract(History.Data,'$.elapsedTime')) AS AverageResponseTime")
|
||||
.Join<History.History, IndexerDefinition>((t, r) => t.IndexerId == r.Id)
|
||||
.GroupBy<IndexerDefinition>(x => x.Id);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NzbDrone.Core.IndexerStats
|
||||
{
|
||||
public interface IIndexerStatisticsService
|
||||
{
|
||||
List<IndexerStatistics> IndexerStatistics();
|
||||
}
|
||||
|
||||
public class IndexerStatisticsService : IIndexerStatisticsService
|
||||
{
|
||||
private readonly IIndexerStatisticsRepository _indexerStatisticsRepository;
|
||||
|
||||
public IndexerStatisticsService(IIndexerStatisticsRepository indexerStatisticsRepository)
|
||||
{
|
||||
_indexerStatisticsRepository = indexerStatisticsRepository;
|
||||
}
|
||||
|
||||
public List<IndexerStatistics> IndexerStatistics()
|
||||
{
|
||||
var seasonStatistics = _indexerStatisticsRepository.IndexerStatistics();
|
||||
|
||||
return seasonStatistics.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NzbDrone.Core.IndexerStats;
|
||||
using Prowlarr.Http;
|
||||
|
||||
namespace Prowlarr.Api.V1.Indexers
|
||||
{
|
||||
public class IndexerStatsModule : ProwlarrRestModule<IndexerStatsResource>
|
||||
{
|
||||
private readonly IIndexerStatisticsService _indexerStatisticsService;
|
||||
|
||||
public IndexerStatsModule(IIndexerStatisticsService indexerStatisticsService)
|
||||
{
|
||||
_indexerStatisticsService = indexerStatisticsService;
|
||||
|
||||
GetResourceAll = GetAll;
|
||||
}
|
||||
|
||||
private List<IndexerStatsResource> GetAll()
|
||||
{
|
||||
return _indexerStatisticsService.IndexerStatistics().ToResource();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.IndexerStats;
|
||||
using Prowlarr.Http.REST;
|
||||
|
||||
namespace Prowlarr.Api.V1.Indexers
|
||||
{
|
||||
public class IndexerStatsResource : RestResource
|
||||
{
|
||||
public int IndexerId { get; set; }
|
||||
public int NumberOfQueries { get; set; }
|
||||
public int AverageResponseTime { get; set; }
|
||||
}
|
||||
|
||||
public static class IndexerStatsResourceMapper
|
||||
{
|
||||
public static IndexerStatsResource ToResource(this IndexerStatistics model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new IndexerStatsResource
|
||||
{
|
||||
IndexerId = model.IndexerId,
|
||||
NumberOfQueries = model.NumberOfQueries,
|
||||
AverageResponseTime = model.AverageResponseTime
|
||||
};
|
||||
}
|
||||
|
||||
public static List<IndexerStatsResource> ToResource(this IEnumerable<IndexerStatistics> models)
|
||||
{
|
||||
return models.Select(ToResource).ToList();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue