2013-04-21 01:36:23 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-07-18 02:07:48 +02:00
|
|
|
|
using System.Text;
|
2013-04-21 01:36:23 +02:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.SeriesStats
|
|
|
|
|
{
|
|
|
|
|
public interface ISeriesStatisticsRepository
|
|
|
|
|
{
|
|
|
|
|
List<SeriesStatistics> SeriesStatistics();
|
2013-07-18 02:07:48 +02:00
|
|
|
|
SeriesStatistics SeriesStatistics(int seriesId);
|
2013-04-21 01:36:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SeriesStatisticsRepository : ISeriesStatisticsRepository
|
|
|
|
|
{
|
2013-07-16 02:45:49 +02:00
|
|
|
|
private readonly IDatabase _database;
|
2013-04-21 01:36:23 +02:00
|
|
|
|
|
|
|
|
|
public SeriesStatisticsRepository(IDatabase database)
|
|
|
|
|
{
|
2013-07-16 02:45:49 +02:00
|
|
|
|
_database = database;
|
2013-04-21 01:36:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SeriesStatistics> SeriesStatistics()
|
|
|
|
|
{
|
2013-07-16 02:45:49 +02:00
|
|
|
|
var mapper = _database.GetDataMapper();
|
2013-04-21 01:36:23 +02:00
|
|
|
|
|
2013-07-16 02:45:49 +02:00
|
|
|
|
mapper.AddParameter("currentDate", DateTime.UtcNow);
|
|
|
|
|
|
2013-07-18 02:07:48 +02:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.AppendLine(GetSelectClause());
|
|
|
|
|
sb.AppendLine(GetGroupByClause());
|
|
|
|
|
var queryText = sb.ToString();
|
2013-04-21 01:36:23 +02:00
|
|
|
|
|
2013-07-16 02:45:49 +02:00
|
|
|
|
return mapper.Query<SeriesStatistics>(queryText);
|
2013-04-21 01:36:23 +02:00
|
|
|
|
}
|
2013-07-18 02:07:48 +02:00
|
|
|
|
|
|
|
|
|
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,
|
2013-07-26 05:25:24 +02:00
|
|
|
|
SUM(CASE WHEN Monitored = 1 AND AirdateUtc <= @currentDate THEN 1 ELSE 0 END) AS EpisodeCount,
|
|
|
|
|
SUM(CASE WHEN Monitored = 1 AND Episodes.EpisodeFileId > 0 AND AirDateUtc <= @currentDate THEN 1 ELSE 0 END) AS EpisodeFileCount,
|
2013-07-18 02:07:48 +02:00
|
|
|
|
MAX(Episodes.SeasonNumber) as SeasonCount,
|
2013-07-26 06:31:57 +02:00
|
|
|
|
MIN(CASE WHEN AirDateUtc < @currentDate THEN NULL ELSE AirDateUtc END) AS NextAiringString
|
2013-07-18 02:07:48 +02:00
|
|
|
|
FROM Episodes";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetGroupByClause()
|
|
|
|
|
{
|
|
|
|
|
return "GROUP BY SeriesId";
|
|
|
|
|
}
|
2013-04-21 01:36:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|