2013-02-24 19:18:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-05-10 22:33:04 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
using NzbDrone.Core.Download;
|
2013-06-09 06:20:38 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
|
{
|
2013-02-24 19:18:48 +00:00
|
|
|
|
public interface IHistoryService
|
|
|
|
|
{
|
|
|
|
|
List<History> All();
|
|
|
|
|
void Purge();
|
|
|
|
|
void Trim();
|
2013-03-25 01:38:11 +00:00
|
|
|
|
QualityModel GetBestQualityInHistory(int episodeId);
|
2013-05-10 22:33:04 +00:00
|
|
|
|
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
|
2013-09-18 05:28:05 +00:00
|
|
|
|
void CleanupOrphaned();
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
2013-06-09 06:20:38 +00:00
|
|
|
|
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>
|
2013-02-23 21:29:22 +00: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 22:33:04 +00:00
|
|
|
|
public PagingSpec<History> Paged(PagingSpec<History> pagingSpec)
|
|
|
|
|
{
|
2013-06-05 00:49:53 +00:00
|
|
|
|
return _historyRepository.GetPaged(pagingSpec);
|
2013-05-10 22:33:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 05:28:05 +00:00
|
|
|
|
public void CleanupOrphaned()
|
|
|
|
|
{
|
|
|
|
|
_historyRepository.CleanupOrphanedBySeries();
|
|
|
|
|
_historyRepository.CleanupOrphanedByEpisode();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 21:29:22 +00:00
|
|
|
|
public void Purge()
|
|
|
|
|
{
|
|
|
|
|
_historyRepository.Purge();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Trim()
|
|
|
|
|
{
|
|
|
|
|
_historyRepository.Trim();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 01:38:11 +00:00
|
|
|
|
public virtual QualityModel GetBestQualityInHistory(int episodeId)
|
2013-02-23 21:29:22 +00:00
|
|
|
|
{
|
2013-05-14 15:27:33 +00:00
|
|
|
|
return _historyRepository.GetEpisodeHistory(episodeId).OrderByDescending(q => q).FirstOrDefault();
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
|
{
|
2013-04-15 01:41:39 +00:00
|
|
|
|
foreach (var episode in message.Episode.Episodes)
|
2013-02-24 19:18:48 +00:00
|
|
|
|
{
|
|
|
|
|
var history = new History
|
|
|
|
|
{
|
2013-08-30 06:38:55 +00:00
|
|
|
|
EventType = HistoryEventType.Grabbed,
|
2013-06-09 20:50:57 +00:00
|
|
|
|
Date = DateTime.UtcNow,
|
2013-04-28 19:46:13 +00:00
|
|
|
|
Quality = message.Episode.ParsedEpisodeInfo.Quality,
|
2013-09-13 23:17:58 +00:00
|
|
|
|
SourceTitle = message.Episode.Release.Title,
|
2013-05-14 05:47:53 +00:00
|
|
|
|
SeriesId = episode.SeriesId,
|
2013-03-25 01:38:11 +00:00
|
|
|
|
EpisodeId = episode.Id,
|
2013-02-24 19:18:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-09-13 23:17:58 +00:00
|
|
|
|
history.Data.Add("Indexer", message.Episode.Release.Indexer);
|
|
|
|
|
history.Data.Add("NzbInfoUrl", message.Episode.Release.InfoUrl);
|
|
|
|
|
history.Data.Add("ReleaseGroup", message.Episode.Release.ReleaseGroup);
|
|
|
|
|
history.Data.Add("Age", message.Episode.Release.Age.ToString());
|
2013-06-09 06:20:38 +00:00
|
|
|
|
|
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeImportedEvent message)
|
|
|
|
|
{
|
2013-08-30 06:38:55 +00:00
|
|
|
|
foreach (var episode in message.DroppedEpisode.Episodes)
|
2013-06-09 06:20:38 +00:00
|
|
|
|
{
|
|
|
|
|
var history = new History
|
|
|
|
|
{
|
2013-06-11 01:55:05 +00:00
|
|
|
|
EventType = HistoryEventType.DownloadFolderImported,
|
2013-06-09 20:50:57 +00:00
|
|
|
|
Date = DateTime.UtcNow,
|
2013-08-30 06:38:55 +00:00
|
|
|
|
Quality = message.DroppedEpisode.Quality,
|
|
|
|
|
SourceTitle = message.ImportedEpisode.SceneName,
|
|
|
|
|
SeriesId = message.ImportedEpisode.SeriesId,
|
2013-08-26 05:31:58 +00:00
|
|
|
|
EpisodeId = episode.Id
|
2013-06-09 06:20:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-30 06:39:41 +00:00
|
|
|
|
//Won't have a value since we publish this event before saving to DB.
|
|
|
|
|
//history.Data.Add("FileId", message.ImportedEpisode.Id.ToString());
|
2013-08-30 06:38:55 +00:00
|
|
|
|
history.Data.Add("DroppedPath", message.DroppedEpisode.Path);
|
|
|
|
|
history.Data.Add("ImportedPath", message.ImportedEpisode.Path);
|
2013-08-26 05:31:58 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|