diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 49f648c2..c64948a2 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -4213,6 +4213,21 @@ namespace RMuseum.Controllers _ganjoorService.FillCategoryWordCountsRowNmbrInCat(); return Ok(); } + /// + /// one time fixer for category word count summries + /// + /// + + [HttpPut("wordcounts/fillwordcountsummeries")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public IActionResult FillCategoryWordCountSummaries() + { + _ganjoorService.FillCategoryWordCountSummaries(); + return Ok(); + } + /// /// category word counts diff --git a/RMuseum/DbContext/RMuseumDbContext.cs b/RMuseum/DbContext/RMuseumDbContext.cs index d0c004f6..001aabe8 100644 --- a/RMuseum/DbContext/RMuseumDbContext.cs +++ b/RMuseum/DbContext/RMuseumDbContext.cs @@ -200,6 +200,11 @@ namespace RMuseum.DbContext .HasIndex(v => new { v.CatId, v.Word }) .IsUnique(); + builder.Entity() + .HasIndex(v => new { v.CatId }) + .IsUnique(); + + } @@ -589,5 +594,10 @@ namespace RMuseum.DbContext /// public DbSet CategoryWordCounts { get; set; } + /// + /// Category Word Count Summaries + /// + public DbSet CategoryWordCountSummaries { get; set; } + } } diff --git a/RMuseum/Models/Ganjoor/CategoryWordCountSummary.cs b/RMuseum/Models/Ganjoor/CategoryWordCountSummary.cs new file mode 100644 index 00000000..4862ce21 --- /dev/null +++ b/RMuseum/Models/Ganjoor/CategoryWordCountSummary.cs @@ -0,0 +1,28 @@ +using System; + +namespace RMuseum.Models.Ganjoor +{ + public class CategoryWordCountSummary + { + /// + /// id + /// + public Guid Id { get; set; } + + /// + /// CatId + /// + public int CatId { get; set; } + + /// + /// unique word count + /// + public int UniqueWordCount { get; set; } + + /// + /// total word count + /// + public int TotalWordCount { get; set; } + + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 79087272..487589e3 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2220,6 +2220,12 @@ + + + one time fixer for category word count summries + + + category word counts @@ -3585,6 +3591,11 @@ Category Word Counts + + + Category Word Count Summaries + + @@ -7244,6 +7255,26 @@ row number in category + + + id + + + + + CatId + + + + + unique word count + + + + + total word count + + correction review result @@ -16581,6 +16612,11 @@ fill CategoryWordCounts.RowNmbrInCat + + + fill CategoryWordCountSummaries + + category words count @@ -18163,6 +18199,11 @@ fill CategoryWordCounts.RowNmbrInCat + + + fill CategoryWordCountSummaries + + set category poem format tag for poems consisting of a single whole poem section diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 14b6d776..76e89715 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1443,6 +1443,11 @@ namespace RMuseum.Services /// void FillCategoryWordCountsRowNmbrInCat(); + /// + /// fill CategoryWordCountSummaries + /// + void FillCategoryWordCountSummaries(); + /// /// category words count /// diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs index 76fc1a7a..a9907989 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CategoryWordCounts.cs @@ -265,6 +265,68 @@ namespace RMuseum.Services.Implementation } + /// + /// fill CategoryWordCountSummaries + /// + public void FillCategoryWordCountSummaries() + { + _backgroundTaskQueue.QueueBackgroundWorkItem + ( + async token => + { + using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so _context might be already been freed/collected by GC + { + LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context); + var job = (await jobProgressServiceEF.NewJob($"FillCategoryWordCountSummaries", "Query data")).Result; + try + { + var poets = await context.GanjoorPoets.AsNoTracking().Where(p => p.Published).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); + if (true == await context.CategoryWordCounts.Where(c => c.CatId == poetCat.Id).AnyAsync()) + { + List catIdList = new List + { + poetCat.Id + }; + await _populateCategoryChildren(context, poetCat.Id, catIdList); + foreach (var catId in catIdList) + { + var uniqueCount = await context.CategoryWordCounts.Where(w => w.CatId == catId).CountAsync(); + if (uniqueCount > 0) + { + var totalCount = await context.CategoryWordCounts.Where(w => w.CatId == catId).SumAsync(w => w.Count); + context.Add + ( + new CategoryWordCountSummary() + { + CatId = catId, + UniqueWordCount = uniqueCount, + TotalWordCount = totalCount + } + ); + await jobProgressServiceEF.UpdateJob(job.Id, poet.Id, poet.Nickname + $": Saving {catId}"); + } + } + } + } + + + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + + } + }); + + } + + } } \ No newline at end of file