using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Dapper; using NodaTime; using Serilog; namespace PluralKit { public class FullMessage { public PKMessage Message; public PKMember Member; public PKSystem System; } public struct PKMessage { public ulong Mid; public ulong Channel; public ulong Sender; public ulong? OriginalMid; } public struct ImportedSwitch { public Instant Timestamp; public IReadOnlyCollection Members; } public struct SwitchListEntry { public ICollection Members; public Instant TimespanStart; public Instant TimespanEnd; } public struct MemberMessageCount { public int Member; public int MessageCount; } public struct FrontBreakdown { public Dictionary MemberSwitchDurations; public Duration NoFronterDuration; public Instant RangeStart; public Instant RangeEnd; } public struct SwitchMembersListEntry { public int Member; public Instant Timestamp; } public struct GuildConfig { public ulong Id { get; set; } public ulong? LogChannel { get; set; } public ISet LogBlacklist { get; set; } public ISet Blacklist { get; set; } } public class SystemGuildSettings { public bool ProxyEnabled { get; set; } = true; } public class MemberGuildSettings { public string DisplayName { get; set; } } public interface IDataStore { /// /// Gets a system by its internal system ID. /// /// The with the given internal ID, or null if no system was found. Task GetSystemById(int systemId); /// /// Gets a system by its user-facing human ID. /// /// The with the given human ID, or null if no system was found. Task GetSystemByHid(string systemHid); /// /// Gets a system by one of its linked Discord account IDs. Multiple IDs can return the same system. /// /// The with the given linked account, or null if no system was found. Task GetSystemByAccount(ulong linkedAccount); /// /// Gets a system by its API token. /// /// The with the given API token, or null if no corresponding system was found. Task GetSystemByToken(string apiToken); /// /// Gets the Discord account IDs linked to a system. /// /// An enumerable of Discord account IDs linked to this system. Task> GetSystemAccounts(PKSystem system); /// /// Gets the member count of a system. /// Task GetSystemMemberCount(PKSystem system); /// /// Gets a list of members with proxy tags that conflict with the given tags. /// /// A set of proxy tags A conflict with proxy tags B if both A's prefix and suffix /// are a "subset" of B's. In other words, if A's prefix *starts* with B's prefix /// and A's suffix *ends* with B's suffix, the tag pairs are considered conflicting. /// /// The system to check in. Task> GetConflictingProxies(PKSystem system, ProxyTag tag); /// /// Gets a specific system's guild-specific settings for a given guild. /// Task GetSystemGuildSettings(PKSystem system, ulong guild); /// /// Saves a specific system's guild-specific settings. /// Task SetSystemGuildSettings(PKSystem system, ulong guild, SystemGuildSettings settings); /// /// Creates a system, auto-generating its corresponding IDs. /// /// An optional system name to set. If `null`, will not set a system name. /// The created system model. Task CreateSystem(string systemName); // TODO: throw exception if account is present (when adding) or account isn't present (when removing) /// /// Links a Discord account to a system. /// /// Throws an exception (TODO: which?) if the given account is already linked to a system. Task AddAccount(PKSystem system, ulong accountToAdd); /// /// Unlinks a Discord account from a system. /// /// Will *not* throw if this results in an orphaned system - this is the caller's responsibility to ensure. /// /// Throws an exception (TODO: which?) if the given account is not linked to the given system. Task RemoveAccount(PKSystem system, ulong accountToRemove); /// /// Saves the information within the given struct to the data store. /// Task SaveSystem(PKSystem system); /// /// Deletes the given system from the database. /// /// /// This will also delete all the system's members, all system switches, and every message that has been proxied /// by members in the system. /// Task DeleteSystem(PKSystem system); /// /// Gets a system by its internal member ID. /// /// The with the given internal ID, or null if no member was found. Task GetMemberById(int memberId); /// /// Gets a member by its user-facing human ID. /// /// The with the given human ID, or null if no member was found. Task GetMemberByHid(string memberHid); /// /// Gets a member by its member name within one system. /// /// /// Member names are *usually* unique within a system (but not always), whereas member names /// are almost certainly *not* unique globally - therefore only intra-system lookup is /// allowed. /// /// The with the given name, or null if no member was found. Task GetMemberByName(PKSystem system, string name); /// /// Gets all members inside a given system. /// /// An enumerable of structs representing each member in the system, in no particular order. Task> GetSystemMembers(PKSystem system); /// /// Gets the amount of messages proxied by a given member. /// /// The message count of the given member. Task GetMemberMessageCount(PKMember member); /// /// Collects a breakdown of each member in a system's message count. /// /// An enumerable of members along with their message counts. Task> GetMemberMessageCountBulk(PKSystem system); /// /// Creates a member, auto-generating its corresponding IDs. /// /// The system in which to create the member. /// The name of the member to create. /// The created system model. Task CreateMember(PKSystem system, string name); /// /// Creates multiple members, auto-generating each corresponding ID. /// /// The system to create the member in. /// A dictionary containing a mapping from an arbitrary key to the member's name. /// A dictionary containing the resulting member structs, each mapped to the key given in the argument dictionary. Task> CreateMembersBulk(PKSystem system, Dictionary memberNames); /// /// Saves the information within the given struct to the data store. /// Task SaveMember(PKMember member); /// /// Deletes the given member from the database. /// /// /// This will remove this member from any switches it's involved in, as well as all the messages /// proxied by this member. /// Task DeleteMember(PKMember member); /// /// Gets a specific member's guild-specific settings for a given guild. /// Task GetMemberGuildSettings(PKMember member, ulong guild); /// /// Saves a specific member's guild-specific settings. /// Task SetMemberGuildSettings(PKMember member, ulong guild, MemberGuildSettings settings); /// /// Gets a message and its information by its ID. /// /// The message ID to look up. This can be either the ID of the trigger message containing the proxy tags or the resulting proxied webhook message. /// An extended message object, containing not only the message data itself but the associated system and member structs. Task GetMessage(ulong id); // id is both original and trigger, also add return type struct /// /// Saves a posted message to the database. /// /// The ID of the account that sent the original trigger message. /// The ID of the channel the message was posted to. /// The ID of the message posted by the webhook. /// The ID of the original trigger message containing the proxy tags. /// The member (and by extension system) that was proxied. /// Task AddMessage(ulong senderAccount, ulong channelId, ulong postedMessageId, ulong triggerMessageId, PKMember proxiedMember); /// /// Deletes a message from the data store. /// /// The ID of the webhook message to delete. Task DeleteMessage(ulong postedMessageId); /// /// Deletes messages from the data store in bulk. /// /// The IDs of the webhook messages to delete. Task DeleteMessagesBulk(IEnumerable postedMessageIds); /// /// Gets switches from a system. /// /// An enumerable of the *count* latest switches in the system, in latest-first order. May contain fewer elements than requested. Task> GetSwitches(PKSystem system, int count); /// /// Gets the latest (temporally; closest to now) switch of a given system. /// Task GetLatestSwitch(PKSystem system); /// /// Gets the members a given switch consists of. /// Task> GetSwitchMembers(PKSwitch sw); /// /// Gets a list of fronters over a given period of time. /// /// /// This list is returned as an enumerable of "switch members", each containing a timestamp /// and a member ID. /// /// Switches containing multiple members will be returned as multiple switch members each with the same /// timestamp, and a change in timestamp should be interpreted as the start of a new switch. /// /// An enumerable of the aforementioned "switch members". Task> GetPeriodFronters(PKSystem system, Instant periodStart, Instant periodEnd); /// /// Calculates a breakdown of a system's fronters over a given period, including how long each member has /// been fronting, and how long *no* member has been fronting. /// /// /// Switches containing multiple members will count the full switch duration for all members, meaning /// the total duration may add up to longer than the breakdown period. /// /// /// /// /// Task GetFrontBreakdown(PKSystem system, Instant periodStart, Instant periodEnd); /// /// Registers a switch with the given members in the given system. /// /// Throws an exception (TODO: which?) if any of the members are not in the given system. Task AddSwitch(PKSystem system, IEnumerable switchMembers); /// /// Registers switches in bulk. /// /// A list of switch structs, each containing a timestamp and a list of members. /// Throws an exception (TODO: which?) if any of the given members are not in the given system. Task AddSwitchesBulk(PKSystem system, IEnumerable switches); /// /// Updates the timestamp of a given switch. /// Task MoveSwitch(PKSwitch sw, Instant time); /// /// Deletes a given switch from the data store. /// Task DeleteSwitch(PKSwitch sw); /// /// Deletes all switches in a given system from the data store. /// Task DeleteAllSwitches(PKSystem system); /// /// Gets the total amount of systems in the data store. /// Task GetTotalSystems(); /// /// Gets the total amount of members in the data store. /// Task GetTotalMembers(); /// /// Gets the total amount of switches in the data store. /// Task GetTotalSwitches(); /// /// Gets the total amount of messages in the data store. /// Task GetTotalMessages(); /// /// Gets the guild configuration struct for a given guild, creating and saving one if none was found. /// /// The guild's configuration struct. Task GetOrCreateGuildConfig(ulong guild); /// /// Saves the given guild configuration struct to the data store. /// Task SaveGuildConfig(GuildConfig cfg); } public class PostgresDataStore: IDataStore { private DbConnectionFactory _conn; private ILogger _logger; public PostgresDataStore(DbConnectionFactory conn, ILogger logger) { _conn = conn; _logger = logger; } public async Task> GetConflictingProxies(PKSystem system, ProxyTag tag) { using (var conn = await _conn.Obtain()) // return await conn.QueryAsync("select * from (select *, (unnest(proxy_tags)).prefix as prefix, (unnest(proxy_tags)).suffix as suffix from members where system = @System) as _ where prefix ilike @Prefix and suffix ilike @Suffix", new // { // System = system.Id, // Prefix = tag.Prefix.Replace("%", "\\%") + "%", // Suffix = "%" + tag.Suffix.Replace("%", "\\%") // }); return await conn.QueryAsync("select * from (select *, (unnest(proxy_tags)).prefix as prefix, (unnest(proxy_tags)).suffix as suffix from members where system = @System) as _ where prefix = @Prefix and suffix = @Suffix", new { System = system.Id, Prefix = tag.Prefix, Suffix = tag.Suffix }); } public async Task GetSystemGuildSettings(PKSystem system, ulong guild) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync( "select * from system_guild where system = @System and guild = @Guild", new {System = system.Id, Guild = guild}) ?? new SystemGuildSettings(); } public async Task SetSystemGuildSettings(PKSystem system, ulong guild, SystemGuildSettings settings) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("insert into system_guild (system, guild, proxy_enabled) values (@System, @Guild, @ProxyEnabled) on conflict (system, guild) do update set proxy_enabled = @ProxyEnabled", new { System = system.Id, Guild = guild, ProxyEnabled = settings.ProxyEnabled }); } public async Task CreateSystem(string systemName = null) { string hid; do { hid = Utils.GenerateHid(); } while (await GetSystemByHid(hid) != null); PKSystem system; using (var conn = await _conn.Obtain()) system = await conn.QuerySingleAsync("insert into systems (hid, name) values (@Hid, @Name) returning *", new { Hid = hid, Name = systemName }); _logger.Information("Created system {System}", system.Id); return system; } public async Task AddAccount(PKSystem system, ulong accountId) { // We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent // This is used in import/export, although the pk;link command checks for this case beforehand using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId) on conflict do nothing", new { Id = accountId, SystemId = system.Id }); _logger.Information("Linked system {System} to account {Account}", system.Id, accountId); } public async Task RemoveAccount(PKSystem system, ulong accountId) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("delete from accounts where uid = @Id and system = @SystemId", new { Id = accountId, SystemId = system.Id }); _logger.Information("Unlinked system {System} from account {Account}", system.Id, accountId); } public async Task GetSystemByAccount(ulong accountId) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id", new { Id = accountId }); } public async Task GetSystemByHid(string hid) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() }); } public async Task GetSystemByToken(string token) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from systems where token = @Token", new { Token = token }); } public async Task GetSystemById(int id) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from systems where id = @Id", new { Id = id }); } public async Task SaveSystem(PKSystem system) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("update systems set name = @Name, description = @Description, tag = @Tag, avatar_url = @AvatarUrl, token = @Token, ui_tz = @UiTz where id = @Id", system); _logger.Information("Updated system {@System}", system); } public async Task DeleteSystem(PKSystem system) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("delete from systems where id = @Id", system); _logger.Information("Deleted system {System}", system.Id); } public async Task> GetSystemAccounts(PKSystem system) { using (var conn = await _conn.Obtain()) return await conn.QueryAsync("select uid from accounts where system = @Id", new { Id = system.Id }); } public async Task DeleteAllSwitches(PKSystem system) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("delete from switches where system = @Id", system); } public async Task GetTotalSystems() { using (var conn = await _conn.Obtain()) return await conn.ExecuteScalarAsync("select count(id) from systems"); } public async Task CreateMember(PKSystem system, string name) { string hid; do { hid = Utils.GenerateHid(); } while (await GetMemberByHid(hid) != null); PKMember member; using (var conn = await _conn.Obtain()) member = await conn.QuerySingleAsync("insert into members (hid, system, name) values (@Hid, @SystemId, @Name) returning *", new { Hid = hid, SystemID = system.Id, Name = name }); _logger.Information("Created member {Member}", member.Id); return member; } public async Task> CreateMembersBulk(PKSystem system, Dictionary names) { using (var conn = await _conn.Obtain()) using (var tx = conn.BeginTransaction()) { var results = new Dictionary(); foreach (var name in names) { string hid; do { hid = await conn.QuerySingleOrDefaultAsync("SELECT @Hid WHERE NOT EXISTS (SELECT id FROM members WHERE hid = @Hid LIMIT 1)", new { Hid = Utils.GenerateHid() }); } while (hid == null); var member = await conn.QuerySingleAsync("INSERT INTO members (hid, system, name) VALUES (@Hid, @SystemId, @Name) RETURNING *", new { Hid = hid, SystemID = system.Id, Name = name.Value }); results.Add(name.Key, member); } tx.Commit(); _logger.Information("Created {MemberCount} members for system {SystemID}", names.Count(), system.Hid); return results; } } public async Task GetMemberById(int id) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from members where id = @Id", new { Id = id }); } public async Task GetMemberByHid(string hid) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleOrDefaultAsync("select * from members where hid = @Hid", new { Hid = hid.ToLower() }); } public async Task GetMemberByName(PKSystem system, string name) { // QueryFirst, since members can (in rare cases) share names using (var conn = await _conn.Obtain()) return await conn.QueryFirstOrDefaultAsync("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system.Id }); } public async Task> GetSystemMembers(PKSystem system) { using (var conn = await _conn.Obtain()) return await conn.QueryAsync("select * from members where system = @SystemID", new { SystemID = system.Id }); } public async Task SaveMember(PKMember member) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("update members set name = @Name, display_name = @DisplayName, description = @Description, color = @Color, avatar_url = @AvatarUrl, birthday = @Birthday, pronouns = @Pronouns, proxy_tags = @ProxyTags, keep_proxy = @KeepProxy where id = @Id", member); _logger.Information("Updated member {@Member}", member); } public async Task DeleteMember(PKMember member) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("delete from members where id = @Id", member); _logger.Information("Deleted member {@Member}", member); } public async Task GetMemberGuildSettings(PKMember member, ulong guild) { using var conn = await _conn.Obtain(); return await conn.QuerySingleOrDefaultAsync( "select * from member_guild where member = @Member and guild = @Guild", new { Member = member.Id, Guild = guild}) ?? new MemberGuildSettings(); } public async Task SetMemberGuildSettings(PKMember member, ulong guild, MemberGuildSettings settings) { using var conn = await _conn.Obtain(); await conn.ExecuteAsync( "insert into member_guild (member, guild, display_name) values (@Member, @Guild, @DisplayName) on conflict (member, guild) do update set display_name = @Displayname", new {Member = member.Id, Guild = guild, DisplayName = settings.DisplayName}); } public async Task GetMemberMessageCount(PKMember member) { using (var conn = await _conn.Obtain()) return await conn.QuerySingleAsync("select count(*) from messages where member = @Id", member); } public async Task> GetMemberMessageCountBulk(PKSystem system) { using (var conn = await _conn.Obtain()) return await conn.QueryAsync( @"SELECT messages.member, COUNT(messages.member) messagecount FROM members JOIN messages ON members.id = messages.member WHERE members.system = @System GROUP BY messages.member", new { System = system.Id }); } public async Task GetSystemMemberCount(PKSystem system) { using (var conn = await _conn.Obtain()) return await conn.ExecuteScalarAsync("select count(*) from members where system = @Id", system); } public async Task GetTotalMembers() { using (var conn = await _conn.Obtain()) return await conn.ExecuteScalarAsync("select count(id) from members"); } public async Task AddMessage(ulong senderId, ulong messageId, ulong channelId, ulong originalMessage, PKMember member) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("insert into messages(mid, channel, member, sender, original_mid) values(@MessageId, @ChannelId, @MemberId, @SenderId, @OriginalMid)", new { MessageId = messageId, ChannelId = channelId, MemberId = member.Id, SenderId = senderId, OriginalMid = originalMessage }); _logger.Information("Stored message {Message} in channel {Channel}", messageId, channelId); } public async Task GetMessage(ulong id) { using (var conn = await _conn.Obtain()) return (await conn.QueryAsync("select messages.*, members.*, systems.* from messages, members, systems where (mid = @Id or original_mid = @Id) and messages.member = members.id and systems.id = members.system", (msg, member, system) => new FullMessage { Message = msg, System = system, Member = member }, new { Id = id })).FirstOrDefault(); } public async Task DeleteMessage(ulong id) { using (var conn = await _conn.Obtain()) if (await conn.ExecuteAsync("delete from messages where mid = @Id", new { Id = id }) > 0) _logger.Information("Deleted message {Message}", id); } public async Task DeleteMessagesBulk(IEnumerable ids) { using (var conn = await _conn.Obtain()) { // Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong // Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway) var foundCount = await conn.ExecuteAsync("delete from messages where mid = any(@Ids)", new {Ids = ids.Select(id => (long) id).ToArray()}); if (foundCount > 0) _logger.Information("Bulk deleted messages {Messages}, {FoundCount} found", ids, foundCount); } } public async Task GetTotalMessages() { using (var conn = await _conn.Obtain()) return await conn.ExecuteScalarAsync("select count(mid) from messages"); } // Same as GuildConfig, but with ISet as long[] instead. private struct DatabaseCompatibleGuildConfig { public ulong Id { get; set; } public ulong? LogChannel { get; set; } public long[] LogBlacklist { get; set; } public long[] Blacklist { get; set; } } public async Task GetOrCreateGuildConfig(ulong guild) { using (var conn = await _conn.Obtain()) { var compat = await conn.QuerySingleOrDefaultAsync( "insert into servers (id) values (@Id) on conflict do nothing; select * from servers where id = @Id", new {Id = guild}); return new GuildConfig { Id = compat.Id, LogChannel = compat.LogChannel, LogBlacklist = new HashSet(compat.LogBlacklist.Select(c => (ulong) c)), Blacklist = new HashSet(compat.Blacklist.Select(c => (ulong) c)), }; } } public async Task SaveGuildConfig(GuildConfig cfg) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("insert into servers (id, log_channel, log_blacklist, blacklist) values (@Id, @LogChannel, @LogBlacklist, @Blacklist) on conflict (id) do update set log_channel = @LogChannel, log_blacklist = @LogBlacklist, blacklist = @Blacklist", new { cfg.Id, cfg.LogChannel, LogBlacklist = cfg.LogBlacklist.Select(c => (long) c).ToList(), Blacklist = cfg.Blacklist.Select(c => (long) c).ToList() }); _logger.Information("Updated guild configuration {@GuildCfg}", cfg); } public async Task AddSwitch(PKSystem system, IEnumerable members) { // Use a transaction here since we're doing multiple executed commands in one using (var conn = await _conn.Obtain()) using (var tx = conn.BeginTransaction()) { // First, we insert the switch itself var sw = await conn.QuerySingleAsync("insert into switches(system) values (@System) returning *", new {System = system.Id}); // Then we insert each member in the switch in the switch_members table // TODO: can we parallelize this or send it in bulk somehow? foreach (var member in members) { await conn.ExecuteAsync( "insert into switch_members(switch, member) values(@Switch, @Member)", new {Switch = sw.Id, Member = member.Id}); } // Finally we commit the tx, since the using block will otherwise rollback it tx.Commit(); _logger.Information("Registered switch {Switch} in system {System} with members {@Members}", sw.Id, system.Id, members.Select(m => m.Id)); } } public async Task AddSwitchesBulk(PKSystem system, IEnumerable switches) { // Read existing switches to enforce unique timestamps var priorSwitches = await GetSwitches(system); var lastSwitchId = priorSwitches.Any() ? priorSwitches.Max(x => x.Id) : 0; using (var conn = (PerformanceTrackingConnection) await _conn.Obtain()) { using (var tx = conn.BeginTransaction()) { // Import switches in bulk using (var importer = conn.BeginBinaryImport("COPY switches (system, timestamp) FROM STDIN (FORMAT BINARY)")) { foreach (var sw in switches) { // If there's already a switch at this time, move on if (priorSwitches.Any(x => x.Timestamp.Equals(sw.Timestamp))) continue; // Otherwise, add it to the importer importer.StartRow(); importer.Write(system.Id, NpgsqlTypes.NpgsqlDbType.Integer); importer.Write(sw.Timestamp, NpgsqlTypes.NpgsqlDbType.Timestamp); } importer.Complete(); // Commits the copy operation so dispose won't roll it back } // Get all switches that were created above and don't have members for ID lookup var switchesWithoutMembers = await conn.QueryAsync(@" SELECT switches.* FROM switches LEFT JOIN switch_members ON switch_members.switch = switches.id WHERE switches.id > @LastSwitchId AND switches.system = @System AND switch_members.id IS NULL", new { LastSwitchId = lastSwitchId, System = system.Id }); // Import switch_members in bulk using (var importer = conn.BeginBinaryImport("COPY switch_members (switch, member) FROM STDIN (FORMAT BINARY)")) { // Iterate over the switches we created above and set their members foreach (var pkSwitch in switchesWithoutMembers) { // If this isn't in our import set, move on var sw = switches.Select(x => (ImportedSwitch?) x).FirstOrDefault(x => x.Value.Timestamp.Equals(pkSwitch.Timestamp)); if (sw == null) continue; // Loop through associated members to add each to the switch foreach (var m in sw.Value.Members) { // Skip switch-outs - these don't have switch_members if (m == null) continue; importer.StartRow(); importer.Write(pkSwitch.Id, NpgsqlTypes.NpgsqlDbType.Integer); importer.Write(m.Id, NpgsqlTypes.NpgsqlDbType.Integer); } } importer.Complete(); // Commits the copy operation so dispose won't roll it back } tx.Commit(); } } _logger.Information("Completed bulk import of switches for system {0}", system.Hid); } public async Task> GetSwitches(PKSystem system, int count = 9999999) { // TODO: refactor the PKSwitch data structure to somehow include a hydrated member list // (maybe when we get caching in?) using (var conn = await _conn.Obtain()) return await conn.QueryAsync("select * from switches where system = @System order by timestamp desc limit @Count", new {System = system.Id, Count = count}); } public async Task> GetSwitchMembersList(PKSystem system, Instant start, Instant end) { // Wrap multiple commands in a single transaction for performance using (var conn = await _conn.Obtain()) using (var tx = conn.BeginTransaction()) { // Find the time of the last switch outside the range as it overlaps the range // If no prior switch exists, the lower bound of the range remains the start time var lastSwitch = await conn.QuerySingleOrDefaultAsync( @"SELECT COALESCE(MAX(timestamp), @Start) FROM switches WHERE switches.system = @System AND switches.timestamp < @Start", new { System = system.Id, Start = start }); // Then collect the time and members of all switches that overlap the range var switchMembersEntries = await conn.QueryAsync( @"SELECT switch_members.member, switches.timestamp FROM switches LEFT JOIN switch_members ON switches.id = switch_members.switch WHERE switches.system = @System AND ( switches.timestamp >= @Start OR switches.timestamp = @LastSwitch ) AND switches.timestamp < @End ORDER BY switches.timestamp DESC", new { System = system.Id, Start = start, End = end, LastSwitch = lastSwitch }); // Commit and return the list tx.Commit(); return switchMembersEntries; } } public async Task> GetSwitchMembers(PKSwitch sw) { using (var conn = await _conn.Obtain()) return await conn.QueryAsync( "select * from switch_members, members where switch_members.member = members.id and switch_members.switch = @Switch order by switch_members.id", new {Switch = sw.Id}); } public async Task GetLatestSwitch(PKSystem system) => (await GetSwitches(system, 1)).FirstOrDefault(); public async Task MoveSwitch(PKSwitch sw, Instant time) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("update switches set timestamp = @Time where id = @Id", new {Time = time, Id = sw.Id}); _logger.Information("Moved switch {Switch} to {Time}", sw.Id, time); } public async Task DeleteSwitch(PKSwitch sw) { using (var conn = await _conn.Obtain()) await conn.ExecuteAsync("delete from switches where id = @Id", new {Id = sw.Id}); _logger.Information("Deleted switch {Switch}"); } public async Task GetTotalSwitches() { using (var conn = await _conn.Obtain()) return await conn.ExecuteScalarAsync("select count(id) from switches"); } public async Task> GetPeriodFronters(PKSystem system, Instant periodStart, Instant periodEnd) { // Returns the timestamps and member IDs of switches overlapping the range, in chronological (newest first) order var switchMembers = await GetSwitchMembersList(system, periodStart, periodEnd); // query DB for all members involved in any of the switches above and collect into a dictionary for future use // this makes sure the return list has the same instances of PKMember throughout, which is important for the dictionary // key used in GetPerMemberSwitchDuration below Dictionary memberObjects; using (var conn = await _conn.Obtain()) { memberObjects = ( await conn.QueryAsync( "select * from members where id = any(@Switches)", // lol postgres specific `= any()` syntax new { Switches = switchMembers.Select(m => m.Member).Distinct().ToList() }) ).ToDictionary(m => m.Id); } // Initialize entries - still need to loop to determine the TimespanEnd below var entries = from item in switchMembers group item by item.Timestamp into g select new SwitchListEntry { TimespanStart = g.Key, Members = g.Where(x => x.Member != 0).Select(x => memberObjects[x.Member]).ToList() }; // Loop through every switch that overlaps the range and add it to the output list // end time is the *FOLLOWING* switch's timestamp - we cheat by working backwards from the range end, so no dates need to be compared var endTime = periodEnd; var outList = new List(); foreach (var e in entries) { // Override the start time of the switch if it's outside the range (only true for the "out of range" switch we included above) var switchStartClamped = e.TimespanStart < periodStart ? periodStart : e.TimespanStart; outList.Add(new SwitchListEntry { Members = e.Members, TimespanStart = switchStartClamped, TimespanEnd = endTime }); // next switch's end is this switch's start (we're working backward in time) endTime = e.TimespanStart; } return outList; } public async Task GetFrontBreakdown(PKSystem system, Instant periodStart, Instant periodEnd) { var dict = new Dictionary(); var noFronterDuration = Duration.Zero; // Sum up all switch durations for each member // switches with multiple members will result in the duration to add up to more than the actual period range var actualStart = periodEnd; // will be "pulled" down var actualEnd = periodStart; // will be "pulled" up foreach (var sw in await GetPeriodFronters(system, periodStart, periodEnd)) { var span = sw.TimespanEnd - sw.TimespanStart; foreach (var member in sw.Members) { if (!dict.ContainsKey(member)) dict.Add(member, span); else dict[member] += span; } if (sw.Members.Count == 0) noFronterDuration += span; if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart; if (sw.TimespanEnd > actualEnd) actualEnd = sw.TimespanEnd; } return new FrontBreakdown { MemberSwitchDurations = dict, NoFronterDuration = noFronterDuration, RangeStart = actualStart, RangeEnd = actualEnd }; } } }