SeriesStats moved to its own Repo

pull/3113/head
Mark McDowall 12 years ago
parent 98df1be981
commit a04a5e8669

@ -6,6 +6,7 @@ using AutoMapper;
using FluentValidation; using FluentValidation;
using NzbDrone.Api.Extensions; using NzbDrone.Api.Extensions;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.SeriesStats;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Api.Validation; using NzbDrone.Api.Validation;
@ -15,11 +16,13 @@ namespace NzbDrone.Api.Series
public class SeriesModule : NzbDroneRestModule<SeriesResource> public class SeriesModule : NzbDroneRestModule<SeriesResource>
{ {
private readonly ISeriesService _seriesService; private readonly ISeriesService _seriesService;
private readonly ISeriesStatisticsService _seriesStatisticsService;
public SeriesModule(ISeriesService seriesService) public SeriesModule(ISeriesService seriesService, ISeriesStatisticsService seriesStatisticsService)
: base("/Series") : base("/Series")
{ {
_seriesService = seriesService; _seriesService = seriesService;
_seriesStatisticsService = seriesStatisticsService;
GetResourceAll = AllSeries; GetResourceAll = AllSeries;
GetResourceById = GetSeries; GetResourceById = GetSeries;
@ -39,7 +42,7 @@ namespace NzbDrone.Api.Series
private List<SeriesResource> AllSeries() private List<SeriesResource> AllSeries()
{ {
var series = _seriesService.GetAllSeries().ToList(); var series = _seriesService.GetAllSeries().ToList();
var seriesStats = _seriesService.SeriesStatistics(); var seriesStats = _seriesStatisticsService.SeriesStatistics();
var seriesModels = Mapper.Map<List<Core.Tv.Series>, List<SeriesResource>>(series); var seriesModels = Mapper.Map<List<Core.Tv.Series>, List<SeriesResource>>(series);
foreach (var s in seriesModels) foreach (var s in seriesModels)

@ -170,7 +170,7 @@
<Compile Include="ProviderTests\DiskProviderTests\ExtractArchiveFixture.cs" /> <Compile Include="ProviderTests\DiskProviderTests\ExtractArchiveFixture.cs" />
<Compile Include="ProviderTests\PostDownloadProviderTests\DropFolderImportServiceFixture.cs" /> <Compile Include="ProviderTests\PostDownloadProviderTests\DropFolderImportServiceFixture.cs" />
<Compile Include="JobTests\TestJobs.cs" /> <Compile Include="JobTests\TestJobs.cs" />
<Compile Include="TvTests\SeriesRepositoryTests\SeriesStatisticsFixture.cs" /> <Compile Include="SeriesStatsTests\SeriesStatisticsFixture.cs" />
<Compile Include="UpdateTests\UpdateServiceFixture.cs" /> <Compile Include="UpdateTests\UpdateServiceFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" /> <Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" /> <Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
@ -344,6 +344,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="ProviderTests\UpdateProviderTests\" /> <Folder Include="ProviderTests\UpdateProviderTests\" />
<Folder Include="TvTests\SeriesRepositoryTests\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

@ -1,17 +1,16 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.SeriesStats;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.TvTests.SeriesRepositoryTests namespace NzbDrone.Core.Test.SeriesStatsTests
{ {
[TestFixture] [TestFixture]
public class SeriesStatisticsFixture : DbTest<SeriesRepository, Series> public class SeriesStatisticsFixture : DbTest<SeriesStatisticsRepository, Series>
{ {
private Episode _episode; private Episode _episode;
@ -35,7 +34,7 @@ namespace NzbDrone.Core.Test.TvTests.SeriesRepositoryTests
} }
[Test] [Test]
public void should_get_episodes() public void should_get_stats_for_series()
{ {
var stats = Subject.SeriesStatistics(); var stats = Subject.SeriesStatistics();

@ -15,6 +15,7 @@ using NzbDrone.Core.Jobs;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
using NzbDrone.Core.RootFolders; using NzbDrone.Core.RootFolders;
using NzbDrone.Core.SeriesStats;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Datastore namespace NzbDrone.Core.Datastore

@ -349,6 +349,7 @@
<Compile Include="Parser\ParsingService.cs" /> <Compile Include="Parser\ParsingService.cs" />
<Compile Include="Qualities\QualitySizeRepository.cs" /> <Compile Include="Qualities\QualitySizeRepository.cs" />
<Compile Include="Qualities\QualityProfileRepository.cs" /> <Compile Include="Qualities\QualityProfileRepository.cs" />
<Compile Include="SeriesStats\SeriesStatisticsService.cs" />
<Compile Include="Tv\EpisodeService.cs" /> <Compile Include="Tv\EpisodeService.cs" />
<Compile Include="Tv\EpisodeStatuses.cs" /> <Compile Include="Tv\EpisodeStatuses.cs" />
<Compile Include="Tv\Events\EpisodeInfoUpdatedEvent.cs" /> <Compile Include="Tv\Events\EpisodeInfoUpdatedEvent.cs" />
@ -387,7 +388,8 @@
<Compile Include="Providers\RecycleBinProvider.cs" /> <Compile Include="Providers\RecycleBinProvider.cs" />
<Compile Include="Tv\SeasonService.cs" /> <Compile Include="Tv\SeasonService.cs" />
<Compile Include="Instrumentation\TrimLogsJob.cs" /> <Compile Include="Instrumentation\TrimLogsJob.cs" />
<Compile Include="Tv\SeriesStatistics.cs" /> <Compile Include="SeriesStats\SeriesStatistics.cs" />
<Compile Include="SeriesStats\SeriesStatisticsRepository.cs" />
<Compile Include="Update\AppUpdateJob.cs" /> <Compile Include="Update\AppUpdateJob.cs" />
<Compile Include="Model\Xbmc\TvShowResponse.cs" /> <Compile Include="Model\Xbmc\TvShowResponse.cs" />
<Compile Include="Model\Xbmc\TvShow.cs" /> <Compile Include="Model\Xbmc\TvShow.cs" />

@ -1,18 +1,26 @@
using System; using System;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Tv namespace NzbDrone.Core.SeriesStats
{ {
public class SeriesStatistics : ResultSet public class SeriesStatistics : ResultSet
{ {
public int SeriesId { get; set; } public int SeriesId { get; set; }
public int NumberOfSeasons { get; set; } public int NumberOfSeasons { get; set; }
public string NextAiringString { get; set; } public string NextAiringString { get; set; }
public int EpisodeFileCount { get; set; }
public int EpisodeCount { get; set; }
public DateTime? NextAiring public DateTime? NextAiring
{ {
get { return Convert.ToDateTime(NextAiringString); } get
{
DateTime nextAiring;
if (!DateTime.TryParse(NextAiringString, out nextAiring)) return null;
return nextAiring;
}
} }
public int EpisodeFileCount { get; set; }
public int EpisodeCount { get; set; }
} }
} }

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using Marr.Data;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.SeriesStats
{
public interface ISeriesStatisticsRepository
{
List<SeriesStatistics> SeriesStatistics();
}
public class SeriesStatisticsRepository : ISeriesStatisticsRepository
{
private readonly IDataMapper _dataMapper;
public SeriesStatisticsRepository(IDatabase database)
{
_dataMapper = database.DataMapper;
}
public List<SeriesStatistics> SeriesStatistics()
{
_dataMapper.AddParameter("currentDate", DateTime.UtcNow);
var queryText = @"SELECT
SeriesId,
SUM(CASE WHEN Ignored = 0 AND Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount,
SUM(CASE WHEN Ignored = 0 AND Episodes.EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount,
MAX(Episodes.SeasonNumber) as NumberOfSeasons,
MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) as NextAiringString
FROM Episodes
GROUP BY SeriesId";
return _dataMapper.Query<SeriesStatistics>(queryText);
}
}
}

@ -0,0 +1,24 @@
using System.Collections.Generic;
namespace NzbDrone.Core.SeriesStats
{
public interface ISeriesStatisticsService
{
List<SeriesStatistics> SeriesStatistics();
}
public class SeriesStatisticsService : ISeriesStatisticsService
{
private readonly SeriesStatisticsRepository _seriesStatisticsRepository;
public SeriesStatisticsService(SeriesStatisticsRepository seriesStatisticsRepository)
{
_seriesStatisticsRepository = seriesStatisticsRepository;
}
public List<SeriesStatistics> SeriesStatistics()
{
return _seriesStatisticsRepository.SeriesStatistics();
}
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.SeriesStats;
namespace NzbDrone.Core.Tv namespace NzbDrone.Core.Tv
{ {
@ -13,17 +14,13 @@ namespace NzbDrone.Core.Tv
Series FindByTvdbId(int tvdbId); Series FindByTvdbId(int tvdbId);
void SetSeriesType(int seriesId, SeriesTypes seriesTypes); void SetSeriesType(int seriesId, SeriesTypes seriesTypes);
void SetTvRageId(int seriesId, int tvRageId); void SetTvRageId(int seriesId, int tvRageId);
List<SeriesStatistics> SeriesStatistics();
} }
public class SeriesRepository : BasicRepository<Series>, ISeriesRepository public class SeriesRepository : BasicRepository<Series>, ISeriesRepository
{ {
private readonly IDatabase _database;
public SeriesRepository(IDatabase database) public SeriesRepository(IDatabase database)
: base(database) : base(database)
{ {
_database = database;
} }
public bool SeriesPathExists(string path) public bool SeriesPathExists(string path)
@ -55,22 +52,5 @@ namespace NzbDrone.Core.Tv
{ {
SetFields(new Series { Id = seriesId, TvRageId = tvRageId }, s => s.TvRageId); SetFields(new Series { Id = seriesId, TvRageId = tvRageId }, s => s.TvRageId);
} }
public List<SeriesStatistics> SeriesStatistics()
{
_database.DataMapper.AddParameter("currentDate", DateTime.UtcNow);
var queryText = @"SELECT
SeriesId,
SUM(CASE WHEN Airdate <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount,
SUM(CASE WHEN EpisodeFileId > 0 AND AirDate <= @currentDate THEN 1 ELSE 0 END) as EpisodeFileCount,
MAX(SeasonNumber) as NumberOfSeasons,
MIN(CASE WHEN AirDate < @currentDate THEN NULL ELSE AirDate END) as NextAiringString
FROM Episodes
WHERE Ignored = 0
GROUP BY SeriesId";
return _database.DataMapper.Query<SeriesStatistics>(queryText);
}
} }
} }

@ -14,6 +14,7 @@ using NzbDrone.Core.Model;
using NzbDrone.Core.Organizer; using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
using NzbDrone.Core.RootFolders; using NzbDrone.Core.RootFolders;
using NzbDrone.Core.SeriesStats;
using NzbDrone.Core.Tv.Events; using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.Tv namespace NzbDrone.Core.Tv
@ -33,7 +34,6 @@ namespace NzbDrone.Core.Tv
void UpdateSeries(Series series); void UpdateSeries(Series series);
bool SeriesPathExists(string folder); bool SeriesPathExists(string folder);
List<Series> GetSeriesInList(IEnumerable<int> seriesIds); List<Series> GetSeriesInList(IEnumerable<int> seriesIds);
List<SeriesStatistics> SeriesStatistics();
} }
public class SeriesService : ISeriesService, IHandleAsync<SeriesAddedEvent> public class SeriesService : ISeriesService, IHandleAsync<SeriesAddedEvent>
@ -179,11 +179,6 @@ namespace NzbDrone.Core.Tv
return _seriesRepository.Get(seriesIds).ToList(); return _seriesRepository.Get(seriesIds).ToList();
} }
public List<SeriesStatistics> SeriesStatistics()
{
return _seriesRepository.SeriesStatistics();
}
public void HandleAsync(SeriesAddedEvent message) public void HandleAsync(SeriesAddedEvent message)
{ {
UpdateSeriesInfo(message.Series.Id); UpdateSeriesInfo(message.Series.Id);

Loading…
Cancel
Save