Selector/Selector.Model/Scrobble/DBPlayCountPuller.cs

71 lines
2.6 KiB
C#
Raw Normal View History

2022-06-22 08:01:59 +01:00
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Selector.Model;
namespace Selector.Cache
{
public class DBPlayCountPuller
{
protected readonly IScrobbleRepository ScrobbleRepository;
private readonly IOptions<NowPlayingOptions> nowOptions;
public DBPlayCountPuller(
IOptions<NowPlayingOptions> options,
IScrobbleRepository scrobbleRepository
)
{
ScrobbleRepository = scrobbleRepository;
nowOptions = options;
}
public Task<PlayCount> Get(string username, string track, string artist, string album, string albumArtist)
{
if (string.IsNullOrWhiteSpace(username)) throw new ArgumentNullException("No username provided");
var userScrobbleCount = ScrobbleRepository.Count(username: username);
var artistScrobbles = ScrobbleRepository.GetAll(username: username, artistName: artist).ToArray();
var albumScrobbles = artistScrobbles.Where(
s => s.AlbumName.Equals(album, StringComparison.CurrentCultureIgnoreCase)).ToArray();
var trackScrobbles = artistScrobbles.Where(
s => s.TrackName.Equals(track, StringComparison.CurrentCultureIgnoreCase)).ToArray();
var postCalc = artistScrobbles.Resample(nowOptions.Value.ArtistResampleWindow).Select(s => s.Value).Sum();
//var postCalc = playCount.ArtistCountData.Select(s => s.Value).Sum();
Debug.Assert(postCalc == artistScrobbles.Count());
PlayCount playCount = new()
{
Username = username,
Artist = artistScrobbles.Count(),
Album = albumScrobbles.Count(),
Track = trackScrobbles.Count(),
User = userScrobbleCount,
ArtistCountData = artistScrobbles
.Resample(nowOptions.Value.ArtistResampleWindow)
//.ResampleByMonth()
.CumulativeSum()
.ToArray(),
AlbumCountData = albumScrobbles
.Resample(nowOptions.Value.AlbumResampleWindow)
//.ResampleByMonth()
.CumulativeSum()
.ToArray(),
TrackCountData = trackScrobbles
.Resample(nowOptions.Value.TrackResampleWindow)
//.ResampleByMonth()
.CumulativeSum()
.ToArray()
};
return Task.FromResult(playCount);
}
}
}