Refactor LiveBotRegistryGrain and PlatformSummaryGrain to improve active bot tracking

- Introduced CalculateActiveBotsCount method in LiveBotRegistryGrain to streamline active bot count calculations.
- Updated logging to reflect active bot counts accurately during registration and unregistration.
- Added historical tracking of strategy activation/deactivation events in PlatformSummaryGrain, including a new StrategyEvent class and related logic to manage event history.
- Enhanced CalculateActiveStrategiesForDate method to compute active strategies based on historical events.
This commit is contained in:
2025-11-21 19:38:32 +07:00
parent eac13dd5e4
commit 153e170ca4
3 changed files with 106 additions and 27 deletions

View File

@@ -33,6 +33,8 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
_state.State.TotalBotsCount);
}
private int CalculateActiveBotsCount() => _state.State.Bots.Values.Count(b => b.Status == BotStatus.Running);
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
_logger.LogInformation("LiveBotRegistryGrain deactivating. Reason: {Reason}. Total bots: {TotalBots}",
@@ -53,16 +55,15 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
var entry = new BotRegistryEntry(identifier, userId);
_state.State.Bots[identifier] = entry;
// O(1) FIX: Increment the counters
// Increment the counters
_state.State.TotalBotsCount++;
_state.State.ActiveBotsCount++;
_state.State.LastUpdated = DateTime.UtcNow;
await _state.WriteStateAsync();
_logger.LogInformation(
"Bot {Identifier} registered successfully for user {UserId}. Total bots: {TotalBots}, Active bots: {ActiveBots}",
identifier, userId, _state.State.TotalBotsCount, _state.State.ActiveBotsCount);
identifier, userId, _state.State.TotalBotsCount, CalculateActiveBotsCount());
// Notify platform summary grain about strategy count change
await NotifyPlatformSummaryAsync();
@@ -86,20 +87,15 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
_state.State.Bots.Remove(identifier);
// O(1) FIX: Decrement the counters based on the removed entry's status
// Decrement the counters
_state.State.TotalBotsCount--;
if (entryToRemove.Status == BotStatus.Running)
{
_state.State.ActiveBotsCount--;
}
_state.State.LastUpdated = DateTime.UtcNow;
await _state.WriteStateAsync();
_logger.LogInformation(
"Bot {Identifier} unregistered successfully from user {UserId}. Total bots: {TotalBots}",
identifier, entryToRemove.UserId, _state.State.TotalBotsCount);
"Bot {Identifier} unregistered successfully from user {UserId}. Total bots: {TotalBots}, Active bots: {ActiveBots}",
identifier, entryToRemove.UserId, _state.State.TotalBotsCount, CalculateActiveBotsCount());
// Notify platform summary grain about strategy count change
await NotifyPlatformSummaryAsync();
@@ -148,16 +144,6 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
return;
}
// O(1) FIX: Conditionally adjust the counter
if (newStatus == BotStatus.Running && previousStatus != BotStatus.Running)
{
_state.State.ActiveBotsCount++;
}
else if (newStatus != BotStatus.Running && previousStatus == BotStatus.Running)
{
_state.State.ActiveBotsCount--;
}
entry.Status = newStatus;
entry.LastStatusUpdate = DateTime.UtcNow;
_state.State.LastUpdated = DateTime.UtcNow;
@@ -166,7 +152,7 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
_logger.LogInformation(
"Bot {Identifier} status updated from {PreviousStatus} to {NewStatus}. Active bots: {ActiveBots}",
identifier, previousStatus, newStatus, _state.State.ActiveBotsCount);
identifier, previousStatus, newStatus, CalculateActiveBotsCount());
}
catch (Exception ex)
{
@@ -190,11 +176,12 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
{
try
{
var activeCount = CalculateActiveBotsCount();
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
await platformGrain.UpdateActiveStrategyCountAsync(_state.State.ActiveBotsCount);
await platformGrain.UpdateActiveStrategyCountAsync(activeCount);
_logger.LogDebug("Notified platform summary about active strategy count change. New count: {ActiveCount}",
_state.State.ActiveBotsCount);
activeCount);
}
catch (Exception ex)
{