add metrics (without reporting, for now)

This commit is contained in:
sam 2024-08-15 17:23:56 +02:00
parent 5585ffd6ea
commit 14b132e466
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
9 changed files with 183 additions and 9 deletions

View file

@ -1,12 +1,12 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using App.Metrics;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Extensions;
using Humanizer;
using Microsoft.EntityFrameworkCore;
using NodaTime;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API.Abstractions.Objects;
@ -15,6 +15,7 @@ using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Gateway;
using Remora.Results;
using IClock = NodaTime.IClock;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
@ -22,11 +23,11 @@ namespace Catalogger.Backend.Bot.Commands;
[Group("catalogger")]
public class MetaCommands(
IClock clock,
DatabaseContext db,
IMetrics metrics,
DiscordGatewayClient client,
IFeedbackService feedbackService,
GuildCacheService guildCache,
ChannelCacheService channelCache,
IFeedbackService feedbackService,
IDiscordRestChannelAPI channelApi) : CommandGroup
{
[Command("ping")]
@ -37,7 +38,6 @@ public class MetaCommands(
var msg = await feedbackService.SendContextualAsync("...").GetOrThrow();
var elapsed = clock.GetCurrentInstant() - t1;
var messageCount = await db.Messages.CountAsync();
var process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64;
@ -49,11 +49,19 @@ public class MetaCommands(
inline: true);
embed.AddField("Memory usage", memoryUsage.Bytes().Humanize(), inline: true);
var messagesReceived = metrics.Snapshot.GetForContext("Bot").Meters
.FirstOrDefault(m => m.MultidimensionalName == CataloggerMetrics.MessagesReceived.Name)?.Value;
if (messagesReceived != null)
embed.AddField("Messages received", $"{messagesReceived.OneMinuteRate * 60:F1}/m", true);
var messageCount = metrics.Snapshot.GetForContext("Bot").Gauges
.FirstOrDefault(m => m.MultidimensionalName == CataloggerMetrics.MessagesStored.Name)?.Value ?? 0;
embed.AddField("Numbers",
$"{messageCount:N0} messages from {guildCache.Size:N0} servers\nCached {channelCache.Size:N0} channels",
inline: false);
List<IEmbed> embeds = [embed.Build().GetOrThrow()];
IEmbed[] embeds = [embed.Build().GetOrThrow()];
return (Result)await channelApi.EditMessageAsync(msg.ChannelID, msg.ID, content: "", embeds: embeds);
}