* rework server responsibilities add remote configuration * start metrics only when compiled as not debug * add some more logging to discord bot * fixes of some casts * make metrics port configurable, minor fixes * add docker bullshit * md formatting * adjustments to docker stuff * fix docker json files, fix some stuff in discord bot, add /useradd for Discord bot * adjust docker configs and fix sharded.bat * fixes for logs, cache file provider repeat trying to open filestream Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
90 lines
3.2 KiB
C#
90 lines
3.2 KiB
C#
using MareSynchronosShared.Metrics;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace MareSynchronosStaticFilesServer;
|
|
|
|
public class FileStatisticsService : IHostedService
|
|
{
|
|
private readonly MareMetrics _metrics;
|
|
private readonly ILogger<FileStatisticsService> _logger;
|
|
private CancellationTokenSource _resetCancellationTokenSource;
|
|
private ConcurrentDictionary<string, long> _pastHourFiles = new(StringComparer.Ordinal);
|
|
private ConcurrentDictionary<string, long> _pastDayFiles = new(StringComparer.Ordinal);
|
|
|
|
public FileStatisticsService(MareMetrics metrics, ILogger<FileStatisticsService> logger)
|
|
{
|
|
_metrics = metrics;
|
|
_logger = logger;
|
|
}
|
|
|
|
public void LogFile(string fileHash, long length)
|
|
{
|
|
if (!_pastHourFiles.ContainsKey(fileHash))
|
|
{
|
|
_pastHourFiles[fileHash] = length;
|
|
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastHour);
|
|
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastHourSize, length);
|
|
}
|
|
if (!_pastDayFiles.ContainsKey(fileHash))
|
|
{
|
|
_pastDayFiles[fileHash] = length;
|
|
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastDay);
|
|
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastDaySize, length);
|
|
}
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("Starting FileStatisticsService");
|
|
_resetCancellationTokenSource = new();
|
|
_ = ResetHourlyFileData();
|
|
_ = ResetDailyFileData();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task ResetHourlyFileData()
|
|
{
|
|
while (!_resetCancellationTokenSource.Token.IsCancellationRequested)
|
|
{
|
|
_logger.LogInformation("Resetting 1h Data");
|
|
|
|
_pastHourFiles = new(StringComparer.Ordinal);
|
|
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastHour, 0);
|
|
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastHourSize, 0);
|
|
|
|
var now = DateTime.UtcNow;
|
|
TimeOnly currentTime = new(now.Hour, now.Minute, now.Second);
|
|
TimeOnly futureTime = new(now.Hour, 0, 0);
|
|
var span = futureTime.AddHours(1) - currentTime;
|
|
|
|
await Task.Delay(span, _resetCancellationTokenSource.Token).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
public async Task ResetDailyFileData()
|
|
{
|
|
while (!_resetCancellationTokenSource.Token.IsCancellationRequested)
|
|
{
|
|
_logger.LogInformation("Resetting 24h Data");
|
|
|
|
_pastDayFiles = new(StringComparer.Ordinal);
|
|
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastDay, 0);
|
|
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastDaySize, 0);
|
|
|
|
var now = DateTime.UtcNow;
|
|
TimeOnly currentTime = new(now.Hour, now.Minute, now.Second);
|
|
TimeOnly futureTime = new(0, 0, 0);
|
|
var span = futureTime - currentTime;
|
|
|
|
await Task.Delay(span, _resetCancellationTokenSource.Token).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_resetCancellationTokenSource.Cancel();
|
|
_logger.LogInformation("Stopping FileStatisticsService");
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|