Convert periodic stat collector

This commit is contained in:
Ske 2021-01-31 14:59:45 +01:00
parent 227d68a2a4
commit b48a77df8d

View File

@ -1,12 +1,11 @@
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using App.Metrics; using App.Metrics;
using Dapper; using Dapper;
using DSharpPlus; using Myriad.Cache;
using DSharpPlus.Entities; using Myriad.Types;
using NodaTime.Extensions; using NodaTime.Extensions;
using PluralKit.Core; using PluralKit.Core;
@ -17,8 +16,8 @@ namespace PluralKit.Bot
{ {
public class PeriodicStatCollector public class PeriodicStatCollector
{ {
private readonly DiscordShardedClient _client;
private readonly IMetrics _metrics; private readonly IMetrics _metrics;
private readonly IDiscordCache _cache;
private readonly CpuStatService _cpu; private readonly CpuStatService _cpu;
private readonly IDatabase _db; private readonly IDatabase _db;
@ -29,14 +28,14 @@ namespace PluralKit.Bot
private readonly ILogger _logger; private readonly ILogger _logger;
public PeriodicStatCollector(DiscordShardedClient client, IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db) public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db, IDiscordCache cache)
{ {
_client = client;
_metrics = metrics; _metrics = metrics;
_webhookCache = webhookCache; _webhookCache = webhookCache;
_countHolder = countHolder; _countHolder = countHolder;
_cpu = cpu; _cpu = cpu;
_db = db; _db = db;
_cache = cache;
_logger = logger.ForContext<PeriodicStatCollector>(); _logger = logger.ForContext<PeriodicStatCollector>();
} }
@ -46,36 +45,22 @@ namespace PluralKit.Bot
stopwatch.Start(); stopwatch.Start();
// Aggregate guild/channel stats // Aggregate guild/channel stats
var guildCount = 0; var guildCount = 0;
var channelCount = 0; var channelCount = 0;
// No LINQ today, sorry // No LINQ today, sorry
foreach (var shard in _client.ShardClients.Values) await foreach (var guild in _cache.GetAllGuilds())
{ {
guildCount += shard.Guilds.Count; guildCount++;
foreach (var guild in shard.Guilds.Values) foreach (var channel in _cache.GetGuildChannels(guild.Id))
foreach (var channel in guild.Channels.Values) {
if (channel.Type == ChannelType.Text) if (channel.Type == Channel.ChannelType.GuildText)
channelCount++; channelCount++;
}
} }
_metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, guildCount); _metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, guildCount);
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount); _metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount);
// Aggregate member stats
var usersKnown = new HashSet<ulong>();
var usersOnline = new HashSet<ulong>();
foreach (var shard in _client.ShardClients.Values)
foreach (var guild in shard.Guilds.Values)
foreach (var user in guild.Members.Values)
{
usersKnown.Add(user.Id);
if (user.Presence?.Status == UserStatus.Online)
usersOnline.Add(user.Id);
}
_metrics.Measure.Gauge.SetValue(BotMetrics.MembersTotal, usersKnown.Count);
_metrics.Measure.Gauge.SetValue(BotMetrics.MembersOnline, usersOnline.Count);
// Aggregate DB stats // Aggregate DB stats
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, (select count(*) from groups) as groups")); 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, (select count(*) from groups) as groups"));