CatStation/Content.Server/Administration/Logs/AdminLogManager.cs
Dmitry 25c5e59248
Upstream (#3172)
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com>
Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
Co-authored-by: Partmedia <kevinz5000@gmail.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: Stomf <5dorkydorks@gmail.com>
Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com>
Co-authored-by: PraxisMapper <praxismapper@gmail.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com>
Co-authored-by: abadaba695 <spacestation13thingy@gmail.com>
Co-authored-by: kosticia <kosticia46@gmail.com>
Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com>
Co-authored-by: pathetic meowmeow <uhhadd@gmail.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com>
Co-authored-by: Myra <vasilis@pikachu.systems>
Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: BIGZi0348 <svalker0348@gmail.com>
Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com>
Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com>
Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com>
Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com>
Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: eoineoineoin <helloworld@eoinrul.es>
Co-authored-by: Tiniest Shark <head.rebel@yahoo.com>
Co-authored-by: nikitosych <boriszyn@gmail.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com>
Co-authored-by: YoungThug <ramialanbagy@gmail.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com>
Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com>
Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com>
Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: J <billsmith116@gmail.com>
Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com>
Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: Minemoder5000 <minemoder50000@gmail.com>
Co-authored-by: Spanky <scott@wearejacob.com>
Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com>
Co-authored-by: Deerstop <edainturner@gmail.com>
Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com>
Co-authored-by: archee1 <archee3@hotmail.co.uk>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com>
Co-authored-by: poklj <compgeek223@gmail.com>
Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com>
Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com>
Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: ScarKy0 <scarky0@onet.eu>
Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
2025-05-24 11:00:17 +03:00

467 lines
14 KiB
C#

using System.Collections.Concurrent;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Systems;
using Content.Server.Database;
using Content.Server.GameTicking;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Content.Shared.Chat;
using Content.Shared.Database;
using Content.Shared.Players.PlayTimeTracking;
using Prometheus;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Reflection;
using Robust.Shared.Timing;
namespace Content.Server.Administration.Logs;
public sealed partial class AdminLogManager : SharedAdminLogManager, IAdminLogManager
{
[Dependency] private readonly IConfigurationManager _configuration = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IServerDbManager _db = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
[Dependency] private readonly IReflectionManager _reflection = default!;
[Dependency] private readonly IDependencyCollection _dependencies = default!;
[Dependency] private readonly ISharedPlayerManager _player = default!;
[Dependency] private readonly ISharedPlaytimeManager _playtime = default!;
[Dependency] private readonly ISharedChatManager _chat = default!;
public const string SawmillId = "admin.logs";
private static readonly Histogram DatabaseUpdateTime = Metrics.CreateHistogram(
"admin_logs_database_time",
"Time used to send logs to the database in ms",
new HistogramConfiguration
{
Buckets = Histogram.LinearBuckets(0, 0.5, 20)
});
private static readonly Gauge Queue = Metrics.CreateGauge(
"admin_logs_queue",
"How many logs are in the queue.");
private static readonly Gauge PreRoundQueue = Metrics.CreateGauge(
"admin_logs_pre_round_queue",
"How many logs are in the pre-round queue.");
private static readonly Gauge QueueCapReached = Metrics.CreateGauge(
"admin_logs_queue_cap_reached",
"Number of times the log queue cap has been reached in a round.");
private static readonly Gauge PreRoundQueueCapReached = Metrics.CreateGauge(
"admin_logs_queue_cap_reached",
"Number of times the pre-round log queue cap has been reached in a round.");
private static readonly Gauge LogsSent = Metrics.CreateGauge(
"admin_logs_sent",
"Amount of logs sent to the database in a round.");
// Init only
private ISawmill _sawmill = default!;
// CVars
private bool _metricsEnabled;
private bool _enabled;
private TimeSpan _queueSendDelay;
private int _queueMax;
private int _preRoundQueueMax;
private int _dropThreshold;
private int _highImpactLogPlaytime;
// Per update
private TimeSpan _nextUpdateTime;
private readonly ConcurrentQueue<AdminLog> _logQueue = new();
private readonly ConcurrentQueue<AdminLog> _preRoundLogQueue = new();
// Per round
private int _currentRoundId;
private int _currentLogId;
private int NextLogId => Interlocked.Increment(ref _currentLogId);
private GameRunLevel _runLevel = GameRunLevel.PreRoundLobby;
// 1 when saving, 0 otherwise
private int _savingLogs;
private int _logsDropped;
public void Initialize()
{
_sawmill = _logManager.GetSawmill(SawmillId);
InitializeJson();
_configuration.OnValueChanged(CVars.MetricsEnabled,
value => _metricsEnabled = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsEnabled,
value => _enabled = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueSendDelay,
value => _queueSendDelay = TimeSpan.FromSeconds(value), true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueMax,
value => _queueMax = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsPreRoundQueueMax,
value => _preRoundQueueMax = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsDropThreshold,
value => _dropThreshold = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsHighLogPlaytime,
value => _highImpactLogPlaytime = value, true);
if (_metricsEnabled)
{
PreRoundQueueCapReached.Set(0);
QueueCapReached.Set(0);
LogsSent.Set(0);
}
}
public async Task Shutdown()
{
if (!_logQueue.IsEmpty)
{
await SaveLogs();
}
}
public async void Update()
{
if (_runLevel == GameRunLevel.PreRoundLobby)
{
await PreRoundUpdate();
return;
}
var count = _logQueue.Count;
Queue.Set(count);
var preRoundCount = _preRoundLogQueue.Count;
PreRoundQueue.Set(preRoundCount);
if (count + preRoundCount == 0)
{
return;
}
if (_timing.RealTime >= _nextUpdateTime)
{
await TrySaveLogs();
return;
}
if (count >= _queueMax)
{
if (_metricsEnabled)
{
QueueCapReached.Inc();
}
await TrySaveLogs();
}
}
private async Task PreRoundUpdate()
{
var preRoundCount = _preRoundLogQueue.Count;
PreRoundQueue.Set(preRoundCount);
if (preRoundCount < _preRoundQueueMax)
{
return;
}
if (_metricsEnabled)
{
PreRoundQueueCapReached.Inc();
}
await TrySaveLogs();
}
private async Task TrySaveLogs()
{
if (Interlocked.Exchange(ref _savingLogs, 1) == 1)
return;
try
{
await SaveLogs();
}
finally
{
Interlocked.Exchange(ref _savingLogs, 0);
}
}
private async Task SaveLogs()
{
_nextUpdateTime = _timing.RealTime.Add(_queueSendDelay);
// TODO ADMIN LOGS array pool
var copy = new List<AdminLog>(_logQueue.Count + _preRoundLogQueue.Count);
copy.AddRange(_logQueue);
if (_logQueue.Count >= _queueMax)
{
_sawmill.Warning($"In-round cap of {_queueMax} reached for admin logs.");
}
var dropped = Interlocked.Exchange(ref _logsDropped, 0);
if (dropped > 0)
{
_sawmill.Error($"Dropped {dropped} logs. Current max threshold: {_dropThreshold}");
}
if (_runLevel == GameRunLevel.PreRoundLobby && !_preRoundLogQueue.IsEmpty)
{
_sawmill.Error($"Dropping {_preRoundLogQueue.Count} pre-round logs. Current cap: {_preRoundQueueMax}");
}
else
{
foreach (var log in _preRoundLogQueue)
{
log.RoundId = _currentRoundId;
CacheLog(log);
}
copy.AddRange(_preRoundLogQueue);
}
_logQueue.Clear();
Queue.Set(0);
_preRoundLogQueue.Clear();
PreRoundQueue.Set(0);
var task = _db.AddAdminLogs(copy);
_sawmill.Debug($"Saving {copy.Count} admin logs.");
if (_metricsEnabled)
{
LogsSent.Inc(copy.Count);
using (DatabaseUpdateTime.NewTimer())
{
await task;
return;
}
}
await task;
}
public void RoundStarting(int id)
{
_currentRoundId = id;
CacheNewRound();
}
public void RunLevelChanged(GameRunLevel level)
{
_runLevel = level;
if (level == GameRunLevel.PreRoundLobby)
{
Interlocked.Exchange(ref _currentLogId, 0);
if (!_preRoundLogQueue.IsEmpty)
{
// This technically means that you could get pre-round logs from
// a previous round passed onto the next one
// If this happens please file a complaint with your nearest lottery
foreach (var log in _preRoundLogQueue)
{
log.Id = NextLogId;
}
}
if (_metricsEnabled)
{
PreRoundQueueCapReached.Set(0);
QueueCapReached.Set(0);
LogsSent.Set(0);
}
}
}
private void Add(LogType type, LogImpact impact, string message, JsonDocument json, HashSet<Guid> players)
{
var preRound = _runLevel == GameRunLevel.PreRoundLobby;
var count = preRound ? _preRoundLogQueue.Count : _logQueue.Count;
if (count >= _dropThreshold)
{
Interlocked.Increment(ref _logsDropped);
return;
}
var log = new AdminLog
{
Id = NextLogId,
RoundId = _currentRoundId,
Type = type,
Impact = impact,
Date = DateTime.UtcNow,
Message = message,
Json = json,
Players = new List<AdminLogPlayer>(players.Count)
};
var adminLog = false;
var adminSys = _entityManager.SystemOrNull<AdminSystem>();
var logMessage = message;
foreach (var id in players)
{
var player = new AdminLogPlayer
{
LogId = log.Id,
PlayerUserId = id
};
log.Players.Add(player);
if (adminSys != null)
{
var cachedInfo = adminSys.GetCachedPlayerInfo(new NetUserId(id));
if (cachedInfo != null && cachedInfo.Antag)
{
var subtype = Loc.GetString(cachedInfo.Subtype ?? cachedInfo.RoleProto.Name);
logMessage = Loc.GetString(
"admin-alert-antag-label",
("message", logMessage),
("name", cachedInfo.CharacterName),
("subtype", subtype));
}
}
if (adminLog)
continue;
if (impact == LogImpact.Extreme) // Always chat-notify Extreme logs
adminLog = true;
if (impact == LogImpact.High) // Only chat-notify High logs if the player is below a threshold playtime
{
if (_highImpactLogPlaytime >= 0 && _player.TryGetSessionById(new NetUserId(id), out var session))
{
var playtimes = _playtime.GetPlayTimes(session);
if (playtimes.TryGetValue(PlayTimeTrackingShared.TrackerOverall, out var overallTime) &&
overallTime <= TimeSpan.FromHours(_highImpactLogPlaytime))
{
adminLog = true;
}
}
}
}
if (adminLog)
_chat.SendAdminAlert(logMessage);
if (preRound)
{
_preRoundLogQueue.Enqueue(log);
}
else
{
_logQueue.Enqueue(log);
CacheLog(log);
}
}
public override void Add(LogType type, LogImpact impact, ref LogStringHandler handler)
{
if (!_enabled)
{
handler.ToStringAndClear();
return;
}
var (json, players) = ToJson(handler.Values);
var message = handler.ToStringAndClear();
Add(type, impact, message, json, players);
}
public override void Add(LogType type, ref LogStringHandler handler)
{
Add(type, LogImpact.Medium, ref handler);
}
public async Task<List<SharedAdminLog>> All(LogFilter? filter = null, Func<List<SharedAdminLog>>? listProvider = null)
{
if (TrySearchCache(filter, out var results))
{
return results;
}
var initialSize = Math.Min(filter?.Limit ?? 0, 1000);
List<SharedAdminLog> list;
if (listProvider != null)
{
list = listProvider();
list.EnsureCapacity(initialSize);
}
else
{
list = new List<SharedAdminLog>(initialSize);
}
await foreach (var log in _db.GetAdminLogs(filter).WithCancellation(filter?.CancellationToken ?? default))
{
list.Add(log);
}
return list;
}
public IAsyncEnumerable<string> AllMessages(LogFilter? filter = null)
{
return _db.GetAdminLogMessages(filter);
}
public IAsyncEnumerable<JsonDocument> AllJson(LogFilter? filter = null)
{
return _db.GetAdminLogsJson(filter);
}
public Task<Round> Round(int roundId)
{
return _db.GetRound(roundId);
}
public Task<List<SharedAdminLog>> CurrentRoundLogs(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = _currentRoundId;
return All(filter);
}
public IAsyncEnumerable<string> CurrentRoundMessages(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = _currentRoundId;
return AllMessages(filter);
}
public IAsyncEnumerable<JsonDocument> CurrentRoundJson(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = _currentRoundId;
return AllJson(filter);
}
public Task<Round> CurrentRound()
{
return Round(_currentRoundId);
}
public Task<int> CountLogs(int round)
{
return _db.CountAdminLogs(round);
}
}