Fix Runtime by adding TotalRuntimeInSeconds

This commit is contained in:
2025-10-05 20:51:46 +07:00
parent 976c1a6580
commit f67ee330b3
18 changed files with 3142 additions and 50 deletions

View File

@@ -170,6 +170,10 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
// Set startup time when bot actually starts running
_state.State.StartupTime = DateTime.UtcNow;
// Track runtime: set LastStartTime when bot starts
_state.State.LastStartTime = DateTime.UtcNow;
await _state.WriteStateAsync();
// Start the in-memory timer and persistent reminder
@@ -281,6 +285,18 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
StopAndDisposeTimer();
await UnregisterReminder();
// Track runtime: accumulate current session runtime when stopping
if (_state.State.LastStartTime.HasValue)
{
var currentSessionSeconds = (long)(DateTime.UtcNow - _state.State.LastStartTime.Value).TotalSeconds;
_state.State.AccumulatedRunTimeSeconds += currentSessionSeconds;
_state.State.LastStopTime = DateTime.UtcNow;
_state.State.LastStartTime = null; // Clear since bot is no longer running
_logger.LogInformation("Bot {GrainId} accumulated {Seconds} seconds of runtime. Total: {TotalSeconds} seconds",
this.GetPrimaryKey(), currentSessionSeconds, _state.State.AccumulatedRunTimeSeconds);
}
// Sync state from the volatile TradingBotBase before destroying it
SyncStateFromBase();
await _state.WriteStateAsync();

View File

@@ -121,4 +121,22 @@ public class TradingBotGrainState
/// </summary>
[Id(18)]
public Candle? LastCandle { get; set; }
/// <summary>
/// The last time the bot was started (for runtime tracking)
/// </summary>
[Id(19)]
public DateTime? LastStartTime { get; set; }
/// <summary>
/// The last time the bot was stopped (for runtime tracking)
/// </summary>
[Id(20)]
public DateTime? LastStopTime { get; set; }
/// <summary>
/// Total accumulated runtime in seconds (excluding current session if running)
/// </summary>
[Id(21)]
public long AccumulatedRunTimeSeconds { get; set; }
}