Added first_seen column to user entity cache

This commit is contained in:
Noikoio 2018-04-24 14:29:19 -07:00
parent 746219d47d
commit 35d37cb827
2 changed files with 18 additions and 14 deletions

View file

@ -16,7 +16,8 @@ namespace Noikoio.RegexBot.EntityCache
{ {
readonly ulong _userId; readonly ulong _userId;
readonly ulong _guildId; readonly ulong _guildId;
readonly DateTime _cacheDate; readonly DateTimeOffset _cacheDate;
readonly DateTimeOffset _firstSeen;
readonly string _username; readonly string _username;
readonly string _discriminator; readonly string _discriminator;
readonly string _nickname; readonly string _nickname;
@ -33,7 +34,11 @@ namespace Noikoio.RegexBot.EntityCache
/// <summary> /// <summary>
/// Timestamp value for when this cache item was last updated, in universal time. /// Timestamp value for when this cache item was last updated, in universal time.
/// </summary> /// </summary>
public DateTime CacheDate => _cacheDate; public DateTimeOffset CacheDate => _cacheDate;
/// <summary>
/// Timestamp value for when this user was first seen by the bot, in universal time.
/// </summary>
public DateTimeOffset FirstSeenDate => _firstSeen;
/// <summary> /// <summary>
/// Display name, including discriminator. Shows the nickname, if available. /// Display name, including discriminator. Shows the nickname, if available.
@ -72,7 +77,7 @@ namespace Noikoio.RegexBot.EntityCache
} }
// Double-check SqlHelper if making changes to this constant // Double-check SqlHelper if making changes to this constant
const string QueryColumns = "user_id, guild_id, cache_date, username, discriminator, nickname, avatar_url"; const string QueryColumns = "user_id, guild_id, first_seen, cache_date, username, discriminator, nickname, avatar_url";
private CacheUser(DbDataReader r) private CacheUser(DbDataReader r)
{ {
// Double-check ordinals if making changes to QueryColumns // Double-check ordinals if making changes to QueryColumns
@ -82,11 +87,12 @@ namespace Noikoio.RegexBot.EntityCache
_userId = (ulong)r.GetInt64(0); _userId = (ulong)r.GetInt64(0);
_guildId = (ulong)r.GetInt64(1); _guildId = (ulong)r.GetInt64(1);
} }
_cacheDate = r.GetDateTime(2).ToUniversalTime(); _firstSeen = r.GetDateTime(2).ToUniversalTime();
_username = r.GetString(3); _cacheDate = r.GetDateTime(3).ToUniversalTime();
_discriminator = r.GetString(4); _username = r.GetString(4);
_nickname = r.IsDBNull(5) ? null : r.GetString(5); _discriminator = r.GetString(5);
_avatarUrl = r.IsDBNull(6) ? null : r.GetString(6); _nickname = r.IsDBNull(6) ? null : r.GetString(6);
_avatarUrl = r.IsDBNull(7) ? null : r.GetString(7);
} }
public override string ToString() => DisplayName; public override string ToString() => DisplayName;

View file

@ -64,6 +64,7 @@ namespace Noikoio.RegexBot.EntityCache
c.CommandText = "CREATE TABLE IF NOT EXISTS " + TableUser + " (" c.CommandText = "CREATE TABLE IF NOT EXISTS " + TableUser + " ("
+ "user_id bigint not null, " + "user_id bigint not null, "
+ $"guild_id bigint not null references {TableGuild}, " + $"guild_id bigint not null references {TableGuild}, "
+ "first_seen timestamptz not null default now(),"
+ "cache_date timestamptz not null, " + "cache_date timestamptz not null, "
+ "username text not null, " + "username text not null, "
+ "discriminator text not null, " + "discriminator text not null, "
@ -98,12 +99,11 @@ namespace Noikoio.RegexBot.EntityCache
using (var c = db.CreateCommand()) using (var c = db.CreateCommand())
{ {
c.CommandText = "INSERT INTO " + TableGuild + " (guild_id, cache_date, current_name) " c.CommandText = "INSERT INTO " + TableGuild + " (guild_id, cache_date, current_name) "
+ "VALUES (@GuildId, @Date, @CurrentName) " + "VALUES (@GuildId, now(), @CurrentName) "
+ "ON CONFLICT (guild_id) DO UPDATE SET " + "ON CONFLICT (guild_id) DO UPDATE SET "
+ "current_name = EXCLUDED.current_name, cache_date = EXCLUDED.cache_date"; + "current_name = EXCLUDED.current_name, cache_date = EXCLUDED.cache_date";
c.Parameters.Add("@GuildId", NpgsqlDbType.Bigint).Value = g.Id; c.Parameters.Add("@GuildId", NpgsqlDbType.Bigint).Value = g.Id;
c.Parameters.Add("@CurrentName", NpgsqlDbType.Text).Value = g.Name; c.Parameters.Add("@CurrentName", NpgsqlDbType.Text).Value = g.Name;
c.Parameters.Add("@Date", NpgsqlDbType.TimestampTZ).Value = DateTime.Now;
c.Prepare(); c.Prepare();
await c.ExecuteNonQueryAsync(); await c.ExecuteNonQueryAsync();
} }
@ -122,7 +122,7 @@ namespace Noikoio.RegexBot.EntityCache
{ {
c.CommandText = "INSERT INTO " + TableUser c.CommandText = "INSERT INTO " + TableUser
+ " (user_id, guild_id, cache_date, username, discriminator, nickname, avatar_url)" + " (user_id, guild_id, cache_date, username, discriminator, nickname, avatar_url)"
+ " VALUES (@Uid, @Gid, @Date, @Uname, @Disc, @Nname, @Url) " + " VALUES (@Uid, @Gid, now(), @Uname, @Disc, @Nname, @Url) "
+ "ON CONFLICT (user_id, guild_id) DO UPDATE SET " + "ON CONFLICT (user_id, guild_id) DO UPDATE SET "
+ "cache_date = EXCLUDED.cache_date, username = EXCLUDED.username, " + "cache_date = EXCLUDED.cache_date, username = EXCLUDED.username, "
+ "discriminator = EXCLUDED.discriminator, " // I've seen someone's discriminator change this one time... + "discriminator = EXCLUDED.discriminator, " // I've seen someone's discriminator change this one time...
@ -130,7 +130,6 @@ namespace Noikoio.RegexBot.EntityCache
var uid = c.Parameters.Add("@Uid", NpgsqlDbType.Bigint); var uid = c.Parameters.Add("@Uid", NpgsqlDbType.Bigint);
var gid = c.Parameters.Add("@Gid", NpgsqlDbType.Bigint); var gid = c.Parameters.Add("@Gid", NpgsqlDbType.Bigint);
c.Parameters.Add("@Date", NpgsqlDbType.TimestampTZ).Value = DateTime.Now;
var uname = c.Parameters.Add("@Uname", NpgsqlDbType.Text); var uname = c.Parameters.Add("@Uname", NpgsqlDbType.Text);
var disc = c.Parameters.Add("@Disc", NpgsqlDbType.Text); var disc = c.Parameters.Add("@Disc", NpgsqlDbType.Text);
var nname = c.Parameters.Add("@Nname", NpgsqlDbType.Text); var nname = c.Parameters.Add("@Nname", NpgsqlDbType.Text);
@ -168,13 +167,12 @@ namespace Noikoio.RegexBot.EntityCache
{ {
c.CommandText = "INSERT INTO " + TableTextChannel c.CommandText = "INSERT INTO " + TableTextChannel
+ " (channel_id, guild_id, cache_date, channel_name)" + " (channel_id, guild_id, cache_date, channel_name)"
+ " VALUES (@Cid, @Gid, @Date, @Name) " + " VALUES (@Cid, @Gid, now(), @Name) "
+ "ON CONFLICT (channel_id, guild_id) DO UPDATE SET " + "ON CONFLICT (channel_id, guild_id) DO UPDATE SET "
+ "cache_date = EXCLUDED.cache_date, channel_name = EXCLUDED.channel_name"; + "cache_date = EXCLUDED.cache_date, channel_name = EXCLUDED.channel_name";
var cid = c.Parameters.Add("@Cid", NpgsqlDbType.Bigint); var cid = c.Parameters.Add("@Cid", NpgsqlDbType.Bigint);
var gid = c.Parameters.Add("@Gid", NpgsqlDbType.Bigint); var gid = c.Parameters.Add("@Gid", NpgsqlDbType.Bigint);
c.Parameters.Add("@Date", NpgsqlDbType.TimestampTZ).Value = DateTime.Now;
var cname = c.Parameters.Add("@Name", NpgsqlDbType.Text); var cname = c.Parameters.Add("@Name", NpgsqlDbType.Text);
c.Prepare(); c.Prepare();