diff --git a/src/Commands/SettingsCommandGroup.cs b/src/Commands/SettingsCommandGroup.cs index bdd01f6..9cea6c9 100644 --- a/src/Commands/SettingsCommandGroup.cs +++ b/src/Commands/SettingsCommandGroup.cs @@ -119,6 +119,7 @@ public class SettingsCommandGroup : CommandGroup private Task SendSettingsListAsync(JsonNode cfg, IUser bot, int page, CancellationToken ct = default) { + _profiler.Push("main"); var description = new StringBuilder(); var footer = new StringBuilder(); @@ -164,6 +165,7 @@ public class SettingsCommandGroup : CommandGroup .WithFooter(footer.ToString()) .Build(); + _profiler.Pop(); return _profiler.PopWithResult(_feedback.SendContextualEmbedResultAsync(embed, ct: ct)); } diff --git a/src/Commands/ToolsCommandGroup.cs b/src/Commands/ToolsCommandGroup.cs index b539355..de85e77 100644 --- a/src/Commands/ToolsCommandGroup.cs +++ b/src/Commands/ToolsCommandGroup.cs @@ -6,6 +6,7 @@ using Octobot.Data; using Octobot.Extensions; using Octobot.Parsers; using Octobot.Services; +using Octobot.Services.Profiler; using Remora.Commands.Attributes; using Remora.Commands.Groups; using Remora.Discord.API.Abstractions.Objects; @@ -31,17 +32,19 @@ public class ToolsCommandGroup : CommandGroup private readonly IDiscordRestGuildAPI _guildApi; private readonly GuildDataService _guildData; private readonly IDiscordRestUserAPI _userApi; + private readonly Profiler _profiler; public ToolsCommandGroup( ICommandContext context, IFeedbackService feedback, GuildDataService guildData, IDiscordRestGuildAPI guildApi, - IDiscordRestUserAPI userApi, IDiscordRestChannelAPI channelApi) + IDiscordRestUserAPI userApi, IDiscordRestChannelAPI channelApi, Profiler profiler) { _context = context; _guildData = guildData; _feedback = feedback; _guildApi = guildApi; _userApi = userApi; + _profiler = profiler; } /// @@ -73,32 +76,44 @@ public class ToolsCommandGroup : CommandGroup [Description("User to show info about")] IUser? target = null) { + _profiler.Push("userinfo_command"); + _profiler.Push("preparation"); if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId)) { - return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + return _profiler.ReportWithResult(new ArgumentInvalidError(nameof(_context), + "Unable to retrieve necessary IDs from command context")); } + _profiler.Push("current_user_get"); var botResult = await _userApi.GetCurrentUserAsync(CancellationToken); if (!botResult.IsDefined(out var bot)) { - return Result.FromError(botResult); + return _profiler.ReportWithResult(Result.FromError(botResult)); } + _profiler.Pop(); + _profiler.Push("executor_get"); var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); if (!executorResult.IsDefined(out var executor)) { - return Result.FromError(executorResult); + return _profiler.ReportWithResult(Result.FromError(executorResult)); } + _profiler.Pop(); + _profiler.Push("guild_settings_get"); var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); + _profiler.Pop(); - return await ShowUserInfoAsync(target ?? executor, bot, data, guildId, CancellationToken); + _profiler.Pop(); + return _profiler.ReportWithResult(await ShowUserInfoAsync(target ?? executor, bot, data, guildId, CancellationToken)); } private async Task ShowUserInfoAsync( IUser target, IUser bot, GuildData data, Snowflake guildId, CancellationToken ct = default) { + _profiler.Push("main"); + _profiler.Push("builder_construction"); var builder = new StringBuilder().AppendLine($"### <@{target.ID}>"); if (target.GlobalName is not null) @@ -114,7 +129,9 @@ public class ToolsCommandGroup : CommandGroup var embedColor = ColorsList.Cyan; + _profiler.Push("guild_member_get"); var guildMemberResult = await _guildApi.GetGuildMemberAsync(guildId, target.ID, ct); + _profiler.Pop(); DateTimeOffset? communicationDisabledUntil = null; if (guildMemberResult.IsDefined(out var guildMember)) { @@ -145,6 +162,8 @@ public class ToolsCommandGroup : CommandGroup embedColor = ColorsList.Default; } + _profiler.Pop(); + _profiler.Push("embed_send"); var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.InformationAbout, target.GetTag()), bot) .WithDescription(builder.ToString()) @@ -153,7 +172,7 @@ public class ToolsCommandGroup : CommandGroup .WithFooter($"ID: {target.ID.ToString()}") .Build(); - return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + return _profiler.PopWithResult(await _feedback.SendContextualEmbedResultAsync(embed, ct: ct)); } private static Color AppendPunishmentsInformation(bool wasMuted, bool wasKicked, bool wasBanned, @@ -266,31 +285,43 @@ public class ToolsCommandGroup : CommandGroup [UsedImplicitly] public async Task ExecuteGuildInfoAsync() { + _profiler.Push("guildinfo_command"); + _profiler.Push("preparation"); if (!_context.TryGetContextIDs(out var guildId, out _, out _)) { - return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + return _profiler.ReportWithResult(new ArgumentInvalidError(nameof(_context), + "Unable to retrieve necessary IDs from command context")); } + _profiler.Push("current_user_get"); var botResult = await _userApi.GetCurrentUserAsync(CancellationToken); if (!botResult.IsDefined(out var bot)) { - return Result.FromError(botResult); + return _profiler.ReportWithResult(Result.FromError(botResult)); } + _profiler.Pop(); + _profiler.Push("guild_get"); var guildResult = await _guildApi.GetGuildAsync(guildId, ct: CancellationToken); if (!guildResult.IsDefined(out var guild)) { - return Result.FromError(guildResult); + return _profiler.ReportWithResult(Result.FromError(guildResult)); } + _profiler.Pop(); + _profiler.Push("guild_settings_get"); var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); + _profiler.Pop(); - return await ShowGuildInfoAsync(bot, guild, CancellationToken); + _profiler.Pop(); + return _profiler.ReportWithResult(await ShowGuildInfoAsync(bot, guild, CancellationToken)); } private Task ShowGuildInfoAsync(IUser bot, IGuild guild, CancellationToken ct) { + _profiler.Push("main"); + _profiler.Push("builder_construction"); var description = new StringBuilder().AppendLine($"## {guild.Name}"); if (guild.Description is not null) @@ -316,6 +347,8 @@ public class ToolsCommandGroup : CommandGroup embedColor = ColorsList.Magenta; } + _profiler.Pop(); + _profiler.Push("embed_send"); var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.InformationAbout, guild.Name), bot) .WithDescription(description.ToString()) @@ -325,7 +358,7 @@ public class ToolsCommandGroup : CommandGroup .WithFooter($"ID: {guild.ID.ToString()}") .Build(); - return _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + return _profiler.PopWithResult(_feedback.SendContextualEmbedResultAsync(embed, ct: ct)); } /// @@ -345,26 +378,35 @@ public class ToolsCommandGroup : CommandGroup [Description("Second number (Default: 0)")] long? second = null) { + _profiler.Push("userinfo_command"); + _profiler.Push("preparation"); if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId)) { - return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + return _profiler.ReportWithResult(new ArgumentInvalidError(nameof(_context), + "Unable to retrieve necessary IDs from command context")); } + _profiler.Push("executor_get"); var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); if (!executorResult.IsDefined(out var executor)) { - return Result.FromError(executorResult); + return _profiler.ReportWithResult(Result.FromError(executorResult)); } + _profiler.Pop(); + _profiler.Push("guild_settings_get"); var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); + _profiler.Pop(); - return await SendRandomNumberAsync(first, second, executor, CancellationToken); + _profiler.Pop(); + return _profiler.ReportWithResult(await SendRandomNumberAsync(first, second, executor, CancellationToken)); } private Task SendRandomNumberAsync(long first, long? secondNullable, IUser executor, CancellationToken ct) { + _profiler.Push("main"); const long secondDefault = 0; var second = secondNullable ?? secondDefault; @@ -373,6 +415,7 @@ public class ToolsCommandGroup : CommandGroup var i = Random.Shared.NextInt64(min, max + 1); + _profiler.Push("builder_construction"); var description = new StringBuilder().Append("# ").Append(i); description.AppendLine().AppendBulletPoint(string.Format( @@ -396,13 +439,15 @@ public class ToolsCommandGroup : CommandGroup embedColor = ColorsList.Red; } + _profiler.Pop(); + _profiler.Push("embed_send"); var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.RandomTitle, executor.GetTag()), executor) .WithDescription(description.ToString()) .WithColour(embedColor) .Build(); - return _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + return _profiler.PopWithResult(_feedback.SendContextualEmbedResultAsync(embed, ct: ct)); } private static readonly TimestampStyle[] AllStyles = @@ -431,47 +476,62 @@ public class ToolsCommandGroup : CommandGroup [Description("Offset from current time")] [Option("offset")] string? stringOffset = null) { + _profiler.Push("timestamp_command"); + _profiler.Push("preparation"); if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId)) { - return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + return _profiler.ReportWithResult(new ArgumentInvalidError(nameof(_context), + "Unable to retrieve necessary IDs from command context")); } + _profiler.Push("current_user_get"); var botResult = await _userApi.GetCurrentUserAsync(CancellationToken); if (!botResult.IsDefined(out var bot)) { - return Result.FromError(botResult); + return _profiler.ReportWithResult(Result.FromError(botResult)); } + _profiler.Pop(); + _profiler.Push("executor_get"); var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); if (!executorResult.IsDefined(out var executor)) { - return Result.FromError(executorResult); + return _profiler.ReportWithResult(Result.FromError(executorResult)); } + _profiler.Pop(); + _profiler.Push("guild_settings_get"); var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); + _profiler.Pop(); if (stringOffset is null) { - return await SendTimestampAsync(null, executor, CancellationToken); + _profiler.Pop(); + return _profiler.ReportWithResult(await SendTimestampAsync(null, executor, CancellationToken)); } + _profiler.Push("parse_input"); var parseResult = TimeSpanParser.TryParse(stringOffset); if (!parseResult.IsDefined(out var offset)) { + _profiler.Push("not_parsed_send"); var failedEmbed = new EmbedBuilder() .WithSmallTitle(Messages.InvalidTimeSpan, bot) .WithColour(ColorsList.Red) .Build(); - return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken); + return _profiler.ReportWithResult(await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken)); } - return await SendTimestampAsync(offset, executor, CancellationToken); + _profiler.Pop(); + return _profiler.ReportWithResult(await SendTimestampAsync(offset, executor, CancellationToken)); } private Task SendTimestampAsync(TimeSpan? offset, IUser executor, CancellationToken ct) { + _profiler.Push("main"); + _profiler.Push("builder_construction"); var timestamp = DateTimeOffset.UtcNow.Add(offset ?? TimeSpan.Zero).ToUnixTimeSeconds(); var description = new StringBuilder().Append("# ").AppendLine(timestamp.ToString()); @@ -488,12 +548,14 @@ public class ToolsCommandGroup : CommandGroup .Append(" → ").AppendLine(markdownTimestamp); } + _profiler.Pop(); + _profiler.Push("embed_send"); var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.TimestampTitle, executor.GetTag()), executor) .WithDescription(description.ToString()) .WithColour(ColorsList.Blue) .Build(); - return _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + return _profiler.PopWithResult(_feedback.SendContextualEmbedResultAsync(embed, ct: ct)); } }