pagination for backtest and optimization

This commit is contained in:
2025-07-16 14:27:07 +07:00
parent 11778aa2a4
commit f51fd5a5f7
15 changed files with 287 additions and 9 deletions

View File

@@ -1,9 +1,11 @@
using Managing.Application.Abstractions.Repositories;
using System.Diagnostics;
using Managing.Application.Abstractions.Repositories;
using Managing.Domain.Backtests;
using Managing.Domain.Users;
using Managing.Infrastructure.Databases.MongoDb;
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
using Managing.Infrastructure.Databases.MongoDb.Collections;
using MongoDB.Driver;
namespace Managing.Infrastructure.Databases;
@@ -41,6 +43,48 @@ public class BacktestRepository : IBacktestRepository
return backtests.Select(b => MongoMappers.Map(b));
}
public (IEnumerable<Backtest> Backtests, int TotalCount) GetBacktestsByRequestIdPaginated(string requestId, int page, int pageSize)
{
var stopwatch = Stopwatch.StartNew();
var collection = _backtestRepository.GetCollection(); // You may need to expose this in your repo
var filter = Builders<BacktestDto>.Filter.Eq(b => b.RequestId, requestId);
var afterQueryMs = stopwatch.ElapsedMilliseconds;
var totalCount = collection.CountDocuments(filter);
var afterCountMs = stopwatch.ElapsedMilliseconds;
var projection = Builders<BacktestDto>.Projection
.Include(b => b.Identifier)
.Include(b => b.FinalPnl)
.Include(b => b.WinRate)
.Include(b => b.GrowthPercentage)
.Include(b => b.HodlPercentage)
.Include(b => b.User)
.Include(b => b.Statistics)
.Include(b => b.StartDate)
.Include(b => b.EndDate)
.Include(b => b.Score)
.Include(b => b.RequestId)
.Include(b => b.Metadata)
.Include(b => b.Config);
var afterProjectionMs = stopwatch.ElapsedMilliseconds;
var backtests = collection
.Find(filter)
.Project<BacktestDto>(projection)
.Skip((page - 1) * pageSize)
.Limit(pageSize)
.ToList();
var afterToListMs = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"[BacktestRepo] Query: {afterQueryMs}ms, Count: {afterCountMs - afterQueryMs}ms, Projection: {afterProjectionMs - afterCountMs}ms, ToList: {afterToListMs - afterProjectionMs}ms, Total: {afterToListMs}ms");
var mappedBacktests = backtests.Select(b => MongoMappers.Map(b));
return (mappedBacktests, (int)totalCount);
}
public Backtest GetBacktestByIdForUser(User user, string id)
{
var backtest = _backtestRepository.FindById(id);

View File

@@ -1,6 +1,6 @@
using Managing.Infrastructure.Databases.MongoDb.Configurations;
using System.Linq.Expressions;
using Managing.Infrastructure.Databases.MongoDb.Configurations;
using MongoDB.Driver;
using System.Linq.Expressions;
namespace Managing.Infrastructure.Databases.MongoDb.Abstractions
{
@@ -53,5 +53,6 @@ namespace Managing.Infrastructure.Databases.MongoDb.Abstractions
void Update(TDocument entity);
void CreateIndex(string column);
void DropCollection();
IMongoCollection<TDocument> GetCollection();
}
}

View File

@@ -1,9 +1,9 @@
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
using System.Linq.Expressions;
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
using Managing.Infrastructure.Databases.MongoDb.Attributes;
using Managing.Infrastructure.Databases.MongoDb.Configurations;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Linq.Expressions;
namespace Managing.Infrastructure.Databases.MongoDb
{
@@ -174,5 +174,10 @@ namespace Managing.Infrastructure.Databases.MongoDb
var model = new CreateIndexModel<TDocument>(keys, indexOptions);
_collection.Indexes.CreateOne(model);
}
public IMongoCollection<TDocument> GetCollection()
{
return _collection;
}
}
}