mirror of
https://github.com/Radarr/Radarr
synced 2024-12-27 02:09:59 +00:00
Less shitty way to do series stats for single requests and fixed tests
This commit is contained in:
parent
c3b8b52644
commit
40f49d7385
3 changed files with 71 additions and 21 deletions
|
@ -75,7 +75,7 @@ private SeriesResource GetSeriesResource(Core.Tv.Series series)
|
|||
|
||||
var resource = series.InjectTo<SeriesResource>();
|
||||
MapCoversToLocal(resource);
|
||||
LinkSeriesStatistics(resource, _seriesStatisticsService.SeriesStatistics());
|
||||
FetchAndLinkSeriesStatistics(resource);
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
@ -85,12 +85,8 @@ private List<SeriesResource> AllSeries()
|
|||
var seriesStats = _seriesStatisticsService.SeriesStatistics();
|
||||
var seriesResources = ToListResource(_seriesService.GetAllSeries);
|
||||
|
||||
foreach (var resource in seriesResources)
|
||||
{
|
||||
LinkSeriesStatistics(resource, seriesStats);
|
||||
}
|
||||
|
||||
MapCoversToLocal(seriesResources.ToArray());
|
||||
LinkSeriesStatistics(seriesResources, seriesStats);
|
||||
|
||||
return seriesResources;
|
||||
}
|
||||
|
@ -104,7 +100,7 @@ private SeriesResource UpdateSeries(SeriesResource seriesResource)
|
|||
{
|
||||
var resource = ToResource<Core.Tv.Series>(_seriesService.UpdateSeries, seriesResource);
|
||||
MapCoversToLocal(resource);
|
||||
LinkSeriesStatistics(resource, _seriesStatisticsService.SeriesStatistics());
|
||||
FetchAndLinkSeriesStatistics(resource);
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
@ -123,14 +119,28 @@ private void MapCoversToLocal(params SeriesResource[] series)
|
|||
}
|
||||
}
|
||||
|
||||
private void LinkSeriesStatistics(SeriesResource resource, List<SeriesStatistics> seriesStatistics)
|
||||
private void FetchAndLinkSeriesStatistics(SeriesResource resource)
|
||||
{
|
||||
var stats = seriesStatistics.Single(ss => ss.SeriesId == resource.Id);
|
||||
LinkSeriesStatistics(resource, _seriesStatisticsService.SeriesStatistics(resource.Id));
|
||||
}
|
||||
|
||||
resource.EpisodeCount = stats.EpisodeCount;
|
||||
resource.EpisodeFileCount = stats.EpisodeFileCount;
|
||||
resource.SeasonCount = stats.SeasonCount;
|
||||
resource.NextAiring = stats.NextAiring;
|
||||
private void LinkSeriesStatistics(List<SeriesResource> resources, List<SeriesStatistics> seriesStatistics)
|
||||
{
|
||||
foreach (var series in resources)
|
||||
{
|
||||
var stats = seriesStatistics.SingleOrDefault(ss => ss.SeriesId == series.Id);
|
||||
if (stats == null) continue;
|
||||
|
||||
LinkSeriesStatistics(series, stats);
|
||||
}
|
||||
}
|
||||
|
||||
private void LinkSeriesStatistics(SeriesResource resource, SeriesStatistics seriesStatistics)
|
||||
{
|
||||
resource.EpisodeCount = seriesStatistics.EpisodeCount;
|
||||
resource.EpisodeFileCount = seriesStatistics.EpisodeFileCount;
|
||||
resource.SeasonCount = seriesStatistics.SeasonCount;
|
||||
resource.NextAiring = seriesStatistics.NextAiring;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.SeriesStats
|
||||
|
@ -7,6 +8,7 @@ namespace NzbDrone.Core.SeriesStats
|
|||
public interface ISeriesStatisticsRepository
|
||||
{
|
||||
List<SeriesStatistics> SeriesStatistics();
|
||||
SeriesStatistics SeriesStatistics(int seriesId);
|
||||
}
|
||||
|
||||
public class SeriesStatisticsRepository : ISeriesStatisticsRepository
|
||||
|
@ -24,16 +26,44 @@ public List<SeriesStatistics> SeriesStatistics()
|
|||
|
||||
mapper.AddParameter("currentDate", DateTime.UtcNow);
|
||||
|
||||
const string queryText = @"SELECT
|
||||
SeriesId,
|
||||
SUM(CASE WHEN Monitored = 1 AND Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount,
|
||||
SUM(CASE WHEN Monitored = 1 AND Episodes.EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount,
|
||||
MAX(Episodes.SeasonNumber) as SeasonCount,
|
||||
MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) as NextAiringString
|
||||
FROM Episodes
|
||||
GROUP BY SeriesId";
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine(GetSelectClause());
|
||||
sb.AppendLine(GetGroupByClause());
|
||||
var queryText = sb.ToString();
|
||||
|
||||
return mapper.Query<SeriesStatistics>(queryText);
|
||||
}
|
||||
|
||||
public SeriesStatistics SeriesStatistics(int seriesId)
|
||||
{
|
||||
var mapper = _database.GetDataMapper();
|
||||
|
||||
mapper.AddParameter("currentDate", DateTime.UtcNow);
|
||||
mapper.AddParameter("seriesId", seriesId);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine(GetSelectClause());
|
||||
sb.AppendLine("WHERE SeriesId = @seriesId");
|
||||
sb.AppendLine(GetGroupByClause());
|
||||
var queryText = sb.ToString();
|
||||
|
||||
return mapper.Find<SeriesStatistics>(queryText);
|
||||
}
|
||||
|
||||
private string GetSelectClause()
|
||||
{
|
||||
return @"SELECT
|
||||
SeriesId,
|
||||
SUM(CASE WHEN Monitored = 1 AND Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount,
|
||||
SUM(CASE WHEN Monitored = 1 AND Episodes.EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeFileCount,
|
||||
MAX(Episodes.SeasonNumber) as SeasonCount,
|
||||
MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) AS NextAiringString
|
||||
FROM Episodes";
|
||||
}
|
||||
|
||||
private string GetGroupByClause()
|
||||
{
|
||||
return "GROUP BY SeriesId";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace NzbDrone.Core.SeriesStats
|
|||
public interface ISeriesStatisticsService
|
||||
{
|
||||
List<SeriesStatistics> SeriesStatistics();
|
||||
SeriesStatistics SeriesStatistics(int seriesId);
|
||||
}
|
||||
|
||||
public class SeriesStatisticsService : ISeriesStatisticsService
|
||||
|
@ -21,5 +22,14 @@ public List<SeriesStatistics> SeriesStatistics()
|
|||
{
|
||||
return _seriesStatisticsRepository.SeriesStatistics();
|
||||
}
|
||||
|
||||
public SeriesStatistics SeriesStatistics(int seriesId)
|
||||
{
|
||||
var stats = _seriesStatisticsRepository.SeriesStatistics(seriesId);
|
||||
|
||||
if (stats == null) return new SeriesStatistics();
|
||||
|
||||
return stats;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue