Move count stat collecting to raw SQL
This commit is contained in:
parent
767a37e637
commit
2038f023a0
@ -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<PeriodicStatCollector>();
|
||||
}
|
||||
|
||||
@ -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<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.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; }
|
||||
}
|
||||
}
|
||||
}
|
@ -283,25 +283,5 @@ namespace PluralKit.Core {
|
||||
/// Deletes all switches in a given system from the data store.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
@ -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<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) {
|
||||
PKMember member;
|
||||
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)
|
||||
{
|
||||
// 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<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)
|
||||
{
|
||||
// TODO: IAsyncEnumerable-ify this one
|
||||
|
Loading…
Reference in New Issue
Block a user