Beta v20180202.1

This commit is contained in:
Steven Hildreth 2019-02-02 18:24:34 -06:00
parent 1989e67a28
commit 69c2afe874
3 changed files with 168 additions and 60 deletions

View file

@ -50,6 +50,65 @@ namespace Roadie.Library.Tests
Console.WriteLine($"Log Level [{ e.Level }] Log Message [{ e.Message }] ");
}
[Theory]
[InlineData("1")]
[InlineData("1/2")]
[InlineData("3/9")]
[InlineData(@"2\2")]
[InlineData("2:2")]
[InlineData("2,2")]
[InlineData("2|2")]
[InlineData("99/100")]
[InlineData("33")]
[InlineData("A")]
[InlineData("B")]
public void ParseDiscNumberGood(string discNumber)
{
var dn = ID3TagsHelper.ParseDiscNumber(discNumber);
Assert.NotNull(dn);
Assert.True(dn > 0);
}
[Theory]
[InlineData("")]
[InlineData(" /2")]
[InlineData(null)]
[InlineData("BATMAN")]
[InlineData(".")]
[InlineData("#")]
public void ParseDiscNumberBad(string discNumber)
{
var dn = ID3TagsHelper.ParseDiscNumber(discNumber);
Assert.Null(dn);
}
[Fact]
public void ReadID3TagsMultipleMediasWithMax()
{
var file = new FileInfo(@"C:\roadie_dev_root\library\Dream Theater\[2016] The Astonishing\01 2285 Entr acte.mp3");
if (file.Exists)
{
var tagLib = this.TagsHelper.MetaDataForFile(file.FullName);
Assert.True(tagLib.IsSuccess);
var metaData = tagLib.Data;
Assert.NotNull(metaData.Artist);
Assert.NotNull(metaData.Release);
Assert.NotNull(metaData.Title);
Assert.True(metaData.Year > 0);
Assert.Equal(2, metaData.Disk);
Assert.NotNull(metaData.TrackNumber);
Assert.True(metaData.TotalSeconds > 0);
Assert.True(metaData.ValidWeight > 30);
Assert.True(metaData.IsValid);
}
else
{
Console.WriteLine($"skipping { file}");
Assert.True(true);
}
}
[Fact]
public void ReadID3TagsFromFileWithTrackArtists()
{

View file

@ -116,7 +116,7 @@ namespace Roadie.Library.MetaData.ID3Tags
result.AudioBitrate = (int?)audioFile.Bitrate;
result.AudioChannels = audioFile.Channels;
result.AudioSampleRate = (int)audioFile.Bitrate;
result.Disk = SafeParser.ToNumber<int?>(id3v2.DiscNumber);
result.Disk = ID3TagsHelper.ParseDiscNumber(id3v2.DiscNumber);
result.Images = id3v2.PictureList?.Select(x => new AudioMetaDataImage
{
Data = x.PictureData,
@ -168,5 +168,40 @@ namespace Roadie.Library.MetaData.ID3Tags
Data = result
};
}
public static int? ParseDiscNumber(string input)
{
var discNumber = SafeParser.ToNumber<int?>(input);
if(!discNumber.HasValue && !string.IsNullOrEmpty(input))
{
input = input.ToUpper().Replace("A", "1");
input = input.ToUpper().Replace("B", "2");
input = input.ToUpper().Replace("C", "3");
input = input.ToUpper().Replace("D", "4");
input = input.ToUpper().Replace("E", "5");
discNumber = SafeParser.ToNumber<int?>(input);
if (!discNumber.HasValue && input.Contains("/"))
{
discNumber = SafeParser.ToNumber<int?>(input.Split("/")[0]);
}
if (!discNumber.HasValue && input.Contains("\\"))
{
discNumber = SafeParser.ToNumber<int?>(input.Split("\\")[0]);
}
if (!discNumber.HasValue && input.Contains(":"))
{
discNumber = SafeParser.ToNumber<int?>(input.Split(":")[0]);
}
if (!discNumber.HasValue && input.Contains(","))
{
discNumber = SafeParser.ToNumber<int?>(input.Split(",")[0]);
}
if (!discNumber.HasValue && input.Contains("|"))
{
discNumber = SafeParser.ToNumber<int?>(input.Split("|")[0]);
}
}
return discNumber;
}
}
}

View file

@ -856,38 +856,45 @@ namespace Roadie.Api.Services
/// </summary>
protected async Task UpdateArtistRank(int artistId, bool updateReleaseRanks = false)
{
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == artistId);
if (artist != null)
try
{
if(updateReleaseRanks)
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == artistId);
if (artist != null)
{
var artistReleaseIds = this.DbContext.Releases.Where(x => x.ArtistId == artistId).Select(x => x.Id).ToArray();
foreach(var artistReleaseId in artistReleaseIds)
if (updateReleaseRanks)
{
await this.UpdateReleaseRank(artistReleaseId, false);
var artistReleaseIds = this.DbContext.Releases.Where(x => x.ArtistId == artistId).Select(x => x.Id).ToArray();
foreach (var artistReleaseId in artistReleaseIds)
{
await this.UpdateReleaseRank(artistReleaseId, false);
}
}
var artistTrackAverage = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
where t.ArtistId == artist.Id
select (double?)ut.Rating).ToArray().Average(x => x) ?? 0;
var artistReleaseRatingRating = (from r in this.DbContext.Releases
join ur in this.DbContext.UserReleases on r.Id equals ur.ReleaseId
where r.ArtistId == artist.Id
select (double?)ur.Rating).ToArray().Average(x => x) ?? 0;
var artistReleaseRankSum = (from r in this.DbContext.Releases
where r.ArtistId == artist.Id
select r.Rank).ToArray().Sum(x => x) ?? 0;
artist.Rank = SafeParser.ToNumber<decimal>(artistTrackAverage + artistReleaseRatingRating) + artistReleaseRankSum + artist.Rating;
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(artist.CacheRegion);
this.Logger.LogInformation("UpdatedArtistRank For Artist `{0}`", artist);
}
var artistTrackAverage = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
where t.ArtistId == artist.Id
select (double?)ut.Rating).ToArray().Average(x => x) ?? 0;
var artistReleaseRatingRating = (from r in this.DbContext.Releases
join ur in this.DbContext.UserReleases on r.Id equals ur.ReleaseId
where r.ArtistId == artist.Id
select (double?)ur.Rating).ToArray().Average(x => x) ?? 0;
var artistReleaseRankSum = (from r in this.DbContext.Releases
where r.ArtistId == artist.Id
select r.Rank).ToArray().Sum(x => x) ?? 0;
artist.Rank = SafeParser.ToNumber<decimal>(artistTrackAverage + artistReleaseRatingRating) + artistReleaseRankSum + artist.Rating;
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(artist.CacheRegion);
this.Logger.LogInformation("UpdatedArtistRank For Artist `{0}`", artist);
}
catch (Exception ex)
{
this.Logger.LogError(ex, "Error in UpdateArtistRank ArtistId [{0}], UpdateReleaseRanks [{1}]", artistId, updateReleaseRanks);
}
}
@ -897,43 +904,50 @@ namespace Roadie.Api.Services
/// </summary>
protected async Task UpdateReleaseRank(int releaseId, bool updateArtistRank = true)
{
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == releaseId);
if (release != null)
try
{
var releaseTrackAverage = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
where rm.ReleaseId == releaseId
select (double?)ut.Rating).ToArray().Average(x => x);
var releaseUserRatingRank = release.Rating > 0 ? (decimal?)release.Rating / (decimal?)release.TrackCount : 0;
var collectionsWithRelease = (from c in this.DbContext.Collections
join cr in this.DbContext.CollectionReleases on c.Id equals cr.CollectionId
where c.CollectionType != Library.Enums.CollectionType.Chart
where cr.ReleaseId == release.Id
select new
{
c.CollectionCount,
cr.ListNumber
});
decimal releaseCollectionRank = 0;
foreach (var collectionWithRelease in collectionsWithRelease)
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == releaseId);
if (release != null)
{
var rank = (decimal)((collectionWithRelease.CollectionCount * .01) - ((collectionWithRelease.ListNumber - 1) * .01));
releaseCollectionRank += rank;
}
release.Rank = SafeParser.ToNumber<decimal>(releaseTrackAverage) + releaseUserRatingRank + releaseCollectionRank;
var releaseTrackAverage = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
where rm.ReleaseId == releaseId
select (double?)ut.Rating).ToArray().Average(x => x);
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(release.CacheRegion);
this.Logger.LogInformation("UpdateReleaseRank For Release `{0}`", release);
if (updateArtistRank)
{
await this.UpdateArtistsRankForRelease(release);
var releaseUserRatingRank = release.Rating > 0 ? (decimal?)release.Rating / (decimal?)release.TrackCount : 0;
var collectionsWithRelease = (from c in this.DbContext.Collections
join cr in this.DbContext.CollectionReleases on c.Id equals cr.CollectionId
where c.CollectionType != Library.Enums.CollectionType.Chart
where cr.ReleaseId == release.Id
select new
{
c.CollectionCount,
cr.ListNumber
});
decimal releaseCollectionRank = 0;
foreach (var collectionWithRelease in collectionsWithRelease)
{
var rank = (decimal)((collectionWithRelease.CollectionCount * .01) - ((collectionWithRelease.ListNumber - 1) * .01));
releaseCollectionRank += rank;
}
release.Rank = SafeParser.ToNumber<decimal>(releaseTrackAverage) + releaseUserRatingRank + releaseCollectionRank;
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(release.CacheRegion);
this.Logger.LogInformation("UpdateReleaseRank For Release `{0}`", release);
if (updateArtistRank)
{
await this.UpdateArtistsRankForRelease(release);
}
}
}
catch (Exception ex)
{
this.Logger.LogError(ex, "Error UpdateReleaseRank RelaseId [{0}], UpdateArtistRank [{1}]", releaseId, updateArtistRank);
}
}
protected Image MakeNewImage(string type)