adding to repos, speeding up hopefully

This commit is contained in:
Andy Pack 2022-11-05 22:39:07 +00:00
parent 2247498c70
commit c646e4ea27
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
6 changed files with 45 additions and 19 deletions

View File

@ -70,6 +70,8 @@ namespace Selector.Model
modelBuilder.Entity<UserScrobble>() modelBuilder.Entity<UserScrobble>()
.Property(s => s.ArtistName) .Property(s => s.ArtistName)
.UseCollation("case_insensitive"); .UseCollation("case_insensitive");
//modelBuilder.Entity<UserScrobble>()
// .HasIndex(x => new { x.UserId, x.ArtistName, x.TrackName });
modelBuilder.Entity<TrackLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri); modelBuilder.Entity<TrackLastfmSpotifyMapping>().HasKey(s => s.SpotifyUri);
modelBuilder.Entity<TrackLastfmSpotifyMapping>() modelBuilder.Entity<TrackLastfmSpotifyMapping>()
@ -102,6 +104,8 @@ namespace Selector.Model
modelBuilder.Entity<SpotifyListen>() modelBuilder.Entity<SpotifyListen>()
.Property(s => s.ArtistName) .Property(s => s.ArtistName)
.UseCollation("case_insensitive"); .UseCollation("case_insensitive");
//modelBuilder.Entity<SpotifyListen>()
// .HasIndex(x => new { x.UserId, x.ArtistName, x.TrackName });
SeedData.Seed(modelBuilder); SeedData.Seed(modelBuilder);
} }

View File

@ -5,7 +5,7 @@ namespace Selector.Model
{ {
public interface IListenRepository public interface IListenRepository
{ {
IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null); IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false);
int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null); int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null);
} }
} }

View File

@ -28,7 +28,8 @@ public class MetaListenRepository: IListenRepository
albumName: albumName, albumName: albumName,
artistName: artistName, artistName: artistName,
from: from, from: from,
to:to).Count(); to:to,
tracking: false).Count();
public IEnumerable<IListen> GetAll( public IEnumerable<IListen> GetAll(
string includes = null, string includes = null,
@ -38,7 +39,9 @@ public class MetaListenRepository: IListenRepository
string albumName = null, string albumName = null,
string artistName = null, string artistName = null,
DateTime? from = null, DateTime? from = null,
DateTime? to = null) DateTime? to = null,
bool tracking = true,
bool orderTime = false)
{ {
var scrobbles = scrobbleRepository.GetAll( var scrobbles = scrobbleRepository.GetAll(
include: includes, include: includes,
@ -48,9 +51,9 @@ public class MetaListenRepository: IListenRepository
albumName: albumName, albumName: albumName,
artistName: artistName, artistName: artistName,
from: from, from: from,
to: to) to: to,
.OrderBy(x => x.Timestamp) tracking: tracking,
.ToArray(); orderTime: true).ToArray();
var spotListens = spotifyRepository.GetAll( var spotListens = spotifyRepository.GetAll(
include: includes, include: includes,
@ -60,10 +63,9 @@ public class MetaListenRepository: IListenRepository
albumName: albumName, albumName: albumName,
artistName: artistName, artistName: artistName,
from: from, from: from,
to: scrobbles.FirstOrDefault()?.Timestamp) to: scrobbles.LastOrDefault()?.Timestamp,
.OrderBy(x => x.Timestamp) tracking: tracking,
.ToArray(); orderTime: orderTime);
return spotListens.Concat(scrobbles); return spotListens.Concat(scrobbles);
} }

View File

@ -39,10 +39,15 @@ namespace Selector.Model
return listens.FirstOrDefault(); return listens.FirstOrDefault();
} }
private IQueryable<SpotifyListen> GetAllQueryable(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) private IQueryable<SpotifyListen> GetAllQueryable(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
{ {
var listens = db.SpotifyListen.AsQueryable(); var listens = db.SpotifyListen.AsQueryable();
if (!tracking)
{
listens = listens.AsNoTracking();
}
if (!string.IsNullOrWhiteSpace(include)) if (!string.IsNullOrWhiteSpace(include))
{ {
listens = listens.Include(include); listens = listens.Include(include);
@ -92,11 +97,16 @@ namespace Selector.Model
listens = listens.Where(u => u.Timestamp < to.Value); listens = listens.Where(u => u.Timestamp < to.Value);
} }
if (orderTime)
{
listens = listens.OrderBy(x => x.Timestamp);
}
return listens; return listens;
} }
public IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) public IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
=> GetAllQueryable(include: include, userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to).AsEnumerable(); => GetAllQueryable(include: include, userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: tracking, orderTime: orderTime).AsEnumerable();
public void Remove(DateTime key) public void Remove(DateTime key)
{ {
@ -124,6 +134,6 @@ namespace Selector.Model
} }
public int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) public int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null)
=> GetAllQueryable(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to).Count(); => GetAllQueryable(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: false).Count();
} }
} }

View File

@ -27,7 +27,7 @@ namespace Selector.Cache
var userScrobbleCount = ScrobbleRepository.Count(username: username); var userScrobbleCount = ScrobbleRepository.Count(username: username);
var artistScrobbles = ScrobbleRepository.GetAll(username: username, artistName: artist).ToArray(); var artistScrobbles = ScrobbleRepository.GetAll(username: username, artistName: artist, tracking: false, orderTime: true).ToArray();
var albumScrobbles = artistScrobbles.Where( var albumScrobbles = artistScrobbles.Where(
s => s.AlbumName.Equals(album, StringComparison.CurrentCultureIgnoreCase)).ToArray(); s => s.AlbumName.Equals(album, StringComparison.CurrentCultureIgnoreCase)).ToArray();
var trackScrobbles = artistScrobbles.Where( var trackScrobbles = artistScrobbles.Where(

View File

@ -39,10 +39,15 @@ namespace Selector.Model
return scrobbles.FirstOrDefault(); return scrobbles.FirstOrDefault();
} }
private IQueryable<UserScrobble> GetAllQueryable(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) private IQueryable<UserScrobble> GetAllQueryable(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
{ {
var scrobbles = db.Scrobble.AsQueryable(); var scrobbles = db.Scrobble.AsQueryable();
if (!tracking)
{
scrobbles = scrobbles.AsNoTracking();
}
if (!string.IsNullOrWhiteSpace(include)) if (!string.IsNullOrWhiteSpace(include))
{ {
scrobbles = scrobbles.Include(include); scrobbles = scrobbles.Include(include);
@ -92,11 +97,16 @@ namespace Selector.Model
scrobbles = scrobbles.Where(u => u.Timestamp < to.Value); scrobbles = scrobbles.Where(u => u.Timestamp < to.Value);
} }
if (orderTime)
{
scrobbles = scrobbles.OrderBy(x => x.Timestamp);
}
return scrobbles; return scrobbles;
} }
public IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) public IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
=> GetAllQueryable(include: include, userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to).AsEnumerable(); => GetAllQueryable(include: include, userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: tracking, orderTime: orderTime).AsEnumerable();
public void Remove(int key) public void Remove(int key)
{ {
@ -124,6 +134,6 @@ namespace Selector.Model
} }
public int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null) public int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null)
=> GetAllQueryable(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to).Count(); => GetAllQueryable(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: false).Count();
} }
} }