2013-02-24 22:18:48 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-02-24 00:29:22 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-05-11 01:33:04 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-24 22:18:48 +03:00
|
|
|
using NzbDrone.Core.Download;
|
2013-06-09 09:20:38 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-09-14 09:36:07 +03:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-01-18 14:44:36 +03:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-02-24 00:29:22 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
{
|
2013-02-24 22:18:48 +03:00
|
|
|
public interface IHistoryService
|
|
|
|
{
|
|
|
|
List<History> All();
|
|
|
|
void Purge();
|
|
|
|
void Trim();
|
2014-02-02 02:38:19 +03:00
|
|
|
QualityModel GetBestQualityInHistory(QualityProfile qualityProfile, int episodeId);
|
2013-05-11 01:33:04 +03:00
|
|
|
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
|
2013-10-22 10:31:36 +03:00
|
|
|
List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType);
|
|
|
|
List<History> Failed();
|
2013-10-24 08:13:04 +03:00
|
|
|
List<History> Grabbed();
|
2014-04-19 18:09:22 +03:00
|
|
|
List<History> Imported();
|
2013-10-24 08:13:04 +03:00
|
|
|
History MostRecentForEpisode(int episodeId);
|
2013-10-31 02:49:53 +03:00
|
|
|
History Get(int id);
|
2014-04-01 23:07:41 +03:00
|
|
|
List<History> FindBySourceTitle(string sourceTitle);
|
2013-02-24 22:18:48 +03:00
|
|
|
}
|
2013-02-24 00:29:22 +03:00
|
|
|
|
2013-10-22 10:31:36 +03:00
|
|
|
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>, IHandle<DownloadFailedEvent>
|
2013-02-24 00:29:22 +03: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-11 01:33:04 +03:00
|
|
|
public PagingSpec<History> Paged(PagingSpec<History> pagingSpec)
|
|
|
|
{
|
2013-06-05 03:49:53 +03:00
|
|
|
return _historyRepository.GetPaged(pagingSpec);
|
2013-05-11 01:33:04 +03:00
|
|
|
}
|
|
|
|
|
2013-10-22 10:31:36 +03:00
|
|
|
public List<History> BetweenDates(DateTime startDate, DateTime endDate, HistoryEventType eventType)
|
|
|
|
{
|
|
|
|
return _historyRepository.BetweenDates(startDate, endDate, eventType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<History> Failed()
|
|
|
|
{
|
|
|
|
return _historyRepository.Failed();
|
|
|
|
}
|
|
|
|
|
2013-10-24 08:13:04 +03:00
|
|
|
public List<History> Grabbed()
|
|
|
|
{
|
|
|
|
return _historyRepository.Grabbed();
|
|
|
|
}
|
|
|
|
|
2014-04-19 18:09:22 +03:00
|
|
|
public List<History> Imported()
|
|
|
|
{
|
|
|
|
return _historyRepository.Imported();
|
|
|
|
}
|
|
|
|
|
2013-10-24 08:13:04 +03:00
|
|
|
public History MostRecentForEpisode(int episodeId)
|
|
|
|
{
|
|
|
|
return _historyRepository.MostRecentForEpisode(episodeId);
|
|
|
|
}
|
|
|
|
|
2013-10-31 02:49:53 +03:00
|
|
|
public History Get(int id)
|
|
|
|
{
|
|
|
|
return _historyRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:07:41 +03:00
|
|
|
public List<History> FindBySourceTitle(string sourceTitle)
|
|
|
|
{
|
|
|
|
return _historyRepository.FindBySourceTitle(sourceTitle);
|
|
|
|
}
|
|
|
|
|
2013-02-24 00:29:22 +03:00
|
|
|
public void Purge()
|
|
|
|
{
|
|
|
|
_historyRepository.Purge();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Trim()
|
|
|
|
{
|
|
|
|
_historyRepository.Trim();
|
|
|
|
}
|
|
|
|
|
2014-02-02 02:38:19 +03:00
|
|
|
public QualityModel GetBestQualityInHistory(QualityProfile qualityProfile, int episodeId)
|
2013-02-24 00:29:22 +03:00
|
|
|
{
|
2014-02-02 02:38:19 +03:00
|
|
|
var comparer = new QualityModelComparer(qualityProfile);
|
|
|
|
return _historyRepository.GetBestQualityInHistory(episodeId)
|
|
|
|
.OrderByDescending(q => q, comparer)
|
|
|
|
.FirstOrDefault();
|
2013-02-24 00:29:22 +03:00
|
|
|
}
|
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
foreach (var episode in message.Episode.Episodes)
|
2013-02-24 22:18:48 +03:00
|
|
|
{
|
|
|
|
var history = new History
|
|
|
|
{
|
2013-08-30 09:38:55 +03:00
|
|
|
EventType = HistoryEventType.Grabbed,
|
2013-06-09 23:50:57 +03:00
|
|
|
Date = DateTime.UtcNow,
|
2013-04-28 22:46:13 +03:00
|
|
|
Quality = message.Episode.ParsedEpisodeInfo.Quality,
|
2013-09-14 02:17:58 +03:00
|
|
|
SourceTitle = message.Episode.Release.Title,
|
2013-05-14 08:47:53 +03:00
|
|
|
SeriesId = episode.SeriesId,
|
2013-03-25 04:38:11 +03:00
|
|
|
EpisodeId = episode.Id,
|
2013-02-24 22:18:48 +03:00
|
|
|
};
|
|
|
|
|
2013-09-14 02:17:58 +03:00
|
|
|
history.Data.Add("Indexer", message.Episode.Release.Indexer);
|
|
|
|
history.Data.Add("NzbInfoUrl", message.Episode.Release.InfoUrl);
|
2013-12-09 09:25:11 +03:00
|
|
|
history.Data.Add("ReleaseGroup", message.Episode.ParsedEpisodeInfo.ReleaseGroup);
|
2013-09-14 02:17:58 +03:00
|
|
|
history.Data.Add("Age", message.Episode.Release.Age.ToString());
|
2014-04-01 23:07:41 +03:00
|
|
|
history.Data.Add("AgeHours", message.Episode.Release.AgeHours.ToString());
|
|
|
|
history.Data.Add("PublishedDate", message.Episode.Release.PublishDate.ToString("s") + "Z");
|
2014-01-05 22:49:04 +03:00
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
2013-06-09 09:20:38 +03:00
|
|
|
|
2013-10-21 04:30:46 +03:00
|
|
|
if (!String.IsNullOrWhiteSpace(message.DownloadClientId))
|
|
|
|
{
|
|
|
|
history.Data.Add("DownloadClientId", message.DownloadClientId);
|
|
|
|
}
|
|
|
|
|
2013-06-09 09:20:38 +03:00
|
|
|
_historyRepository.Insert(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(EpisodeImportedEvent message)
|
|
|
|
{
|
2014-01-26 10:14:55 +03:00
|
|
|
if (!message.NewDownload)
|
2014-01-22 08:22:09 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-18 04:28:30 +03:00
|
|
|
foreach (var episode in message.EpisodeInfo.Episodes)
|
2013-06-09 09:20:38 +03:00
|
|
|
{
|
|
|
|
var history = new History
|
|
|
|
{
|
2013-06-11 04:55:05 +03:00
|
|
|
EventType = HistoryEventType.DownloadFolderImported,
|
2013-06-09 23:50:57 +03:00
|
|
|
Date = DateTime.UtcNow,
|
2013-10-18 04:28:30 +03:00
|
|
|
Quality = message.EpisodeInfo.Quality,
|
2013-08-30 09:38:55 +03:00
|
|
|
SourceTitle = message.ImportedEpisode.SceneName,
|
|
|
|
SeriesId = message.ImportedEpisode.SeriesId,
|
2013-08-26 08:31:58 +03:00
|
|
|
EpisodeId = episode.Id
|
2013-06-09 09:20:38 +03:00
|
|
|
};
|
|
|
|
|
2013-08-30 09:39:41 +03:00
|
|
|
//Won't have a value since we publish this event before saving to DB.
|
|
|
|
//history.Data.Add("FileId", message.ImportedEpisode.Id.ToString());
|
2013-10-18 04:28:30 +03:00
|
|
|
history.Data.Add("DroppedPath", message.EpisodeInfo.Path);
|
2013-08-30 09:38:55 +03:00
|
|
|
history.Data.Add("ImportedPath", message.ImportedEpisode.Path);
|
2014-04-19 18:09:22 +03:00
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
|
|
|
history.Data.Add("DownloadClientId", message.DownloadClientId);
|
2013-08-26 08:31:58 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
_historyRepository.Insert(history);
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 10:31:36 +03:00
|
|
|
|
|
|
|
public void Handle(DownloadFailedEvent message)
|
|
|
|
{
|
2013-10-24 08:13:04 +03:00
|
|
|
foreach (var episodeId in message.EpisodeIds)
|
2013-10-22 10:31:36 +03:00
|
|
|
{
|
2013-10-24 08:13:04 +03:00
|
|
|
var history = new History
|
|
|
|
{
|
|
|
|
EventType = HistoryEventType.DownloadFailed,
|
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
Quality = message.Quality,
|
|
|
|
SourceTitle = message.SourceTitle,
|
|
|
|
SeriesId = message.SeriesId,
|
|
|
|
EpisodeId = episodeId,
|
|
|
|
};
|
|
|
|
|
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
|
|
|
history.Data.Add("DownloadClientId", message.DownloadClientId);
|
2013-10-25 04:25:04 +03:00
|
|
|
history.Data.Add("Message", message.Message);
|
2013-10-24 08:13:04 +03:00
|
|
|
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
}
|
2013-10-22 10:31:36 +03:00
|
|
|
}
|
2013-02-24 00:29:22 +03:00
|
|
|
}
|
|
|
|
}
|