From 729d1142b0259ca8730d0d8fc51c0da19c56ac62 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 25 Sep 2017 23:44:53 -0400 Subject: [PATCH] Log TrackImport and TrackFileDeleted to History --- .../History/Details/HistoryDetailsModal.js | 6 +-- frontend/src/Activity/History/HistoryRow.js | 16 ++++++ .../src/Store/Reducers/historyReducers.js | 5 ++ src/Lidarr.Api.V3/History/HistoryModule.cs | 2 + src/Lidarr.Api.V3/History/HistoryResource.cs | 4 ++ .../HistoryTests/HistoryServiceFixture.cs | 20 +++---- .../Migration/118_history_trackid.cs | 20 +++++++ src/NzbDrone.Core/Datastore/TableMapping.cs | 2 +- src/NzbDrone.Core/History/History.cs | 6 ++- .../History/HistoryRepository.cs | 3 +- src/NzbDrone.Core/History/HistoryService.cs | 53 +++++++++---------- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 12 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/118_history_trackid.cs diff --git a/frontend/src/Activity/History/Details/HistoryDetailsModal.js b/frontend/src/Activity/History/Details/HistoryDetailsModal.js index 0523d4397..ca8b9ca3a 100644 --- a/frontend/src/Activity/History/Details/HistoryDetailsModal.js +++ b/frontend/src/Activity/History/Details/HistoryDetailsModal.js @@ -18,11 +18,11 @@ function getHeaderTitle(eventType) { case 'downloadFailed': return 'Download Failed'; case 'downloadFolderImported': - return 'Episode Imported'; + return 'Track Imported'; case 'trackFileDeleted': - return 'Episode File Deleted'; + return 'Track File Deleted'; case 'trackFileRenamed': - return 'Episode File Renamed'; + return 'Track File Renamed'; default: return 'Unknown'; } diff --git a/frontend/src/Activity/History/HistoryRow.js b/frontend/src/Activity/History/HistoryRow.js index 550b373dd..f87d05e93 100644 --- a/frontend/src/Activity/History/HistoryRow.js +++ b/frontend/src/Activity/History/HistoryRow.js @@ -56,6 +56,7 @@ class HistoryRow extends Component { albumId, artist, album, + track, language, quality, eventType, @@ -121,6 +122,14 @@ class HistoryRow extends Component { ); } + if (name === 'trackTitle') { + return ( + + {track.title} + + ); + } + if (name === 'language') { return ( @@ -220,6 +229,7 @@ HistoryRow.propTypes = { albumId: PropTypes.number, artist: PropTypes.object.isRequired, album: PropTypes.object, + track: PropTypes.object, language: PropTypes.object.isRequired, quality: PropTypes.object.isRequired, eventType: PropTypes.string.isRequired, @@ -234,4 +244,10 @@ HistoryRow.propTypes = { onMarkAsFailedPress: PropTypes.func.isRequired }; +HistoryRow.defaultProps = { + track: { + title: '' + } +}; + export default HistoryRow; diff --git a/frontend/src/Store/Reducers/historyReducers.js b/frontend/src/Store/Reducers/historyReducers.js index 80c6bf441..8104c3814 100644 --- a/frontend/src/Store/Reducers/historyReducers.js +++ b/frontend/src/Store/Reducers/historyReducers.js @@ -37,6 +37,11 @@ export const defaultState = { label: 'Album Title', isVisible: true }, + { + name: 'trackTitle', + label: 'Track Title', + isVisible: true + }, { name: 'language', label: 'Language', diff --git a/src/Lidarr.Api.V3/History/HistoryModule.cs b/src/Lidarr.Api.V3/History/HistoryModule.cs index 601c6364c..9eccd8f0c 100644 --- a/src/Lidarr.Api.V3/History/HistoryModule.cs +++ b/src/Lidarr.Api.V3/History/HistoryModule.cs @@ -6,6 +6,7 @@ using NzbDrone.Core.Download; using NzbDrone.Core.History; using Lidarr.Api.V3.Albums; using Lidarr.Api.V3.Artist; +using Lidarr.Api.V3.Tracks; using Lidarr.Http; using Lidarr.Http.Extensions; @@ -35,6 +36,7 @@ namespace Lidarr.Api.V3.History resource.Artist = model.Artist.ToResource(); resource.Album = model.Album.ToResource(); + resource.Track = model.Track.ToResource(); if (model.Artist != null) { diff --git a/src/Lidarr.Api.V3/History/HistoryResource.cs b/src/Lidarr.Api.V3/History/HistoryResource.cs index 05e1bb522..86d8a9964 100644 --- a/src/Lidarr.Api.V3/History/HistoryResource.cs +++ b/src/Lidarr.Api.V3/History/HistoryResource.cs @@ -5,6 +5,7 @@ using NzbDrone.Core.Languages; using NzbDrone.Core.Qualities; using Lidarr.Api.V3.Albums; using Lidarr.Api.V3.Artist; +using Lidarr.Api.V3.Tracks; using Lidarr.Http.REST; namespace Lidarr.Api.V3.History @@ -13,6 +14,7 @@ namespace Lidarr.Api.V3.History { public int AlbumId { get; set; } public int ArtistId { get; set; } + public int TrackId { get; set; } public string SourceTitle { get; set; } public Language Language { get; set; } public QualityModel Quality { get; set; } @@ -26,6 +28,7 @@ namespace Lidarr.Api.V3.History public AlbumResource Album { get; set; } public ArtistResource Artist { get; set; } + public TrackResource Track { get; set; } } public static class HistoryResourceMapper @@ -40,6 +43,7 @@ namespace Lidarr.Api.V3.History AlbumId = model.AlbumId, ArtistId = model.ArtistId, + TrackId = model.TrackId, SourceTitle = model.SourceTitle, Language = model.Language, Quality = model.Quality, diff --git a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs index 854a5c9de..513017414 100644 --- a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs +++ b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs @@ -11,7 +11,7 @@ using NzbDrone.Core.Test.Framework; using NzbDrone.Core.History; using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Qualities; -using NzbDrone.Core.Tv; +using NzbDrone.Core.Music; using NzbDrone.Core.Languages; using NzbDrone.Core.Profiles.Languages; @@ -54,23 +54,23 @@ namespace NzbDrone.Core.Test.HistoryTests [Test] public void should_use_file_name_for_source_title_if_scene_name_is_null() { - var series = Builder.CreateNew().Build(); - var episodes = Builder.CreateListOfSize(1).Build().ToList(); - var episodeFile = Builder.CreateNew() + var artist = Builder.CreateNew().Build(); + var tracks = Builder.CreateListOfSize(1).Build().ToList(); + var trackFile = Builder.CreateNew() .With(f => f.SceneName = null) .Build(); - var localEpisode = new LocalEpisode + var localTrack = new LocalTrack { - Series = series, - Episodes = episodes, - Path = @"C:\Test\Unsorted\Series.s01e01.mkv" + Artist = artist, + Tracks = tracks, + Path = @"C:\Test\Unsorted\Artist.01.Hymn.mp3" }; - Subject.Handle(new EpisodeImportedEvent(localEpisode, episodeFile, true, "sab", "abcd")); + Subject.Handle(new TrackImportedEvent(localTrack, trackFile, true, "sab", "abcd")); Mocker.GetMock() - .Verify(v => v.Insert(It.Is(h => h.SourceTitle == Path.GetFileNameWithoutExtension(localEpisode.Path)))); + .Verify(v => v.Insert(It.Is(h => h.SourceTitle == Path.GetFileNameWithoutExtension(localTrack.Path)))); } } } diff --git a/src/NzbDrone.Core/Datastore/Migration/118_history_trackid.cs b/src/NzbDrone.Core/Datastore/Migration/118_history_trackid.cs new file mode 100644 index 000000000..d4ae1f7d0 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/118_history_trackid.cs @@ -0,0 +1,20 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(118)] + public class history_trackid : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Alter.Table("History") + .AddColumn("TrackId").AsInt32().WithDefaultValue(0); + } + + } +} diff --git a/src/NzbDrone.Core/Datastore/TableMapping.cs b/src/NzbDrone.Core/Datastore/TableMapping.cs index bc93a60dc..6b46cd2c7 100644 --- a/src/NzbDrone.Core/Datastore/TableMapping.cs +++ b/src/NzbDrone.Core/Datastore/TableMapping.cs @@ -113,7 +113,7 @@ namespace NzbDrone.Core.Datastore .Relationships.AutoMapICollectionOrComplexProperties() .For("Tracks") .LazyLoad(condition: parent => parent.Id > 0, - query: (db, parent) => db.Query().Where(c => c.ArtistId == parent.Id).ToList()) // TODO: Figure what the hell to do here + query: (db, parent) => db.Query().Where(c => c.TrackFileId == parent.Id).ToList()) // TODO: Figure what the hell to do here .HasOne(file => file.Artist, file => file.ArtistId); Mapper.Entity().RegisterModel("Tracks") diff --git a/src/NzbDrone.Core/History/History.cs b/src/NzbDrone.Core/History/History.cs index 8cbced081..a3e9a62d7 100644 --- a/src/NzbDrone.Core/History/History.cs +++ b/src/NzbDrone.Core/History/History.cs @@ -15,7 +15,8 @@ namespace NzbDrone.Core.History { Data = new Dictionary(); } - + + public int TrackId { get; set; } public int AlbumId { get; set; } public int ArtistId { get; set; } public string SourceTitle { get; set; } @@ -23,6 +24,7 @@ namespace NzbDrone.Core.History public DateTime Date { get; set; } public Album Album { get; set; } public Artist Artist { get; set; } + public Track Track { get; set; } public HistoryEventType EventType { get; set; } public Dictionary Data { get; set; } public Language Language { get; set; } @@ -38,6 +40,6 @@ namespace NzbDrone.Core.History SeriesFolderImported = 2, DownloadFolderImported = 3, DownloadFailed = 4, - EpisodeFileDeleted = 5 + TrackFileDeleted = 5 } } diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index 47346f003..9cc0049bf 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -64,7 +64,8 @@ namespace NzbDrone.Core.History protected override SortBuilder GetPagedQuery(QueryBuilder query, PagingSpec pagingSpec) { var baseQuery = query.Join(JoinType.Inner, h => h.Artist, (h, s) => h.ArtistId == s.Id) - .Join(JoinType.Inner, h => h.Album, (h, e) => h.AlbumId == e.Id); + .Join(JoinType.Inner, h => h.Album, (h, e) => h.AlbumId == e.Id) + .Join(JoinType.Left, h => h.Track, (h, e) => h.TrackId == e.Id); return base.GetPagedQuery(baseQuery, pagingSpec); } diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index 9a52b859f..696eca938 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -30,9 +30,9 @@ namespace NzbDrone.Core.History public class HistoryService : IHistoryService, IHandle, - IHandle, + IHandle, IHandle, - IHandle, + IHandle, IHandle { private readonly IHistoryRepository _historyRepository; @@ -74,14 +74,13 @@ namespace NzbDrone.Core.History return _historyRepository.FindByDownloadId(downloadId); } - [Obsolete("Used for Sonarr, not Lidarr")] - private string FindDownloadId(EpisodeImportedEvent trackedDownload) + private string FindDownloadId(TrackImportedEvent trackedDownload) { - _logger.Debug("Trying to find downloadId for {0} from history", trackedDownload.ImportedEpisode.Path); + _logger.Debug("Trying to find downloadId for {0} from history", trackedDownload.ImportedTrack.Path); - var albumIds = trackedDownload.EpisodeInfo.Episodes.Select(c => c.Id).ToList(); + var albumIds = trackedDownload.TrackInfo.Tracks.Select(c => c.AlbumId).ToList(); - var allHistory = _historyRepository.FindDownloadHistory(trackedDownload.EpisodeInfo.Series.Id, trackedDownload.ImportedEpisode.Quality); + var allHistory = _historyRepository.FindDownloadHistory(trackedDownload.TrackInfo.Artist.Id, trackedDownload.ImportedTrack.Quality); //Find download related items for these episdoes @@ -97,7 +96,7 @@ namespace NzbDrone.Core.History if (stillDownloading.Any()) { - foreach (var matchingHistory in trackedDownload.EpisodeInfo.Episodes.Select(e => stillDownloading.Where(c => c.AlbumId == e.Id).ToList())) + foreach (var matchingHistory in trackedDownload.TrackInfo.Tracks.Select(e => stillDownloading.Where(c => c.AlbumId == e.AlbumId).ToList())) { if (matchingHistory.Count != 1) { @@ -165,8 +164,7 @@ namespace NzbDrone.Core.History } } - [Obsolete("Used for Sonarr, not Lidarr")] - public void Handle(EpisodeImportedEvent message) + public void Handle(TrackImportedEvent message) { if (!message.NewDownload) { @@ -180,24 +178,25 @@ namespace NzbDrone.Core.History downloadId = FindDownloadId(message); } - foreach (var episode in message.EpisodeInfo.Episodes) + foreach (var track in message.TrackInfo.Tracks) { var history = new History { EventType = HistoryEventType.DownloadFolderImported, Date = DateTime.UtcNow, - Quality = message.EpisodeInfo.Quality, - SourceTitle = message.ImportedEpisode.SceneName ?? Path.GetFileNameWithoutExtension(message.EpisodeInfo.Path), - ArtistId = message.ImportedEpisode.SeriesId, - AlbumId = episode.Id, + Quality = message.TrackInfo.Quality, + SourceTitle = message.ImportedTrack.SceneName ?? Path.GetFileNameWithoutExtension(message.TrackInfo.Path), + ArtistId = message.ImportedTrack.ArtistId, + AlbumId = message.ImportedTrack.AlbumId, + TrackId = track.Id, DownloadId = downloadId, - Language = message.EpisodeInfo.Language + Language = message.TrackInfo.Language }; //Won't have a value since we publish this event before saving to DB. //history.Data.Add("FileId", message.ImportedEpisode.Id.ToString()); - history.Data.Add("DroppedPath", message.EpisodeInfo.Path); - history.Data.Add("ImportedPath", Path.Combine(message.EpisodeInfo.Series.Path, message.ImportedEpisode.RelativePath)); + history.Data.Add("DroppedPath", message.TrackInfo.Path); + history.Data.Add("ImportedPath", Path.Combine(message.TrackInfo.Artist.Path, message.ImportedTrack.RelativePath)); history.Data.Add("DownloadClient", message.DownloadClient); _historyRepository.Insert(history); @@ -227,25 +226,25 @@ namespace NzbDrone.Core.History } } - [Obsolete("Used for Sonarr, not Lidarr")] - public void Handle(EpisodeFileDeletedEvent message) + public void Handle(TrackFileDeletedEvent message) { if (message.Reason == DeleteMediaFileReason.NoLinkedEpisodes) { - _logger.Debug("Removing episode file from DB as part of cleanup routine, not creating history event."); + _logger.Debug("Removing track file from DB as part of cleanup routine, not creating history event."); return; } - foreach (var episode in message.EpisodeFile.Episodes.Value) + foreach (var track in message.TrackFile.Tracks.Value) { var history = new History { - EventType = HistoryEventType.EpisodeFileDeleted, + EventType = HistoryEventType.TrackFileDeleted, Date = DateTime.UtcNow, - Quality = message.EpisodeFile.Quality, - SourceTitle = message.EpisodeFile.Path, - ArtistId = message.EpisodeFile.SeriesId, - AlbumId = episode.Id, + Quality = message.TrackFile.Quality, + SourceTitle = message.TrackFile.Path, + ArtistId = message.TrackFile.ArtistId, + AlbumId = message.TrackFile.AlbumId, + TrackId = track.Id, }; history.Data.Add("Reason", message.Reason.ToString()); diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 68ad7bfdb..12fbd92fd 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -293,6 +293,7 @@ +