More rework to resolve EF evaluated locally issues.

This commit is contained in:
Steven Hildreth 2019-08-03 23:36:36 -05:00
parent 891f57d89a
commit e0433d7e47
6 changed files with 85 additions and 77 deletions

View file

@ -933,11 +933,13 @@ namespace Roadie.Api.Services
tsw.Stop();
timings.Add("adaptArtist", tsw.ElapsedMilliseconds);
result.Thumbnail = MakeArtistThumbnailImage(id);
result.MediumThumbnail = MakeThumbnailImage(id, "artist", Configuration.MediumImageSize.Width,
Configuration.MediumImageSize.Height);
result.MediumThumbnail = MakeThumbnailImage(id, "artist", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
tsw.Restart();
result.Genres = artist.Genres.Select(x => new DataToken
{ Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() });
{
Text = x.Genre.Name,
Value = x.Genre.RoadieId.ToString()
});
tsw.Stop();
timings.Add("genres", tsw.ElapsedMilliseconds);
@ -946,8 +948,12 @@ namespace Roadie.Api.Services
if (includes.Contains("releases"))
{
var dtoReleases = new List<ReleaseList>();
foreach (var release in DbContext.Releases.Include("Medias").Include("Medias.Tracks")
.Include("Medias.Tracks").Where(x => x.ArtistId == artist.Id).ToArray())
foreach (var release in DbContext.Releases
.Include("Medias")
.Include("Medias.Tracks")
.Include("Medias.Tracks")
.Where(x => x.ArtistId == artist.Id)
.ToArray())
{
var releaseList = release.Adapt<ReleaseList>();
releaseList.Thumbnail = MakeReleaseThumbnailImage(release.RoadieId);
@ -957,8 +963,10 @@ namespace Roadie.Api.Services
{
var dtoMedia = releasemedia.Adapt<ReleaseMediaList>();
var tracks = new List<TrackList>();
foreach (var t in DbContext.Tracks.Where(x => x.ReleaseMediaId == releasemedia.Id)
.OrderBy(x => x.TrackNumber).ToArray())
foreach (var t in DbContext.Tracks
.Where(x => x.ReleaseMediaId == releasemedia.Id)
.OrderBy(x => x.TrackNumber)
.ToArray())
{
var track = t.Adapt<TrackList>();
ArtistList trackArtist = null;
@ -1210,7 +1218,10 @@ namespace Roadie.Api.Services
FilterToArtistId = artist.RoadieId
};
var r = await PlaylistService.List(pg);
if (r.IsSuccess) result.PlaylistsWithArtistReleases = r.Rows.ToArray();
if (r.IsSuccess)
{
result.PlaylistsWithArtistReleases = r.Rows.ToArray();
}
tsw.Stop();
timings.Add("playlists", tsw.ElapsedMilliseconds);
}
@ -1218,21 +1229,28 @@ namespace Roadie.Api.Services
if (includes.Contains("contributions"))
{
tsw.Restart();
result.ArtistContributionReleases = (from t in DbContext.Tracks
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases.Include(x => x.Artist) on rm.ReleaseId equals r.Id
where t.ArtistId == artist.Id
group r by r.Id
into rr
select rr)
.ToArray()
.Select(rr => rr.First())
.Select(r => ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl,
MakeArtistThumbnailImage(r.Artist.RoadieId), MakeReleaseThumbnailImage(r.RoadieId)))
.ToArray().OrderBy(x => x.Release.Text).ToArray();
result.ArtistContributionReleases = result.ArtistContributionReleases.Any()
? result.ArtistContributionReleases
: null;
var artistContributingTracks = (from t in DbContext.Tracks
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases on rm.ReleaseId equals r.Id
where t.ArtistId == artist.Id
select r.Id)
.ToArray()
.Distinct();
if (artistContributingTracks?.Any() ?? false)
{
result.ArtistContributionReleases = (from r in DbContext.Releases.Include(x => x.Artist)
where artistContributingTracks.Contains(r.Id)
select r)
.OrderBy(x => x.Title)
.ToArray()
.Select(r => ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl, MakeArtistThumbnailImage(r.Artist.RoadieId), MakeReleaseThumbnailImage(r.RoadieId)));
result.ArtistContributionReleases = result.ArtistContributionReleases.Any()
? result.ArtistContributionReleases
: null;
}
tsw.Stop();
timings.Add("contributions", tsw.ElapsedMilliseconds);
}
@ -1246,9 +1264,12 @@ namespace Roadie.Api.Services
where r.ArtistId == artist.Id
orderby l.SortName
select LabelList.FromDataLabel(l, MakeLabelThumbnailImage(l.RoadieId)))
.ToArray()
.GroupBy(x => x.Label.Value).Select(x => x.First()).OrderBy(x => x.SortName)
.ThenBy(x => x.Label.Text).ToArray();
.ToArray()
.GroupBy(x => x.Label.Value)
.Select(x => x.First())
.OrderBy(x => x.SortName)
.ThenBy(x => x.Label.Text)
.ToArray();
result.ArtistLabels = result.ArtistLabels.Any() ? result.ArtistLabels : null;
tsw.Stop();
timings.Add("labels", tsw.ElapsedMilliseconds);
@ -1257,9 +1278,7 @@ namespace Roadie.Api.Services
sw.Stop();
timings.Add("operation", sw.ElapsedMilliseconds);
Logger.LogDebug("ArtistByIdAction Timings: id [{0}], includes [{1}], timings [{3}]", id, includes,
JsonConvert.SerializeObject(timings));
Logger.LogDebug("ArtistByIdAction Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>
{
Data = result,

View file

@ -82,19 +82,19 @@ namespace Roadie.Api.Services
var sw = Stopwatch.StartNew();
sw.Start();
var cacheKey = string.Format("urn:collection_by_id_operation:{0}:{1}", id,
includes == null ? "0" : string.Join("|", includes));
var result = await CacheManager.GetAsync(cacheKey,
async () => { return await CollectionByIdAction(id, includes); }, data.Artist.CacheRegionUrn(id));
var cacheKey = string.Format("urn:collection_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
var result = await CacheManager.GetAsync(cacheKey, async () =>
{
return await CollectionByIdAction(id, includes);
}, data.Artist.CacheRegionUrn(id));
sw.Stop();
if (result?.Data != null && roadieUser != null)
{
var userBookmarkResult =
await BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Collection);
var userBookmarkResult = await BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Collection);
if (userBookmarkResult.IsSuccess)
result.Data.UserBookmarked =
userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Text == result.Data.Id.ToString()) !=
null;
{
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Text == result.Data.Id.ToString()) != null;
}
if (result.Data.Comments.Any())
{
var commentIds = result.Data.Comments.Select(x => x.DatabaseId).ToArray();
@ -337,9 +337,9 @@ namespace Roadie.Api.Services
var collection = GetCollection(id);
if (collection == null)
return Task.FromResult(new OperationResult<Collection>(true,
string.Format("Collection Not Found [{0}]", id)));
{
return Task.FromResult(new OperationResult<Collection>(true, string.Format("Collection Not Found [{0}]", id)));
}
var result = collection.Adapt<Collection>();
var maintainer = DbContext.Users.FirstOrDefault(x => x.Id == collection.MaintainerId);
result.Maintainer = new DataToken
@ -376,19 +376,18 @@ namespace Roadie.Api.Services
select new CollectionRelease
{
ListNumber = crc.ListNumber,
Release = ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl,
MakeArtistThumbnailImage(r.Artist.RoadieId), MakeReleaseThumbnailImage(r.RoadieId))
Release = ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl, MakeArtistThumbnailImage(r.Artist.RoadieId), MakeReleaseThumbnailImage(r.RoadieId))
}).ToArray();
if (includes.Contains("stats"))
{
var collectionReleases = from crc in DbContext.CollectionReleases
join r in DbContext.Releases.Include(x => x.Artist) on crc.ReleaseId equals r.Id
join r in DbContext.Releases on crc.ReleaseId equals r.Id
where crc.CollectionId == collection.Id
select r;
var collectionTracks = from crc in DbContext.CollectionReleases
join r in DbContext.Releases.Include(x => x.Artist) on crc.ReleaseId equals r.Id
join r in DbContext.Releases on crc.ReleaseId equals r.Id
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId
where crc.CollectionId == collection.Id
@ -410,7 +409,9 @@ namespace Roadie.Api.Services
if (includes.Contains("comments"))
{
var collectionComments = DbContext.Comments.Include(x => x.User)
.Where(x => x.CollectionId == collection.Id).OrderByDescending(x => x.CreatedDate).ToArray();
.Where(x => x.CollectionId == collection.Id)
.OrderByDescending(x => x.CreatedDate)
.ToArray();
if (collectionComments.Any())
{
var comments = new List<Comment>();
@ -422,15 +423,11 @@ namespace Roadie.Api.Services
{
var comment = collectionComment.Adapt<Comment>();
comment.DatabaseId = collectionComment.Id;
comment.User = UserList.FromDataUser(collectionComment.User,
MakeUserThumbnailImage(collectionComment.User.RoadieId));
comment.DislikedCount = userCommentReactions.Count(x =>
x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Dislike);
comment.LikedCount = userCommentReactions.Count(x =>
x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Like);
comment.User = UserList.FromDataUser(collectionComment.User, MakeUserThumbnailImage(collectionComment.User.RoadieId));
comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Dislike);
comment.LikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Like);
comments.Add(comment);
}
result.Comments = comments;
}
}

View file

@ -460,8 +460,7 @@ namespace Roadie.Api.Services
var maintainer = DbContext.Users.Include(x => x.UserRoles).Include("UserRoles.Role").FirstOrDefault(x => x.Id == playlist.UserId);
result.Maintainer = UserList.FromDataUser(maintainer, MakeUserThumbnailImage(maintainer.RoadieId));
result.Thumbnail = MakePlaylistThumbnailImage(playlist.RoadieId);
result.MediumThumbnail = MakeThumbnailImage(id, "playlist", Configuration.MediumImageSize.Width,
Configuration.MediumImageSize.Height);
result.MediumThumbnail = MakeThumbnailImage(id, "playlist", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (includes != null && includes.Any())
{
var playlistTracks = (from pl in DbContext.Playlists

View file

@ -192,8 +192,7 @@ namespace Roadie.Api.Services
};
}
public Task<Library.Models.Pagination.PagedResult<ReleaseList>> List(User roadieUser, PagedRequest request,
bool? doRandomize = false, IEnumerable<string> includes = null)
public Task<Library.Models.Pagination.PagedResult<ReleaseList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, IEnumerable<string> includes = null)
{
var sw = new Stopwatch();
sw.Start();
@ -231,9 +230,7 @@ namespace Roadie.Api.Services
}
else if (!string.IsNullOrEmpty(request.FilterByGenre) || !string.IsNullOrEmpty(request.Filter) && request.Filter.StartsWith(":genre", StringComparison.OrdinalIgnoreCase))
{
var genreFilter = request.FilterByGenre ??
(request.Filter ?? string.Empty).Replace(":genre ", "",
StringComparison.OrdinalIgnoreCase);
var genreFilter = request.FilterByGenre ?? (request.Filter ?? string.Empty).Replace(":genre ", "", StringComparison.OrdinalIgnoreCase);
genreReleaseIds = (from rg in DbContext.ReleaseGenres
join g in DbContext.Genres on rg.GenreId equals g.Id
where g.Name.Contains(genreFilter)
@ -349,8 +346,8 @@ namespace Roadie.Api.Services
Thumbnail = MakeReleaseThumbnailImage(r.RoadieId),
TrackCount = r.TrackCount,
TrackPlayedCount = r.PlayedCount
}
);
});
ReleaseList[] rows = null;
rowCount = rowCount ?? result.Count();

View file

@ -192,16 +192,16 @@ namespace Roadie.Api.Services
var playlistTrackIds = new int[0];
if (request.FilterToPlaylistId.HasValue)
{
var playlistTrackInfos = from plt in DbContext.PlaylistTracks
join p in DbContext.Playlists on plt.PlayListId equals p.Id
join t in DbContext.Tracks on plt.TrackId equals t.Id
where p.RoadieId == request.FilterToPlaylistId.Value
orderby plt.ListNumber
select new
{
plt.ListNumber,
t.Id
};
var playlistTrackInfos = (from plt in DbContext.PlaylistTracks
join p in DbContext.Playlists on plt.PlayListId equals p.Id
join t in DbContext.Tracks on plt.TrackId equals t.Id
where p.RoadieId == request.FilterToPlaylistId.Value
orderby plt.ListNumber
select new
{
plt.ListNumber,
t.Id
}).ToArray();
rowCount = playlistTrackInfos.Count();
playListTrackPositions = playlistTrackInfos
@ -507,10 +507,6 @@ namespace Roadie.Api.Services
if(doRandomize ?? false)
{
//rows = result.OrderBy(x => x.Artist.RandomSortId)
// .ThenBy(x => x.RandomSortId)
// .ToArray();
rows = result.ToArray();
}
else

View file

@ -13,7 +13,7 @@
"Override": {
"System": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
"Microsoft.EntityFrameworkCore": "Warning"
}
},
"WriteTo": [