parent
1d0df366fb
commit
1eb0d3b11a
@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(052)]
|
||||
public class download_history : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Create.TableForModel("DownloadHistory")
|
||||
.WithColumn("EventType").AsInt32().NotNullable()
|
||||
.WithColumn("ArtistId").AsInt32().NotNullable()
|
||||
.WithColumn("DownloadId").AsString().NotNullable()
|
||||
.WithColumn("SourceTitle").AsString().NotNullable()
|
||||
.WithColumn("Date").AsDateTime().NotNullable()
|
||||
.WithColumn("Protocol").AsInt32().Nullable()
|
||||
.WithColumn("IndexerId").AsInt32().Nullable()
|
||||
.WithColumn("DownloadClientId").AsInt32().Nullable()
|
||||
.WithColumn("Release").AsString().Nullable()
|
||||
.WithColumn("Data").AsString().Nullable();
|
||||
|
||||
Create.Index().OnTable("DownloadHistory").OnColumn("EventType");
|
||||
Create.Index().OnTable("DownloadHistory").OnColumn("ArtistId");
|
||||
Create.Index().OnTable("DownloadHistory").OnColumn("DownloadId");
|
||||
|
||||
Execute.WithConnection(InitialImportedDownloadHistory);
|
||||
}
|
||||
|
||||
private static readonly Dictionary<int, int> EventTypeMap = new Dictionary<int, int>()
|
||||
{
|
||||
// EntityHistoryType.Grabbed -> DownloadHistoryType.Grabbed
|
||||
{ 1, 1 },
|
||||
|
||||
// EntityHistoryType.DownloadFolderImported -> DownloadHistoryType.DownloadImported
|
||||
{ 8, 2 },
|
||||
|
||||
// EntityHistoryType.DownloadFailed -> DownloadHistoryType.DownloadFailed
|
||||
{ 4, 3 },
|
||||
|
||||
// EntityHistoryType.DownloadIgnored -> DownloadHistoryType.DownloadIgnored
|
||||
{ 10, 4 },
|
||||
|
||||
// EntityHistoryType.DownloadImportIncomplete -> DownloadHistoryType.DownloadImportIncomplete
|
||||
{ 7, 6 }
|
||||
};
|
||||
|
||||
private void InitialImportedDownloadHistory(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
using (var cmd = conn.CreateCommand())
|
||||
{
|
||||
cmd.Transaction = tran;
|
||||
cmd.CommandText = "SELECT ArtistId, DownloadId, EventType, SourceTitle, Date, Data FROM History WHERE DownloadId IS NOT NULL AND EventType IN (1, 8, 4, 10, 7) GROUP BY EventType, DownloadId";
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var artistId = reader.GetInt32(0);
|
||||
var downloadId = reader.GetString(1);
|
||||
var eventType = reader.GetInt32(2);
|
||||
var sourceTitle = reader.GetString(3);
|
||||
var date = reader.GetDateTime(4);
|
||||
var rawData = reader.GetString(5);
|
||||
var data = Json.Deserialize<Dictionary<string, string>>(rawData);
|
||||
|
||||
var downloadHistoryEventType = EventTypeMap[eventType];
|
||||
var protocol = data.ContainsKey("protocol") ? Convert.ToInt32(data["protocol"]) : (int?)null;
|
||||
var downloadHistoryData = new Dictionary<string, string>();
|
||||
|
||||
if (data.ContainsKey("indexer"))
|
||||
{
|
||||
downloadHistoryData.Add("indexer", data["indexer"]);
|
||||
}
|
||||
|
||||
if (data.ContainsKey("downloadClient"))
|
||||
{
|
||||
downloadHistoryData.Add("downloadClient", data["downloadClient"]);
|
||||
}
|
||||
|
||||
using (var updateCmd = conn.CreateCommand())
|
||||
{
|
||||
updateCmd.Transaction = tran;
|
||||
updateCmd.CommandText = @"INSERT INTO DownloadHistory (EventType, ArtistId, DownloadId, SourceTitle, Date, Protocol, Data) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
updateCmd.AddParameter(downloadHistoryEventType);
|
||||
updateCmd.AddParameter(artistId);
|
||||
updateCmd.AddParameter(downloadId);
|
||||
updateCmd.AddParameter(sourceTitle);
|
||||
updateCmd.AddParameter(date);
|
||||
updateCmd.AddParameter(protocol);
|
||||
updateCmd.AddParameter(downloadHistoryData.ToJson());
|
||||
|
||||
updateCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Download.Clients;
|
||||
using NzbDrone.Core.Download.History;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.Download
|
||||
{
|
||||
public interface IDownloadSeedConfigProvider
|
||||
{
|
||||
TorrentSeedConfiguration GetSeedConfiguration(string infoHash);
|
||||
}
|
||||
|
||||
public class DownloadSeedConfigProvider : IDownloadSeedConfigProvider
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly ISeedConfigProvider _indexerSeedConfigProvider;
|
||||
private readonly IDownloadHistoryService _downloadHistoryService;
|
||||
|
||||
private class CachedSeedConfiguration
|
||||
{
|
||||
public int IndexerId { get; set; }
|
||||
public bool Discography { get; set; }
|
||||
}
|
||||
|
||||
private readonly ICached<CachedSeedConfiguration> _cacheDownloads;
|
||||
|
||||
public DownloadSeedConfigProvider(IDownloadHistoryService downloadHistoryService, ISeedConfigProvider indexerSeedConfigProvider, ICacheManager cacheManager, Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_indexerSeedConfigProvider = indexerSeedConfigProvider;
|
||||
_downloadHistoryService = downloadHistoryService;
|
||||
|
||||
_cacheDownloads = cacheManager.GetRollingCache<CachedSeedConfiguration>(GetType(), "indexerByHash", TimeSpan.FromHours(1));
|
||||
}
|
||||
|
||||
public TorrentSeedConfiguration GetSeedConfiguration(string infoHash)
|
||||
{
|
||||
if (infoHash.IsNullOrWhiteSpace())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
infoHash = infoHash.ToUpper();
|
||||
|
||||
var cachedConfig = _cacheDownloads.Get(infoHash, () => FetchIndexer(infoHash));
|
||||
|
||||
if (cachedConfig == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var seedConfig = _indexerSeedConfigProvider.GetSeedConfiguration(cachedConfig.IndexerId, cachedConfig.Discography);
|
||||
|
||||
return seedConfig;
|
||||
}
|
||||
|
||||
private CachedSeedConfiguration FetchIndexer(string infoHash)
|
||||
{
|
||||
var historyItem = _downloadHistoryService.GetLatestGrab(infoHash);
|
||||
|
||||
if (historyItem == null)
|
||||
{
|
||||
_logger.Debug("No download history item for infohash {0}, unable to provide seed configuration", infoHash);
|
||||
return null;
|
||||
}
|
||||
|
||||
ParsedAlbumInfo parsedAlbumInfo = null;
|
||||
if (historyItem.Release != null)
|
||||
{
|
||||
parsedAlbumInfo = Parser.Parser.ParseAlbumTitle(historyItem.Release.Title);
|
||||
}
|
||||
|
||||
if (parsedAlbumInfo == null)
|
||||
{
|
||||
_logger.Debug("No parsed title in download history item for infohash {0}, unable to provide seed configuration", infoHash);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CachedSeedConfiguration
|
||||
{
|
||||
IndexerId = historyItem.IndexerId,
|
||||
Discography = parsedAlbumInfo.Discography
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.Download.History
|
||||
{
|
||||
public class DownloadHistory : ModelBase
|
||||
{
|
||||
public DownloadHistoryEventType EventType { get; set; }
|
||||
public int ArtistId { get; set; }
|
||||
public string DownloadId { get; set; }
|
||||
public string SourceTitle { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public DownloadProtocol Protocol { get; set; }
|
||||
public int IndexerId { get; set; }
|
||||
public int DownloadClientId { get; set; }
|
||||
public ReleaseInfo Release { get; set; }
|
||||
public Dictionary<string, string> Data { get; set; }
|
||||
public DownloadHistory()
|
||||
{
|
||||
Data = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
public enum DownloadHistoryEventType
|
||||
{
|
||||
DownloadGrabbed = 1,
|
||||
DownloadImported = 2,
|
||||
DownloadFailed = 3,
|
||||
DownloadIgnored = 4,
|
||||
FileImported = 5,
|
||||
DownloadImportIncomplete = 6
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
|
||||
namespace NzbDrone.Core.Download.History
|
||||
{
|
||||
public interface IDownloadHistoryRepository : IBasicRepository<DownloadHistory>
|
||||
{
|
||||
List<DownloadHistory> FindByDownloadId(string downloadId);
|
||||
void DeleteByArtistIds(List<int> artistIds);
|
||||
}
|
||||
|
||||
public class DownloadHistoryRepository : BasicRepository<DownloadHistory>, IDownloadHistoryRepository
|
||||
{
|
||||
public DownloadHistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
||||
: base(database, eventAggregator)
|
||||
{
|
||||
}
|
||||
|
||||
public List<DownloadHistory> FindByDownloadId(string downloadId)
|
||||
{
|
||||
return Query(x => x.DownloadId == downloadId).OrderByDescending(h => h.Date).ToList();
|
||||
}
|
||||
|
||||
public void DeleteByArtistIds(List<int> artistIds)
|
||||
{
|
||||
Delete(r => artistIds.Contains(r.ArtistId));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,248 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.History;
|
||||
using NzbDrone.Core.MediaFiles.Events;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Music.Events;
|
||||
|
||||
namespace NzbDrone.Core.Download.History
|
||||
{
|
||||
public interface IDownloadHistoryService
|
||||
{
|
||||
bool DownloadAlreadyImported(string downloadId);
|
||||
DownloadHistory GetLatestDownloadHistoryItem(string downloadId);
|
||||
DownloadHistory GetLatestGrab(string downloadId);
|
||||
}
|
||||
|
||||
public class DownloadHistoryService : IDownloadHistoryService,
|
||||
IHandle<AlbumGrabbedEvent>,
|
||||
IHandle<TrackImportedEvent>,
|
||||
IHandle<AlbumImportIncompleteEvent>,
|
||||
IHandle<DownloadCompletedEvent>,
|
||||
IHandle<DownloadFailedEvent>,
|
||||
IHandle<DownloadIgnoredEvent>,
|
||||
IHandle<ArtistsDeletedEvent>
|
||||
{
|
||||
private readonly IDownloadHistoryRepository _repository;
|
||||
private readonly IHistoryService _historyService;
|
||||
|
||||
public DownloadHistoryService(IDownloadHistoryRepository repository, IHistoryService historyService)
|
||||
{
|
||||
_repository = repository;
|
||||
_historyService = historyService;
|
||||
}
|
||||
|
||||
public bool DownloadAlreadyImported(string downloadId)
|
||||
{
|
||||
var events = _repository.FindByDownloadId(downloadId);
|
||||
|
||||
// Events are ordered by date descending, if a grabbed event comes before an imported event then it was never imported
|
||||
// or grabbed again after importing and should be reprocessed.
|
||||
foreach (var e in events)
|
||||
{
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadGrabbed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadImported)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public DownloadHistory GetLatestDownloadHistoryItem(string downloadId)
|
||||
{
|
||||
var events = _repository.FindByDownloadId(downloadId);
|
||||
|
||||
// Events are ordered by date descending. We'll return the most recent expected event.
|
||||
foreach (var e in events)
|
||||
{
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadGrabbed)
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadImported)
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadFailed)
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
if (e.EventType == DownloadHistoryEventType.DownloadImportIncomplete)
|
||||
{
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public DownloadHistory GetLatestGrab(string downloadId)
|
||||
{
|
||||
return _repository.FindByDownloadId(downloadId)
|
||||
.FirstOrDefault(d => d.EventType == DownloadHistoryEventType.DownloadGrabbed);
|
||||
}
|
||||
|
||||
public void Handle(AlbumGrabbedEvent message)
|
||||
{
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.DownloadGrabbed,
|
||||
ArtistId = message.Album.Artist.Id,
|
||||
DownloadId = message.DownloadId,
|
||||
SourceTitle = message.Album.Release.Title,
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.Album.Release.DownloadProtocol,
|
||||
IndexerId = message.Album.Release.IndexerId,
|
||||
DownloadClientId = message.DownloadClientId,
|
||||
Release = message.Album.Release
|
||||
};
|
||||
|
||||
history.Data.Add("Indexer", message.Album.Release.Indexer);
|
||||
history.Data.Add("DownloadClient", message.DownloadClient);
|
||||
history.Data.Add("DownloadClientName", message.DownloadClientName);
|
||||
history.Data.Add("PreferredWordScore", message.Album.PreferredWordScore.ToString());
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(TrackImportedEvent message)
|
||||
{
|
||||
if (!message.NewDownload)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var downloadId = message.DownloadId;
|
||||
|
||||
// Try to find the downloadId if the user used manual import (from wanted: missing) or the
|
||||
// API to import and downloadId wasn't provided.
|
||||
if (downloadId.IsNullOrWhiteSpace())
|
||||
{
|
||||
downloadId = _historyService.FindDownloadId(message);
|
||||
}
|
||||
|
||||
if (downloadId.IsNullOrWhiteSpace())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.FileImported,
|
||||
ArtistId = message.TrackInfo.Artist.Id,
|
||||
DownloadId = downloadId,
|
||||
SourceTitle = message.TrackInfo.Path,
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.DownloadClientInfo.Protocol,
|
||||
DownloadClientId = message.DownloadClientInfo.Id
|
||||
};
|
||||
|
||||
history.Data.Add("DownloadClient", message.DownloadClientInfo.Type);
|
||||
history.Data.Add("DownloadClientName", message.DownloadClientInfo.Name);
|
||||
history.Data.Add("SourcePath", message.TrackInfo.Path);
|
||||
history.Data.Add("DestinationPath", message.ImportedTrack.Path);
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(AlbumImportIncompleteEvent message)
|
||||
{
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.DownloadImportIncomplete,
|
||||
ArtistId = message.TrackedDownload.RemoteAlbum?.Artist.Id ?? 0,
|
||||
DownloadId = message.TrackedDownload.DownloadItem.DownloadId,
|
||||
SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(),
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.TrackedDownload.Protocol,
|
||||
DownloadClientId = message.TrackedDownload.DownloadClient
|
||||
};
|
||||
|
||||
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
|
||||
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
|
||||
history.Data.Add("StatusMessages", message.TrackedDownload.StatusMessages.ToJson());
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(DownloadCompletedEvent message)
|
||||
{
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.DownloadImported,
|
||||
ArtistId = message.TrackedDownload.RemoteAlbum.Artist.Id,
|
||||
DownloadId = message.TrackedDownload.DownloadItem.DownloadId,
|
||||
SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(),
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.TrackedDownload.Protocol,
|
||||
DownloadClientId = message.TrackedDownload.DownloadClient
|
||||
};
|
||||
|
||||
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
|
||||
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(DownloadFailedEvent message)
|
||||
{
|
||||
// Don't track failed download for an unknown download
|
||||
if (message.TrackedDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.DownloadFailed,
|
||||
ArtistId = message.ArtistId,
|
||||
DownloadId = message.DownloadId,
|
||||
SourceTitle = message.SourceTitle,
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.TrackedDownload.Protocol,
|
||||
DownloadClientId = message.TrackedDownload.DownloadClient
|
||||
};
|
||||
|
||||
history.Data.Add("DownloadClient", message.TrackedDownload.DownloadItem.DownloadClientInfo.Type);
|
||||
history.Data.Add("DownloadClientName", message.TrackedDownload.DownloadItem.DownloadClientInfo.Name);
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(DownloadIgnoredEvent message)
|
||||
{
|
||||
var history = new DownloadHistory
|
||||
{
|
||||
EventType = DownloadHistoryEventType.DownloadIgnored,
|
||||
ArtistId = message.ArtistId,
|
||||
DownloadId = message.DownloadId,
|
||||
SourceTitle = message.SourceTitle,
|
||||
Date = DateTime.UtcNow,
|
||||
Protocol = message.DownloadClientInfo.Protocol,
|
||||
DownloadClientId = message.DownloadClientInfo.Id
|
||||
};
|
||||
|
||||
history.Data.Add("DownloadClient", message.DownloadClientInfo.Type);
|
||||
history.Data.Add("DownloadClientName", message.DownloadClientInfo.Name);
|
||||
|
||||
_repository.Insert(history);
|
||||
}
|
||||
|
||||
public void Handle(ArtistsDeletedEvent message)
|
||||
{
|
||||
_repository.DeleteByArtistIds(message.Artists.Select(a => a.Id).ToList());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Music;
|
||||
using NzbDrone.Core.Qualities;
|
||||
|
||||
namespace NzbDrone.Core.History
|
||||
{
|
||||
public interface IHistoryRepository : IBasicRepository<EntityHistory>
|
||||
{
|
||||
EntityHistory MostRecentForAlbum(int albumId);
|
||||
EntityHistory MostRecentForDownloadId(string downloadId);
|
||||
List<EntityHistory> FindByDownloadId(string downloadId);
|
||||
List<EntityHistory> GetByArtist(int artistId, EntityHistoryEventType? eventType);
|
||||
List<EntityHistory> GetByAlbum(int albumId, EntityHistoryEventType? eventType);
|
||||
List<EntityHistory> FindDownloadHistory(int idArtistId, QualityModel quality);
|
||||
void DeleteForArtists(List<int> artistIds);
|
||||
List<EntityHistory> Since(DateTime date, EntityHistoryEventType? eventType);
|
||||
}
|
||||
|
||||
public class EntityHistoryRepository : BasicRepository<EntityHistory>, IHistoryRepository
|
||||
{
|
||||
public EntityHistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
||||
: base(database, eventAggregator)
|
||||
{
|
||||
}
|
||||
|
||||
public EntityHistory MostRecentForAlbum(int albumId)
|
||||
{
|
||||
return Query(h => h.AlbumId == albumId)
|
||||
.OrderByDescending(h => h.Date)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public EntityHistory MostRecentForDownloadId(string downloadId)
|
||||
{
|
||||
return Query(h => h.DownloadId == downloadId)
|
||||
.OrderByDescending(h => h.Date)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<EntityHistory> FindByDownloadId(string downloadId)
|
||||
{
|
||||
return _database.QueryJoined<EntityHistory, Artist, Album>(
|
||||
Builder()
|
||||
.Join<EntityHistory, Artist>((h, a) => h.ArtistId == a.Id)
|
||||
.Join<EntityHistory, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.Where<EntityHistory>(h => h.DownloadId == downloadId),
|
||||
(history, artist, album) =>
|
||||
{
|
||||
history.Artist = artist;
|
||||
history.Album = album;
|
||||
return history;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public List<EntityHistory> GetByArtist(int artistId, EntityHistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder().Where<EntityHistory>(h => h.ArtistId == artistId);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<EntityHistory>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return Query(builder).OrderByDescending(h => h.Date).ToList();
|
||||
}
|
||||
|
||||
public List<EntityHistory> GetByAlbum(int albumId, EntityHistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder()
|
||||
.Join<EntityHistory, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.Where<EntityHistory>(h => h.AlbumId == albumId);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<EntityHistory>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return _database.QueryJoined<EntityHistory, Album>(
|
||||
builder,
|
||||
(history, album) =>
|
||||
{
|
||||
history.Album = album;
|
||||
return history;
|
||||
}).OrderByDescending(h => h.Date).ToList();
|
||||
}
|
||||
|
||||
public List<EntityHistory> FindDownloadHistory(int idArtistId, QualityModel quality)
|
||||
{
|
||||
var allowed = new[] { EntityHistoryEventType.Grabbed, EntityHistoryEventType.DownloadFailed, EntityHistoryEventType.TrackFileImported };
|
||||
|
||||
return Query(h => h.ArtistId == idArtistId &&
|
||||
h.Quality == quality &&
|
||||
allowed.Contains(h.EventType));
|
||||
}
|
||||
|
||||
public void DeleteForArtists(List<int> artistIds)
|
||||
{
|
||||
Delete(c => artistIds.Contains(c.ArtistId));
|
||||
}
|
||||
|
||||
protected override SqlBuilder PagedBuilder() => new SqlBuilder()
|
||||
.Join<EntityHistory, Artist>((h, a) => h.ArtistId == a.Id)
|
||||
.Join<EntityHistory, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.LeftJoin<EntityHistory, Track>((h, t) => h.TrackId == t.Id);
|
||||
protected override IEnumerable<EntityHistory> PagedQuery(SqlBuilder builder) =>
|
||||
_database.QueryJoined<EntityHistory, Artist, Album, Track>(builder, (history, artist, album, track) =>
|
||||
{
|
||||
history.Artist = artist;
|
||||
history.Album = album;
|
||||
history.Track = track;
|
||||
return history;
|
||||
});
|
||||
|
||||
public List<EntityHistory> Since(DateTime date, EntityHistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder().Where<EntityHistory>(x => x.Date >= date);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<EntityHistory>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return Query(builder).OrderBy(h => h.Date).ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Music;
|
||||
using NzbDrone.Core.Qualities;
|
||||
|
||||
namespace NzbDrone.Core.History
|
||||
{
|
||||
public interface IHistoryRepository : IBasicRepository<History>
|
||||
{
|
||||
History MostRecentForAlbum(int albumId);
|
||||
History MostRecentForDownloadId(string downloadId);
|
||||
List<History> FindByDownloadId(string downloadId);
|
||||
List<History> GetByArtist(int artistId, HistoryEventType? eventType);
|
||||
List<History> GetByAlbum(int albumId, HistoryEventType? eventType);
|
||||
List<History> FindDownloadHistory(int idArtistId, QualityModel quality);
|
||||
void DeleteForArtists(List<int> artistIds);
|
||||
List<History> Since(DateTime date, HistoryEventType? eventType);
|
||||
}
|
||||
|
||||
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
|
||||
{
|
||||
public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregator)
|
||||
: base(database, eventAggregator)
|
||||
{
|
||||
}
|
||||
|
||||
public History MostRecentForAlbum(int albumId)
|
||||
{
|
||||
return Query(h => h.AlbumId == albumId)
|
||||
.OrderByDescending(h => h.Date)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public History MostRecentForDownloadId(string downloadId)
|
||||
{
|
||||
return Query(h => h.DownloadId == downloadId)
|
||||
.OrderByDescending(h => h.Date)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<History> FindByDownloadId(string downloadId)
|
||||
{
|
||||
return _database.QueryJoined<History, Artist, Album>(
|
||||
Builder()
|
||||
.Join<History, Artist>((h, a) => h.ArtistId == a.Id)
|
||||
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.Where<History>(h => h.DownloadId == downloadId),
|
||||
(history, artist, album) =>
|
||||
{
|
||||
history.Artist = artist;
|
||||
history.Album = album;
|
||||
return history;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public List<History> GetByArtist(int artistId, HistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder().Where<History>(h => h.ArtistId == artistId);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<History>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return Query(builder).OrderByDescending(h => h.Date).ToList();
|
||||
}
|
||||
|
||||
public List<History> GetByAlbum(int albumId, HistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder()
|
||||
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.Where<History>(h => h.AlbumId == albumId);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<History>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return _database.QueryJoined<History, Album>(
|
||||
builder,
|
||||
(history, album) =>
|
||||
{
|
||||
history.Album = album;
|
||||
return history;
|
||||
}).OrderByDescending(h => h.Date).ToList();
|
||||
}
|
||||
|
||||
public List<History> FindDownloadHistory(int idArtistId, QualityModel quality)
|
||||
{
|
||||
var allowed = new[] { HistoryEventType.Grabbed, HistoryEventType.DownloadFailed, HistoryEventType.TrackFileImported };
|
||||
|
||||
return Query(h => h.ArtistId == idArtistId &&
|
||||
h.Quality == quality &&
|
||||
allowed.Contains(h.EventType));
|
||||
}
|
||||
|
||||
public void DeleteForArtists(List<int> artistIds)
|
||||
{
|
||||
Delete(c => artistIds.Contains(c.ArtistId));
|
||||
}
|
||||
|
||||
protected override SqlBuilder PagedBuilder() => new SqlBuilder()
|
||||
.Join<History, Artist>((h, a) => h.ArtistId == a.Id)
|
||||
.Join<History, Album>((h, a) => h.AlbumId == a.Id)
|
||||
.LeftJoin<History, Track>((h, t) => h.TrackId == t.Id);
|
||||
protected override IEnumerable<History> PagedQuery(SqlBuilder builder) =>
|
||||
_database.QueryJoined<History, Artist, Album, Track>(builder, (history, artist, album, track) =>
|
||||
{
|
||||
history.Artist = artist;
|
||||
history.Album = album;
|
||||
history.Track = track;
|
||||
return history;
|
||||
});
|
||||
|
||||
public List<History> Since(DateTime date, HistoryEventType? eventType)
|
||||
{
|
||||
var builder = Builder().Where<History>(x => x.Date >= date);
|
||||
|
||||
if (eventType.HasValue)
|
||||
{
|
||||
builder.Where<History>(h => h.EventType == eventType);
|
||||
}
|
||||
|
||||
return Query(builder).OrderBy(h => h.Date).ToList();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue