From 185d55c1e19d013fa3be6bd560559a04f09e5000 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 28 Jan 2025 11:38:43 +0330 Subject: [PATCH] stats for poems couplet counts in categories #464 --- RMuseum/Controllers/GanjoorController.cs | 19 ++++++ RMuseum/RMuseum.xml | 18 +++++ RMuseum/Services/IGanjoorService.cs | 7 ++ .../GanjoorService-Maintenance.cs | 67 +++++++++++++++++++ 4 files changed, 111 insertions(+) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 3e298c12..8f88d037 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -2616,6 +2616,25 @@ namespace RMuseum.Controllers return BadRequest(res.ExceptionString); } + /// + /// fill section couplet counts + /// + /// + + [HttpPost] + [Route("fillsectioncoupletcounts")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public IActionResult StartFillingSectionCoupletCounts() + { + RServiceResult res = + _ganjoorService.StartFillingSectionCoupletCounts(); + if (res.Result) + return Ok(); + return BadRequest(res.ExceptionString); + } + /// /// regenerate poem full titles to fix an old bug /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 6f52f0c1..9cff8b76 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1746,6 +1746,12 @@ + + + fill section couplet counts + + + regenerate poem full titles to fix an old bug @@ -16298,6 +16304,12 @@ + + + fill section couplets count + + + regenerate poem full titles to fix an old bug @@ -19026,6 +19038,12 @@ + + + fill section couplets count + + + regenerate poem full titles to fix an old bug diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 804638d7..686eb51d 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -628,6 +628,13 @@ namespace RMuseum.Services /// RServiceResult StartFillingPoemsCoupletIndices(); + /// + /// fill section couplets count + /// + /// + RServiceResult StartFillingSectionCoupletCounts(); + + /// /// regenerate poem full titles to fix an old bug /// diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs index 19bc6005..9b913241 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Maintenance.cs @@ -692,6 +692,73 @@ namespace RMuseum.Services.Implementation return new RServiceResult(true); } + /// + /// fill section couplets count + /// + /// + public RServiceResult StartFillingSectionCoupletCounts() + { + _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("FillingSectionCoupletCounts", "Query data")).Result; + + try + { + var sectionIds = await context.GanjoorPoemSections.AsNoTracking().Select(p => p.Id).ToListAsync(); + + + int percent = 0; + for (int i = 0; i < sectionIds.Count; i++) + { + if (i * 100 / sectionIds.Count > percent) + { + percent++; + await jobProgressServiceEF.UpdateJob(job.Id, percent); + } + + await _FillSectionCoupletCountsAsync(context, sectionIds[i]); + } + + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + + } + } + ); + + return new RServiceResult(true); + } + + private async Task _FillSectionCoupletCountsAsync(RMuseumDbContext context, int sectionId) + { + var section = await context.GanjoorPoemSections.Where(s => s.Id == sectionId).SingleAsync(); + + section.CoupletsCount = await context.GanjoorVerses.Where( + v => v.PoemId == section.PoemId + && + ( + v.SectionIndex1 == section.Index || + v.SectionIndex2 == section.Index || + v.SectionIndex3 == section.Index || + v.SectionIndex4 == section.Index + ) + && + (v.VersePosition == VersePosition.Left || v.VersePosition == VersePosition.CenteredVerse1) + ).CountAsync(); + + context.Update(section); + } + + /// /// regenerate poem full titles to fix an old bug ///