This commit is contained in:
Joe Milazzo 2024-12-07 08:57:04 -06:00 committed by GitHub
parent bcfb4a6172
commit b24b8686f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 510 additions and 347 deletions

View File

@ -129,15 +129,6 @@ public class ServerController : BaseApiController
return Ok(); return Ok();
} }
/// <summary>
/// Returns non-sensitive information about the current system
/// </summary>
/// <returns></returns>
[HttpGet("server-info")]
public async Task<ActionResult<ServerInfoDto>> GetVersion()
{
return Ok(await _statsService.GetServerInfo());
}
/// <summary> /// <summary>
/// Returns non-sensitive information about the current system /// Returns non-sensitive information about the current system
@ -145,7 +136,7 @@ public class ServerController : BaseApiController
/// <remarks>This is just for the UI and is extremely lightweight</remarks> /// <remarks>This is just for the UI and is extremely lightweight</remarks>
/// <returns></returns> /// <returns></returns>
[HttpGet("server-info-slim")] [HttpGet("server-info-slim")]
public async Task<ActionResult<ServerInfoDto>> GetSlimVersion() public async Task<ActionResult<ServerInfoSlimDto>> GetSlimVersion()
{ {
return Ok(await _statsService.GetServerInfoSlim()); return Ok(await _statsService.GetServerInfoSlim());
} }

View File

@ -1,15 +0,0 @@
using API.Entities.Enums;
namespace API.DTOs.Stats;
public class FileFormatDto
{
/// <summary>
/// The extension with the ., in lowercase
/// </summary>
public required string Extension { get; set; }
/// <summary>
/// Format of extension
/// </summary>
public required MangaFormat Format { get; set; }
}

View File

@ -1,184 +0,0 @@
using System;
using System.Collections.Generic;
using API.Entities.Enums;
namespace API.DTOs.Stats;
#nullable enable
/// <summary>
/// Represents information about a Kavita Installation
/// </summary>
public class ServerInfoDto
{
/// <summary>
/// Unique Id that represents a unique install
/// </summary>
public required string InstallId { get; set; }
public required string Os { get; set; }
/// <summary>
/// If the Kavita install is using Docker
/// </summary>
public bool IsDocker { get; set; }
/// <summary>
/// Version of .NET instance is running
/// </summary>
public required string DotnetVersion { get; set; }
/// <summary>
/// Version of Kavita
/// </summary>
public required string KavitaVersion { get; set; }
/// <summary>
/// Number of Cores on the instance
/// </summary>
public int NumOfCores { get; set; }
/// <summary>
/// The number of libraries on the instance
/// </summary>
public int NumberOfLibraries { get; set; }
/// <summary>
/// Does any user have bookmarks
/// </summary>
public bool HasBookmarks { get; set; }
/// <summary>
/// The site theme the install is using
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public string? ActiveSiteTheme { get; set; }
/// <summary>
/// The reading mode the main user has as a preference
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public ReaderMode MangaReaderMode { get; set; }
/// <summary>
/// Number of users on the install
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public int NumberOfUsers { get; set; }
/// <summary>
/// Number of collections on the install
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public int NumberOfCollections { get; set; }
/// <summary>
/// Number of reading lists on the install (Sum of all users)
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public int NumberOfReadingLists { get; set; }
/// <summary>
/// Is OPDS enabled
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public bool OPDSEnabled { get; set; }
/// <summary>
/// Total number of files in the instance
/// </summary>
/// <remarks>Introduced in v0.5.2</remarks>
public int TotalFiles { get; set; }
/// <summary>
/// Total number of Genres in the instance
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int TotalGenres { get; set; }
/// <summary>
/// Total number of People in the instance
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int TotalPeople { get; set; }
/// <summary>
/// Number of users on this instance using Card Layout
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int UsersOnCardLayout { get; set; }
/// <summary>
/// Number of users on this instance using List Layout
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int UsersOnListLayout { get; set; }
/// <summary>
/// Max number of Series for any library on the instance
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int MaxSeriesInALibrary { get; set; }
/// <summary>
/// Max number of Volumes for any library on the instance
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int MaxVolumesInASeries { get; set; }
/// <summary>
/// Max number of Chapters for any library on the instance
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public int MaxChaptersInASeries { get; set; }
/// <summary>
/// Does this instance have relationships setup between series
/// </summary>
/// <remarks>Introduced in v0.5.4</remarks>
public bool UsingSeriesRelationships { get; set; }
/// <summary>
/// A list of background colors set on the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public required IEnumerable<string> MangaReaderBackgroundColors { get; set; }
/// <summary>
/// A list of Page Split defaults being used on the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public required IEnumerable<PageSplitOption> MangaReaderPageSplittingModes { get; set; }
/// <summary>
/// A list of Layout Mode defaults being used on the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public required IEnumerable<LayoutMode> MangaReaderLayoutModes { get; set; }
/// <summary>
/// A list of file formats existing in the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public required IEnumerable<FileFormatDto> FileFormats { get; set; }
/// <summary>
/// If there is at least one user that is using an age restricted profile on the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public bool UsingRestrictedProfiles { get; set; }
/// <summary>
/// Number of users using the Emulate Comic Book setting
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public int UsersWithEmulateComicBook { get; set; }
/// <summary>
/// Percent (0.0-1.0) of libraries with folder watching enabled
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public float PercentOfLibrariesWithFolderWatchingEnabled { get; set; }
/// <summary>
/// Percent (0.0-1.0) of libraries included in Search
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public float PercentOfLibrariesIncludedInSearch { get; set; }
/// <summary>
/// Percent (0.0-1.0) of libraries included in Recommended
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public float PercentOfLibrariesIncludedInRecommended { get; set; }
/// <summary>
/// Percent (0.0-1.0) of libraries included in Dashboard
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public float PercentOfLibrariesIncludedInDashboard { get; set; }
/// <summary>
/// Total reading hours of all users
/// </summary>
/// <remarks>Introduced in v0.7.0</remarks>
public long TotalReadingHours { get; set; }
/// <summary>
/// The encoding the server is using to save media
/// </summary>
/// <remarks>Added in v0.7.3</remarks>
public EncodeFormat EncodeMediaAs { get; set; }
/// <summary>
/// The last user reading progress on the server (in UTC)
/// </summary>
/// <remarks>Added in v0.7.4</remarks>
public DateTime LastReadTime { get; set; }
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using API.Entities.Enums;
namespace API.DTOs.Stats.V3;
public class LibraryStatV3
{
public bool IncludeInDashboard { get; set; }
public bool IncludeInSearch { get; set; }
public bool UsingFolderWatching { get; set; }
/// <summary>
/// Are any exclude patterns setup
/// </summary>
public bool UsingExcludePatterns { get; set; }
/// <summary>
/// Will this library create collections from ComicInfo
/// </summary>
public bool CreateCollectionsFromMetadata { get; set; }
/// <summary>
/// Will this library create reading lists from ComicInfo
/// </summary>
public bool CreateReadingListsFromMetadata { get; set; }
/// <summary>
/// Type of the Library
/// </summary>
public LibraryType LibraryType { get; set; }
public ICollection<FileTypeGroup> FileTypes { get; set; }
/// <summary>
/// Last time library was fully scanned
/// </summary>
public DateTime LastScanned { get; set; }
/// <summary>
/// Number of folders the library has
/// </summary>
public int NumberOfFolders { get; set; }
}

View File

@ -0,0 +1,12 @@
using API.Entities.Enums;
namespace API.DTOs.Stats.V3;
/// <summary>
/// KavitaStats - Information about Series Relationships
/// </summary>
public class RelationshipStatV3
{
public int Count { get; set; }
public RelationKind Relationship { get; set; }
}

View File

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using API.Entities.Enums;
namespace API.DTOs.Stats.V3;
/// <summary>
/// Represents information about a Kavita Installation for Kavita Stats v3 API
/// </summary>
public class ServerInfoV3Dto
{
/// <summary>
/// Unique Id that represents a unique install
/// </summary>
public required string InstallId { get; set; }
public required string Os { get; set; }
/// <summary>
/// If the Kavita install is using Docker
/// </summary>
public bool IsDocker { get; set; }
/// <summary>
/// Version of .NET instance is running
/// </summary>
public required string DotnetVersion { get; set; }
/// <summary>
/// Version of Kavita
/// </summary>
public required string KavitaVersion { get; set; }
/// <summary>
/// Version of Kavita on Installation
/// </summary>
public required string InitialKavitaVersion { get; set; }
/// <summary>
/// Date of first Installation
/// </summary>
public DateTime InitialInstallDate { get; set; }
/// <summary>
/// Number of Cores on the instance
/// </summary>
public int NumOfCores { get; set; }
/// <summary>
/// OS locale on the instance
/// </summary>
public string OsLocale { get; set; }
/// <summary>
/// Milliseconds to open a random archive (zip/cbz) for reading
/// </summary>
public long TimeToOpeCbzMs { get; set; }
/// <summary>
/// Number of pages for said archive (zip/cbz)
/// </summary>
public long TimeToOpenCbzPages { get; set; }
/// <summary>
/// Milliseconds to get a response from KavitaStats API
/// </summary>
/// <remarks>This pings a health check and does not capture any IP Information</remarks>
public long TimeToPingKavitaStatsApi { get; set; }
#region Media
/// <summary>
/// Number of collections on the install
/// </summary>
public int NumberOfCollections { get; set; }
/// <summary>
/// Number of reading lists on the install (Sum of all users)
/// </summary>
public int NumberOfReadingLists { get; set; }
/// <summary>
/// Total number of files in the instance
/// </summary>
public int TotalFiles { get; set; }
/// <summary>
/// Total number of Genres in the instance
/// </summary>
public int TotalGenres { get; set; }
/// <summary>
/// Total number of Series in the instance
/// </summary>
public int TotalSeries { get; set; }
/// <summary>
/// Total number of Libraries in the instance
/// </summary>
public int TotalLibraries { get; set; }
/// <summary>
/// Total number of People in the instance
/// </summary>
public int TotalPeople { get; set; }
/// <summary>
/// Max number of Series for any library on the instance
/// </summary>
public int MaxSeriesInALibrary { get; set; }
/// <summary>
/// Max number of Volumes for any library on the instance
/// </summary>
public int MaxVolumesInASeries { get; set; }
/// <summary>
/// Max number of Chapters for any library on the instance
/// </summary>
public int MaxChaptersInASeries { get; set; }
/// <summary>
/// Everything about the Libraries on the instance
/// </summary>
public IList<LibraryStatV3> Libraries { get; set; }
/// <summary>
/// Everything around Series Relationships between series
/// </summary>
public IList<RelationshipStatV3> Relationships { get; set; }
#endregion
#region Server
/// <summary>
/// Is OPDS enabled
/// </summary>
public bool OpdsEnabled { get; set; }
/// <summary>
/// The encoding the server is using to save media
/// </summary>
public EncodeFormat EncodeMediaAs { get; set; }
/// <summary>
/// The last user reading progress on the server (in UTC)
/// </summary>
public DateTime LastReadTime { get; set; }
/// <summary>
/// Is this server using Kavita+
/// </summary>
public bool ActiveKavitaPlusSubscription { get; set; }
#endregion
#region Users
/// <summary>
/// If there is at least one user that is using an age restricted profile on the instance
/// </summary>
/// <remarks>Introduced in v0.6.0</remarks>
public bool UsingRestrictedProfiles { get; set; }
public IList<UserStatV3> Users { get; set; }
#endregion
}

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using API.Data.Misc;
using API.Entities.Enums.Device;
namespace API.DTOs.Stats.V3;
public class UserStatV3
{
public AgeRestriction AgeRestriction { get; set; }
/// <summary>
/// The last reading progress on the server (in UTC)
/// </summary>
public DateTime LastReadTime { get; set; }
/// <summary>
/// The last login on the server (in UTC)
/// </summary>
public DateTime LastLogin { get; set; }
/// <summary>
/// Has the user gone through email confirmation
/// </summary>
public bool IsEmailConfirmed { get; set; }
/// <summary>
/// Is the Email a valid address
/// </summary>
public bool HasValidEmail { get; set; }
/// <summary>
/// Float between 0-1 to showcase how much of the libraries a user has access to
/// </summary>
public float PercentageOfLibrariesHasAccess { get; set; }
/// <summary>
/// Number of reading lists this user created
/// </summary>
public int ReadingListsCreatedCount { get; set; }
/// <summary>
/// Number of collections this user created
/// </summary>
public int CollectionsCreatedCount { get; set; }
/// <summary>
/// Number of series in want to read for this user
/// </summary>
public int WantToReadSeriesCount { get; set; }
/// <summary>
/// Active locale for the user
/// </summary>
public string Locale { get; set; }
/// <summary>
/// Active Theme (name)
/// </summary>
public string ActiveTheme { get; set; }
/// <summary>
/// Number of series with Bookmarks created
/// </summary>
public int SeriesBookmarksCreatedCount { get; set; }
/// <summary>
/// Kavita+ only - Has an AniList Token set
/// </summary>
public bool HasAniListToken { get; set; }
/// <summary>
/// Kavita+ only - Has a MAL Token set
/// </summary>
public bool HasMALToken { get; set; }
/// <summary>
/// Number of Smart Filters a user has created
/// </summary>
public int SmartFilterCreatedCount { get; set; }
/// <summary>
/// Is the user sharing reviews
/// </summary>
public bool IsSharingReviews { get; set; }
/// <summary>
/// The number of devices setup and their platforms
/// </summary>
public ICollection<DevicePlatform> DevicePlatforms { get; set; }
/// <summary>
/// Roles for this user
/// </summary>
public ICollection<string> Roles { get; set; }
}

View File

@ -39,7 +39,7 @@ public interface ILibraryRepository
Task<bool> LibraryExists(string libraryName); Task<bool> LibraryExists(string libraryName);
Task<Library?> GetLibraryForIdAsync(int libraryId, LibraryIncludes includes = LibraryIncludes.None); Task<Library?> GetLibraryForIdAsync(int libraryId, LibraryIncludes includes = LibraryIncludes.None);
IEnumerable<LibraryDto> GetLibraryDtosForUsernameAsync(string userName); IEnumerable<LibraryDto> GetLibraryDtosForUsernameAsync(string userName);
Task<IEnumerable<Library>> GetLibrariesAsync(LibraryIncludes includes = LibraryIncludes.None); Task<IEnumerable<Library>> GetLibrariesAsync(LibraryIncludes includes = LibraryIncludes.None, bool track = true);
Task<IEnumerable<Library>> GetLibrariesForUserIdAsync(int userId); Task<IEnumerable<Library>> GetLibrariesForUserIdAsync(int userId);
IEnumerable<int> GetLibraryIdsForUserIdAsync(int userId, QueryContext queryContext = QueryContext.None); IEnumerable<int> GetLibraryIdsForUserIdAsync(int userId, QueryContext queryContext = QueryContext.None);
Task<LibraryType> GetLibraryTypeAsync(int libraryId); Task<LibraryType> GetLibraryTypeAsync(int libraryId);
@ -104,13 +104,16 @@ public class LibraryRepository : ILibraryRepository
/// </summary> /// </summary>
/// <param name="includes"></param> /// <param name="includes"></param>
/// <returns></returns> /// <returns></returns>
public async Task<IEnumerable<Library>> GetLibrariesAsync(LibraryIncludes includes = LibraryIncludes.None) public async Task<IEnumerable<Library>> GetLibrariesAsync(LibraryIncludes includes = LibraryIncludes.None, bool track = true)
{ {
return await _context.Library var query = _context.Library
.Include(l => l.AppUsers) .Include(l => l.AppUsers)
.Includes(includes) .Includes(includes)
.AsSplitQuery() .AsSplitQuery();
.ToListAsync();
if (track) return await query.ToListAsync();
return await query.AsNoTracking().ToListAsync();
} }
/// <summary> /// <summary>

View File

@ -164,6 +164,7 @@ public interface ISeriesRepository
Task ClearOnDeckRemoval(int seriesId, int userId); Task ClearOnDeckRemoval(int seriesId, int userId);
Task<PagedList<SeriesDto>> GetSeriesDtoForLibraryIdV2Async(int userId, UserParams userParams, FilterV2Dto filterDto, QueryContext queryContext = QueryContext.None); Task<PagedList<SeriesDto>> GetSeriesDtoForLibraryIdV2Async(int userId, UserParams userParams, FilterV2Dto filterDto, QueryContext queryContext = QueryContext.None);
Task<PlusSeriesDto?> GetPlusSeriesDto(int seriesId); Task<PlusSeriesDto?> GetPlusSeriesDto(int seriesId);
Task<int> GetCountAsync();
} }
public class SeriesRepository : ISeriesRepository public class SeriesRepository : ISeriesRepository
@ -726,6 +727,10 @@ public class SeriesRepository : ISeriesRepository
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
} }
public async Task<int> GetCountAsync()
{
return await _context.Series.CountAsync();
}
public async Task AddSeriesModifiers(int userId, IList<SeriesDto> series) public async Task AddSeriesModifiers(int userId, IList<SeriesDto> series)
{ {

View File

@ -78,7 +78,7 @@ public interface IUserRepository
Task<IEnumerable<AppUserPreferences>> GetAllPreferencesByThemeAsync(int themeId); Task<IEnumerable<AppUserPreferences>> GetAllPreferencesByThemeAsync(int themeId);
Task<bool> HasAccessToLibrary(int libraryId, int userId); Task<bool> HasAccessToLibrary(int libraryId, int userId);
Task<bool> HasAccessToSeries(int userId, int seriesId); Task<bool> HasAccessToSeries(int userId, int seriesId);
Task<IEnumerable<AppUser>> GetAllUsersAsync(AppUserIncludes includeFlags = AppUserIncludes.None); Task<IEnumerable<AppUser>> GetAllUsersAsync(AppUserIncludes includeFlags = AppUserIncludes.None, bool track = true);
Task<AppUser?> GetUserByConfirmationToken(string token); Task<AppUser?> GetUserByConfirmationToken(string token);
Task<AppUser> GetDefaultAdminUser(AppUserIncludes includes = AppUserIncludes.None); Task<AppUser> GetDefaultAdminUser(AppUserIncludes includes = AppUserIncludes.None);
Task<IEnumerable<AppUserRating>> GetSeriesWithRatings(int userId); Task<IEnumerable<AppUserRating>> GetSeriesWithRatings(int userId);
@ -283,10 +283,17 @@ public class UserRepository : IUserRepository
.AnyAsync(s => s.Id == seriesId); .AnyAsync(s => s.Id == seriesId);
} }
public async Task<IEnumerable<AppUser>> GetAllUsersAsync(AppUserIncludes includeFlags = AppUserIncludes.None) public async Task<IEnumerable<AppUser>> GetAllUsersAsync(AppUserIncludes includeFlags = AppUserIncludes.None, bool track = true)
{ {
return await _context.AppUser var query = _context.AppUser
.Includes(includeFlags) .Includes(includeFlags);
if (track)
{
return await query.ToListAsync();
}
return await query
.AsNoTracking()
.ToListAsync(); .ToListAsync();
} }

View File

@ -15,5 +15,4 @@ public enum FileTypeGroup
Pdf = 3, Pdf = 3,
[Description("Images")] [Description("Images")]
Images = 4 Images = 4
} }

View File

@ -44,9 +44,6 @@ public class Library : IEntityDate, IHasCoverImage
public bool AllowScrobbling { get; set; } = true; public bool AllowScrobbling { get; set; } = true;
public DateTime Created { get; set; } public DateTime Created { get; set; }
public DateTime LastModified { get; set; } public DateTime LastModified { get; set; }
public DateTime CreatedUtc { get; set; } public DateTime CreatedUtc { get; set; }

View File

@ -1,19 +1,24 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.Diagnostics;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Data; using API.Data;
using API.Data.Misc;
using API.Data.Repositories; using API.Data.Repositories;
using API.DTOs.Stats; using API.DTOs.Stats;
using API.DTOs.Stats.V3;
using API.Entities;
using API.Entities.Enums; using API.Entities.Enums;
using API.Entities.Enums.UserPreferences; using API.Services.Plus;
using Flurl.Http; using Flurl.Http;
using Kavita.Common.EnvironmentInfo; using Kavita.Common.EnvironmentInfo;
using Kavita.Common.Helpers; using Kavita.Common.Helpers;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -24,7 +29,6 @@ namespace API.Services.Tasks;
public interface IStatsService public interface IStatsService
{ {
Task Send(); Task Send();
Task<ServerInfoDto> GetServerInfo();
Task<ServerInfoSlimDto> GetServerInfoSlim(); Task<ServerInfoSlimDto> GetServerInfoSlim();
Task SendCancellation(); Task SendCancellation();
} }
@ -36,15 +40,24 @@ public class StatsService : IStatsService
private readonly ILogger<StatsService> _logger; private readonly ILogger<StatsService> _logger;
private readonly IUnitOfWork _unitOfWork; private readonly IUnitOfWork _unitOfWork;
private readonly DataContext _context; private readonly DataContext _context;
private readonly IStatisticService _statisticService; private readonly ILicenseService _licenseService;
private readonly UserManager<AppUser> _userManager;
private readonly IEmailService _emailService;
private readonly ICacheService _cacheService;
private const string ApiUrl = "https://stats.kavitareader.com"; private const string ApiUrl = "https://stats.kavitareader.com";
private const string ApiKey = "MsnvA2DfQqxSK5jh"; // It's not important this is public, just a way to keep bots from hitting the API willy-nilly
public StatsService(ILogger<StatsService> logger, IUnitOfWork unitOfWork, DataContext context, IStatisticService statisticService) public StatsService(ILogger<StatsService> logger, IUnitOfWork unitOfWork, DataContext context,
ILicenseService licenseService, UserManager<AppUser> userManager, IEmailService emailService,
ICacheService cacheService)
{ {
_logger = logger; _logger = logger;
_unitOfWork = unitOfWork; _unitOfWork = unitOfWork;
_context = context; _context = context;
_statisticService = statisticService; _licenseService = licenseService;
_userManager = userManager;
_emailService = emailService;
_cacheService = cacheService;
FlurlHttp.ConfigureClient(ApiUrl, cli => FlurlHttp.ConfigureClient(ApiUrl, cli =>
cli.Settings.HttpClientFactory = new UntrustedCertClientFactory()); cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
@ -52,7 +65,7 @@ public class StatsService : IStatsService
/// <summary> /// <summary>
/// Due to all instances firing this at the same time, we can DDOS our server. This task when fired will schedule the task to be run /// Due to all instances firing this at the same time, we can DDOS our server. This task when fired will schedule the task to be run
/// randomly over a 6 hour spread /// randomly over a six-hour spread
/// </summary> /// </summary>
public async Task Send() public async Task Send()
{ {
@ -71,21 +84,24 @@ public class StatsService : IStatsService
// ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once MemberCanBePrivate.Global
public async Task SendData() public async Task SendData()
{ {
var data = await GetServerInfo(); var sw = Stopwatch.StartNew();
var data = await GetStatV3Payload();
_logger.LogDebug("Collecting stats took {Time} ms", sw.ElapsedMilliseconds);
sw.Stop();
await SendDataToStatsServer(data); await SendDataToStatsServer(data);
} }
private async Task SendDataToStatsServer(ServerInfoDto data) private async Task SendDataToStatsServer(ServerInfoV3Dto data)
{ {
var responseContent = string.Empty; var responseContent = string.Empty;
try try
{ {
var response = await (ApiUrl + "/api/v2/stats") var response = await (ApiUrl + "/api/v3/stats")
.WithHeader("Accept", "application/json") .WithHeader("Accept", "application/json")
.WithHeader("User-Agent", "Kavita") .WithHeader("User-Agent", "Kavita")
.WithHeader("x-api-key", "MsnvA2DfQqxSK5jh") .WithHeader("x-api-key", ApiKey)
.WithHeader("x-kavita-version", BuildInfo.Version) .WithHeader("x-kavita-version", BuildInfo.Version)
.WithHeader("Content-Type", "application/json") .WithHeader("Content-Type", "application/json")
.WithTimeout(TimeSpan.FromSeconds(30)) .WithTimeout(TimeSpan.FromSeconds(30))
@ -112,67 +128,6 @@ public class StatsService : IStatsService
} }
} }
public async Task<ServerInfoDto> GetServerInfo()
{
var serverSettings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
var serverInfo = new ServerInfoDto
{
InstallId = serverSettings.InstallId,
Os = RuntimeInformation.OSDescription,
KavitaVersion = serverSettings.InstallVersion,
DotnetVersion = Environment.Version.ToString(),
IsDocker = OsInfo.IsDocker,
NumOfCores = Math.Max(Environment.ProcessorCount, 1),
UsersWithEmulateComicBook = await _context.AppUserPreferences.CountAsync(p => p.EmulateBook),
TotalReadingHours = await _statisticService.TimeSpentReadingForUsersAsync(ArraySegment<int>.Empty, ArraySegment<int>.Empty),
PercentOfLibrariesWithFolderWatchingEnabled = await GetPercentageOfLibrariesWithFolderWatchingEnabled(),
PercentOfLibrariesIncludedInRecommended = await GetPercentageOfLibrariesIncludedInRecommended(),
PercentOfLibrariesIncludedInDashboard = await GetPercentageOfLibrariesIncludedInDashboard(),
PercentOfLibrariesIncludedInSearch = await GetPercentageOfLibrariesIncludedInSearch(),
HasBookmarks = (await _unitOfWork.UserRepository.GetAllBookmarksAsync()).Any(),
NumberOfLibraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).Count(),
NumberOfCollections = (await _unitOfWork.CollectionTagRepository.GetAllCollectionsAsync()).Count(),
NumberOfReadingLists = await _unitOfWork.ReadingListRepository.Count(),
OPDSEnabled = serverSettings.EnableOpds,
NumberOfUsers = (await _unitOfWork.UserRepository.GetAllUsersAsync()).Count(),
TotalFiles = await _unitOfWork.LibraryRepository.GetTotalFiles(),
TotalGenres = await _unitOfWork.GenreRepository.GetCountAsync(),
TotalPeople = await _unitOfWork.PersonRepository.GetCountAsync(),
UsingSeriesRelationships = await GetIfUsingSeriesRelationship(),
EncodeMediaAs = serverSettings.EncodeMediaAs,
MaxSeriesInALibrary = await MaxSeriesInAnyLibrary(),
MaxVolumesInASeries = await MaxVolumesInASeries(),
MaxChaptersInASeries = await MaxChaptersInASeries(),
MangaReaderBackgroundColors = await AllMangaReaderBackgroundColors(),
MangaReaderPageSplittingModes = await AllMangaReaderPageSplitting(),
MangaReaderLayoutModes = await AllMangaReaderLayoutModes(),
FileFormats = AllFormats(),
UsingRestrictedProfiles = await GetUsingRestrictedProfiles(),
LastReadTime = await _unitOfWork.AppUserProgressRepository.GetLatestProgress()
};
var usersWithPref = (await _unitOfWork.UserRepository.GetAllUsersAsync(AppUserIncludes.UserPreferences)).ToList();
serverInfo.UsersOnCardLayout =
usersWithPref.Count(u => u.UserPreferences.GlobalPageLayoutMode == PageLayoutMode.Cards);
serverInfo.UsersOnListLayout =
usersWithPref.Count(u => u.UserPreferences.GlobalPageLayoutMode == PageLayoutMode.List);
var firstAdminUser = (await _unitOfWork.UserRepository.GetAdminUsersAsync()).FirstOrDefault();
if (firstAdminUser != null)
{
var firstAdminUserPref = (await _unitOfWork.UserRepository.GetPreferencesAsync(firstAdminUser.UserName!));
var activeTheme = firstAdminUserPref?.Theme ?? Seed.DefaultThemes.First(t => t.IsDefault);
serverInfo.ActiveSiteTheme = activeTheme.Name;
if (firstAdminUserPref != null) serverInfo.MangaReaderMode = firstAdminUserPref.ReaderMode;
}
return serverInfo;
}
public async Task<ServerInfoSlimDto> GetServerInfoSlim() public async Task<ServerInfoSlimDto> GetServerInfoSlim()
{ {
@ -199,7 +154,7 @@ public class StatsService : IStatsService
var response = await (ApiUrl + "/api/v2/stats/opt-out?installId=" + installId) var response = await (ApiUrl + "/api/v2/stats/opt-out?installId=" + installId)
.WithHeader("Accept", "application/json") .WithHeader("Accept", "application/json")
.WithHeader("User-Agent", "Kavita") .WithHeader("User-Agent", "Kavita")
.WithHeader("x-api-key", "MsnvA2DfQqxSK5jh") .WithHeader("x-api-key", ApiKey)
.WithHeader("x-kavita-version", BuildInfo.Version) .WithHeader("x-kavita-version", BuildInfo.Version)
.WithHeader("Content-Type", "application/json") .WithHeader("Content-Type", "application/json")
.WithTimeout(TimeSpan.FromSeconds(30)) .WithTimeout(TimeSpan.FromSeconds(30))
@ -220,37 +175,32 @@ public class StatsService : IStatsService
} }
} }
private async Task<float> GetPercentageOfLibrariesWithFolderWatchingEnabled() private static async Task<long> PingStatsApi()
{ {
var libraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).ToList(); try
if (libraries.Count == 0) return 0.0f; {
return libraries.Count(l => l.FolderWatching) / (1.0f * libraries.Count); var sw = Stopwatch.StartNew();
} var response = await (ApiUrl + "/api/health/")
.WithHeader("Accept", "application/json")
.WithHeader("User-Agent", "Kavita")
.WithHeader("x-api-key", ApiKey)
.WithHeader("x-kavita-version", BuildInfo.Version)
.WithHeader("Content-Type", "application/json")
.WithTimeout(TimeSpan.FromSeconds(30))
.GetAsync();
private async Task<float> GetPercentageOfLibrariesIncludedInRecommended() if (response.StatusCode == StatusCodes.Status200OK)
{ {
var libraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).ToList(); sw.Stop();
if (libraries.Count == 0) return 0.0f; return sw.ElapsedMilliseconds;
return libraries.Count(l => l.IncludeInRecommended) / (1.0f * libraries.Count); }
} }
catch (Exception)
{
/* Swallow */
}
private async Task<float> GetPercentageOfLibrariesIncludedInDashboard() return 0;
{
var libraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).ToList();
if (libraries.Count == 0) return 0.0f;
return libraries.Count(l => l.IncludeInDashboard) / (1.0f * libraries.Count);
}
private async Task<float> GetPercentageOfLibrariesIncludedInSearch()
{
var libraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).ToList();
if (libraries.Count == 0) return 0.0f;
return libraries.Count(l => l.IncludeInSearch) / (1.0f * libraries.Count);
}
private Task<bool> GetIfUsingSeriesRelationship()
{
return _context.SeriesRelation.AnyAsync();
} }
private async Task<int> MaxSeriesInAnyLibrary() private async Task<int> MaxSeriesInAnyLibrary()
@ -290,41 +240,178 @@ public class StatsService : IStatsService
.Count()); .Count());
} }
private async Task<IEnumerable<string>> AllMangaReaderBackgroundColors() private async Task<ServerInfoV3Dto> GetStatV3Payload()
{ {
return await _context.AppUserPreferences.Select(p => p.BackgroundColor).Distinct().ToListAsync(); var serverSettings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
} var dto = new ServerInfoV3Dto()
{
InstallId = serverSettings.InstallId,
KavitaVersion = serverSettings.InstallVersion,
InitialKavitaVersion = serverSettings.FirstInstallVersion,
InitialInstallDate = (DateTime)serverSettings.FirstInstallDate!,
IsDocker = OsInfo.IsDocker,
Os = RuntimeInformation.OSDescription,
NumOfCores = Math.Max(Environment.ProcessorCount, 1),
DotnetVersion = Environment.Version.ToString(),
OpdsEnabled = serverSettings.EnableOpds,
EncodeMediaAs = serverSettings.EncodeMediaAs,
};
private async Task<IEnumerable<PageSplitOption>> AllMangaReaderPageSplitting() dto.OsLocale = CultureInfo.CurrentCulture.EnglishName;
{ dto.LastReadTime = await _unitOfWork.AppUserProgressRepository.GetLatestProgress();
return await _context.AppUserPreferences.Select(p => p.PageSplitOption).Distinct().ToListAsync(); dto.MaxSeriesInALibrary = await MaxSeriesInAnyLibrary();
} dto.MaxVolumesInASeries = await MaxVolumesInASeries();
dto.MaxChaptersInASeries = await MaxChaptersInASeries();
dto.TotalFiles = await _unitOfWork.LibraryRepository.GetTotalFiles();
dto.TotalGenres = await _unitOfWork.GenreRepository.GetCountAsync();
dto.TotalPeople = await _unitOfWork.PersonRepository.GetCountAsync();
dto.TotalSeries = await _unitOfWork.SeriesRepository.GetCountAsync();
dto.TotalLibraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync()).Count();
dto.NumberOfCollections = (await _unitOfWork.CollectionTagRepository.GetAllCollectionsAsync()).Count();
dto.NumberOfReadingLists = await _unitOfWork.ReadingListRepository.Count();
try
{
var license = (await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey)).Value;
dto.ActiveKavitaPlusSubscription = await _licenseService.HasActiveSubscription(license);
}
catch (Exception)
{
dto.ActiveKavitaPlusSubscription = false;
}
private async Task<IEnumerable<LayoutMode>> AllMangaReaderLayoutModes() // Find a random cbz/zip file and open it for reading
{ await OpenRandomFile(dto);
return await _context.AppUserPreferences.Select(p => p.LayoutMode).Distinct().ToListAsync(); dto.TimeToPingKavitaStatsApi = await PingStatsApi();
}
private IEnumerable<FileFormatDto> AllFormats() #region Relationships
{
var results = _context.MangaFile dto.Relationships = await _context.SeriesRelation
.AsNoTracking() .GroupBy(sr => sr.RelationKind)
.AsEnumerable() .Select(g => new RelationshipStatV3
.Select(m => new FileFormatDto()
{ {
Format = m.Format, Relationship = g.Key,
Extension = m.Extension Count = g.Count()
}) })
.DistinctBy(f => f.Extension) .ToListAsync();
.ToList();
return results; #endregion
#region Libraries
var allLibraries = (await _unitOfWork.LibraryRepository.GetLibrariesAsync(LibraryIncludes.Folders |
LibraryIncludes.FileTypes | LibraryIncludes.ExcludePatterns | LibraryIncludes.AppUser)).ToList();
dto.Libraries ??= [];
foreach (var library in allLibraries)
{
var libDto = new LibraryStatV3();
libDto.IncludeInDashboard = library.IncludeInDashboard;
libDto.IncludeInSearch = library.IncludeInSearch;
libDto.LastScanned = library.LastScanned;
libDto.NumberOfFolders = library.Folders.Count;
libDto.FileTypes = library.LibraryFileTypes.Select(s => s.FileTypeGroup).Distinct().ToList();
libDto.UsingExcludePatterns = library.LibraryExcludePatterns.Any(p => !string.IsNullOrEmpty(p.Pattern));
libDto.UsingFolderWatching = library.FolderWatching;
libDto.CreateCollectionsFromMetadata = library.ManageCollections;
libDto.CreateReadingListsFromMetadata = library.ManageReadingLists;
dto.Libraries.Add(libDto);
}
#endregion
#region Users
// Create a dictionary mapping user IDs to the libraries they have access to
var userLibraryAccess = allLibraries
.SelectMany(l => l.AppUsers.Select(appUser => new { l, appUser.Id }))
.GroupBy(x => x.Id)
.ToDictionary(g => g.Key, g => g.Select(x => x.l).ToList());
dto.Users ??= [];
var allUsers = await _unitOfWork.UserRepository.GetAllUsersAsync(AppUserIncludes.UserPreferences
| AppUserIncludes.ReadingLists | AppUserIncludes.Bookmarks
| AppUserIncludes.Collections | AppUserIncludes.Devices
| AppUserIncludes.Progress | AppUserIncludes.Ratings
| AppUserIncludes.SmartFilters | AppUserIncludes.WantToRead, false);
foreach (var user in allUsers)
{
var userDto = new UserStatV3();
userDto.HasMALToken = !string.IsNullOrEmpty(user.MalAccessToken);
userDto.HasAniListToken = !string.IsNullOrEmpty(user.AniListAccessToken);
userDto.AgeRestriction = new AgeRestriction()
{
AgeRating = user.AgeRestriction,
IncludeUnknowns = user.AgeRestrictionIncludeUnknowns
};
userDto.Locale = user.UserPreferences.Locale;
userDto.Roles = [.. _userManager.GetRolesAsync(user).Result];
userDto.LastLogin = user.LastActiveUtc;
userDto.HasValidEmail = user.Email != null && _emailService.IsValidEmail(user.Email);
userDto.IsEmailConfirmed = user.EmailConfirmed;
userDto.ActiveTheme = user.UserPreferences.Theme.Name;
userDto.CollectionsCreatedCount = user.Collections.Count;
userDto.ReadingListsCreatedCount = user.ReadingLists.Count;
userDto.LastReadTime = user.Progresses
.Select(p => p.LastModifiedUtc)
.DefaultIfEmpty()
.Max();
userDto.DevicePlatforms = user.Devices.Select(d => d.Platform).ToList();
userDto.SeriesBookmarksCreatedCount = user.Bookmarks.Count;
userDto.SmartFilterCreatedCount = user.SmartFilters.Count;
userDto.WantToReadSeriesCount = user.WantToRead.Count;
if (allLibraries.Count > 0 && userLibraryAccess.TryGetValue(user.Id, out var accessibleLibraries))
{
userDto.PercentageOfLibrariesHasAccess = (1f * accessibleLibraries.Count) / allLibraries.Count;
}
else
{
userDto.PercentageOfLibrariesHasAccess = 0;
}
dto.Users.Add(userDto);
}
#endregion
return dto;
} }
private Task<bool> GetUsingRestrictedProfiles() private async Task OpenRandomFile(ServerInfoV3Dto dto)
{ {
return _context.Users.AnyAsync(u => u.AgeRestriction > AgeRating.NotApplicable); var random = new Random();
List<string> extensions = [".cbz", ".zip"];
// Count the total number of files that match the criteria
var count = await _context.MangaFile.AsNoTracking()
.Where(r => r.Extension != null && extensions.Contains(r.Extension))
.CountAsync();
if (count == 0)
{
dto.TimeToOpeCbzMs = 0;
dto.TimeToOpenCbzPages = 0;
return;
}
// Generate a random skip value
var skip = random.Next(count);
// Fetch the random file
var randomFile = await _context.MangaFile.AsNoTracking()
.Where(r => r.Extension != null && extensions.Contains(r.Extension))
.Skip(skip)
.Take(1)
.FirstAsync();
var sw = Stopwatch.StartNew();
await _cacheService.Ensure(randomFile.ChapterId);
var time = sw.ElapsedMilliseconds;
sw.Stop();
dto.TimeToOpeCbzMs = time;
dto.TimeToOpenCbzPages = randomFile.Pages;
} }
} }