roadie/Roadie.Api.Services/StatisticsService.cs

101 lines
3.7 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
2018-11-12 00:28:37 +00:00
using Roadie.Library;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context;
2018-11-12 00:28:37 +00:00
using Roadie.Library.Encoding;
using Roadie.Library.Models.Statistics;
using Roadie.Library.Utility;
using System;
2018-12-09 03:16:05 +00:00
using System.Collections.Generic;
2018-11-12 00:28:37 +00:00
using System.Diagnostics;
2018-12-09 03:16:05 +00:00
using System.Linq;
2018-11-12 00:28:37 +00:00
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
public class StatisticsService : ServiceBase, IStatisticsService
{
public StatisticsService(IRoadieSettings configuration,
2019-06-30 22:14:36 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
2019-11-17 14:10:17 +00:00
IRoadieDbContext context,
2019-06-30 22:14:36 +00:00
ICacheManager cacheManager,
ILogger<StatisticsService> logger)
2018-11-12 00:28:37 +00:00
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
{
}
2019-07-07 03:16:33 +00:00
public Task<OperationResult<LibraryStats>> LibraryStatistics()
2018-11-12 00:28:37 +00:00
{
LibraryStats result = null;
var sw = new Stopwatch();
sw.Start();
try
2018-11-12 00:28:37 +00:00
{
result = new LibraryStats
{
UserCount = DbContext.Users.Count(),
CollectionCount = DbContext.Collections.Count(),
2019-09-30 01:53:13 +00:00
PlaylistCount = DbContext.Playlists.Count(),
ArtistCount = DbContext.Artists.Count(),
LabelCount = DbContext.Labels.Count(),
ReleaseCount = DbContext.Releases.Count(),
ReleaseMediaCount = DbContext.ReleaseMedias.Count()
};
result.PlayedCount = DbContext.UserTracks.Sum(x => x.PlayedCount);
var tracks = DbContext.Tracks.Where(x => x.Hash != null);
result.TrackCount = tracks.Count();
result.TotalTrackDuration = tracks.Sum(x => (long?)x.Duration);
result.TotalTrackSize = tracks.Sum(x => (long?)x.FileSize);
2019-06-30 22:14:36 +00:00
var lastScan = DbContext.ScanHistories.OrderByDescending(x => x.CreatedDate).FirstOrDefault();
result.LastScan = lastScan?.CreatedDate;
sw.Stop();
2018-11-12 00:28:37 +00:00
}
catch (Exception ex)
2018-12-09 03:16:05 +00:00
{
2019-06-30 22:14:36 +00:00
Logger.LogError(ex);
2018-12-09 03:16:05 +00:00
}
2019-06-30 22:14:36 +00:00
2019-07-07 03:16:33 +00:00
return Task.FromResult(new OperationResult<LibraryStats>
2018-11-12 00:28:37 +00:00
{
OperationTime = sw.ElapsedMilliseconds,
2019-07-07 03:16:33 +00:00
IsSuccess = result != null,
2018-11-12 00:28:37 +00:00
Data = result
2019-07-07 03:16:33 +00:00
});
2018-11-12 00:28:37 +00:00
}
2019-01-08 22:40:26 +00:00
public Task<OperationResult<IEnumerable<DateAndCount>>> ReleasesByDate()
{
var sw = new Stopwatch();
sw.Start();
var result = new List<DateAndCount>();
var dateInfos = (from r in DbContext.Releases
orderby r.CreatedDate
select r.CreatedDate)
.ToArray()
.GroupBy(x => x.ToString("yyyy-MM-dd"))
.Select(x => new
{
date = x.Key,
count = x.Count()
});
foreach (var dateInfo in dateInfos)
2019-01-08 22:40:26 +00:00
{
result.Add(new DateAndCount
2019-01-08 22:40:26 +00:00
{
Date = dateInfo.date,
Count = dateInfo.count
});
2019-01-08 22:40:26 +00:00
}
sw.Stop();
return Task.FromResult(new OperationResult<IEnumerable<DateAndCount>>
{
OperationTime = sw.ElapsedMilliseconds,
IsSuccess = result != null,
2019-01-08 22:40:26 +00:00
Data = result
});
}
2018-11-12 00:28:37 +00:00
}
}