mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-01 12:44:44 -04:00
* Refactored all files to have Interfaces within the same file. Started moving over to file-scoped namespaces. * Refactored common methods for getting underlying file's cover, pages, and extracting into 1 interface. * More refactoring around removing dependence on explicit filetype testing for getting information. * Code is buildable, tests are broken. Huge refactor (not completed) which makes most of DirectoryService testable with a mock filesystem (and thus the services that utilize it). * Finished porting DirectoryService to use mocked filesystem implementation. * Added a null check * Added a null check * Finished all unit tests for DirectoryService. * Some misc cleanup on the code * Fixed up some bugs from refactoring scan loop. * Implemented CleanupService testing and refactored more of DirectoryService to be non-static. Fixed a bug where cover file cleanup wasn't properly finding files due to a regex bug. * Fixed an issue in CleanupBackup() where we weren't properly selecting database files older than 30 days. Finished CleanupService Tests. * Refactored Flatten and RemoveNonImages to directory service to allow CacheService to be testable. * Finally have CacheService tested. Rewrote GetCachedPagePath() to be much more straightforward & performant. * Updated DefaultParserTests.cs to contain all existing tests and follow new test layout format. * All tests fixed up
113 lines
3.4 KiB
C#
113 lines
3.4 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.DTOs.Stats;
|
|
using API.Entities.Enums;
|
|
using Flurl.Http;
|
|
using Kavita.Common.EnvironmentInfo;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Services.Tasks;
|
|
|
|
public interface IStatsService
|
|
{
|
|
Task Send();
|
|
Task<ServerInfoDto> GetServerInfo();
|
|
}
|
|
public class StatsService : IStatsService
|
|
{
|
|
private readonly ILogger<StatsService> _logger;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private const string ApiUrl = "https://stats.kavitareader.com";
|
|
|
|
public StatsService(ILogger<StatsService> logger, IUnitOfWork unitOfWork)
|
|
{
|
|
_logger = logger;
|
|
_unitOfWork = unitOfWork;
|
|
|
|
FlurlHttp.ConfigureClient(ApiUrl, cli =>
|
|
cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
|
|
}
|
|
|
|
/// <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
|
|
/// randomly over a 6 hour spread
|
|
/// </summary>
|
|
public async Task Send()
|
|
{
|
|
var allowStatCollection = (await _unitOfWork.SettingsRepository.GetSettingsDtoAsync()).AllowStatCollection;
|
|
if (!allowStatCollection)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await SendData();
|
|
}
|
|
|
|
/// <summary>
|
|
/// This must be public for Hangfire. Do not call this directly.
|
|
/// </summary>
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
public async Task SendData()
|
|
{
|
|
var data = await GetServerInfo();
|
|
await SendDataToStatsServer(data);
|
|
}
|
|
|
|
|
|
private async Task SendDataToStatsServer(ServerInfoDto data)
|
|
{
|
|
var responseContent = string.Empty;
|
|
|
|
try
|
|
{
|
|
var response = await (ApiUrl + "/api/v2/stats")
|
|
.WithHeader("Accept", "application/json")
|
|
.WithHeader("User-Agent", "Kavita")
|
|
.WithHeader("x-api-key", "MsnvA2DfQqxSK5jh")
|
|
.WithHeader("x-kavita-version", BuildInfo.Version)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithTimeout(TimeSpan.FromSeconds(30))
|
|
.PostJsonAsync(data);
|
|
|
|
if (response.StatusCode != StatusCodes.Status200OK)
|
|
{
|
|
_logger.LogError("KavitaStats did not respond successfully. {Content}", response);
|
|
}
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
var info = new
|
|
{
|
|
dataSent = data,
|
|
response = responseContent
|
|
};
|
|
|
|
_logger.LogError(e, "KavitaStats did not respond successfully. {Content}", info);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "An error happened during the request to KavitaStats");
|
|
}
|
|
}
|
|
|
|
public async Task<ServerInfoDto> GetServerInfo()
|
|
{
|
|
var installId = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.InstallId);
|
|
var serverInfo = new ServerInfoDto
|
|
{
|
|
InstallId = installId.Value,
|
|
Os = RuntimeInformation.OSDescription,
|
|
KavitaVersion = BuildInfo.Version.ToString(),
|
|
DotnetVersion = Environment.Version.ToString(),
|
|
IsDocker = new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker,
|
|
NumOfCores = Math.Max(Environment.ProcessorCount, 1)
|
|
};
|
|
|
|
return serverInfo;
|
|
}
|
|
}
|