mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
Improved .shardstats format + small cleanup
This commit is contained in:
@@ -256,38 +256,49 @@ namespace NadekoBot.Modules.Administration
|
|||||||
|
|
||||||
var statuses = _coord.GetAllShardStatuses();
|
var statuses = _coord.GetAllShardStatuses();
|
||||||
|
|
||||||
var status = string.Join(", ", statuses
|
var status = string.Join(" : ", statuses
|
||||||
.GroupBy(x => x.ConnectionState)
|
.Select(x => (ConnectionStateToEmoji(x), x))
|
||||||
.Select(x => $"{x.Count()} {x.Key}")
|
.GroupBy(x => x.Item1)
|
||||||
|
.Select(x => $"`{x.Count()} {x.Key}`")
|
||||||
.ToArray());
|
.ToArray());
|
||||||
|
|
||||||
var allShardStrings = statuses
|
var allShardStrings = statuses
|
||||||
.Select(x =>
|
.Select(st =>
|
||||||
{
|
{
|
||||||
var timeDiff = DateTime.UtcNow - x.Time;
|
var stateStr = ConnectionStateToEmoji(st);
|
||||||
if (timeDiff >= TimeSpan.FromSeconds(30))
|
var timeDiff = DateTime.UtcNow - st.LastUpdate;
|
||||||
return $"Shard #{Format.Bold(x.ShardId.ToString())} **UNRESPONSIVE** for {timeDiff.ToString(@"hh\:mm\:ss")}";
|
var maxGuildCountLength = statuses.Max(x => x.GuildCount).ToString().Length;
|
||||||
return GetText("shard_stats_txt", x.ShardId.ToString(),
|
return $"`{stateStr} " +
|
||||||
Format.Bold(x.ConnectionState.ToString()), Format.Bold(x.Guilds.ToString()), timeDiff.ToString(@"hh\:mm\:ss"));
|
$"| #{st.ShardId.ToString().PadBoth(3)} " +
|
||||||
|
$"| {timeDiff:mm\\:ss} " +
|
||||||
|
$"| {st.GuildCount.ToString().PadBoth(maxGuildCountLength)} `";
|
||||||
})
|
})
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
await ctx.SendPaginatedConfirmAsync(page, (curPage) =>
|
await ctx.SendPaginatedConfirmAsync(page, (curPage) =>
|
||||||
{
|
{
|
||||||
|
|
||||||
var str = string.Join("\n", allShardStrings.Skip(25 * curPage).Take(25));
|
var str = string.Join("\n", allShardStrings.Skip(25 * curPage).Take(25));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(str))
|
if (string.IsNullOrWhiteSpace(str))
|
||||||
str = GetText("no_shards_on_page");
|
str = GetText("no_shards_on_page");
|
||||||
|
|
||||||
return new EmbedBuilder()
|
return new EmbedBuilder()
|
||||||
.WithAuthor(a => a.WithName(GetText("shard_stats")))
|
|
||||||
.WithTitle(status)
|
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithDescription(str);
|
.WithDescription($"{status}\n\n{str}");
|
||||||
}, allShardStrings.Length, 25).ConfigureAwait(false);
|
}, 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]
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
[OwnerOnly]
|
[OwnerOnly]
|
||||||
public async Task RestartShard(int shardId)
|
public async Task RestartShard(int shardId)
|
||||||
|
@@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Music.Services
|
|||||||
var responseSpan = response.AsSpan().Slice(140_000);
|
var responseSpan = response.AsSpan().Slice(140_000);
|
||||||
var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA);
|
var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA);
|
||||||
if (startIndex == -1)
|
if (startIndex == -1)
|
||||||
return null; // todo try selecting html
|
return null; // todo future try selecting html
|
||||||
startIndex += YT_RESULT_INITIAL_DATA.Length;
|
startIndex += YT_RESULT_INITIAL_DATA.Length;
|
||||||
|
|
||||||
var endIndex = 140_000 + startIndex + responseSpan.Slice(startIndex + 20_000).IndexOf(YT_RESULT_JSON_END) + 20_000;
|
var endIndex = 140_000 + startIndex + responseSpan.Slice(startIndex + 20_000).IndexOf(YT_RESULT_JSON_END) + 20_000;
|
||||||
|
@@ -8,15 +8,15 @@ namespace NadekoBot.Services
|
|||||||
bool RestartBot();
|
bool RestartBot();
|
||||||
void Die();
|
void Die();
|
||||||
bool RestartShard(int shardId);
|
bool RestartShard(int shardId);
|
||||||
IEnumerable<ShardStatus> GetAllShardStatuses();
|
IList<ShardStatus> GetAllShardStatuses();
|
||||||
int GetGuildCount();
|
int GetGuildCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ShardStatus
|
public class ShardStatus
|
||||||
{
|
{
|
||||||
public Discord.ConnectionState ConnectionState { get; set; }
|
public Discord.ConnectionState ConnectionState { get; set; }
|
||||||
public DateTime Time { get; set; }
|
public DateTime LastUpdate { get; set; }
|
||||||
public int ShardId { get; set; }
|
public int ShardId { get; set; }
|
||||||
public int Guilds { get; set; }
|
public int GuildCount { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -54,7 +54,7 @@ namespace NadekoBot.Services
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ShardStatus> GetAllShardStatuses()
|
public IList<ShardStatus> GetAllShardStatuses()
|
||||||
{
|
{
|
||||||
var res = _coordClient.GetAllStatuses(new GetAllStatusesRequest());
|
var res = _coordClient.GetAllStatuses(new GetAllStatusesRequest());
|
||||||
|
|
||||||
@@ -63,9 +63,9 @@ namespace NadekoBot.Services
|
|||||||
.Map(s => new ShardStatus()
|
.Map(s => new ShardStatus()
|
||||||
{
|
{
|
||||||
ConnectionState = FromCoordConnState(s.State),
|
ConnectionState = FromCoordConnState(s.State),
|
||||||
Guilds = s.GuildCount,
|
GuildCount = s.GuildCount,
|
||||||
ShardId = s.ShardId,
|
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
|
var reply = await _coordClient.HeartbeatAsync(new HeartbeatRequest
|
||||||
{
|
{
|
||||||
State = ToCoordConnState(_client.ConnectionState),
|
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,
|
ShardId = _client.ShardId,
|
||||||
}, deadline: DateTime.UtcNow + TimeSpan.FromSeconds(10));
|
}, deadline: DateTime.UtcNow + TimeSpan.FromSeconds(10));
|
||||||
gracefulImminent = reply.GracefulImminent;
|
gracefulImminent = reply.GracefulImminent;
|
||||||
@@ -119,20 +119,20 @@ namespace NadekoBot.Services
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnState ToCoordConnState(Discord.ConnectionState state)
|
private ConnState ToCoordConnState(ConnectionState state)
|
||||||
=> state switch
|
=> state switch
|
||||||
{
|
{
|
||||||
Discord.ConnectionState.Connecting => ConnState.Connecting,
|
ConnectionState.Connecting => ConnState.Connecting,
|
||||||
Discord.ConnectionState.Connected => ConnState.Connected,
|
ConnectionState.Connected => ConnState.Connected,
|
||||||
_ => ConnState.Disconnected
|
_ => ConnState.Disconnected
|
||||||
};
|
};
|
||||||
|
|
||||||
private Discord.ConnectionState FromCoordConnState(ConnState state)
|
private ConnectionState FromCoordConnState(ConnState state)
|
||||||
=> state switch
|
=> state switch
|
||||||
{
|
{
|
||||||
ConnState.Connecting => Discord.ConnectionState.Connecting,
|
ConnState.Connecting => ConnectionState.Connecting,
|
||||||
ConnState.Connected => Discord.ConnectionState.Connected,
|
ConnState.Connected => ConnectionState.Connected,
|
||||||
_ => Discord.ConnectionState.Disconnected
|
_ => ConnectionState.Disconnected
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -12,6 +12,13 @@ namespace NadekoBot.Extensions
|
|||||||
{
|
{
|
||||||
public static class StringExtensions
|
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)
|
public static T MapJson<T>(this string str)
|
||||||
=> JsonConvert.DeserializeObject<T>(str);
|
=> JsonConvert.DeserializeObject<T>(str);
|
||||||
|
|
||||||
|
@@ -1446,7 +1446,9 @@ stringsreload:
|
|||||||
args:
|
args:
|
||||||
- ""
|
- ""
|
||||||
shardstats:
|
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:
|
args:
|
||||||
- ""
|
- ""
|
||||||
- "2"
|
- "2"
|
||||||
|
Reference in New Issue
Block a user