Performance tweaks.

This commit is contained in:
Steven Hildreth 2018-12-05 10:04:21 -06:00
parent c97281b05b
commit 49cfe0dc8c
6 changed files with 124 additions and 69 deletions

View file

@ -1,6 +1,7 @@
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Roadie.Library;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
@ -50,22 +51,36 @@ namespace Roadie.Api.Services
public async Task<OperationResult<Artist>> ById(User roadieUser, Guid id, IEnumerable<string> includes)
{
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
var sw = Stopwatch.StartNew();
sw.Start();
var cacheKey = string.Format("urn:artist_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
var result = await this.CacheManager.GetAsync<OperationResult<Artist>>(cacheKey, async () =>
{
return await this.ArtistByIdAction(id, includes);
tsw.Restart();
var rr = await this.ArtistByIdAction(id, includes);
tsw.Stop();
timings.Add("ArtistByIdAction", tsw.ElapsedMilliseconds);
return rr;
}, data.Artist.CacheRegionUrn(id));
if (result?.Data != null && roadieUser != null)
{
tsw.Restart();
var artist = this.GetArtist(id);
tsw.Stop();
timings.Add("GetArtist", tsw.ElapsedMilliseconds);
tsw.Restart();
var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Artist);
if(userBookmarkResult.IsSuccess)
if (userBookmarkResult.IsSuccess)
{
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Value == artist.RoadieId.ToString()) != null;
}
tsw.Stop();
timings.Add("userBookmarkResult", tsw.ElapsedMilliseconds);
tsw.Restart();
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == roadieUser.Id);
if (userArtist != null)
{
@ -76,8 +91,12 @@ namespace Roadie.Api.Services
Rating = userArtist.Rating
};
}
tsw.Stop();
timings.Add("userArtist", tsw.ElapsedMilliseconds);
}
sw.Stop();
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ById Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>(result.Messages)
{
Data = result?.Data,
@ -90,19 +109,32 @@ namespace Roadie.Api.Services
private async Task<OperationResult<Artist>> ArtistByIdAction(Guid id, IEnumerable<string> includes)
{
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
var sw = Stopwatch.StartNew();
sw.Start();
tsw.Restart();
var artist = this.GetArtist(id);
tsw.Stop();
timings.Add("getArtist", tsw.ElapsedMilliseconds);
if (artist == null)
{
return new OperationResult<Artist>(true, string.Format("Artist Not Found [{0}]", id));
}
tsw.Restart();
var result = artist.Adapt<Artist>();
tsw.Stop();
timings.Add("adaptArtist", tsw.ElapsedMilliseconds);
result.Thumbnail = base.MakeArtistThumbnailImage(id);
result.MediumThumbnail = base.MakeThumbnailImage(id, "artist", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
tsw.Restart();
result.Genres = artist.Genres.Select(x => new DataToken { Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() });
tsw.Stop();
timings.Add("genres", tsw.ElapsedMilliseconds);
if (includes != null && includes.Any())
{
if (includes.Contains("releases"))
@ -147,9 +179,10 @@ namespace Roadie.Api.Services
}
result.Releases = dtoReleases;
}
if (includes.Contains("stats"))
{
tsw.Restart();
var artistTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
@ -178,13 +211,19 @@ namespace Roadie.Api.Services
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
select ut.PlayedCount).Sum() ?? 0
};
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
}
if (includes.Contains("images"))
{
tsw.Restart();
result.Images = this.DbContext.Images.Where(x => x.ArtistId == artist.Id).Select(x => MakeFullsizeImage(x.RoadieId, x.Caption)).ToArray();
tsw.Stop();
timings.Add("images", tsw.ElapsedMilliseconds);
}
if (includes.Contains("associatedartists"))
{
tsw.Restart();
var associatedWithArtists = (from aa in this.DbContext.ArtistAssociations
join a in this.DbContext.Artists on aa.AssociatedArtistId equals a.Id
where aa.ArtistId == artist.Id
@ -232,10 +271,13 @@ namespace Roadie.Api.Services
}).ToArray();
result.AssociatedArtists = associatedArtists.Union(associatedWithArtists).OrderBy(x => x.SortName);
tsw.Stop();
timings.Add("associatedartists", tsw.ElapsedMilliseconds);
}
if (includes.Contains("collections"))
{
tsw.Restart();
var collectionPagedRequest = new PagedRequest
{
Limit = 100
@ -246,9 +288,12 @@ namespace Roadie.Api.Services
{
result.CollectionsWithArtistReleases = r.Rows.ToArray();
}
tsw.Stop();
timings.Add("collections", tsw.ElapsedMilliseconds);
}
if (includes.Contains("playlists"))
{
tsw.Restart();
var pg = new PagedRequest
{
FilterToArtistId = artist.RoadieId
@ -258,9 +303,12 @@ namespace Roadie.Api.Services
{
result.PlaylistsWithArtistReleases = r.Rows.ToArray();
}
tsw.Stop();
timings.Add("playlists", tsw.ElapsedMilliseconds);
}
if (includes.Contains("contributions"))
{
tsw.Restart();
result.ArtistContributionReleases = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in this.DbContext.Releases.Include(x => x.Artist) on rm.ReleaseId equals r.Id
@ -271,6 +319,7 @@ namespace Roadie.Api.Services
.Select(rr => rr.First())
.Select(r => new ReleaseList
{
Id = r.RoadieId,
Release = new DataToken
{
Text = r.Title,
@ -295,9 +344,12 @@ namespace Roadie.Api.Services
Thumbnail = MakeReleaseThumbnailImage(r.RoadieId)
}).ToArray().OrderBy(x => x.Release.Text).ToArray();
result.ArtistContributionReleases = result.ArtistContributionReleases.Any() ? result.ArtistContributionReleases : null;
tsw.Stop();
timings.Add("contributions", tsw.ElapsedMilliseconds);
}
if (includes.Contains("labels"))
{
tsw.Restart();
result.ArtistLabels = (from l in this.DbContext.Labels
join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId
join r in this.DbContext.Releases on rl.ReleaseId equals r.Id
@ -320,9 +372,14 @@ namespace Roadie.Api.Services
Thumbnail = MakeLabelThumbnailImage(l.RoadieId)
}).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);
}
}
sw.Stop();
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ArtistByIdAction Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>
{
Data = result,

View file

@ -61,90 +61,77 @@ namespace Roadie.Api.Services
var rowCount = result.Count();
BookmarkList[] rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
var datas = (from b in rows
join a in this.DbContext.Artists on b.BookmarkTargetId equals a.Id into aa
from a in aa.DefaultIfEmpty()
join r in this.DbContext.Releases on b.BookmarkTargetId equals r.Id into rr
from r in rr.DefaultIfEmpty()
join t in this.DbContext.Tracks on b.BookmarkTargetId equals t.Id into tt
from t in tt.DefaultIfEmpty()
join p in this.DbContext.Playlists on b.BookmarkTargetId equals p.Id into pp
from p in pp.DefaultIfEmpty()
join c in this.DbContext.Collections on b.BookmarkTargetId equals c.Id into cc
from c in cc.DefaultIfEmpty()
join l in this.DbContext.Labels on b.BookmarkTargetId equals l.Id into ll
from l in ll.DefaultIfEmpty()
select new
{ b, a, r, t, p, c, l });
foreach (var row in rows)
{
var d = datas.FirstOrDefault(x => x.b.DatabaseId == row.DatabaseId);
switch (row.Type)
{
case BookmarkType.Artist:
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.a.Name,
Value = d.a.RoadieId.ToString()
Text = artist.Name,
Value = artist.RoadieId.ToString()
};
row.Thumbnail = this.MakeArtistThumbnailImage(d.a.RoadieId);
row.SortName = d.a.SortName ?? d.a.Name;
row.Thumbnail = this.MakeArtistThumbnailImage(artist.RoadieId);
row.SortName = artist.SortName ?? artist.Name;
break;
case BookmarkType.Release:
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.r.Title,
Value = d.r.RoadieId.ToString()
Text = release.Title,
Value = release.RoadieId.ToString()
};
row.Thumbnail = this.MakeReleaseThumbnailImage(d.r.RoadieId);
row.SortName = d.r.Title;
row.Thumbnail = this.MakeReleaseThumbnailImage(release.RoadieId);
row.SortName = release.Title;
break;
case BookmarkType.Track:
var track = this.DbContext.Tracks.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.t.Title,
Value = d.t.RoadieId.ToString()
Text = track.Title,
Value = track.RoadieId.ToString()
};
row.Thumbnail = this.MakeTrackThumbnailImage(d.t.RoadieId);
row.SortName = d.t.Title;
row.Thumbnail = this.MakeTrackThumbnailImage(track.RoadieId);
row.SortName = track.Title;
break;
case BookmarkType.Playlist:
var playlist = this.DbContext.Playlists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.p.Name,
Value = d.p.RoadieId.ToString()
Text = playlist.Name,
Value = playlist.RoadieId.ToString()
};
row.Thumbnail = this.MakePlaylistThumbnailImage(d.p.RoadieId);
row.SortName = d.p.Name;
row.Thumbnail = this.MakePlaylistThumbnailImage(playlist.RoadieId);
row.SortName = playlist.Name;
break;
case BookmarkType.Collection:
var collection = this.DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.c.Name,
Value = d.c.RoadieId.ToString()
Text = collection.Name,
Value = collection.RoadieId.ToString()
};
row.Thumbnail = this.MakeCollectionThumbnailImage(d.c.RoadieId);
row.SortName = d.c.SortName ?? d.c.Name;
row.Thumbnail = this.MakeCollectionThumbnailImage(collection.RoadieId);
row.SortName = collection.SortName ?? collection.Name;
break;
case BookmarkType.Label:
var label = this.DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
Text = d.l.Name,
Value = d.l.RoadieId.ToString()
Text = label.Name,
Value = label.RoadieId.ToString()
};
row.Thumbnail = this.MakeLabelThumbnailImage(d.l.RoadieId);
row.SortName = d.l.SortName ?? d.l.Name;
row.Thumbnail = this.MakeLabelThumbnailImage(label.RoadieId);
row.SortName = label.SortName ?? label.Name;
break;
}
};
sw.Stop();
sw.Stop();
return new Library.Models.Pagination.PagedResult<BookmarkList>
{

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Encoding;
@ -34,13 +35,33 @@ namespace Roadie.Api.Services
{
var sw = new Stopwatch();
sw.Start();
if (!string.IsNullOrEmpty(request.Sort))
IQueryable<data.Collection> collections = null;
if(artistId.HasValue)
{
request.Sort = request.Sort.Replace("createdDate", "createdDateTime");
request.Sort = request.Sort.Replace("lastUpdated", "lastUpdatedDateTime");
var sql = @"select DISTINCT c.*
from `collectionrelease` cr
join `collection` c on c.id = cr.collectionId
join `release` r on r.id = cr.releaseId
join `artist` a on r.artistId = a.id
where a.roadieId = {0}";
collections = this.DbContext.Collections.FromSql(sql, artistId);
}
var result = (from c in this.DbContext.Collections
else if(releaseId.HasValue)
{
var sql = @"select DISTINCT c.*
from `collectionrelease` cr
join `collection` c on c.id = cr.collectionId
join `release` r on r.id = cr.releaseId
where r.roadieId = {0}";
collections = this.DbContext.Collections.FromSql(sql, releaseId);
}
else
{
collections = this.DbContext.Collections;
}
var result = (from c in collections
where (request.FilterValue.Length == 0 || (request.FilterValue.Length > 0 && c.Name.Contains(request.Filter)))
select new CollectionList
{
@ -60,19 +81,6 @@ namespace Roadie.Api.Services
LastUpdated = c.LastUpdated,
Thumbnail = MakeCollectionThumbnailImage(c.RoadieId)
});
if (artistId.HasValue || releaseId.HasValue)
{
result = (from re in result
join cr in this.DbContext.CollectionReleases on re.DatabaseId equals cr.CollectionId into crs
from cr in crs.DefaultIfEmpty()
join r in this.DbContext.Releases on cr.ReleaseId equals r.Id into rs
from r in rs.DefaultIfEmpty()
join a in this.DbContext.Artists on r.ArtistId equals a.Id into aas
from a in aas.DefaultIfEmpty()
where (releaseId == null || r.RoadieId == releaseId)
where (artistId == null || a.RoadieId == artistId)
select re).GroupBy(x => x.DatabaseId).Select(x => x.First());
}
var sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "Collection.Text", "ASC" } }) : request.OrderValue(null);
var rowCount = result.Count();
var rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();

View file

@ -9,10 +9,11 @@
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFileAlternate" ],
"MinimumLevel": {
"Default": "Information",
"Default": "Verbose",
"Override": {
"Microsoft": "Information",
"System": "Warning"
"System": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
}
},
"WriteTo": [

View file

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Roadie.Library.Identity;
using Roadie.Library.Models.Releases;
using System;
using System.Collections.Generic;
using System.Threading;

View file

@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Roadie.Library.Enums;
using Roadie.Library.Identity;
using Roadie.Library.Models.Releases;
using System;
namespace Roadie.Library.Data