From b6c201d5a572e513777779e9e9c2659e4a0638f9 Mon Sep 17 00:00:00 2001 From: Noi Date: Sun, 19 Jul 2020 20:54:18 -0700 Subject: [PATCH] Fix user last_seen not updating properly Also now displays basic statistics on how many records are updated in each pass whenever StaleDataCleaner does its work. --- BackgroundServices/StaleDataCleaner.cs | 12 +++++++----- BirthdayBot.csproj | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/BackgroundServices/StaleDataCleaner.cs b/BackgroundServices/StaleDataCleaner.cs index 6a8a5dc..1e97517 100644 --- a/BackgroundServices/StaleDataCleaner.cs +++ b/BackgroundServices/StaleDataCleaner.cs @@ -19,9 +19,6 @@ namespace BirthdayBot.BackgroundServices var updateList = new Dictionary>(); foreach (var gi in BotInstance.GuildCache) { - var existingUsers = new List(); - updateList[gi.Key] = existingUsers; - var guild = BotInstance.DiscordClient.GetGuild(gi.Key); if (guild == null) continue; // Have cache without being in guild. Unlikely, but... @@ -29,6 +26,7 @@ namespace BirthdayBot.BackgroundServices var cachedUserIds = from cu in gi.Value.Users select cu.UserId; var guildUserIds = from gu in guild.Users select gu.Id; var existingCachedIds = cachedUserIds.Intersect(guildUserIds); + updateList[gi.Key] = new List(existingCachedIds); } using (var db = await BotInstance.Config.DatabaseSettings.OpenConnectionAsync()) @@ -47,6 +45,9 @@ namespace BirthdayBot.BackgroundServices var pUpdateGU_u = cUpdateGuildUser.Parameters.Add("@Uid", NpgsqlDbType.Bigint); cUpdateGuildUser.Prepare(); + int updatedGuilds = 0; + int updatedUsers = 0; + // Do actual updates foreach (var item in updateList) { @@ -54,15 +55,16 @@ namespace BirthdayBot.BackgroundServices var userlist = item.Value; pUpdateG.Value = (long)guild; - cUpdateGuild.ExecuteNonQuery(); + updatedGuilds += cUpdateGuild.ExecuteNonQuery(); pUpdateGU_g.Value = (long)guild; foreach (var userid in userlist) { pUpdateGU_u.Value = (long)userid; - cUpdateGuildUser.ExecuteNonQuery(); + updatedUsers += cUpdateGuildUser.ExecuteNonQuery(); } } + Log($"Updated last-seen records: {updatedGuilds} guilds, {updatedUsers} users"); // Delete all old values - expects referencing tables to have 'on delete cascade' using (var t = db.BeginTransaction()) diff --git a/BirthdayBot.csproj b/BirthdayBot.csproj index e55f7a7..9e1fc44 100644 --- a/BirthdayBot.csproj +++ b/BirthdayBot.csproj @@ -3,7 +3,7 @@ Exe netcoreapp3.1 - 2.2.0 + 2.2.1 BirthdayBot NoiTheCat BirthdayBot