#444 improvements

This commit is contained in:
Hamid Reza Mohammadi 2024-06-20 11:03:51 +03:30
parent 43fae4180e
commit 2cf87e6657
4 changed files with 51 additions and 16 deletions

View File

@ -4193,9 +4193,9 @@ namespace RMuseum.Controllers
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public IActionResult BuildCategoryWordCounts()
public async Task<IActionResult> BuildCategoryWordCountsAsync(bool reset = false)
{
_ganjoorService.BuildCategoryWordCounts();
await _ganjoorService.BuildCategoryWordCountsAsync(reset);
return Ok();
}

View File

@ -2208,7 +2208,7 @@
<param name="page"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.BuildCategoryWordCounts">
<member name="M:RMuseum.Controllers.GanjoorController.BuildCategoryWordCountsAsync(System.Boolean)">
<summary>
build word counts
</summary>
@ -16543,7 +16543,7 @@
<param name="page"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.BuildCategoryWordCounts">
<member name="M:RMuseum.Services.IGanjoorService.BuildCategoryWordCountsAsync(System.Boolean)">
<summary>
build word counts
</summary>
@ -18102,7 +18102,7 @@
<param name="q"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.BuildCategoryWordCounts">
<member name="M:RMuseum.Services.Implementation.GanjoorService.BuildCategoryWordCountsAsync(System.Boolean)">
<summary>
build word counts
</summary>

View File

@ -1435,6 +1435,6 @@ namespace RMuseum.Services
/// <summary>
/// build word counts
/// </summary>
void BuildCategoryWordCounts();
Task BuildCategoryWordCountsAsync(bool reset);
}
}

View File

@ -7,7 +7,7 @@ using RMuseum.DbContext;
using System.Collections.Generic;
using RSecurityBackend.Services.Implementation;
using System.Threading.Tasks;
using Org.BouncyCastle.Utilities.Zlib;
using RSecurityBackend.Models.Generic.Db;
namespace RMuseum.Services.Implementation
{
@ -19,8 +19,17 @@ namespace RMuseum.Services.Implementation
/// <summary>
/// build word counts
/// </summary>
public void BuildCategoryWordCounts()
public async Task BuildCategoryWordCountsAsync(bool reset)
{
int lastFinishedPoetId = 0;
if (!reset)
{
var resOption = await _optionsService.GetValueAsync("CategoryWordCountsLastPoetId", null);
if (!string.IsNullOrEmpty(resOption.Result))
{
lastFinishedPoetId = int.Parse(resOption.Result);
}
}
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
@ -31,23 +40,48 @@ namespace RMuseum.Services.Implementation
var job = (await jobProgressServiceEF.NewJob($"BuildCategoryWordCounts", "Query data")).Result;
try
{
var existing = await context.CategoryWordCounts.ToListAsync();
if(existing.Any())
if (reset)
{
context.RemoveRange(existing);
await context.SaveChangesAsync();
var existing = await context.CategoryWordCounts.ToListAsync();
if (existing.Any())
{
context.RemoveRange(existing);
await context.SaveChangesAsync();
}
}
var poets = await context.GanjoorPoets.AsNoTracking().Where(p => p.Published).OrderBy(p => p.Id).ToListAsync();
var poets = await context.GanjoorPoets.AsNoTracking().Where(p => p.Published && p.Id > lastFinishedPoetId).OrderBy(p => p.Id).ToListAsync();
foreach (var poet in poets)
{
var poetCat = await context.GanjoorCategories.AsNoTracking().Where(c => c.PoetId == poet.Id && c.ParentId == null).SingleAsync();
await jobProgressServiceEF.UpdateJob(job.Id, poet.Id, poet.Nickname);
var wordCounts = await _BuildCategoryWordStatsAsync(context, poetCat, true);
var wordCounts = await _BuildCategoryWordStatsAsync(context, poetCat, true, jobProgressServiceEF, job);
if(wordCounts.Any())
{
await context.AddRangeAsync(wordCounts);
}
await jobProgressServiceEF.UpdateJob(job.Id, poet.Id, poet.Nickname + ": Saving");
RGenericOption option = await context.Options.Where(o => o.Name == "CategoryWordCountsLastPoetId" && o.RAppUserId == null).SingleOrDefaultAsync();
if (option != null)
{
option.Value = poetCat.PoetId.ToString();
context.Options.Update(option);
await context.SaveChangesAsync();
}
else
{
RGenericOption newOption = new RGenericOption
{
Name = "CategoryWordCountsLastPoetId",
Value = poetCat.PoetId.ToString(),
RAppUserId = null
};
context.Options.Add(newOption);
await context.SaveChangesAsync();
}
}
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
@ -59,13 +93,13 @@ namespace RMuseum.Services.Implementation
});
}
private async Task<List<CategoryWordCount>> _BuildCategoryWordStatsAsync(RMuseumDbContext context, GanjoorCat cat, bool poetCat)
private async Task<List<CategoryWordCount>> _BuildCategoryWordStatsAsync(RMuseumDbContext context, GanjoorCat cat, bool poetCat, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job)
{
List<CategoryWordCount> counts = new List<CategoryWordCount>();
var children = await context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == cat.Id).ToListAsync();
foreach (var child in children)
{
var subcatCounts = await _BuildCategoryWordStatsAsync(context, child, false);
var subcatCounts = await _BuildCategoryWordStatsAsync(context, child, false, jobProgressServiceEF, job);
if (subcatCounts.Any())
{
foreach (var count in subcatCounts)
@ -104,6 +138,7 @@ namespace RMuseum.Services.Implementation
}
}
}
await jobProgressServiceEF.UpdateJob(job.Id, poem.Id);
}
return counts;
}