SeriesStats moved to its own Repo

This commit is contained in:
Mark McDowall 2013-04-20 16:36:23 -07:00
parent 98df1be981
commit a04a5e8669
10 changed files with 92 additions and 41 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -349,6 +349,7 @@
<Compile Include="Parser\ParsingService.cs" />
<Compile Include="Qualities\QualitySizeRepository.cs" />
<Compile Include="Qualities\QualityProfileRepository.cs" />
<Compile Include="SeriesStats\SeriesStatisticsService.cs" />
<Compile Include="Tv\EpisodeService.cs" />
<Compile Include="Tv\EpisodeStatuses.cs" />
<Compile Include="Tv\Events\EpisodeInfoUpdatedEvent.cs" />
@ -387,7 +388,8 @@
<Compile Include="Providers\RecycleBinProvider.cs" />
<Compile Include="Tv\SeasonService.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="Model\Xbmc\TvShowResponse.cs" />
<Compile Include="Model\Xbmc\TvShow.cs" />

View File

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

View File

@ -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);
}
}
}

View File

@ -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();
}
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.SeriesStats;
namespace NzbDrone.Core.Tv
{
@ -13,17 +14,13 @@ namespace NzbDrone.Core.Tv
Series FindByTvdbId(int tvdbId);
void SetSeriesType(int seriesId, SeriesTypes seriesTypes);
void SetTvRageId(int seriesId, int tvRageId);
List<SeriesStatistics> SeriesStatistics();
}
public class SeriesRepository : BasicRepository<Series>, ISeriesRepository
{
private readonly IDatabase _database;
public SeriesRepository(IDatabase database)
: base(database)
{
_database = database;
}
public bool SeriesPathExists(string path)
@ -55,22 +52,5 @@ namespace NzbDrone.Core.Tv
{
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);
}
}
}

View File

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