diff --git a/PluralKit.Bot/Services/PeriodicStatCollector.cs b/PluralKit.Bot/Services/PeriodicStatCollector.cs index 73114be3..4e2a3ff0 100644 --- a/PluralKit.Bot/Services/PeriodicStatCollector.cs +++ b/PluralKit.Bot/Services/PeriodicStatCollector.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Threading.Tasks; using App.Metrics; +using Dapper; + using DSharpPlus; using DSharpPlus.Entities; @@ -21,7 +23,7 @@ namespace PluralKit.Bot private IMetrics _metrics; private CpuStatService _cpu; - private IDataStore _data; + private Database _db; private WebhookCacheService _webhookCache; @@ -29,14 +31,14 @@ namespace PluralKit.Bot private ILogger _logger; - public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, IDataStore data, CpuStatService cpu) + public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, Database db) { _client = client; _metrics = metrics; _webhookCache = webhookCache; _countHolder = countHolder; - _data = data; _cpu = cpu; + _db = db; _logger = logger.ForContext(); } @@ -78,10 +80,11 @@ namespace PluralKit.Bot _metrics.Measure.Gauge.SetValue(BotMetrics.MembersOnline, usersOnline.Count); // Aggregate DB stats - _metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, await _data.GetTotalSystems()); - _metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, await _data.GetTotalMembers()); - _metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, await _data.GetTotalSwitches()); - _metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, await _data.GetTotalMessages()); + var counts = await _db.Execute(c => c.QueryFirstAsync("select (select count(*) from systems) as systems, (select count(*) from members) as members, (select count(*) from switches) as switches, (select count(*) from messages) as messages")); + _metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.Systems); + _metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.Members); + _metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.Switches); + _metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.Messages); // Process info var process = Process.GetCurrentProcess(); @@ -101,5 +104,13 @@ namespace PluralKit.Bot stopwatch.Stop(); _logger.Information("Updated metrics in {Time}", stopwatch.ElapsedDuration()); } + + public class Counts + { + public int Systems { get; } + public int Members { get; } + public int Switches { get; } + public int Messages { get; } + } } } \ No newline at end of file diff --git a/PluralKit.Core/Services/IDataStore.cs b/PluralKit.Core/Services/IDataStore.cs index 94f2aec9..b7ac925a 100644 --- a/PluralKit.Core/Services/IDataStore.cs +++ b/PluralKit.Core/Services/IDataStore.cs @@ -283,25 +283,5 @@ namespace PluralKit.Core { /// 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(); } } \ No newline at end of file diff --git a/PluralKit.Core/Services/PostgresDataStore.cs b/PluralKit.Core/Services/PostgresDataStore.cs index cf41cfff..b8f7e9b8 100644 --- a/PluralKit.Core/Services/PostgresDataStore.cs +++ b/PluralKit.Core/Services/PostgresDataStore.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -113,12 +113,6 @@ namespace PluralKit.Core { 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) { PKMember member; using (var conn = await _conn.Obtain()) @@ -226,12 +220,6 @@ namespace PluralKit.Core { } } - public async Task GetTotalMessages() - { - using (var conn = await _conn.Obtain()) - return await conn.ExecuteScalarAsync("select count(mid) from messages"); - } - public async Task AddSwitch(PKSystem system, IEnumerable members) { // Use a transaction here since we're doing multiple executed commands in one @@ -336,12 +324,6 @@ namespace PluralKit.Core { _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) { // TODO: IAsyncEnumerable-ify this one