using queryable more and being a bit smarter about db

This commit is contained in:
Andy Pack 2023-01-03 23:15:17 +00:00
parent 99cb615161
commit 57b80c9902
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
7 changed files with 56 additions and 25 deletions

View File

@ -51,8 +51,9 @@ namespace Selector
{ {
logger.LogInformation("Mapping scrobble tracks"); logger.LogInformation("Mapping scrobble tracks");
var currentTracks = mappingRepo.GetTracks(); var currentTracks = mappingRepo.GetTracks().AsEnumerable();
var scrobbleTracks = scrobbleRepo.GetAll() var scrobbleTracks = scrobbleRepo.GetAll()
.AsEnumerable()
.GroupBy(x => (x.ArtistName, x.TrackName)) .GroupBy(x => (x.ArtistName, x.TrackName))
.Select(x => (x.Key, x.Count())) .Select(x => (x.Key, x.Count()))
.OrderByDescending(x => x.Item2) .OrderByDescending(x => x.Item2)

View File

@ -1,11 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Selector.Model 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, bool tracking = true, bool orderTime = false); IQueryable<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

@ -22,16 +22,33 @@ 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) => GetAll(userId: userId, DateTime? to = null)
{
var scrobbles = scrobbleRepository.GetAll(
userId: userId,
username: username, username: username,
trackName: trackName, trackName: trackName,
albumName: albumName, albumName: albumName,
artistName: artistName, artistName: artistName,
from: from, from: from,
to: to, to: to,
tracking: false).Count(); tracking: false,
orderTime: true);
public IEnumerable<IListen> GetAll( var spotListens = spotifyRepository.GetAll(
userId: userId,
username: username,
trackName: trackName,
albumName: albumName,
artistName: artistName,
from: from,
to: scrobbles.FirstOrDefault()?.Timestamp,
tracking: false);
return scrobbles.Count() + spotListens.Count();
}
public IQueryable<IListen> GetAll(
string includes = null, string includes = null,
string userId = null, string userId = null,
string username = null, string username = null,
@ -53,7 +70,7 @@ public class MetaListenRepository: IListenRepository
from: from, from: from,
to: to, to: to,
tracking: tracking, tracking: tracking,
orderTime: true).ToArray(); orderTime: true);
var spotListens = spotifyRepository.GetAll( var spotListens = spotifyRepository.GetAll(
include: includes, include: includes,
@ -67,7 +84,19 @@ public class MetaListenRepository: IListenRepository
tracking: tracking, tracking: tracking,
orderTime: orderTime); orderTime: orderTime);
return spotListens.Concat(scrobbles); return spotListens.Select(sp => new Listen
{
Timestamp = sp.Timestamp,
TrackName = sp.TrackName,
AlbumName = sp.AlbumName,
ArtistName = sp.ArtistName
}).Concat(scrobbles.Select(sp => new Listen
{
Timestamp = sp.Timestamp,
TrackName = sp.TrackName,
AlbumName = sp.AlbumName,
ArtistName = sp.ArtistName
}));
} }
} }

View File

@ -39,7 +39,7 @@ 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, bool tracking = true, bool orderTime = false) public IQueryable<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)
{ {
var listens = db.SpotifyListen.AsQueryable(); var listens = db.SpotifyListen.AsQueryable();
@ -105,8 +105,8 @@ namespace Selector.Model
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, bool tracking = true, bool orderTime = false) // 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, tracking: tracking, orderTime: orderTime).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)
{ {
@ -134,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, tracking: false).Count(); => GetAll(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: false).Count();
} }
} }

View File

@ -14,9 +14,9 @@ namespace Selector.Model
void AddRange(IEnumerable<TrackLastfmSpotifyMapping> item); void AddRange(IEnumerable<TrackLastfmSpotifyMapping> item);
void AddRange(IEnumerable<AlbumLastfmSpotifyMapping> item); void AddRange(IEnumerable<AlbumLastfmSpotifyMapping> item);
void AddRange(IEnumerable<ArtistLastfmSpotifyMapping> item); void AddRange(IEnumerable<ArtistLastfmSpotifyMapping> item);
IEnumerable<TrackLastfmSpotifyMapping> GetTracks(string include = null, string trackName = null, string albumName = null, string artistName = null); IQueryable<TrackLastfmSpotifyMapping> GetTracks(string include = null, string trackName = null, string albumName = null, string artistName = null);
IEnumerable<AlbumLastfmSpotifyMapping> GetAlbums(string include = null, string albumName = null, string artistName = null); IQueryable<AlbumLastfmSpotifyMapping> GetAlbums(string include = null, string albumName = null, string artistName = null);
IEnumerable<ArtistLastfmSpotifyMapping> GetArtists(string include = null, string artistName = null); IQueryable<ArtistLastfmSpotifyMapping> GetArtists(string include = null, string artistName = null);
public void Remove(TrackLastfmSpotifyMapping mapping); public void Remove(TrackLastfmSpotifyMapping mapping);
public void Remove(AlbumLastfmSpotifyMapping mapping); public void Remove(AlbumLastfmSpotifyMapping mapping);

View File

@ -47,7 +47,7 @@ namespace Selector.Model
db.ArtistMapping.AddRange(item); db.ArtistMapping.AddRange(item);
} }
public IEnumerable<AlbumLastfmSpotifyMapping> GetAlbums(string include = null, string albumName = null, string artistName = null) public IQueryable<AlbumLastfmSpotifyMapping> GetAlbums(string include = null, string albumName = null, string artistName = null)
{ {
var mappings = db.AlbumMapping.AsQueryable(); var mappings = db.AlbumMapping.AsQueryable();
@ -66,10 +66,10 @@ namespace Selector.Model
mappings = mappings.Where(s => s.LastfmArtistName == artistName); mappings = mappings.Where(s => s.LastfmArtistName == artistName);
} }
return mappings.AsEnumerable(); return mappings;
} }
public IEnumerable<ArtistLastfmSpotifyMapping> GetArtists(string include = null, string artistName = null) public IQueryable<ArtistLastfmSpotifyMapping> GetArtists(string include = null, string artistName = null)
{ {
var mappings = db.ArtistMapping.AsQueryable(); var mappings = db.ArtistMapping.AsQueryable();
@ -83,10 +83,10 @@ namespace Selector.Model
mappings = mappings.Where(s => s.LastfmArtistName == artistName); mappings = mappings.Where(s => s.LastfmArtistName == artistName);
} }
return mappings.AsEnumerable(); return mappings;
} }
public IEnumerable<TrackLastfmSpotifyMapping> GetTracks(string include = null, string trackName = null, string albumName = null, string artistName = null) public IQueryable<TrackLastfmSpotifyMapping> GetTracks(string include = null, string trackName = null, string albumName = null, string artistName = null)
{ {
var mappings = db.TrackMapping.AsQueryable(); var mappings = db.TrackMapping.AsQueryable();
@ -105,7 +105,7 @@ namespace Selector.Model
mappings = mappings.Where(s => s.LastfmArtistName == artistName); mappings = mappings.Where(s => s.LastfmArtistName == artistName);
} }
return mappings.AsEnumerable(); return mappings;
} }
public void Remove(TrackLastfmSpotifyMapping mapping) public void Remove(TrackLastfmSpotifyMapping mapping)

View File

@ -39,7 +39,7 @@ 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, bool tracking = true, bool orderTime = false) public IQueryable<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)
{ {
var scrobbles = db.Scrobble.AsQueryable(); var scrobbles = db.Scrobble.AsQueryable();
@ -105,8 +105,8 @@ namespace Selector.Model
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, bool tracking = true, bool orderTime = false) // 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, tracking: tracking, orderTime: orderTime).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)
{ {
@ -134,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, tracking: false).Count(); => GetAll(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: false).Count();
} }
} }