2013-02-24 11:18:48 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-02-23 13:29:22 -08:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-04-23 18:56:00 -07:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-05-10 15:33:04 -07:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-24 11:18:48 -08:00
|
|
|
using NzbDrone.Core.Download;
|
2013-06-08 23:20:38 -07:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-02-23 13:29:22 -08:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
{
|
2013-02-24 11:18:48 -08:00
|
|
|
public interface IHistoryService
|
|
|
|
{
|
|
|
|
List<History> All();
|
|
|
|
void Purge();
|
|
|
|
void Trim();
|
2013-03-24 18:38:11 -07:00
|
|
|
QualityModel GetBestQualityInHistory(int episodeId);
|
2013-05-10 15:33:04 -07:00
|
|
|
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
|
2013-02-24 11:18:48 -08:00
|
|
|
}
|
2013-02-23 13:29:22 -08:00
|
|
|
|
2013-06-08 23:20:38 -07:00
|
|
|
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>
|
2013-02-23 13:29:22 -08:00
|
|
|
{
|
|
|
|
private readonly IHistoryRepository _historyRepository;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public HistoryService(IHistoryRepository historyRepository, Logger logger)
|
|
|
|
{
|
|
|
|
_historyRepository = historyRepository;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<History> All()
|
|
|
|
{
|
|
|
|
return _historyRepository.All().ToList();
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:33:04 -07:00
|
|
|
public PagingSpec<History> Paged(PagingSpec<History> pagingSpec)
|
|
|
|
{
|
2013-06-04 17:49:53 -07:00
|
|
|
return _historyRepository.GetPaged(pagingSpec);
|
2013-05-10 15:33:04 -07:00
|
|
|
}
|
|
|
|
|
2013-02-23 13:29:22 -08:00
|
|
|
public void Purge()
|
|
|
|
{
|
|
|
|
_historyRepository.Purge();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Trim()
|
|
|
|
{
|
|
|
|
_historyRepository.Trim();
|
|
|
|
}
|
|
|
|
|
2013-03-24 18:38:11 -07:00
|
|
|
public virtual QualityModel GetBestQualityInHistory(int episodeId)
|
2013-02-23 13:29:22 -08:00
|
|
|
{
|
2013-05-14 08:27:33 -07:00
|
|
|
return _historyRepository.GetEpisodeHistory(episodeId).OrderByDescending(q => q).FirstOrDefault();
|
2013-02-23 13:29:22 -08:00
|
|
|
}
|
|
|
|
|
2013-02-24 11:18:48 -08:00
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
{
|
2013-04-14 18:41:39 -07:00
|
|
|
foreach (var episode in message.Episode.Episodes)
|
2013-02-24 11:18:48 -08:00
|
|
|
{
|
|
|
|
var history = new History
|
|
|
|
{
|
2013-06-10 18:55:05 -07:00
|
|
|
EventType = HistoryEventType.Grabbed,
|
2013-06-09 13:50:57 -07:00
|
|
|
Date = DateTime.UtcNow,
|
2013-04-28 12:46:13 -07:00
|
|
|
Quality = message.Episode.ParsedEpisodeInfo.Quality,
|
2013-06-08 23:20:38 -07:00
|
|
|
SourceTitle = message.Episode.Report.Title,
|
2013-05-13 22:47:53 -07:00
|
|
|
SeriesId = episode.SeriesId,
|
2013-03-24 18:38:11 -07:00
|
|
|
EpisodeId = episode.Id,
|
2013-02-24 11:18:48 -08:00
|
|
|
};
|
|
|
|
|
2013-06-08 23:20:38 -07:00
|
|
|
history.Data.Add("Indexer", message.Episode.Report.Indexer);
|
|
|
|
history.Data.Add("NzbInfoUrl", message.Episode.Report.NzbInfoUrl);
|
|
|
|
history.Data.Add("ReleaseGroup", message.Episode.Report.ReleaseGroup);
|
|
|
|
history.Data.Add("Age", message.Episode.Report.Age.ToString());
|
|
|
|
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(EpisodeImportedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.EpisodeFile.Episodes.Value)
|
|
|
|
{
|
|
|
|
var history = new History
|
|
|
|
{
|
2013-06-10 18:55:05 -07:00
|
|
|
EventType = HistoryEventType.DownloadFolderImported,
|
2013-06-09 13:50:57 -07:00
|
|
|
Date = DateTime.UtcNow,
|
2013-06-08 23:20:38 -07:00
|
|
|
Quality = message.EpisodeFile.Quality,
|
|
|
|
SourceTitle = message.EpisodeFile.Path,
|
|
|
|
SeriesId = message.EpisodeFile.SeriesId,
|
|
|
|
EpisodeId = episode.Id,
|
|
|
|
};
|
|
|
|
|
2013-02-24 11:18:48 -08:00
|
|
|
_historyRepository.Insert(history);
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 13:29:22 -08:00
|
|
|
}
|
|
|
|
}
|