New: Fetch all albums from metadata and filter client-side (#803)

* Retrieve all albums from metadata and filter client-side

Should significantly increase the proportion of artist requests hitting
cloudflare cache

* Add tests for album filtering
pull/6/head
ta264 5 years ago committed by GitHub
parent 801ece337c
commit d381bab9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ using NzbDrone.Core.Music;
using NzbDrone.Test.Common.Categories; using NzbDrone.Test.Common.Categories;
using Moq; using Moq;
using NzbDrone.Core.Profiles.Metadata; using NzbDrone.Core.Profiles.Metadata;
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
namespace NzbDrone.Core.Test.MetadataSource.SkyHook namespace NzbDrone.Core.Test.MetadataSource.SkyHook
{ {
@ -17,12 +18,14 @@ namespace NzbDrone.Core.Test.MetadataSource.SkyHook
[IntegrationTest] [IntegrationTest]
public class SkyHookProxyFixture : CoreTest<SkyHookProxy> public class SkyHookProxyFixture : CoreTest<SkyHookProxy>
{ {
private MetadataProfile _metadataProfile;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
UseRealHttp(); UseRealHttp();
var _metadataProfile = new MetadataProfile _metadataProfile = new MetadataProfile
{ {
PrimaryAlbumTypes = new List<ProfilePrimaryAlbumTypeItem> PrimaryAlbumTypes = new List<ProfilePrimaryAlbumTypeItem>
{ {
@ -30,7 +33,6 @@ namespace NzbDrone.Core.Test.MetadataSource.SkyHook
{ {
PrimaryAlbumType = PrimaryAlbumType.Album, PrimaryAlbumType = PrimaryAlbumType.Album,
Allowed = true Allowed = true
} }
}, },
SecondaryAlbumTypes = new List<ProfileSecondaryAlbumTypeItem> SecondaryAlbumTypes = new List<ProfileSecondaryAlbumTypeItem>
@ -60,6 +62,30 @@ namespace NzbDrone.Core.Test.MetadataSource.SkyHook
.Returns(true); .Returns(true);
} }
public List<AlbumResource> GivenExampleAlbums()
{
var result = new List<AlbumResource>();
foreach (var primaryType in PrimaryAlbumType.All)
{
foreach (var secondaryType in SecondaryAlbumType.All)
{
var secondaryTypes = secondaryType.Name == "Studio" ? new List<string>() : new List<string> { secondaryType.Name };
foreach (var releaseStatus in ReleaseStatus.All)
{
var releaseStatuses = new List<string> { releaseStatus.Name };
result.Add(new AlbumResource {
Type = primaryType.Name,
SecondaryTypes = secondaryTypes,
ReleaseStatuses = releaseStatuses
});
}
}
}
return result;
}
[TestCase("f59c5520-5f46-4d2c-b2c4-822eabf53419", "Linkin Park")] [TestCase("f59c5520-5f46-4d2c-b2c4-822eabf53419", "Linkin Park")]
[TestCase("66c662b6-6e2f-4930-8610-912e24c63ed1", "AC/DC")] [TestCase("66c662b6-6e2f-4930-8610-912e24c63ed1", "AC/DC")]
public void should_be_able_to_get_artist_detail(string mbId, string name) public void should_be_able_to_get_artist_detail(string mbId, string name)
@ -67,11 +93,65 @@ namespace NzbDrone.Core.Test.MetadataSource.SkyHook
var details = Subject.GetArtistInfo(mbId, 1); var details = Subject.GetArtistInfo(mbId, 1);
ValidateArtist(details); ValidateArtist(details);
ValidateAlbums(details.Albums.Value); ValidateAlbums(details.Albums.Value, true);
details.Name.Should().Be(name); details.Name.Should().Be(name);
} }
[TestCaseSource(typeof(PrimaryAlbumType), "All")]
public void should_filter_albums_by_primary_release_type(PrimaryAlbumType type)
{
_metadataProfile.PrimaryAlbumTypes = new List<ProfilePrimaryAlbumTypeItem> {
new ProfilePrimaryAlbumTypeItem
{
PrimaryAlbumType = type,
Allowed = true
}
};
var albums = GivenExampleAlbums();
Subject.FilterAlbums(albums, 1).Select(x => x.Type).Distinct()
.Should().BeEquivalentTo(new List<string> { type.Name });
}
[TestCaseSource(typeof(SecondaryAlbumType), "All")]
public void should_filter_albums_by_secondary_release_type(SecondaryAlbumType type)
{
_metadataProfile.SecondaryAlbumTypes = new List<ProfileSecondaryAlbumTypeItem> {
new ProfileSecondaryAlbumTypeItem
{
SecondaryAlbumType = type,
Allowed = true
}
};
var albums = GivenExampleAlbums();
var filtered = Subject.FilterAlbums(albums, 1);
TestLogger.Debug(filtered.Count());
filtered.SelectMany(x => x.SecondaryTypes.Select(SkyHookProxy.MapSecondaryTypes))
.Select(x => x.Name)
.Distinct()
.Should().BeEquivalentTo(type.Name == "Studio" ? new List<string>() : new List<string> { type.Name });
}
[TestCaseSource(typeof(ReleaseStatus), "All")]
public void should_filter_albums_by_release_status(ReleaseStatus type)
{
_metadataProfile.ReleaseStatuses = new List<ProfileReleaseStatusItem> {
new ProfileReleaseStatusItem
{
ReleaseStatus = type,
Allowed = true
}
};
var albums = GivenExampleAlbums();
Subject.FilterAlbums(albums, 1).SelectMany(x => x.ReleaseStatuses).Distinct()
.Should().BeEquivalentTo(new List<string> { type.Name });
}
[TestCase("12fa3845-7c62-36e5-a8da-8be137155a72", "Hysteria")] [TestCase("12fa3845-7c62-36e5-a8da-8be137155a72", "Hysteria")]
public void should_be_able_to_get_album_detail(string mbId, string name) public void should_be_able_to_get_album_detail(string mbId, string name)
{ {
@ -130,15 +210,23 @@ namespace NzbDrone.Core.Test.MetadataSource.SkyHook
artist.ForeignArtistId.Should().NotBeNullOrWhiteSpace(); artist.ForeignArtistId.Should().NotBeNullOrWhiteSpace();
} }
private void ValidateAlbums(List<Album> albums) private void ValidateAlbums(List<Album> albums, bool idOnly = false)
{ {
albums.Should().NotBeEmpty(); albums.Should().NotBeEmpty();
foreach (var album in albums) foreach (var album in albums)
{
album.ForeignAlbumId.Should().NotBeNullOrWhiteSpace();
if (!idOnly)
{ {
ValidateAlbum(album); ValidateAlbum(album);
}
}
//if atleast one album has title it means parse it working. //if atleast one album has title it means parse it working.
if (!idOnly)
{
albums.Should().Contain(c => !string.IsNullOrWhiteSpace(c.Title)); albums.Should().Contain(c => !string.IsNullOrWhiteSpace(c.Title));
} }
} }

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.MetadataSource.SkyHook.Resource namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
{ {
@ -20,5 +18,6 @@ namespace NzbDrone.Core.MetadataSource.SkyHook.Resource
public List<string> SecondaryTypes { get; set; } public List<string> SecondaryTypes { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Type { get; set; } public string Type { get; set; }
public List<string> ReleaseStatuses { get; set; }
} }
} }

@ -50,17 +50,8 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
_logger.Debug("Getting Artist with LidarrAPI.MetadataID of {0}", foreignArtistId); _logger.Debug("Getting Artist with LidarrAPI.MetadataID of {0}", foreignArtistId);
var metadataProfile = _metadataProfileService.Exists(metadataProfileId) ? _metadataProfileService.Get(metadataProfileId) : _metadataProfileService.All().First();
var primaryTypes = metadataProfile.PrimaryAlbumTypes.Where(s => s.Allowed).Select(s => s.PrimaryAlbumType.Name);
var secondaryTypes = metadataProfile.SecondaryAlbumTypes.Where(s => s.Allowed).Select(s => s.SecondaryAlbumType.Name);
var releaseStatuses = metadataProfile.ReleaseStatuses.Where(s => s.Allowed).Select(s => s.ReleaseStatus.Name);
var httpRequest = _requestBuilder.GetRequestBuilder().Create() var httpRequest = _requestBuilder.GetRequestBuilder().Create()
.SetSegment("route", "artist/" + foreignArtistId) .SetSegment("route", "artist/" + foreignArtistId)
.AddQueryParam("primTypes", string.Join("|", primaryTypes))
.AddQueryParam("secTypes", string.Join("|", secondaryTypes))
.AddQueryParam("releaseStatuses", string.Join("|", releaseStatuses))
.Build(); .Build();
httpRequest.AllowAutoRedirect = true; httpRequest.AllowAutoRedirect = true;
@ -89,11 +80,30 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
artist.Metadata = MapArtistMetadata(httpResponse.Resource); artist.Metadata = MapArtistMetadata(httpResponse.Resource);
artist.CleanName = Parser.Parser.CleanArtistName(artist.Metadata.Value.Name); artist.CleanName = Parser.Parser.CleanArtistName(artist.Metadata.Value.Name);
artist.SortName = Parser.Parser.NormalizeTitle(artist.Metadata.Value.Name); artist.SortName = Parser.Parser.NormalizeTitle(artist.Metadata.Value.Name);
artist.Albums = httpResponse.Resource.Albums.Select(x => MapAlbum(x, null)).ToList();
artist.Albums = FilterAlbums(httpResponse.Resource.Albums, metadataProfileId)
.Select(x => new Album {
ForeignAlbumId = x.Id
})
.ToList();
return artist; return artist;
} }
public IEnumerable<AlbumResource> FilterAlbums(IEnumerable<AlbumResource> albums, int metadataProfileId)
{
var metadataProfile = _metadataProfileService.Exists(metadataProfileId) ? _metadataProfileService.Get(metadataProfileId) : _metadataProfileService.All().First();
var primaryTypes = new HashSet<string>(metadataProfile.PrimaryAlbumTypes.Where(s => s.Allowed).Select(s => s.PrimaryAlbumType.Name));
var secondaryTypes = new HashSet<string>(metadataProfile.SecondaryAlbumTypes.Where(s => s.Allowed).Select(s => s.SecondaryAlbumType.Name));
var releaseStatuses = new HashSet<string>(metadataProfile.ReleaseStatuses.Where(s => s.Allowed).Select(s => s.ReleaseStatus.Name));
return albums.Where(album => primaryTypes.Contains(album.Type) &&
(!album.SecondaryTypes.Any() && secondaryTypes.Contains("Studio") ||
album.SecondaryTypes.Any(x => secondaryTypes.Contains(x))) &&
album.ReleaseStatuses.Any(x => releaseStatuses.Contains(x)));
}
public Tuple<string, Album, List<ArtistMetadata>> GetAlbumInfo(string foreignAlbumId) public Tuple<string, Album, List<ArtistMetadata>> GetAlbumInfo(string foreignAlbumId)
{ {
_logger.Debug("Getting Album with LidarrAPI.MetadataID of {0}", foreignAlbumId); _logger.Debug("Getting Album with LidarrAPI.MetadataID of {0}", foreignAlbumId);
@ -470,7 +480,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook
} }
} }
private static SecondaryAlbumType MapSecondaryTypes(string albumType) public static SecondaryAlbumType MapSecondaryTypes(string albumType)
{ {
switch (albumType.ToLowerInvariant()) switch (albumType.ToLowerInvariant())
{ {

Loading…
Cancel
Save