Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitor incoming trades #3201

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 66 additions & 1 deletion ArchiSteamFarm.OfficialPlugins.Monitoring/MonitoringPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +36,7 @@
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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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"
);
}
}
1 change: 1 addition & 0 deletions ArchiSteamFarm.OfficialPlugins.Monitoring/TagNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
76 changes: 76 additions & 0 deletions ArchiSteamFarm.OfficialPlugins.Monitoring/TradeStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: [email protected]
// |
// 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;
JustArchi marked this conversation as resolved.
Show resolved Hide resolved

break;
case ParseTradeResult.EResult.Rejected:
++RejectedOffers;

break;
case ParseTradeResult.EResult.Blacklisted:
++BlacklistedOffers;

break;
case ParseTradeResult.EResult.Ignored:
++IgnoredOffers;

break;
}
}
}
}