2013-02-24 19:18:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-07-23 23:43:54 +00:00
|
|
|
|
using System.IO;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2013-05-10 22:33:04 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-24 19:18:48 +00:00
|
|
|
|
using NzbDrone.Core.Download;
|
2014-08-21 06:29:34 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
2013-06-09 06:20:38 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2015-04-20 03:25:59 +00:00
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
using NzbDrone.Core.Profiles;
|
2014-01-18 11:44:36 +00:00
|
|
|
|
using NzbDrone.Core.Qualities;
|
2017-08-14 02:58:42 +00:00
|
|
|
|
using NzbDrone.Core.Music.Events;
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.History
|
|
|
|
|
{
|
2013-02-24 19:18:48 +00:00
|
|
|
|
public interface IHistoryService
|
|
|
|
|
{
|
2014-06-08 08:22:55 +00:00
|
|
|
|
QualityModel GetBestQualityInHistory(Profile profile, int episodeId);
|
2013-05-10 22:33:04 +00:00
|
|
|
|
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
|
2017-08-14 02:58:42 +00:00
|
|
|
|
History MostRecentForAlbum(int episodeId);
|
2014-12-19 00:26:42 +00:00
|
|
|
|
History MostRecentForDownloadId(string downloadId);
|
|
|
|
|
History Get(int historyId);
|
|
|
|
|
List<History> Find(string downloadId, HistoryEventType eventType);
|
2015-01-19 23:53:31 +00:00
|
|
|
|
List<History> FindByDownloadId(string downloadId);
|
2013-02-24 19:18:48 +00:00
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
2014-08-21 06:29:34 +00:00
|
|
|
|
public class HistoryService : IHistoryService,
|
2017-08-14 02:58:42 +00:00
|
|
|
|
IHandle<AlbumGrabbedEvent>,
|
2014-08-21 06:29:34 +00:00
|
|
|
|
IHandle<EpisodeImportedEvent>,
|
|
|
|
|
IHandle<DownloadFailedEvent>,
|
2016-07-20 22:53:49 +00:00
|
|
|
|
IHandle<EpisodeFileDeletedEvent>,
|
2017-08-14 02:58:42 +00:00
|
|
|
|
IHandle<ArtistDeletedEvent>
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
public History MostRecentForAlbum(int albumId)
|
2013-10-24 05:13:04 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return _historyRepository.MostRecentForAlbum(albumId);
|
2013-10-24 05:13:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
|
public History MostRecentForDownloadId(string downloadId)
|
2013-10-30 23:49:53 +00:00
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
|
return _historyRepository.MostRecentForDownloadId(downloadId);
|
2013-10-30 23:49:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
|
public History Get(int historyId)
|
2014-04-01 20:07:41 +00:00
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
|
return _historyRepository.Get(historyId);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
|
public List<History> Find(string downloadId, HistoryEventType eventType)
|
2013-02-23 21:29:22 +00:00
|
|
|
|
{
|
2014-12-19 00:26:42 +00:00
|
|
|
|
return _historyRepository.FindByDownloadId(downloadId).Where(c => c.EventType == eventType).ToList();
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 23:53:31 +00:00
|
|
|
|
public List<History> FindByDownloadId(string downloadId)
|
|
|
|
|
{
|
|
|
|
|
return _historyRepository.FindByDownloadId(downloadId);
|
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
public QualityModel GetBestQualityInHistory(Profile profile, int albumId)
|
2013-02-23 21:29:22 +00:00
|
|
|
|
{
|
2014-06-08 08:22:55 +00:00
|
|
|
|
var comparer = new QualityModelComparer(profile);
|
2017-08-14 02:58:42 +00:00
|
|
|
|
return _historyRepository.GetBestQualityInHistory(albumId)
|
2014-02-01 23:38:19 +00:00
|
|
|
|
.OrderByDescending(q => q, comparer)
|
|
|
|
|
.FirstOrDefault();
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 03:25:59 +00:00
|
|
|
|
private string FindDownloadId(EpisodeImportedEvent trackedDownload)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Trying to find downloadId for {0} from history", trackedDownload.ImportedEpisode.Path);
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var albumIds = trackedDownload.EpisodeInfo.Episodes.Select(c => c.Id).ToList();
|
2015-04-20 03:25:59 +00:00
|
|
|
|
|
|
|
|
|
var allHistory = _historyRepository.FindDownloadHistory(trackedDownload.EpisodeInfo.Series.Id, trackedDownload.ImportedEpisode.Quality);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Find download related items for these episdoes
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var albumsHistory = allHistory.Where(h => albumIds.Contains(h.AlbumId)).ToList();
|
2015-04-20 03:25:59 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var processedDownloadId = albumsHistory
|
2015-04-20 03:25:59 +00:00
|
|
|
|
.Where(c => c.EventType != HistoryEventType.Grabbed && c.DownloadId != null)
|
|
|
|
|
.Select(c => c.DownloadId);
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var stillDownloading = albumsHistory.Where(c => c.EventType == HistoryEventType.Grabbed && !processedDownloadId.Contains(c.DownloadId)).ToList();
|
2015-04-20 03:25:59 +00:00
|
|
|
|
|
|
|
|
|
string downloadId = null;
|
|
|
|
|
|
|
|
|
|
if (stillDownloading.Any())
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
foreach (var matchingHistory in trackedDownload.EpisodeInfo.Episodes.Select(e => stillDownloading.Where(c => c.AlbumId == e.Id).ToList()))
|
2015-04-20 03:25:59 +00:00
|
|
|
|
{
|
|
|
|
|
if (matchingHistory.Count != 1)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newDownloadId = matchingHistory.Single().DownloadId;
|
|
|
|
|
|
|
|
|
|
if (downloadId == null || downloadId == newDownloadId)
|
|
|
|
|
{
|
|
|
|
|
downloadId = newDownloadId;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return downloadId;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
public void Handle(AlbumGrabbedEvent message)
|
2013-02-24 19:18:48 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
foreach (var album in message.Album.Albums)
|
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,
|
2017-08-14 02:58:42 +00:00
|
|
|
|
Quality = message.Album.ParsedAlbumInfo.Quality,
|
|
|
|
|
SourceTitle = message.Album.Release.Title,
|
|
|
|
|
ArtistId = album.ArtistId,
|
|
|
|
|
AlbumId = album.Id,
|
2014-12-19 00:26:42 +00:00
|
|
|
|
DownloadId = message.DownloadId
|
2013-02-24 19:18:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
history.Data.Add("Indexer", message.Album.Release.Indexer);
|
|
|
|
|
history.Data.Add("NzbInfoUrl", message.Album.Release.InfoUrl);
|
|
|
|
|
history.Data.Add("ReleaseGroup", message.Album.ParsedAlbumInfo.ReleaseGroup);
|
|
|
|
|
history.Data.Add("Age", message.Album.Release.Age.ToString());
|
|
|
|
|
history.Data.Add("AgeHours", message.Album.Release.AgeHours.ToString());
|
|
|
|
|
history.Data.Add("AgeMinutes", message.Album.Release.AgeMinutes.ToString());
|
|
|
|
|
history.Data.Add("PublishedDate", message.Album.Release.PublishDate.ToString("s") + "Z");
|
2014-01-05 19:49:04 +00:00
|
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
2017-08-14 02:58:42 +00:00
|
|
|
|
history.Data.Add("Size", message.Album.Release.Size.ToString());
|
|
|
|
|
history.Data.Add("DownloadUrl", message.Album.Release.DownloadUrl);
|
|
|
|
|
history.Data.Add("Guid", message.Album.Release.Guid);
|
|
|
|
|
history.Data.Add("Protocol", ((int)message.Album.Release.DownloadProtocol).ToString());
|
|
|
|
|
|
|
|
|
|
if (!message.Album.ParsedAlbumInfo.ReleaseHash.IsNullOrWhiteSpace())
|
2014-05-19 19:14:41 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
history.Data.Add("ReleaseHash", message.Album.ParsedAlbumInfo.ReleaseHash);
|
2014-05-19 19:14:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
var torrentRelease = message.Album.Release as TorrentInfo;
|
2015-04-20 03:25:59 +00:00
|
|
|
|
|
|
|
|
|
if (torrentRelease != null)
|
|
|
|
|
{
|
|
|
|
|
history.Data.Add("TorrentInfoHash", torrentRelease.InfoHash);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 06:20:38 +00:00
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeImportedEvent message)
|
|
|
|
|
{
|
2014-01-26 07:14:55 +00:00
|
|
|
|
if (!message.NewDownload)
|
2014-01-22 05:22:09 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 00:26:42 +00:00
|
|
|
|
var downloadId = message.DownloadId;
|
|
|
|
|
|
|
|
|
|
if (downloadId.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
downloadId = FindDownloadId(message);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 01:28:30 +00:00
|
|
|
|
foreach (var episode in message.EpisodeInfo.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-10-18 01:28:30 +00:00
|
|
|
|
Quality = message.EpisodeInfo.Quality,
|
2014-09-15 23:46:12 +00:00
|
|
|
|
SourceTitle = message.ImportedEpisode.SceneName ?? Path.GetFileNameWithoutExtension(message.EpisodeInfo.Path),
|
2017-08-14 02:58:42 +00:00
|
|
|
|
ArtistId = message.ImportedEpisode.SeriesId,
|
|
|
|
|
AlbumId = episode.Id,
|
2014-12-19 00:26:42 +00:00
|
|
|
|
DownloadId = downloadId
|
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-10-18 01:28:30 +00:00
|
|
|
|
history.Data.Add("DroppedPath", message.EpisodeInfo.Path);
|
2014-07-23 23:43:54 +00:00
|
|
|
|
history.Data.Add("ImportedPath", Path.Combine(message.EpisodeInfo.Series.Path, message.ImportedEpisode.RelativePath));
|
2014-04-19 15:09:22 +00:00
|
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
2013-08-26 05:31:58 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-22 07:31:36 +00:00
|
|
|
|
|
|
|
|
|
public void Handle(DownloadFailedEvent message)
|
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
foreach (var albumId in message.AlbumIds)
|
2013-10-22 07:31:36 +00:00
|
|
|
|
{
|
2013-10-24 05:13:04 +00:00
|
|
|
|
var history = new History
|
|
|
|
|
{
|
|
|
|
|
EventType = HistoryEventType.DownloadFailed,
|
|
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
|
Quality = message.Quality,
|
|
|
|
|
SourceTitle = message.SourceTitle,
|
2017-08-14 02:58:42 +00:00
|
|
|
|
ArtistId = message.SeriesId,
|
|
|
|
|
AlbumId = albumId,
|
2014-12-19 00:26:42 +00:00
|
|
|
|
DownloadId = message.DownloadId
|
2013-10-24 05:13:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
history.Data.Add("DownloadClient", message.DownloadClient);
|
2013-10-25 01:25:04 +00:00
|
|
|
|
history.Data.Add("Message", message.Message);
|
2013-10-24 05:13:04 +00:00
|
|
|
|
|
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
2013-10-22 07:31:36 +00:00
|
|
|
|
}
|
2014-08-21 06:29:34 +00:00
|
|
|
|
|
|
|
|
|
public void Handle(EpisodeFileDeletedEvent message)
|
|
|
|
|
{
|
|
|
|
|
if (message.Reason == DeleteMediaFileReason.NoLinkedEpisodes)
|
|
|
|
|
{
|
2016-11-24 00:38:40 +00:00
|
|
|
|
_logger.Debug("Removing episode file from DB as part of cleanup routine, not creating history event.");
|
2014-08-21 06:29:34 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var episode in message.EpisodeFile.Episodes.Value)
|
|
|
|
|
{
|
|
|
|
|
var history = new History
|
|
|
|
|
{
|
|
|
|
|
EventType = HistoryEventType.EpisodeFileDeleted,
|
|
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
|
Quality = message.EpisodeFile.Quality,
|
|
|
|
|
SourceTitle = message.EpisodeFile.Path,
|
2017-08-14 02:58:42 +00:00
|
|
|
|
ArtistId = message.EpisodeFile.SeriesId,
|
|
|
|
|
AlbumId = episode.Id,
|
2014-08-21 06:29:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
history.Data.Add("Reason", message.Reason.ToString());
|
|
|
|
|
|
|
|
|
|
_historyRepository.Insert(history);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-20 22:53:49 +00:00
|
|
|
|
|
2017-08-14 02:58:42 +00:00
|
|
|
|
public void Handle(ArtistDeletedEvent message)
|
2016-07-20 22:53:49 +00:00
|
|
|
|
{
|
2017-08-14 02:58:42 +00:00
|
|
|
|
_historyRepository.DeleteForArtist(message.Artist.Id);
|
2016-07-20 22:53:49 +00:00
|
|
|
|
}
|
2013-02-23 21:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|