sonarr-repo-only/NzbDrone.Core/SeriesStats/SeriesStatisticsRepository.cs

70 lines
2.2 KiB
C#
Raw Normal View History

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