mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 17:34:13 +00:00
Move server count reporting to be handled per-shard
This commit is contained in:
parent
5fcf69136d
commit
59afd8ce3b
3 changed files with 60 additions and 36 deletions
58
BackgroundServices/ExternalStatisticsReporting.cs
Normal file
58
BackgroundServices/ExternalStatisticsReporting.cs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BirthdayBot.BackgroundServices
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reports user count statistics to external services on a shard by shard basis.
|
||||||
|
/// </summary>
|
||||||
|
class ExternalStatisticsReporting : BackgroundService
|
||||||
|
{
|
||||||
|
const int ProcessInterval = 600 / ShardBackgroundWorker.Interval; // Process every ~5 minutes
|
||||||
|
private int _tickCount = 0;
|
||||||
|
|
||||||
|
private static readonly HttpClient _httpClient = new HttpClient();
|
||||||
|
|
||||||
|
public ExternalStatisticsReporting(ShardInstance instance) : base(instance) { }
|
||||||
|
|
||||||
|
public override async Task OnTick(CancellationToken token)
|
||||||
|
{
|
||||||
|
if (++_tickCount % ProcessInterval != 0) return;
|
||||||
|
|
||||||
|
var botId = ShardInstance.DiscordClient.CurrentUser.Id;
|
||||||
|
if (botId == 0) return;
|
||||||
|
|
||||||
|
await SendDiscordBots(ShardInstance.DiscordClient.Guilds.Count, botId, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendDiscordBots(int userCount, ulong botId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var dbotsToken = ShardInstance.Config.DBotsToken;
|
||||||
|
if (dbotsToken != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
||||||
|
const string Body = "{{ \"guildCount\": {0}, \"shardCount\": {1}, \"shardId\": {2} }}";
|
||||||
|
var uri = new Uri(string.Format(dBotsApiUrl, botId));
|
||||||
|
|
||||||
|
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
||||||
|
post.Headers.Add("Authorization", dbotsToken);
|
||||||
|
post.Content = new StringContent(string.Format(Body,
|
||||||
|
userCount, ShardInstance.Config.ShardTotal, ShardInstance.ShardId),
|
||||||
|
Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
await _httpClient.SendAsync(post, token);
|
||||||
|
Log("Discord Bots: Update successful.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log("Discord Bots: Exception encountered during update: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,7 +39,8 @@ namespace BirthdayBot.BackgroundServices
|
||||||
{
|
{
|
||||||
{UserDownloader},
|
{UserDownloader},
|
||||||
{BirthdayUpdater},
|
{BirthdayUpdater},
|
||||||
{new DataRetention(instance)}
|
{new DataRetention(instance)},
|
||||||
|
{new ExternalStatisticsReporting(instance)}
|
||||||
};
|
};
|
||||||
|
|
||||||
_workerTask = Task.Factory.StartNew(WorkerLoop, _workerCanceller.Token);
|
_workerTask = Task.Factory.StartNew(WorkerLoop, _workerCanceller.Token);
|
||||||
|
|
|
@ -5,7 +5,6 @@ using Discord.WebSocket;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -167,8 +166,6 @@ namespace BirthdayBot
|
||||||
var guildTotal = guildCounts.Sum();
|
var guildTotal = guildCounts.Sum();
|
||||||
var guildAverage = guildCounts.Any() ? guildCounts.Average() : 0;
|
var guildAverage = guildCounts.Any() ? guildCounts.Average() : 0;
|
||||||
Log($"Currently in {guildTotal} guilds. Average shard load: {guildAverage:0.0}.");
|
Log($"Currently in {guildTotal} guilds. Average shard load: {guildAverage:0.0}.");
|
||||||
if (nullShards.Count == 0 && botId.HasValue)
|
|
||||||
await SendExternalStatistics(guildTotal, botId.Value, _watchdogCancel.Token).ConfigureAwait(false);
|
|
||||||
|
|
||||||
// Health report
|
// Health report
|
||||||
var goodShards = new List<int>();
|
var goodShards = new List<int>();
|
||||||
|
@ -243,37 +240,5 @@ namespace BirthdayBot
|
||||||
}
|
}
|
||||||
catch (TaskCanceledException) { }
|
catch (TaskCanceledException) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Statistical reporting
|
|
||||||
private static readonly HttpClient _httpClient = new HttpClient();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Send statistical information to external services.
|
|
||||||
/// </summary>
|
|
||||||
private async Task SendExternalStatistics(int count, ulong botId, CancellationToken token)
|
|
||||||
{
|
|
||||||
var dbotsToken = Config.DBotsToken;
|
|
||||||
if (dbotsToken != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats";
|
|
||||||
const string Body = "{{ \"guildCount\": {0} }}";
|
|
||||||
var uri = new Uri(string.Format(dBotsApiUrl, botId));
|
|
||||||
|
|
||||||
var post = new HttpRequestMessage(HttpMethod.Post, uri);
|
|
||||||
post.Headers.Add("Authorization", dbotsToken);
|
|
||||||
post.Content = new StringContent(string.Format(Body, count), Encoding.UTF8, "application/json");
|
|
||||||
|
|
||||||
await _httpClient.SendAsync(post, token);
|
|
||||||
Log("Discord Bots: Update successful.");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log("Discord Bots: Exception encountered during update: " + ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue