From b931e935b511e37c30b8315290b12fec1b58201e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Tue, 9 Apr 2024 03:45:06 +0200 Subject: [PATCH] Misc MonitoringPlugin enhancements --- .../MonitoringPlugin.cs | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs b/ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs index 9d8c668954b4f..9b95b85482747 100644 --- a/ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs +++ b/ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs @@ -52,13 +52,13 @@ internal sealed class MonitoringPlugin : OfficialPlugin, IWebServiceProvider, IG private const string UnknownLabelValueFallback = "unknown"; - private static readonly Measurement BuildInfo = new( + private static readonly Measurement BuildInfo = new( 1, new KeyValuePair(TagNames.Version, SharedInfo.Version.ToString()), new KeyValuePair(TagNames.Variant, SharedInfo.BuildInfo.Variant) ); - private static readonly Measurement RuntimeInfo = new( + private static readonly Measurement RuntimeInfo = new( 1, new KeyValuePair(TagNames.Framework, OS.Framework ?? UnknownLabelValueFallback), new KeyValuePair(TagNames.Runtime, OS.Runtime ?? UnknownLabelValueFallback), @@ -147,13 +147,29 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bots", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); - - return new List>(4) { - new(bots.Count, new KeyValuePair(TagNames.BotState, "configured")), - new(bots.Count(static bot => bot.IsConnectedAndLoggedOn), new KeyValuePair(TagNames.BotState, "online")), - new(bots.Count(static bot => !bot.IsConnectedAndLoggedOn), new KeyValuePair(TagNames.BotState, "offline")), - new(bots.Count(static bot => bot.CardsFarmer.NowFarming), new KeyValuePair(TagNames.BotState, "farming")) + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); + + int onlineCount = 0; + int offlineCount = 0; + int farmingCount = 0; + + foreach (Bot bot in bots) { + if (bot.IsConnectedAndLoggedOn) { + onlineCount++; + } else { + offlineCount++; + } + + if (bot.CardsFarmer.NowFarming) { + farmingCount++; + } + } + + return new HashSet>(4) { + new(onlineCount + offlineCount, new KeyValuePair(TagNames.BotState, "configured")), + new(onlineCount, new KeyValuePair(TagNames.BotState, "online")), + new(offlineCount, new KeyValuePair(TagNames.BotState, "offline")), + new(farmingCount, new KeyValuePair(TagNames.BotState, "farming")) }; }, description: "Number of bots that are currently loaded in ASF" @@ -161,7 +177,7 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_friends", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); return bots.Where(static bot => bot.IsConnectedAndLoggedOn).Select(static bot => new Measurement(bot.SteamFriends.GetFriendCount(), new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); }, @@ -170,7 +186,7 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_clans", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); return bots.Where(static bot => bot.IsConnectedAndLoggedOn).Select(static bot => new Measurement(bot.SteamFriends.GetClanCount(), new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); }, @@ -180,7 +196,7 @@ private void InitializeMeter() { // Keep in mind that we use a unit here and the unit needs to be a suffix to the name Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_farming_time_remaining_{Units.Minutes}", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); return bots.Select(static bot => new Measurement(bot.CardsFarmer.TimeRemaining.TotalMinutes, new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); }, @@ -190,7 +206,7 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_heartbeat_failures", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); return bots.Select(static bot => new Measurement(bot.HeartBeatFailures, new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); }, @@ -199,7 +215,7 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_wallet_balance", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); return bots.Where(static bot => bot.WalletCurrency != ECurrencyCode.Invalid).Select(static bot => new Measurement(bot.WalletBalance, new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID), new KeyValuePair(TagNames.CurrencyCode, bot.WalletCurrency.ToString()))); }, @@ -208,9 +224,9 @@ private void InitializeMeter() { Meter.CreateObservableGauge( $"{MetricNamePrefix}_bot_bgr_keys_remaining", static () => { - ICollection bots = Bot.Bots?.Values ?? Array.Empty(); + IEnumerable bots = Bot.Bots?.Values ?? Enumerable.Empty(); - return bots.Select(static bot => new Measurement(bot.GamesToRedeemInBackgroundCount, new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); + return bots.Select(static bot => new Measurement((int) bot.GamesToRedeemInBackgroundCount, new KeyValuePair(TagNames.BotName, bot.BotName), new KeyValuePair(TagNames.SteamID, bot.SteamID))); }, description: "Remaining games to redeem in background per bot" );