2013-02-23 21:29:22 +00:00
|
|
|
|
using System;
|
2013-05-07 02:32:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using System.Linq;
|
2013-05-10 22:33:04 +00:00
|
|
|
|
using Marr.Data.QGen;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-11-28 07:08:40 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Extentions;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-01-18 11:44:36 +00:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
|
{
|
|
|
|
|
public interface IHistoryRepository : IBasicRepository<History>
|
|
|
|
|
{
|
|
|
|
|
void Trim();
|
2013-10-06 18:06:39 +00:00
|
|
|
|
List<QualityModel> GetBestQualityInHistory(int episodeId);
|
2013-10-22 07:31:36 +00:00
|
|
|
|
List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType);
|
|
|
|
|
List<History> Failed();
|
2013-10-24 05:13:04 +00:00
|
|
|
|
List<History> Grabbed();
|
|
|
|
|
History MostRecentForEpisode(int episodeId);
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
|
|
|
|
|
{
|
2013-09-18 05:28:05 +00:00
|
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
|
public HistoryRepository(IDatabase database, IEventAggregator eventAggregator)
|
|
|
|
|
: base(database, eventAggregator)
|
2013-02-23 21:29:22 +00:00
|
|
|
|
{
|
2013-09-18 05:28:05 +00:00
|
|
|
|
_database = database;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Trim()
|
|
|
|
|
{
|
2013-06-09 20:50:57 +00:00
|
|
|
|
var cutoff = DateTime.UtcNow.AddDays(-30).Date;
|
2013-03-27 03:44:52 +00:00
|
|
|
|
Delete(c=> c.Date < cutoff);
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 18:06:39 +00:00
|
|
|
|
public List<QualityModel> GetBestQualityInHistory(int episodeId)
|
2013-02-23 21:29:22 +00:00
|
|
|
|
{
|
2013-05-14 05:40:06 +00:00
|
|
|
|
var history = Query.Where(c => c.EpisodeId == episodeId);
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
2013-05-14 05:40:06 +00:00
|
|
|
|
return history.Select(h => h.Quality).ToList();
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
2013-05-07 02:32:43 +00:00
|
|
|
|
|
2013-10-22 07:31:36 +00:00
|
|
|
|
public List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType)
|
|
|
|
|
{
|
|
|
|
|
return Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)
|
|
|
|
|
.Join<History, Episode>(JoinType.Inner, h => h.Episode, (h, e) => h.EpisodeId == e.Id)
|
|
|
|
|
.Where(h => h.Date >= startDate)
|
|
|
|
|
.AndWhere(h => h.Date <= endDate)
|
|
|
|
|
.AndWhere(h => h.EventType == eventType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<History> Failed()
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(h => h.EventType == HistoryEventType.DownloadFailed);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 05:13:04 +00:00
|
|
|
|
public List<History> Grabbed()
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(h => h.EventType == HistoryEventType.Grabbed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public History MostRecentForEpisode(int episodeId)
|
|
|
|
|
{
|
|
|
|
|
return Query.Where(h => h.EpisodeId == episodeId)
|
|
|
|
|
.OrderByDescending(h => h.Date)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-01 22:09:22 +00:00
|
|
|
|
protected override SortBuilder<History> GetPagedQuery(QueryBuilder<History> query, PagingSpec<History> pagingSpec)
|
2013-10-04 06:01:02 +00:00
|
|
|
|
{
|
2014-02-01 22:09:22 +00:00
|
|
|
|
var baseQuery = query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)
|
|
|
|
|
.Join<History, Episode>(JoinType.Inner, h => h.Episode, (h, e) => h.EpisodeId == e.Id);
|
2013-10-06 18:06:39 +00:00
|
|
|
|
|
2014-02-01 22:09:22 +00:00
|
|
|
|
return base.GetPagedQuery(baseQuery, pagingSpec);
|
2013-05-10 22:33:04 +00:00
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|