ReportService.DeleteAllOldReportsAsync(..) optimize EF queries

This commit is contained in:
ngfrolov 2023-11-10 16:34:15 +05:00
parent 3451d7ca98
commit c34d6d39f5
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 6 additions and 10 deletions

View File

@ -28,7 +28,7 @@ namespace AsbCloudInfrastructure.Background
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
{
var reportService = services.GetRequiredService<IReportService>();
await reportService.DeleteAllOldReportsAsync(new TimeSpan(30, 0, 0, 0), token);
await reportService.DeleteAllOldReportsAsync(TimeSpan.FromDays(30), token);
}
}
}

View File

@ -192,17 +192,13 @@ namespace AsbCloudInfrastructure.Services
public async Task<int> DeleteAllOldReportsAsync(TimeSpan lifetime, CancellationToken token)
{
var lifeTimeStartDate = DateTime.UtcNow.Date.Add(-lifetime);
var fileIds = db.ReportProperties
var lifeTimeStartDate = DateTime.UtcNow.Date - lifetime;
var fileIds = await db.ReportProperties
.Where(r => r.File.UploadDate.Date < lifeTimeStartDate)
.Select(r => r.IdFile);
.Select(r => r.IdFile)
.ToArrayAsync(token);
if (fileIds.Any())
{
return await fileService.DeleteAsync(fileIds, token);
}
return 0;
return await fileService.DeleteAsync(fileIds, token);
}
}
}