Improved .shardstats format + small cleanup

This commit is contained in:
Kwoth
2021-06-20 06:15:30 +02:00
parent c86bf6f300
commit 77904a3b00
6 changed files with 50 additions and 30 deletions

View File

@@ -256,38 +256,49 @@ namespace NadekoBot.Modules.Administration
var statuses = _coord.GetAllShardStatuses();
var status = string.Join(", ", statuses
.GroupBy(x => x.ConnectionState)
.Select(x => $"{x.Count()} {x.Key}")
var status = string.Join(" : ", statuses
.Select(x => (ConnectionStateToEmoji(x), x))
.GroupBy(x => x.Item1)
.Select(x => $"`{x.Count()} {x.Key}`")
.ToArray());
var allShardStrings = statuses
.Select(x =>
.Select(st =>
{
var timeDiff = DateTime.UtcNow - x.Time;
if (timeDiff >= TimeSpan.FromSeconds(30))
return $"Shard #{Format.Bold(x.ShardId.ToString())} **UNRESPONSIVE** for {timeDiff.ToString(@"hh\:mm\:ss")}";
return GetText("shard_stats_txt", x.ShardId.ToString(),
Format.Bold(x.ConnectionState.ToString()), Format.Bold(x.Guilds.ToString()), timeDiff.ToString(@"hh\:mm\:ss"));
var stateStr = ConnectionStateToEmoji(st);
var timeDiff = DateTime.UtcNow - st.LastUpdate;
var maxGuildCountLength = statuses.Max(x => x.GuildCount).ToString().Length;
return $"`{stateStr} " +
$"| #{st.ShardId.ToString().PadBoth(3)} " +
$"| {timeDiff:mm\\:ss} " +
$"| {st.GuildCount.ToString().PadBoth(maxGuildCountLength)} `";
})
.ToArray();
await ctx.SendPaginatedConfirmAsync(page, (curPage) =>
{
var str = string.Join("\n", allShardStrings.Skip(25 * curPage).Take(25));
if (string.IsNullOrWhiteSpace(str))
str = GetText("no_shards_on_page");
return new EmbedBuilder()
.WithAuthor(a => a.WithName(GetText("shard_stats")))
.WithTitle(status)
.WithOkColor()
.WithDescription(str);
.WithDescription($"{status}\n\n{str}");
}, allShardStrings.Length, 25).ConfigureAwait(false);
}
private static string ConnectionStateToEmoji(ShardStatus status)
{
var timeDiff = DateTime.UtcNow - status.LastUpdate;
return status.ConnectionState switch
{
ConnectionState.Connected => "✅",
ConnectionState.Disconnected => "🔻",
_ when timeDiff > TimeSpan.FromSeconds(30) => " ❗ ",
_ => " ⏳"
};
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task RestartShard(int shardId)

View File

@@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Music.Services
var responseSpan = response.AsSpan().Slice(140_000);
var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA);
if (startIndex == -1)
return null; // todo try selecting html
return null; // todo future try selecting html
startIndex += YT_RESULT_INITIAL_DATA.Length;
var endIndex = 140_000 + startIndex + responseSpan.Slice(startIndex + 20_000).IndexOf(YT_RESULT_JSON_END) + 20_000;

View File

@@ -8,15 +8,15 @@ namespace NadekoBot.Services
bool RestartBot();
void Die();
bool RestartShard(int shardId);
IEnumerable<ShardStatus> GetAllShardStatuses();
IList<ShardStatus> GetAllShardStatuses();
int GetGuildCount();
}
public class ShardStatus
{
public Discord.ConnectionState ConnectionState { get; set; }
public DateTime Time { get; set; }
public DateTime LastUpdate { get; set; }
public int ShardId { get; set; }
public int Guilds { get; set; }
public int GuildCount { get; set; }
}
}

View File

@@ -54,7 +54,7 @@ namespace NadekoBot.Services
return true;
}
public IEnumerable<ShardStatus> GetAllShardStatuses()
public IList<ShardStatus> GetAllShardStatuses()
{
var res = _coordClient.GetAllStatuses(new GetAllStatusesRequest());
@@ -63,9 +63,9 @@ namespace NadekoBot.Services
.Map(s => new ShardStatus()
{
ConnectionState = FromCoordConnState(s.State),
Guilds = s.GuildCount,
GuildCount = s.GuildCount,
ShardId = s.ShardId,
Time = s.LastUpdate.ToDateTime(),
LastUpdate = s.LastUpdate.ToDateTime(),
});
}
@@ -88,7 +88,7 @@ namespace NadekoBot.Services
var reply = await _coordClient.HeartbeatAsync(new HeartbeatRequest
{
State = ToCoordConnState(_client.ConnectionState),
GuildCount = _client.ConnectionState == Discord.ConnectionState.Connected ? _client.Guilds.Count : 0,
GuildCount = _client.ConnectionState == ConnectionState.Connected ? _client.Guilds.Count : 0,
ShardId = _client.ShardId,
}, deadline: DateTime.UtcNow + TimeSpan.FromSeconds(10));
gracefulImminent = reply.GracefulImminent;
@@ -119,20 +119,20 @@ namespace NadekoBot.Services
return Task.CompletedTask;
}
private ConnState ToCoordConnState(Discord.ConnectionState state)
private ConnState ToCoordConnState(ConnectionState state)
=> state switch
{
Discord.ConnectionState.Connecting => ConnState.Connecting,
Discord.ConnectionState.Connected => ConnState.Connected,
ConnectionState.Connecting => ConnState.Connecting,
ConnectionState.Connected => ConnState.Connected,
_ => ConnState.Disconnected
};
private Discord.ConnectionState FromCoordConnState(ConnState state)
private ConnectionState FromCoordConnState(ConnState state)
=> state switch
{
ConnState.Connecting => Discord.ConnectionState.Connecting,
ConnState.Connected => Discord.ConnectionState.Connected,
_ => Discord.ConnectionState.Disconnected
ConnState.Connecting => ConnectionState.Connecting,
ConnState.Connected => ConnectionState.Connected,
_ => ConnectionState.Disconnected
};
}
}

View File

@@ -12,6 +12,13 @@ namespace NadekoBot.Extensions
{
public static class StringExtensions
{
public static string PadBoth(this string str, int length)
{
int spaces = length - str.Length;
int padLeft = spaces / 2 + str.Length;
return str.PadLeft(padLeft).PadRight(length);
}
public static T MapJson<T>(this string str)
=> JsonConvert.DeserializeObject<T>(str);

View File

@@ -1446,7 +1446,9 @@ stringsreload:
args:
- ""
shardstats:
desc: "Stats for shards. Paginated with 25 shards per page."
desc: |-
Stats for shards. Paginated with 25 shards per page.
Format: `[status] | # [shard_id] | [last_heartbeat] | [server_count]`
args:
- ""
- "2"