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
{
2022-10-07 23:33:54 +01:00
protected readonly IListenRepository ScrobbleRepository;
2022-06-22 08:01:59 +01:00
private readonly IOptions<NowPlayingOptions> nowOptions;
public DBPlayCountPuller(
IOptions<NowPlayingOptions> options,
2022-10-07 23:33:54 +01:00
IListenRepository scrobbleRepository
2022-06-22 08:01:59 +01:00
)
{
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);
2022-11-05 22:39:07 +00:00
var artistScrobbles = ScrobbleRepository.GetAll(username: username, artistName: artist, tracking: false, orderTime: true).ToArray();
2022-06-22 08:01:59 +01:00
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);
}
}
}