Refactor LibraryDebugCollector and TelegramApi for improved data handling and clarity
Updated the LibraryDebugCollector to introduce a new CompositionEntry record for better organization of group composition data. Refactored the composition logic to utilize this new structure, enhancing readability and maintainability. In the TelegramApi, replaced inline allowed_updates array with a static readonly field to reduce redundancy and improve clarity in API call configurations. Adjusted related tests to ensure proper handling of message assertions, enhancing overall test reliability.
This commit is contained in:
+35
-14
@@ -430,6 +430,24 @@ public sealed class LibraryDebugCollector(
|
||||
/// <summary>Группа в дампе: готовые факты и числа, по которым сверяется её кэш статистики.</summary>
|
||||
private sealed record GroupSnapshot(Group Group, object Facts, int Units, int PlayableUnits);
|
||||
|
||||
/// <summary>
|
||||
/// Строка вычисленного состава группы. Именованная запись, а не анонимный тип: сведения об
|
||||
/// элементе берутся один раз, а не по разу на каждое поле.
|
||||
/// </summary>
|
||||
private sealed record CompositionEntry(
|
||||
GroupElementKind Kind,
|
||||
Guid Id,
|
||||
string? Name,
|
||||
int Position,
|
||||
int Weight,
|
||||
bool Pinned,
|
||||
int? UnitCount,
|
||||
int PlayableUnits,
|
||||
double? Minutes,
|
||||
ShowKind? ShowKind,
|
||||
ShowAudience? Audience
|
||||
);
|
||||
|
||||
private async Task<IReadOnlyList<GroupSnapshot>> GroupSnapshotsAsync(
|
||||
IReadOnlyList<Group> groups,
|
||||
IReadOnlyDictionary<Guid, string> showNames,
|
||||
@@ -486,21 +504,24 @@ public sealed class LibraryDebugCollector(
|
||||
item.Weight,
|
||||
item.Role,
|
||||
}),
|
||||
Composition = resolved.Select(element => new
|
||||
Composition = resolved.Select(element =>
|
||||
{
|
||||
element.Kind,
|
||||
element.Id,
|
||||
Name = Name(element.Kind, element.Id),
|
||||
element.Position,
|
||||
element.Weight,
|
||||
element.Pinned,
|
||||
UnitCount = info.GetValueOrDefault((element.Kind, element.Id))?.UnitCount,
|
||||
PlayableUnits = PlayableOf(element.Kind, element.Id),
|
||||
Minutes = info.GetValueOrDefault((element.Kind, element.Id)) is { } resolvedInfo
|
||||
? Math.Round(resolvedInfo.TotalDuration.TotalMinutes, 1)
|
||||
: (double?)null,
|
||||
ShowKind = info.GetValueOrDefault((element.Kind, element.Id))?.ShowKind,
|
||||
Audience = info.GetValueOrDefault((element.Kind, element.Id))?.Audience,
|
||||
var resolvedInfo = info.GetValueOrDefault((element.Kind, element.Id));
|
||||
return new CompositionEntry(
|
||||
element.Kind,
|
||||
element.Id,
|
||||
Name(element.Kind, element.Id),
|
||||
element.Position,
|
||||
element.Weight,
|
||||
element.Pinned,
|
||||
resolvedInfo?.UnitCount,
|
||||
PlayableOf(element.Kind, element.Id),
|
||||
resolvedInfo is null
|
||||
? null
|
||||
: Math.Round(resolvedInfo.TotalDuration.TotalMinutes, 1),
|
||||
resolvedInfo?.ShowKind,
|
||||
resolvedInfo?.Audience
|
||||
);
|
||||
}),
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,12 @@ public sealed class TelegramApi(ILogger<TelegramApi> logger) : ITelegramApi
|
||||
/// </summary>
|
||||
private const int LongPollSeconds = 25;
|
||||
|
||||
/// <summary>
|
||||
/// Что просим у Telegram. Разговор ведут только команды и нажатия кнопок; всё остальное
|
||||
/// (правки сообщений, участники чата) боту не нужно, а лишний тип обновления — лишний трафик.
|
||||
/// </summary>
|
||||
private static readonly string[] AllowedUpdates = ["message", "callback_query"];
|
||||
|
||||
private static readonly JsonSerializerOptions Json = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
||||
@@ -51,7 +57,7 @@ public sealed class TelegramApi(ILogger<TelegramApi> logger) : ITelegramApi
|
||||
{
|
||||
offset,
|
||||
timeout = LongPollSeconds,
|
||||
allowed_updates = new[] { "message", "callback_query" },
|
||||
allowed_updates = AllowedUpdates,
|
||||
};
|
||||
|
||||
var result = await CallAsync<UpdatesResult>(
|
||||
@@ -178,7 +184,7 @@ public sealed class TelegramApi(ILogger<TelegramApi> logger) : ITelegramApi
|
||||
{
|
||||
url = settings.WebhookUrl,
|
||||
secret_token = settings.WebhookSecret,
|
||||
allowed_updates = new[] { "message", "callback_query" },
|
||||
allowed_updates = AllowedUpdates,
|
||||
},
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user