Revert "Admin logs (#5419)"

This reverts commit 319aec109d.
This commit is contained in:
DrSmugleaf
2021-11-22 18:55:17 +01:00
parent edeccabb36
commit c18d07538a
65 changed files with 236 additions and 7021 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Linq;
using Content.Server.Administration.Managers;
using Content.Server.Players;

View File

@@ -1,64 +0,0 @@
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Shared.Administration;
using Content.Shared.Administration.Logs;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.Timing;
namespace Content.Server.Administration.Commands;
#if DEBUG
[AdminCommand(AdminFlags.Host)]
public class AdminLogBulk : IConsoleCommand
{
public string Command => "adminlogbulk";
public string Description => "Adds debug logs to the database.";
public string Help => $"Usage: {Command} <amount> <parallel>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player?.AttachedEntity is not { } entity)
{
shell.WriteError("This command can only be ran by a player with an attached entity.");
return;
}
int amount;
var parallel = false;
switch (args)
{
case {Length: 1} when int.TryParse(args[0], out amount):
case {Length: 2} when int.TryParse(args[0], out amount) &&
bool.TryParse(args[1], out parallel):
break;
default:
shell.WriteError(Help);
return;
}
var logs = EntitySystem.Get<AdminLogSystem>();
var stopwatch = new Stopwatch();
stopwatch.Start();
if (parallel)
{
Parallel.For(0, amount, _ =>
{
logs.Add(LogType.Unknown, $"Debug log added by {entity:Player}");
});
}
else
{
for (var i = 0; i < amount; i++)
{
logs.Add(LogType.Unknown, $"Debug log added by {entity:Player}");
}
}
shell.WriteLine($"Added {amount} logs in {stopwatch.Elapsed.TotalMilliseconds} ms");
}
}
#endif

View File

@@ -1,30 +0,0 @@
using Content.Server.Administration.Logs;
using Content.Server.Administration.UI;
using Content.Server.EUI;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.IoC;
namespace Content.Server.Administration.Commands;
[AdminCommand(AdminFlags.Logs)]
public class OpenAdminLogsCommand : IConsoleCommand
{
public string Command => "adminlogs";
public string Description => "Opens the admin logs panel.";
public string Help => $"Usage: {Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player is not IPlayerSession player)
{
shell.WriteLine("This does not work from the server console.");
return;
}
var eui = IoCManager.Resolve<EuiManager>();
var ui = new AdminLogsEui();
eui.OpenEui(ui, player);
}
}

View File

@@ -1,302 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Content.Server.Administration.Logs.Converters;
using Content.Server.Database;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Events;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Prometheus;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Reflection;
namespace Content.Server.Administration.Logs;
public class AdminLogSystem : SharedAdminLogSystem
{
[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 IDynamicTypeFactory _typeFactory = default!;
[Dependency] private readonly IReflectionManager _reflection = default!;
[Dependency] private readonly GameTicker _gameTicker = 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 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 LogsSent = Metrics.CreateGauge(
"admin_logs_sent",
"Amount of logs sent to the database in a round.");
private static readonly JsonNamingPolicy NamingPolicy = JsonNamingPolicy.CamelCase;
// Init only
private ISawmill _sawmill = default!;
private JsonSerializerOptions _jsonOptions = default!;
// CVars
private bool _metricsEnabled;
private TimeSpan _queueSendDelay;
private int _queueMax;
// Per update
private float _accumulatedFrameTime;
private readonly ConcurrentQueue<QueuedLog> _logsToAdd = new();
private int CurrentRoundId => _gameTicker.RoundId;
public override void Initialize()
{
base.Initialize();
_sawmill = _logManager.GetSawmill(SawmillId);
_jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = NamingPolicy
};
foreach (var converter in _reflection.FindTypesWithAttribute<AdminLogConverterAttribute>())
{
var instance = _typeFactory.CreateInstance<JsonConverter>(converter);
_jsonOptions.Converters.Add(instance);
}
var converterNames = _jsonOptions.Converters.Select(converter => converter.GetType().Name);
_sawmill.Info($"Admin log converters found: {string.Join(" ", converterNames)}");
_configuration.OnValueChanged(CVars.MetricsEnabled,
value => _metricsEnabled = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueSendDelay,
value => _queueSendDelay = TimeSpan.FromSeconds(value), true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueMax,
value => _queueMax = value, true);
if (_metricsEnabled)
{
QueueCapReached.Set(0);
LogsSent.Set(0);
}
SubscribeLocalEvent<RoundStartingEvent>(RoundStarting);
}
public override async void Shutdown()
{
base.Shutdown();
if (!_logsToAdd.IsEmpty)
{
await SendLogs();
}
}
public override async void Update(float frameTime)
{
var count = _logsToAdd.Count;
if (count == 0)
{
return;
}
if (count < _queueMax && _accumulatedFrameTime < _queueSendDelay.TotalSeconds)
{
_accumulatedFrameTime += frameTime;
return;
}
await SendLogs();
}
private async Task SendLogs()
{
var copy = new List<QueuedLog>(_logsToAdd);
_logsToAdd.Clear();
_accumulatedFrameTime = 0;
// ship the logs to Azkaban
var task = Task.Run(() =>
{
_db.AddAdminLogs(copy);
});
if (_metricsEnabled)
{
if (copy.Count >= _queueMax)
{
QueueCapReached.Inc();
}
LogsSent.Inc(copy.Count);
using (DatabaseUpdateTime.NewTimer())
{
await task;
return;
}
}
await task;
}
private void RoundStarting(RoundStartingEvent ev)
{
if (_metricsEnabled)
{
QueueCapReached.Set(0);
LogsSent.Set(0);
}
}
public (JsonDocument json, List<Guid> players, List<(int id, string? name)> entities) ToJson(
Dictionary<string, object?> properties)
{
var entities = new List<(int id, string? name)>();
var players = new List<Guid>();
var parsed = new Dictionary<string, object?>();
foreach (var key in properties.Keys)
{
var value = properties[key];
var parsedKey = NamingPolicy.ConvertName(key);
parsed.Add(parsedKey, value);
EntityUid? entityId = properties[key] switch
{
EntityUid id => id,
IEntity entity => entity.Uid,
IPlayerSession {AttachedEntityUid: { }} session => session.AttachedEntityUid.Value,
IComponent component => component.OwnerUid,
_ => null
};
if (entityId is not { } uid)
{
continue;
}
var entityName = _entityManager.TryGetEntity(uid, out var resolvedEntity)
? resolvedEntity.Name
: null;
entities.Add(((int) uid, entityName));
if (_entityManager.TryGetComponent(uid, out ActorComponent? actor))
{
players.Add(actor.PlayerSession.UserId.UserId);
}
}
return (JsonSerializer.SerializeToDocument(parsed, _jsonOptions), players, entities);
}
private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, List<Guid> players, List<(int id, string? name)> entities)
{
var log = new AdminLog
{
RoundId = CurrentRoundId,
Type = type,
Impact = impact,
Date = DateTime.UtcNow,
Message = message,
Json = json,
Players = new List<AdminLogPlayer>(players.Count)
};
var queued = new QueuedLog(log, entities);
_logsToAdd.Enqueue(queued);
foreach (var id in players)
{
var player = new AdminLogPlayer
{
PlayerUserId = id,
RoundId = CurrentRoundId
};
log.Players.Add(player);
}
}
public override void Add(LogType type, LogImpact impact, ref LogStringHandler handler)
{
var (json, players, entities) = ToJson(handler.Values);
var message = handler.ToStringAndClear();
Add(type, impact, message, json, players, entities);
}
public override void Add(LogType type, ref LogStringHandler handler)
{
Add(type, LogImpact.Medium, ref handler);
}
public IAsyncEnumerable<LogRecord> All(LogFilter? filter = null)
{
return _db.GetAdminLogs(filter);
}
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 IAsyncEnumerable<LogRecord> 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);
}
}

View File

@@ -1,204 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Managers;
using Content.Server.EUI;
using Content.Server.GameTicking;
using Content.Shared.Administration;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Content.Shared.Eui;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using static Content.Shared.Administration.AdminLogsEuiMsg;
namespace Content.Server.Administration.Logs;
public sealed class AdminLogsEui : BaseEui
{
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configuration = default!;
private readonly ISawmill _sawmill;
private readonly AdminLogSystem _logSystem;
private int _clientBatchSize;
private bool _isLoading = true;
private readonly Dictionary<Guid, string> _players = new();
private CancellationTokenSource _logSendCancellation = new();
private LogFilter _filter;
public AdminLogsEui()
{
IoCManager.InjectDependencies(this);
_sawmill = _logManager.GetSawmill(AdminLogSystem.SawmillId);
_configuration.OnValueChanged(CCVars.AdminLogsClientBatchSize, ClientBatchSizeChanged, true);
_logSystem = EntitySystem.Get<AdminLogSystem>();
_filter = new LogFilter
{
CancellationToken = _logSendCancellation.Token,
Limit = _clientBatchSize
};
}
public int CurrentRoundId => EntitySystem.Get<GameTicker>().RoundId;
public override async void Opened()
{
base.Opened();
_adminManager.OnPermsChanged += OnPermsChanged;
var roundId = _filter.Round ?? EntitySystem.Get<GameTicker>().RoundId;
LoadFromDb(roundId);
}
private void ClientBatchSizeChanged(int value)
{
_clientBatchSize = value;
}
private void OnPermsChanged(AdminPermsChangedEventArgs args)
{
if (args.Player == Player && !_adminManager.HasAdminFlag(Player, AdminFlags.Logs))
{
Close();
}
}
public override EuiStateBase GetNewState()
{
if (_isLoading)
{
return new AdminLogsEuiState(CurrentRoundId, new Dictionary<Guid, string>())
{
IsLoading = true
};
}
var state = new AdminLogsEuiState(CurrentRoundId, _players);
return state;
}
public override async void HandleMessage(EuiMessageBase msg)
{
if (!_adminManager.HasAdminFlag(Player, AdminFlags.Logs))
{
return;
}
switch (msg)
{
case Close _:
{
Close();
break;
}
case LogsRequest request:
{
_sawmill.Info($"Admin log request from admin with id {Player.UserId.UserId} and name {Player.Name}");
_logSendCancellation.Cancel();
_logSendCancellation = new CancellationTokenSource();
_filter = new LogFilter
{
CancellationToken = _logSendCancellation.Token,
Round = request.RoundId,
Types = request.Types,
Impacts = request.Impacts,
Before = request.Before,
After = request.After,
AnyPlayers = request.AnyPlayers,
AllPlayers = request.AllPlayers,
LastLogId = 0,
Limit = _clientBatchSize
};
var roundId = _filter.Round ??= EntitySystem.Get<GameTicker>().RoundId;
LoadFromDb(roundId);
SendLogs(true);
break;
}
case NextLogsRequest:
{
_sawmill.Info($"Admin log next batch request from admin with id {Player.UserId.UserId} and name {Player.Name}");
SendLogs(false);
break;
}
}
}
private async void SendLogs(bool replace)
{
var logs = new List<SharedAdminLog>(_clientBatchSize);
await Task.Run(async () =>
{
var results = await Task.Run(() => _logSystem.All(_filter));
await foreach (var record in results.WithCancellation(_logSendCancellation.Token))
{
var log = new SharedAdminLog(record.Id, record.Type, record.Impact, record.Date, record.Message, record.Players);
logs.Add(log);
}
}, _filter.CancellationToken);
if (logs.Count > 0)
{
var largestId = _filter.DateOrder switch
{
DateOrder.Ascending => ^1,
DateOrder.Descending => 0,
_ => throw new ArgumentOutOfRangeException(nameof(_filter.DateOrder), _filter.DateOrder, null)
};
_filter.LastLogId = logs[largestId].Id;
}
var message = new NewLogs(logs.ToArray(), replace);
SendMessage(message);
}
public override void Closed()
{
base.Closed();
_configuration.UnsubValueChanged(CCVars.AdminLogsClientBatchSize, ClientBatchSizeChanged);
_adminManager.OnPermsChanged -= OnPermsChanged;
_logSendCancellation.Cancel();
_logSendCancellation.Dispose();
}
private async void LoadFromDb(int roundId)
{
_isLoading = true;
StateDirty();
var round = await Task.Run(() => _logSystem.Round(roundId));
var players = round.Players
.ToDictionary(player => player.UserId, player => player.LastSeenUserName);
_players.Clear();
foreach (var (id, name) in players)
{
_players.Add(id, name);
}
_isLoading = false;
StateDirty();
}
}

View File

@@ -1,15 +0,0 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Content.Server.Administration.Logs.Converters;
public abstract class AdminLogConverter<T> : JsonConverter<T>
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
public abstract override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
}

View File

@@ -1,10 +0,0 @@
using System;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs.Converters;
[AttributeUsage(AttributeTargets.Class)]
[BaseTypeRequired(typeof(AdminLogConverter<>))]
public class AdminLogConverterAttribute : Attribute
{
}

View File

@@ -1,27 +0,0 @@
using System.Text.Json;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Administration.Logs.Converters;
[AdminLogConverter]
public class EntityJsonConverter : AdminLogConverter<Entity>
{
[Dependency] private readonly IEntityManager _entities = default!;
public override void Write(Utf8JsonWriter writer, Entity value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("id", (int) value.Uid);
writer.WriteString("name", value.Name);
if (_entities.TryGetComponent(value.Uid, out ActorComponent? actor))
{
writer.WriteString("player", actor.PlayerSession.UserId.UserId);
}
writer.WriteEndObject();
}
}

View File

@@ -1,13 +0,0 @@
using System.Text.Json;
using Content.Shared.FixedPoint;
namespace Content.Server.Administration.Logs.Converters;
[AdminLogConverter]
public class FixedPoint2Converter : AdminLogConverter<FixedPoint2>
{
public override void Write(Utf8JsonWriter writer, FixedPoint2 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Int());
}
}

View File

@@ -1,33 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Content.Shared.Administration.Logs;
namespace Content.Server.Administration.Logs;
public sealed class LogFilter
{
public CancellationToken CancellationToken { get; set; }
public int? Round { get; set; }
public string? Search { get; set; }
public List<LogType>? Types { get; set; }
public List<LogImpact>? Impacts { get; set; }
public DateTime? Before { get; set; }
public DateTime? After { get; set; }
public Guid[]? AnyPlayers { get; set; }
public Guid[]? AllPlayers { get; set; }
public int? LastLogId { get; set; }
public int? Limit { get; set; }
public DateOrder DateOrder { get; set; } = DateOrder.Descending;
}

View File

@@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using Content.Server.Database;
using Content.Shared.Administration.Logs;
namespace Content.Server.Administration.Logs;
public class LogRecord
{
public LogRecord(
int id,
int roundId,
LogType type,
LogImpact impact,
DateTime date,
string message,
Guid[] players)
{
Id = id;
RoundId = roundId;
Type = type;
Impact = impact;
Date = date;
Message = message;
Players = players;
}
public int Id { get; }
public int RoundId { get; }
public LogType Type { get; }
public LogImpact Impact { get; }
public DateTime Date { get; }
public string Message { get; }
public Guid[] Players { get; }
}

View File

@@ -1,24 +0,0 @@
using System.Collections.Generic;
using Content.Server.Database;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs;
public readonly struct QueuedLog
{
public QueuedLog(AdminLog log, List<(int id, string? name)> entities)
{
Log = log;
Entities = entities;
}
public AdminLog Log { get; }
public List<(int id, string? name)> Entities { get; }
public void Deconstruct(out AdminLog log, out List<(int id, string? name)> entities)
{
log = Log;
entities = Entities;
}
}