Move count stat collecting to raw SQL

This commit is contained in:
Ske 2020-06-13 19:15:29 +02:00
parent 767a37e637
commit 2038f023a0
3 changed files with 19 additions and 46 deletions

View File

@ -5,6 +5,8 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using App.Metrics; using App.Metrics;
using Dapper;
using DSharpPlus; using DSharpPlus;
using DSharpPlus.Entities; using DSharpPlus.Entities;
@ -21,7 +23,7 @@ namespace PluralKit.Bot
private IMetrics _metrics; private IMetrics _metrics;
private CpuStatService _cpu; private CpuStatService _cpu;
private IDataStore _data; private Database _db;
private WebhookCacheService _webhookCache; private WebhookCacheService _webhookCache;
@ -29,14 +31,14 @@ namespace PluralKit.Bot
private ILogger _logger; 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; _client = client;
_metrics = metrics; _metrics = metrics;
_webhookCache = webhookCache; _webhookCache = webhookCache;
_countHolder = countHolder; _countHolder = countHolder;
_data = data;
_cpu = cpu; _cpu = cpu;
_db = db;
_logger = logger.ForContext<PeriodicStatCollector>(); _logger = logger.ForContext<PeriodicStatCollector>();
} }
@ -78,10 +80,11 @@ namespace PluralKit.Bot
_metrics.Measure.Gauge.SetValue(BotMetrics.MembersOnline, usersOnline.Count); _metrics.Measure.Gauge.SetValue(BotMetrics.MembersOnline, usersOnline.Count);
// Aggregate DB stats // Aggregate DB stats
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, await _data.GetTotalSystems()); var counts = await _db.Execute(c => c.QueryFirstAsync<Counts>("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.MemberCount, await _data.GetTotalMembers()); _metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.Systems);
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, await _data.GetTotalSwitches()); _metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.Members);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, await _data.GetTotalMessages()); _metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.Switches);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.Messages);
// Process info // Process info
var process = Process.GetCurrentProcess(); var process = Process.GetCurrentProcess();
@ -101,5 +104,13 @@ namespace PluralKit.Bot
stopwatch.Stop(); stopwatch.Stop();
_logger.Information("Updated metrics in {Time}", stopwatch.ElapsedDuration()); _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; }
}
} }
} }

View File

@ -283,25 +283,5 @@ namespace PluralKit.Core {
/// Deletes all switches in a given system from the data store. /// Deletes all switches in a given system from the data store.
/// </summary> /// </summary>
Task DeleteAllSwitches(PKSystem system); Task DeleteAllSwitches(PKSystem system);
/// <summary>
/// Gets the total amount of systems in the data store.
/// </summary>
Task<ulong> GetTotalSystems();
/// <summary>
/// Gets the total amount of members in the data store.
/// </summary>
Task<ulong> GetTotalMembers();
/// <summary>
/// Gets the total amount of switches in the data store.
/// </summary>
Task<ulong> GetTotalSwitches();
/// <summary>
/// Gets the total amount of messages in the data store.
/// </summary>
Task<ulong> GetTotalMessages();
} }
} }

View File

@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -113,12 +113,6 @@ namespace PluralKit.Core {
await conn.ExecuteAsync("delete from switches where system = @Id", system); await conn.ExecuteAsync("delete from switches where system = @Id", system);
} }
public async Task<ulong> GetTotalSystems()
{
using (var conn = await _conn.Obtain())
return await conn.ExecuteScalarAsync<ulong>("select count(id) from systems");
}
public async Task<PKMember> CreateMember(PKSystem system, string name) { public async Task<PKMember> CreateMember(PKSystem system, string name) {
PKMember member; PKMember member;
using (var conn = await _conn.Obtain()) using (var conn = await _conn.Obtain())
@ -226,12 +220,6 @@ namespace PluralKit.Core {
} }
} }
public async Task<ulong> GetTotalMessages()
{
using (var conn = await _conn.Obtain())
return await conn.ExecuteScalarAsync<ulong>("select count(mid) from messages");
}
public async Task AddSwitch(PKSystem system, IEnumerable<PKMember> members) public async Task AddSwitch(PKSystem system, IEnumerable<PKMember> members)
{ {
// Use a transaction here since we're doing multiple executed commands in one // 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}"); _logger.Information("Deleted switch {Switch}");
} }
public async Task<ulong> GetTotalSwitches()
{
using (var conn = await _conn.Obtain())
return await conn.ExecuteScalarAsync<ulong>("select count(id) from switches");
}
public async Task<IEnumerable<SwitchListEntry>> GetPeriodFronters(PKSystem system, Instant periodStart, Instant periodEnd) public async Task<IEnumerable<SwitchListEntry>> GetPeriodFronters(PKSystem system, Instant periodStart, Instant periodEnd)
{ {
// TODO: IAsyncEnumerable-ify this one // TODO: IAsyncEnumerable-ify this one