From a2890195a9c0bdb901bdf68ee14c1f4feec0530f Mon Sep 17 00:00:00 2001 From: Toastie Date: Sat, 29 Jun 2024 01:33:36 +1200 Subject: [PATCH] Using new stopwatch, some cleanup. No functional change --- src/EllieBot/Bot.cs | 10 ++++------ .../Modules/Games/ChatterBot/ChatterbotService.cs | 2 +- .../Games/ChatterBot/_common/OfficialGptSession.cs | 2 ++ .../Searches/Search/Searx/SearxSearchService.cs | 13 +++++++------ .../Modules/Utility/Ai/CommandPromptResultModel.cs | 4 ++-- src/EllieBot/_common/_Extensions/Extensions.cs | 6 ------ 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/EllieBot/Bot.cs b/src/EllieBot/Bot.cs index 1ce8774..b312624 100644 --- a/src/EllieBot/Bot.cs +++ b/src/EllieBot/Bot.cs @@ -93,7 +93,7 @@ public sealed class Bot : IBot private void AddServices() { var startingGuildIdList = GetCurrentGuildIds(); - var sw = Stopwatch.StartNew(); + var startTime = Stopwatch.GetTimestamp(); var bot = Client.CurrentUser; using (var uow = _db.GetDbContext()) @@ -161,8 +161,7 @@ public sealed class Bot : IBot LoadTypeReaders(a); } - sw.Stop(); - Log.Information("All services loaded in {ServiceLoadTime:F2}s", sw.Elapsed.TotalSeconds); + Log.Information("All services loaded in {ServiceLoadTime:F2}s", Stopwatch.GetElapsedTime(startTime).TotalSeconds); } private void LoadTypeReaders(Assembly assembly) @@ -259,7 +258,7 @@ public sealed class Bot : IBot if (ShardId == 0) await _db.SetupAsync(); - var sw = Stopwatch.StartNew(); + var startTime = Stopwatch.GetTimestamp(); await LoginAsync(_creds.Token); @@ -274,8 +273,7 @@ public sealed class Bot : IBot Helpers.ReadErrorAndExit(9); } - sw.Stop(); - Log.Information("Shard {ShardId} connected in {Elapsed:F2}s", Client.ShardId, sw.Elapsed.TotalSeconds); + Log.Information("Shard {ShardId} connected in {Elapsed:F2}s", Client.ShardId, Stopwatch.GetElapsedTime(startTime).TotalSeconds); var commandHandler = Services.GetRequiredService(); // start handling messages received in commandhandler diff --git a/src/EllieBot/Modules/Games/ChatterBot/ChatterbotService.cs b/src/EllieBot/Modules/Games/ChatterBot/ChatterbotService.cs index 532dd5a..bc357d6 100644 --- a/src/EllieBot/Modules/Games/ChatterBot/ChatterbotService.cs +++ b/src/EllieBot/Modules/Games/ChatterBot/ChatterbotService.cs @@ -165,7 +165,7 @@ public class ChatterBotService : IExecOnMessage } else { - Log.Warning("Error in chatterbot: {Error}", error); + Log.Warning("Error in chatterbot: {Error}", error.Value); } Log.Information(""" diff --git a/src/EllieBot/Modules/Games/ChatterBot/_common/OfficialGptSession.cs b/src/EllieBot/Modules/Games/ChatterBot/_common/OfficialGptSession.cs index 4f7d35a..7a40df0 100644 --- a/src/EllieBot/Modules/Games/ChatterBot/_common/OfficialGptSession.cs +++ b/src/EllieBot/Modules/Games/ChatterBot/_common/OfficialGptSession.cs @@ -103,6 +103,8 @@ public class OfficialGptSession : IChatterBotSession try { var response = JsonConvert.DeserializeObject(dataString); + + Log.Information("Received response: {response}", dataString); var res = response?.Choices?[0]; var message = res?.Message?.Content; diff --git a/src/EllieBot/Modules/Searches/Search/Searx/SearxSearchService.cs b/src/EllieBot/Modules/Searches/Search/Searx/SearxSearchService.cs index 87e4103..0d2d257 100644 --- a/src/EllieBot/Modules/Searches/Search/Searx/SearxSearchService.cs +++ b/src/EllieBot/Modules/Searches/Search/Searx/SearxSearchService.cs @@ -32,20 +32,21 @@ public sealed class SearxSearchService : SearchServiceBase, IEService var instanceUrl = GetRandomInstance(); Log.Information("Using {Instance} instance for web search...", instanceUrl); - var sw = Stopwatch.StartNew(); + var startTime = Stopwatch.GetTimestamp(); + using var http = _http.CreateClient(); await using var res = await http.GetStreamAsync($"{instanceUrl}" + $"?q={Uri.EscapeDataString(query)}" + $"&format=json" + $"&strict=2"); - sw.Stop(); + var elapsed = Stopwatch.GetElapsedTime(startTime); var dat = await JsonSerializer.DeserializeAsync(res); if (dat is null) return new SearxSearchResult(); - dat.SearchTime = sw.Elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture); + dat.SearchTime = elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture); return dat; } @@ -56,7 +57,7 @@ public sealed class SearxSearchService : SearchServiceBase, IEService var instanceUrl = GetRandomInstance(); Log.Information("Using {Instance} instance for img search...", instanceUrl); - var sw = Stopwatch.StartNew(); + var startTime = Stopwatch.GetTimestamp(); using var http = _http.CreateClient(); await using var res = await http.GetStreamAsync($"{instanceUrl}" + $"?q={Uri.EscapeDataString(query)}" @@ -64,13 +65,13 @@ public sealed class SearxSearchService : SearchServiceBase, IEService + $"&category_images=on" + $"&strict=2"); - sw.Stop(); + var elapsed = Stopwatch.GetElapsedTime(startTime); var dat = await JsonSerializer.DeserializeAsync(res); if (dat is null) return new SearxImageSearchResult(); - dat.SearchTime = sw.Elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture); + dat.SearchTime = elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture); return dat; } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Utility/Ai/CommandPromptResultModel.cs b/src/EllieBot/Modules/Utility/Ai/CommandPromptResultModel.cs index 78d6179..559d5b8 100644 --- a/src/EllieBot/Modules/Utility/Ai/CommandPromptResultModel.cs +++ b/src/EllieBot/Modules/Utility/Ai/CommandPromptResultModel.cs @@ -8,9 +8,9 @@ public sealed class CommandPromptResultModel public required string Name { get; set; } [JsonPropertyName("arguments")] - public required Dictionary Arguments { get; set; } + public Dictionary Arguments { get; set; } = new(); [JsonPropertyName("remaining")] [JsonConverter(typeof(NumberToStringConverter))] - public required string Remaining { get; set; } + public string Remaining { get; set; } = string.Empty; } \ No newline at end of file diff --git a/src/EllieBot/_common/_Extensions/Extensions.cs b/src/EllieBot/_common/_Extensions/Extensions.cs index f0cb9f9..33aa564 100644 --- a/src/EllieBot/_common/_Extensions/Extensions.cs +++ b/src/EllieBot/_common/_Extensions/Extensions.cs @@ -229,10 +229,4 @@ public static class Extensions public static IEnumerable GetRoles(this IGuildUser user) => user.RoleIds.Select(r => user.Guild.GetRole(r)).Where(r => r is not null); - // todo remove - public static void Lap(this Stopwatch sw, string checkpoint) - { - Log.Information("Checkpoint {CheckPoint}: {Time}ms", checkpoint, sw.Elapsed.TotalMilliseconds); - sw.Restart(); - } } \ No newline at end of file