Add strategies paginated

This commit is contained in:
2025-10-07 01:21:25 +07:00
parent 85000644a6
commit f43117e6c6
2 changed files with 154 additions and 49 deletions

View File

@@ -46,11 +46,48 @@ public class AgentBalanceRepository : IAgentBalanceRepository
{
var results = await _influxDbRepository.QueryAsync(async query =>
{
var effectiveEnd = end ?? DateTime.UtcNow;
var timeRange = effectiveEnd - start;
// Determine sampling interval based on time range to limit data points
string samplingInterval;
if (timeRange.TotalDays <= 1)
{
// Less than 1 day: 5-minute intervals (max ~288 points)
samplingInterval = "5m";
}
else if (timeRange.TotalDays <= 7)
{
// 1-7 days: 30-minute intervals (max ~336 points)
samplingInterval = "30m";
}
else if (timeRange.TotalDays <= 30)
{
// 1-30 days: 2-hour intervals (max ~360 points)
samplingInterval = "2h";
}
else if (timeRange.TotalDays <= 90)
{
// 1-3 months: 6-hour intervals (max ~360 points)
samplingInterval = "6h";
}
else if (timeRange.TotalDays <= 365)
{
// 3-12 months: 1-day intervals (max ~365 points)
samplingInterval = "1d";
}
else
{
// More than 1 year: 7-day intervals (max ~52 points)
samplingInterval = "7d";
}
var flux = $"from(bucket:\"{_balanceBucket}\") " +
$"|> range(start: {start:s}Z" +
(end.HasValue ? $", stop: {end.Value:s}Z" : "") +
$") " +
$"|> filter(fn: (r) => r[\"user_id\"] == \"{userId}\") " +
$"|> aggregateWindow(every: {samplingInterval}, fn: last, createEmpty: false) " +
$"|> pivot(rowKey: [\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
var result = await query.QueryAsync<AgentBalanceDto>(flux, _influxDbRepository.Organization);
@@ -69,52 +106,4 @@ public class AgentBalanceRepository : IAgentBalanceRepository
return results;
}
public async Task<(IList<AgentBalanceHistory> result, int totalCount)> GetAllAgentBalancesWithHistory(
DateTime start, DateTime? end)
{
var results = await _influxDbRepository.QueryAsync(async query =>
{
// Get all balances within the time range, pivoted so each row is a full AgentBalanceDto
var flux = $@"
from(bucket: ""{_balanceBucket}"")
|> range(start: {start:s}Z{(end.HasValue ? $", stop: {end.Value:s}Z" : "")})
|> filter(fn: (r) => r._measurement == ""agent_balance"")
|> pivot(rowKey: [""_time""], columnKey: [""_field""], valueColumn: ""_value"")
";
var balances = await query.QueryAsync<AgentBalanceDto>(flux, _influxDbRepository.Organization);
// Group balances by user ID
var agentGroups = balances
.GroupBy(b => b.UserId)
.Select(g =>
{
var userBalances = g.Select(b => new AgentBalance
{
UserId = b.UserId,
TotalBalanceValue = b.TotalBalanceValue,
UsdcWalletValue = b.UsdcWalletValue,
UsdcInPositionsValue = b.UsdcInPositionsValue,
BotsAllocationUsdValue = b.BotsAllocationUsdValue,
PnL = b.PnL,
Time = b.Time
}).OrderBy(b => b.Time).ToList();
// Use a default agent name since we don't store it in AgentBalance anymore
var mostRecentAgentName = $"User_{g.Key}";
return new AgentBalanceHistory
{
UserId = g.Key,
AgentName = mostRecentAgentName,
AgentBalances = userBalances
};
}).ToList();
return (agentGroups, agentGroups.Count);
});
return results;
}
}