Fix worker and signal

This commit is contained in:
2025-07-21 23:30:54 +07:00
parent 83ed78a1fa
commit 27c1e9d1ba
13 changed files with 132 additions and 60 deletions

View File

@@ -320,8 +320,24 @@ public class BacktestRepository : IBacktestRepository
public IEnumerable<BundleBacktestRequest> GetBundleBacktestRequestsByUser(User user)
{
var bundleRequests = _bundleBacktestRepository.AsQueryable()
.Where(b => b.User.Name == user.Name)
var projection = Builders<BundleBacktestRequestDto>.Projection
.Include(b => b.RequestId)
.Include(b => b.Status)
.Include(b => b.CreatedAt)
.Include(b => b.CurrentBacktest)
.Include(b => b.EstimatedTimeRemainingSeconds)
.Include(b => b.TotalBacktests)
.Include(b => b.CurrentBacktest)
.Include(b => b.CompletedAt)
.Include(b => b.ErrorMessage)
.Include(b => b.ProgressInfo)
.Include(b => b.Name)
.Include(b => b.User);
var filter = Builders<BundleBacktestRequestDto>.Filter.Eq(b => b.User.Name, user.Name);
var bundleRequests = _bundleBacktestRepository.GetCollection()
.Find(filter)
.Project<BundleBacktestRequestDto>(projection)
.ToList();
return bundleRequests.Select(MongoMappers.Map);
@@ -329,7 +345,7 @@ public class BacktestRepository : IBacktestRepository
public BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, string id)
{
var bundleRequest = _bundleBacktestRepository.FindOne(b => b.RequestId == id);
var bundleRequest = _bundleBacktestRepository.FindOne(b => b.RequestId == id && b.User.Name == user.Name);
if (bundleRequest != null && bundleRequest.User.Name == user.Name)
{
@@ -341,8 +357,13 @@ public class BacktestRepository : IBacktestRepository
public void UpdateBundleBacktestRequest(BundleBacktestRequest bundleRequest)
{
var dto = MongoMappers.Map(bundleRequest);
_bundleBacktestRepository.ReplaceOne(dto);
var existingRequest = _bundleBacktestRepository.FindOne(b => b.RequestId == bundleRequest.RequestId);
if (existingRequest != null)
{
var dto = MongoMappers.Map(bundleRequest);
dto.Id = existingRequest.Id; // Preserve the MongoDB ObjectId
_bundleBacktestRepository.ReplaceOne(dto);
}
}
public void DeleteBundleBacktestRequestByIdForUser(User user, string id)
@@ -355,12 +376,12 @@ public class BacktestRepository : IBacktestRepository
}
}
public IEnumerable<BundleBacktestRequest> GetPendingBundleBacktestRequests()
public IEnumerable<BundleBacktestRequest> GetBundleBacktestRequestsByStatus(BundleBacktestRequestStatus status)
{
var pendingRequests = _bundleBacktestRepository.AsQueryable()
.Where(b => b.Status == BundleBacktestRequestStatus.Pending)
var requests = _bundleBacktestRepository.AsQueryable()
.Where(b => b.Status == status)
.ToList();
return pendingRequests.Select(MongoMappers.Map);
return requests.Select(MongoMappers.Map);
}
}

View File

@@ -20,4 +20,5 @@ public class BundleBacktestRequestDto : Document
public string? CurrentBacktest { get; set; }
public int? EstimatedTimeRemainingSeconds { get; set; }
public string Name { get; set; } = string.Empty;
public List<string> Results { get; set; } = new();
}

View File

@@ -36,6 +36,8 @@ public class IndexService
// Create indexes for BacktestDto
await CreateBacktestIndexesAsync();
await CreateBundleBacktestIndexesAsync();
// Create indexes for GeneticRequestDto
await CreateGeneticRequestIndexesAsync();
@@ -74,6 +76,17 @@ public class IndexService
}
}
private async Task CreateBundleBacktestIndexesAsync()
{
var bundleCollection = _database.GetCollection<BundleBacktestRequestDto>("BundleBacktestRequests");
// Index on RequestId (unique)
var requestIdIndex = Builders<BundleBacktestRequestDto>.IndexKeys.Ascending(b => b.RequestId);
await bundleCollection.Indexes.CreateOneAsync(new CreateIndexModel<BundleBacktestRequestDto>(requestIdIndex, new CreateIndexOptions { Unique = true }));
// Index on User.Name (non-unique)
var userNameIndex = Builders<BundleBacktestRequestDto>.IndexKeys.Ascending("User.Name");
await bundleCollection.Indexes.CreateOneAsync(new CreateIndexModel<BundleBacktestRequestDto>(userNameIndex));
}
/// <summary>
/// Creates indexes for the GeneticRequestDto collection
/// </summary>

View File

@@ -1128,7 +1128,8 @@ public static class MongoMappers
ProgressInfo = domain.ProgressInfo,
CurrentBacktest = domain.CurrentBacktest,
EstimatedTimeRemainingSeconds = domain.EstimatedTimeRemainingSeconds,
Name = domain.Name
Name = domain.Name,
Results = domain.Results
};
}
@@ -1150,7 +1151,8 @@ public static class MongoMappers
ProgressInfo = dto.ProgressInfo,
CurrentBacktest = dto.CurrentBacktest,
EstimatedTimeRemainingSeconds = dto.EstimatedTimeRemainingSeconds,
Name = dto.Name
Name = dto.Name,
Results = dto.Results
};
}