Stats Rework (#765)
* Fixed a duplicate check for updates. Changed checking from weekly to daily. * Refactored how dark variables were accessed to reduce size of component css. Refactored Stats code to use lesser information for reporting. * Use the installId from the database which is most unlikely to change. * Fixed a missing interface with stat service * Added DotnetVersion back into collection * Updated url to new host.
This commit is contained in:
parent
b2831c7606
commit
1ada34984f
17 changed files with 44 additions and 349 deletions
|
@ -1,44 +1,29 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.DTOs.Stats;
|
||||
using API.Entities.Enums;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
using Flurl.Http;
|
||||
using Hangfire;
|
||||
using Kavita.Common;
|
||||
using Kavita.Common.EnvironmentInfo;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Services.Tasks
|
||||
{
|
||||
public class StatsService : IStatsService
|
||||
{
|
||||
private const string StatFileName = "app_stats.json";
|
||||
|
||||
private readonly DataContext _dbContext;
|
||||
private readonly ILogger<StatsService> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
#pragma warning disable S1075
|
||||
private const string ApiUrl = "http://stats.kavitareader.com";
|
||||
private const string ApiUrl = "http://stats2.kavitareader.com";
|
||||
#pragma warning restore S1075
|
||||
private static readonly string StatsFilePath = Path.Combine(DirectoryService.StatsDirectory, StatFileName);
|
||||
|
||||
private static bool FileExists => File.Exists(StatsFilePath);
|
||||
|
||||
public StatsService(DataContext dbContext, ILogger<StatsService> logger,
|
||||
IUnitOfWork unitOfWork)
|
||||
public StatsService(ILogger<StatsService> logger, IUnitOfWork unitOfWork)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
@ -55,17 +40,7 @@ namespace API.Services.Tasks
|
|||
return;
|
||||
}
|
||||
|
||||
var rnd = new Random();
|
||||
var offset = rnd.Next(0, 6);
|
||||
if (offset == 0)
|
||||
{
|
||||
await SendData();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("KavitaStats upload has been schedule to run in {Offset} hours", offset);
|
||||
BackgroundJob.Schedule(() => SendData(), DateTimeOffset.Now.AddHours(offset));
|
||||
}
|
||||
await SendData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -74,55 +49,21 @@ namespace API.Services.Tasks
|
|||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
public async Task SendData()
|
||||
{
|
||||
await CollectRelevantData();
|
||||
await FinalizeStats();
|
||||
var data = await GetServerInfo();
|
||||
await SendDataToStatsServer(data);
|
||||
}
|
||||
|
||||
public async Task RecordClientInfo(ClientInfoDto clientInfoDto)
|
||||
{
|
||||
var statisticsDto = await GetData();
|
||||
statisticsDto.AddClientInfo(clientInfoDto);
|
||||
|
||||
await SaveFile(statisticsDto);
|
||||
}
|
||||
|
||||
private async Task CollectRelevantData()
|
||||
{
|
||||
var usageInfo = await GetUsageInfo();
|
||||
var serverInfo = GetServerInfo();
|
||||
|
||||
await PathData(serverInfo, usageInfo);
|
||||
}
|
||||
|
||||
private async Task FinalizeStats()
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await GetExistingData<UsageStatisticsDto>();
|
||||
var successful = await SendDataToStatsServer(data);
|
||||
|
||||
if (successful)
|
||||
{
|
||||
ResetStats();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an exception while sending data to KavitaStats");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> SendDataToStatsServer(UsageStatisticsDto data)
|
||||
private async Task SendDataToStatsServer(ServerInfoDto data)
|
||||
{
|
||||
var responseContent = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var response = await (ApiUrl + "/api/InstallationStats")
|
||||
var response = await (ApiUrl + "/api/v2/stats")
|
||||
.WithHeader("Accept", "application/json")
|
||||
.WithHeader("User-Agent", "Kavita")
|
||||
.WithHeader("x-api-key", "MsnvA2DfQqxSK5jh")
|
||||
.WithHeader("api-key", "MsnvA2DfQqxSK5jh")
|
||||
.WithHeader("x-kavita-version", BuildInfo.Version)
|
||||
.WithTimeout(TimeSpan.FromSeconds(30))
|
||||
.PostJsonAsync(data);
|
||||
|
@ -130,10 +71,7 @@ namespace API.Services.Tasks
|
|||
if (response.StatusCode != StatusCodes.Status200OK)
|
||||
{
|
||||
_logger.LogError("KavitaStats did not respond successfully. {Content}", response);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
|
@ -149,84 +87,22 @@ namespace API.Services.Tasks
|
|||
{
|
||||
_logger.LogError(e, "An error happened during the request to KavitaStats");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void ResetStats()
|
||||
{
|
||||
if (FileExists) File.Delete(StatsFilePath);
|
||||
}
|
||||
|
||||
private async Task PathData(ServerInfoDto serverInfoDto, UsageInfoDto usageInfoDto)
|
||||
{
|
||||
var data = await GetData();
|
||||
|
||||
data.ServerInfo = serverInfoDto;
|
||||
data.UsageInfo = usageInfoDto;
|
||||
|
||||
data.MarkAsUpdatedNow();
|
||||
|
||||
await SaveFile(data);
|
||||
}
|
||||
|
||||
private static async ValueTask<UsageStatisticsDto> GetData()
|
||||
{
|
||||
if (!FileExists) return new UsageStatisticsDto {InstallId = HashUtil.AnonymousToken()};
|
||||
|
||||
return await GetExistingData<UsageStatisticsDto>();
|
||||
}
|
||||
|
||||
private async Task<UsageInfoDto> GetUsageInfo()
|
||||
{
|
||||
var usersCount = await _dbContext.Users.CountAsync();
|
||||
|
||||
var libsCountByType = await _dbContext.Library
|
||||
.AsNoTracking()
|
||||
.GroupBy(x => x.Type)
|
||||
.Select(x => new LibInfo {Type = x.Key, Count = x.Count()})
|
||||
.ToArrayAsync();
|
||||
|
||||
var uniqueFileTypes = await _unitOfWork.FileRepository.GetFileExtensions();
|
||||
|
||||
var usageInfo = new UsageInfoDto
|
||||
{
|
||||
UsersCount = usersCount,
|
||||
LibraryTypesCreated = libsCountByType,
|
||||
FileTypes = uniqueFileTypes
|
||||
};
|
||||
|
||||
return usageInfo;
|
||||
}
|
||||
|
||||
public static ServerInfoDto GetServerInfo()
|
||||
public async Task<ServerInfoDto> GetServerInfo()
|
||||
{
|
||||
var installId = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.InstallId);
|
||||
var serverInfo = new ServerInfoDto
|
||||
{
|
||||
InstallId = installId.Value,
|
||||
Os = RuntimeInformation.OSDescription,
|
||||
DotNetVersion = Environment.Version.ToString(),
|
||||
RunTimeVersion = RuntimeInformation.FrameworkDescription,
|
||||
KavitaVersion = BuildInfo.Version.ToString(),
|
||||
Culture = Thread.CurrentThread.CurrentCulture.Name,
|
||||
BuildBranch = BuildInfo.Branch,
|
||||
DotnetVersion = Environment.Version.ToString(),
|
||||
IsDocker = new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker,
|
||||
NumOfCores = Environment.ProcessorCount
|
||||
NumOfCores = Math.Max(Environment.ProcessorCount, 1)
|
||||
};
|
||||
|
||||
return serverInfo;
|
||||
}
|
||||
|
||||
private static async Task<T> GetExistingData<T>()
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(StatsFilePath);
|
||||
return JsonSerializer.Deserialize<T>(json);
|
||||
}
|
||||
|
||||
private static async Task SaveFile(UsageStatisticsDto statisticsDto)
|
||||
{
|
||||
DirectoryService.ExistOrCreate(DirectoryService.StatsDirectory);
|
||||
|
||||
await File.WriteAllTextAsync(StatsFilePath, JsonSerializer.Serialize(statisticsDto));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue