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.
This commit is contained in:
Noi 2020-07-19 20:54:18 -07:00
parent e3a86dd6dc
commit b6c201d5a5
2 changed files with 8 additions and 6 deletions

View file

@ -19,9 +19,6 @@ namespace BirthdayBot.BackgroundServices
var updateList = new Dictionary<ulong, List<ulong>>(); var updateList = new Dictionary<ulong, List<ulong>>();
foreach (var gi in BotInstance.GuildCache) foreach (var gi in BotInstance.GuildCache)
{ {
var existingUsers = new List<ulong>();
updateList[gi.Key] = existingUsers;
var guild = BotInstance.DiscordClient.GetGuild(gi.Key); var guild = BotInstance.DiscordClient.GetGuild(gi.Key);
if (guild == null) continue; // Have cache without being in guild. Unlikely, but... 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 cachedUserIds = from cu in gi.Value.Users select cu.UserId;
var guildUserIds = from gu in guild.Users select gu.Id; var guildUserIds = from gu in guild.Users select gu.Id;
var existingCachedIds = cachedUserIds.Intersect(guildUserIds); var existingCachedIds = cachedUserIds.Intersect(guildUserIds);
updateList[gi.Key] = new List<ulong>(existingCachedIds);
} }
using (var db = await BotInstance.Config.DatabaseSettings.OpenConnectionAsync()) using (var db = await BotInstance.Config.DatabaseSettings.OpenConnectionAsync())
@ -47,6 +45,9 @@ namespace BirthdayBot.BackgroundServices
var pUpdateGU_u = cUpdateGuildUser.Parameters.Add("@Uid", NpgsqlDbType.Bigint); var pUpdateGU_u = cUpdateGuildUser.Parameters.Add("@Uid", NpgsqlDbType.Bigint);
cUpdateGuildUser.Prepare(); cUpdateGuildUser.Prepare();
int updatedGuilds = 0;
int updatedUsers = 0;
// Do actual updates // Do actual updates
foreach (var item in updateList) foreach (var item in updateList)
{ {
@ -54,15 +55,16 @@ namespace BirthdayBot.BackgroundServices
var userlist = item.Value; var userlist = item.Value;
pUpdateG.Value = (long)guild; pUpdateG.Value = (long)guild;
cUpdateGuild.ExecuteNonQuery(); updatedGuilds += cUpdateGuild.ExecuteNonQuery();
pUpdateGU_g.Value = (long)guild; pUpdateGU_g.Value = (long)guild;
foreach (var userid in userlist) foreach (var userid in userlist)
{ {
pUpdateGU_u.Value = (long)userid; 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' // Delete all old values - expects referencing tables to have 'on delete cascade'
using (var t = db.BeginTransaction()) using (var t = db.BeginTransaction())

View file

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Version>2.2.0</Version> <Version>2.2.1</Version>
<PackageId>BirthdayBot</PackageId> <PackageId>BirthdayBot</PackageId>
<Authors>NoiTheCat</Authors> <Authors>NoiTheCat</Authors>
<Product>BirthdayBot</Product> <Product>BirthdayBot</Product>