Monitor incoming trades (#3201)

* Monitor incoming trades

* Apply feedback

* Misc.
This commit is contained in:
Sebastian Göls 2024-05-28 19:03:52 +02:00 committed by GitHub
parent e9ca1e3537
commit 668bf5009b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 143 additions and 1 deletions

View file

@ -22,6 +22,7 @@
// limitations under the License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Composition;
@ -35,6 +36,7 @@ using ArchiSteamFarm.IPC.Integration;
using ArchiSteamFarm.Plugins;
using ArchiSteamFarm.Plugins.Interfaces;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Exchange;
using ArchiSteamFarm.Storage;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
@ -45,7 +47,7 @@ namespace ArchiSteamFarm.OfficialPlugins.Monitoring;
[Export(typeof(IPlugin))]
[SuppressMessage("ReSharper", "MemberCanBeFileLocal")]
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider, IBotTradeOfferResults {
private const string MeterName = SharedInfo.AssemblyName;
private const string MetricNamePrefix = "asf";
@ -77,10 +79,25 @@ internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialG
[Required]
public override Version Version => typeof(MonitoringPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));
private readonly ConcurrentDictionary<Bot, TradeStatistics> TradeStatistics = new();
private Meter? Meter;
public void Dispose() => Meter?.Dispose();
public Task OnBotTradeOfferResults(Bot bot, IReadOnlyCollection<ParseTradeResult> tradeResults) {
ArgumentNullException.ThrowIfNull(bot);
ArgumentNullException.ThrowIfNull(tradeResults);
TradeStatistics statistics = TradeStatistics.GetOrAdd(bot, static _ => new TradeStatistics());
foreach (ParseTradeResult result in tradeResults) {
statistics.Include(result);
}
return Task.CompletedTask;
}
public void OnConfiguringEndpoints(IApplicationBuilder app) {
ArgumentNullException.ThrowIfNull(app);
@ -230,5 +247,53 @@ internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialG
},
description: "Remaining games to redeem in background per bot"
);
Meter.CreateObservableCounter(
$"{MetricNamePrefix}_bot_trades", () => TradeStatistics.SelectMany<KeyValuePair<Bot, TradeStatistics>, Measurement<uint>>(
static kv => [
new Measurement<uint>(
kv.Value.AcceptedOffers,
new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName),
new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID),
new KeyValuePair<string, object?>(TagNames.TradeOfferResult, "accepted")
),
new Measurement<uint>(
kv.Value.RejectedOffers,
new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName),
new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID),
new KeyValuePair<string, object?>(TagNames.TradeOfferResult, "rejected")
),
new Measurement<uint>(
kv.Value.IgnoredOffers,
new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName),
new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID),
new KeyValuePair<string, object?>(TagNames.TradeOfferResult, "ignored")
),
new Measurement<uint>(
kv.Value.BlacklistedOffers,
new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName),
new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID),
new KeyValuePair<string, object?>(TagNames.TradeOfferResult, "blacklisted")
),
new Measurement<uint>(
kv.Value.ConfirmedOffers,
new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName),
new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID),
new KeyValuePair<string, object?>(TagNames.TradeOfferResult, "confirmed")
)
]
),
description: "Trade offers per bot and action taken by ASF"
);
Meter.CreateObservableCounter(
$"{MetricNamePrefix}_bot_items_given", () => TradeStatistics.Select(static kv => new Measurement<uint>(kv.Value.ItemsGiven, new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName), new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID))),
description: "Items given per bot"
);
Meter.CreateObservableCounter(
$"{MetricNamePrefix}_bot_items_received", () => TradeStatistics.Select(static kv => new Measurement<uint>(kv.Value.ItemsReceived, new KeyValuePair<string, object?>(TagNames.BotName, kv.Key.BotName), new KeyValuePair<string, object?>(TagNames.SteamID, kv.Key.SteamID))),
description: "Items received per bot"
);
}
}

View file

@ -31,6 +31,7 @@ internal static class TagNames {
internal const string OS = "operating_system";
internal const string Runtime = "runtime";
internal const string SteamID = "steamid";
internal const string TradeOfferResult = "result";
internal const string Variant = "variant";
internal const string Version = "version";
}

View file

@ -0,0 +1,76 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using ArchiSteamFarm.Steam.Exchange;
namespace ArchiSteamFarm.OfficialPlugins.Monitoring;
internal sealed class TradeStatistics {
private readonly object Lock = new();
internal uint AcceptedOffers { get; private set; }
internal uint BlacklistedOffers { get; private set; }
internal uint ConfirmedOffers { get; private set; }
internal uint IgnoredOffers { get; private set; }
internal uint ItemsGiven { get; private set; }
internal uint ItemsReceived { get; private set; }
internal uint RejectedOffers { get; private set; }
internal void Include(ParseTradeResult result) {
ArgumentNullException.ThrowIfNull(result);
lock (Lock) {
switch (result.Result) {
case ParseTradeResult.EResult.Accepted when result.Confirmed:
++ConfirmedOffers;
ItemsGiven += (uint) (result.ItemsToGive?.Count ?? 0);
ItemsReceived += (uint) (result.ItemsToReceive?.Count ?? 0);
goto case ParseTradeResult.EResult.Accepted;
case ParseTradeResult.EResult.Accepted:
++AcceptedOffers;
break;
case ParseTradeResult.EResult.Rejected:
++RejectedOffers;
break;
case ParseTradeResult.EResult.Blacklisted:
++BlacklistedOffers;
break;
case ParseTradeResult.EResult.Ignored:
++IgnoredOffers;
break;
}
}
}
}