diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index cc9d7201..a61f48bc 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -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 BuildCategoryWordCountsAsync(bool reset = false) { - _ganjoorService.BuildCategoryWordCounts(); + await _ganjoorService.BuildCategoryWordCountsAsync(reset); return Ok(); } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index f1c02216..f341c154 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2208,7 +2208,7 @@ - + build word counts @@ -16543,7 +16543,7 @@ - + build word counts @@ -18102,7 +18102,7 @@ - + build word counts diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 8dbc0d4a..d16475ee 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1435,6 +1435,6 @@ namespace RMuseum.Services /// /// build word counts /// - void BuildCategoryWordCounts(); + Task BuildCategoryWordCountsAsync(bool reset); } } diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs index 221e2201..040d05b1 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs @@ -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 /// /// build word counts /// - 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> _BuildCategoryWordStatsAsync(RMuseumDbContext context, GanjoorCat cat, bool poetCat) + private async Task> _BuildCategoryWordStatsAsync(RMuseumDbContext context, GanjoorCat cat, bool poetCat, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job) { List counts = new List(); 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; }