Fix dailySnapshot for platformsummary
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Managing.Application.Abstractions.Models;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Orleans;
|
||||
using Managing.Domain.Bots;
|
||||
@@ -58,11 +57,32 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
// Initial data load if state is empty
|
||||
if (_state.State.LastUpdated == default)
|
||||
{
|
||||
// Create initial empty daily snapshot if none exist
|
||||
if (!_state.State.DailySnapshots.Any())
|
||||
{
|
||||
var today = DateTime.UtcNow.Date.AddSeconds(1); // Today at 00:00:01 UTC
|
||||
var initialSnapshot = new DailySnapshot
|
||||
{
|
||||
Date = today,
|
||||
TotalAgents = 0,
|
||||
TotalStrategies = 0,
|
||||
TotalVolume = 0,
|
||||
TotalPnL = 0,
|
||||
TotalOpenInterest = 0,
|
||||
TotalPositionCount = 0,
|
||||
};
|
||||
|
||||
_state.State.DailySnapshots.Add(initialSnapshot);
|
||||
_state.State.LastSnapshot = today;
|
||||
_state.State.LastUpdated = today;
|
||||
_logger.LogInformation("Created initial empty daily snapshot for {Date}", today);
|
||||
}
|
||||
|
||||
await RefreshDataAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PlatformSummaryViewModel> GetPlatformSummaryAsync()
|
||||
public async Task<PlatformSummaryGrainState> GetPlatformSummaryAsync()
|
||||
{
|
||||
// If data is stale or has pending changes, refresh it
|
||||
if (IsDataStale() || _state.State.HasPendingChanges)
|
||||
@@ -70,7 +90,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
await RefreshDataAsync();
|
||||
}
|
||||
|
||||
return MapToViewModel(_state.State);
|
||||
return _state.State;
|
||||
}
|
||||
|
||||
public async Task RefreshDataAsync()
|
||||
@@ -238,20 +258,6 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
return Task.FromResult(_state.State.TotalPlatformFees);
|
||||
}
|
||||
|
||||
public Task<List<VolumeHistoryPoint>> GetVolumeHistoryAsync()
|
||||
{
|
||||
var historyPoints = _state.State.DailySnapshots
|
||||
.OrderBy(s => s.Date)
|
||||
.Select(s => new VolumeHistoryPoint
|
||||
{
|
||||
Date = s.Date,
|
||||
Volume = s.TotalVolume
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult(historyPoints);
|
||||
}
|
||||
|
||||
// Event handlers for immediate updates
|
||||
public async Task UpdateActiveStrategyCountAsync(int newActiveCount)
|
||||
{
|
||||
@@ -395,15 +401,6 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
{
|
||||
_logger.LogInformation("Taking daily snapshot");
|
||||
|
||||
// Store 24-hour ago values for comparison
|
||||
_state.State.TotalAgents24hAgo = _state.State.TotalAgents;
|
||||
_state.State.TotalActiveStrategies24hAgo = _state.State.TotalActiveStrategies;
|
||||
_state.State.TotalPlatformPnL24hAgo = _state.State.TotalPlatformPnL;
|
||||
_state.State.TotalPlatformVolume24hAgo = _state.State.TotalPlatformVolume;
|
||||
_state.State.TotalOpenInterest24hAgo = _state.State.OpenInterest;
|
||||
_state.State.TotalPositionCount24hAgo = _state.State.TotalPositionCount;
|
||||
_state.State.TotalPlatformFees24hAgo = _state.State.TotalPlatformFees;
|
||||
|
||||
// Add daily snapshot
|
||||
var dailySnapshot = new DailySnapshot
|
||||
{
|
||||
@@ -414,13 +411,12 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
TotalPnL = _state.State.TotalPlatformPnL,
|
||||
TotalOpenInterest = _state.State.OpenInterest,
|
||||
TotalPositionCount = _state.State.TotalPositionCount,
|
||||
TotalFees = _state.State.TotalPlatformFees
|
||||
};
|
||||
|
||||
_state.State.DailySnapshots.Add(dailySnapshot);
|
||||
|
||||
// Keep only last 30 days
|
||||
var cutoff = DateTime.UtcNow.AddDays(-30);
|
||||
// Keep only last 60 days
|
||||
var cutoff = DateTime.UtcNow.AddDays(-60);
|
||||
_state.State.DailySnapshots.RemoveAll(s => s.Date < cutoff);
|
||||
|
||||
_state.State.LastSnapshot = DateTime.UtcNow;
|
||||
@@ -433,50 +429,4 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
var timeSinceLastUpdate = DateTime.UtcNow - _state.State.LastUpdated;
|
||||
return timeSinceLastUpdate > TimeSpan.FromMinutes(5);
|
||||
}
|
||||
|
||||
private PlatformSummaryViewModel MapToViewModel(PlatformSummaryGrainState state)
|
||||
{
|
||||
// Generate volume history from daily snapshots
|
||||
var volumeHistory = state.DailySnapshots
|
||||
.OrderBy(s => s.Date)
|
||||
.Select(s => new VolumeHistoryPoint
|
||||
{
|
||||
Date = s.Date,
|
||||
Volume = s.TotalVolume
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new PlatformSummaryViewModel
|
||||
{
|
||||
TotalAgents = state.TotalAgents,
|
||||
TotalActiveStrategies = state.TotalActiveStrategies,
|
||||
TotalPlatformPnL = state.TotalPlatformPnL,
|
||||
TotalPlatformVolume = state.TotalPlatformVolume,
|
||||
TotalPlatformVolumeLast24h = state.TotalPlatformVolume - state.TotalPlatformVolume24hAgo,
|
||||
TotalOpenInterest = state.OpenInterest,
|
||||
TotalPositionCount = state.TotalPositionCount,
|
||||
TotalPlatformFees = state.TotalPlatformFees,
|
||||
|
||||
// 24-hour changes
|
||||
AgentsChange24h = state.TotalAgents - state.TotalAgents24hAgo,
|
||||
StrategiesChange24h = state.TotalActiveStrategies - state.TotalActiveStrategies24hAgo,
|
||||
PnLChange24h = state.TotalPlatformPnL - state.TotalPlatformPnL24hAgo,
|
||||
VolumeChange24h = state.TotalPlatformVolume - state.TotalPlatformVolume24hAgo,
|
||||
OpenInterestChange24h = state.OpenInterest - state.TotalOpenInterest24hAgo,
|
||||
PositionCountChange24h = state.TotalPositionCount - state.TotalPositionCount24hAgo,
|
||||
FeesChange24h = state.TotalPlatformFees - state.TotalPlatformFees24hAgo,
|
||||
|
||||
// Breakdowns
|
||||
VolumeByAsset = state.VolumeByAsset ?? new Dictionary<Ticker, decimal>(),
|
||||
PositionCountByAsset = state.PositionCountByAsset ?? new Dictionary<Ticker, int>(),
|
||||
PositionCountByDirection = state.PositionCountByDirection ?? new Dictionary<TradeDirection, int>(),
|
||||
|
||||
// Volume history for charting (last 30 days)
|
||||
VolumeHistory = volumeHistory,
|
||||
|
||||
// Metadata
|
||||
LastUpdated = state.LastUpdated,
|
||||
Last24HourSnapshot = state.LastSnapshot
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user