#241 adding some data structures

This commit is contained in:
Hamid Reza Mohammadi 2021-11-14 18:40:14 +03:30
parent ca90fd31cc
commit 9c20419455
5 changed files with 289 additions and 2 deletions

View File

@ -8,8 +8,8 @@
},
"AllowedHosts": "*",
"GoogleAnalyticsCode": "",
"APIRoot": "https://ganjgah.ir",
"GlobalAPIRoot": "https://ganjgah.ir",
"APIRoot": "http://localhost:3439",
"GlobalAPIRoot": "http://localhost:3439",
"SiteUrl": "http://localhost:33081",
"ReadOnlyMode": "False",
"MaintenanceMode": "False"

View File

@ -136,6 +136,8 @@ namespace RMuseum.DbContext
builder.Entity<GanjoorVerse>()
.HasIndex(v => new { v.PoemId, v.CoupletIndex });
builder.Entity<GanjoorCachedRelatedPoem>()
.HasIndex(c => new { c.PoemId, c.RelationOrder });
//Index set suggested by SQL Server Tuning Wizard -- end
@ -384,5 +386,10 @@ namespace RMuseum.DbContext
/// </summary>
public DbSet<GanjoorGeoLocation> GanjoorGeoLocations { get; set; }
/// <summary>
/// related poems to each poem (having same rhyme letters and prosody metre)
/// </summary>
public DbSet<GanjoorCachedRelatedPoem> GanjoorCachedRelatedPoems { get; set; }
}
}

View File

@ -0,0 +1,55 @@
namespace RMuseum.Models.Ganjoor
{
public class GanjoorCachedRelatedPoem
{
/// <summary>
/// id
/// </summary>
public int Id { get; set; }
/// <summary>
/// poem id
/// </summary>
public int PoemId { get; set; }
/// <summary>
/// poem
/// </summary>
public GanjoorPoem Poem { get; set; }
/// <summary>
/// order
/// </summary>
public int RelationOrder { get; set; }
/// <summary>
/// poet name
/// </summary>
public string PoetName { get; set; }
/// <summary>
/// poet image url
/// </summary>
public string PoetImageUrl { get; set; }
/// <summary>
/// poem full url
/// </summary>
public string FullUrl { get; set; }
/// <summary>
/// poem full title
/// </summary>
public string FullTitle { get; set; }
/// <summary>
/// excerpt
/// </summary>
public string HtmlExcerpt { get; set; }
/// <summary>
/// other poems
/// </summary>
public int PoetMorePoemsLikeThisCount { get; set; }
}
}

View File

@ -2157,6 +2157,11 @@
ganjoor cities
</summary>
</member>
<member name="P:RMuseum.DbContext.RMuseumDbContext.GanjoorCachedRelatedPoems">
<summary>
related poems to each poem (having same rhyme letters and prosody metre)
</summary>
</member>
<member name="T:RMuseum.Models.Accounting.DonationExpenditure">
<summary>
donation expenditures
@ -4605,6 +4610,56 @@
correction review result
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.Id">
<summary>
id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.PoemId">
<summary>
poem id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.Poem">
<summary>
poem
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.RelationOrder">
<summary>
order
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.PoetName">
<summary>
poet name
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.PoetImageUrl">
<summary>
poet image url
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.FullUrl">
<summary>
poem full url
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.FullTitle">
<summary>
poem full title
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.HtmlExcerpt">
<summary>
excerpt
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorCachedRelatedPoem.PoetMorePoemsLikeThisCount">
<summary>
other poems
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.GanjoorCat">
<summary>
Ganjoor Category
@ -10574,6 +10629,9 @@
<summary>
IGanjoorService implementation
</summary>
<summary>
IGanjoorService implementation
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.SwitchCoupletBookmark(System.Guid,System.Int32,System.Int32)">
<summary>
@ -10716,6 +10774,13 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemHtmlExcerpt(System.String)">
<summary>
get an excerpt for the poem
</summary>
<param name="poemHtml"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartBuildingSitemap">
<summary>
build sitemap

View File

@ -0,0 +1,160 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services.Implementation;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace RMuseum.Services.Implementation
{
/// <summary>
/// IGanjoorService implementation
/// </summary>
public partial class GanjoorService : IGanjoorService
{
private async Task _UpdatePoemRelatedPoemsInfoNoSaveChanges(RMuseumDbContext context, int poemId)
{
var poem = await context.GanjoorPoems.AsNoTracking().Where(p => p.Id == poemId).SingleAsync();
var oldRelations = await context.GanjoorCachedRelatedPoems.Where(r => r.PoemId == poemId).ToListAsync();
context.GanjoorCachedRelatedPoems.RemoveRange(oldRelations);
if (poem.GanjoorMetreId == null || string.IsNullOrEmpty(poem.RhymeLetters))
return;
int metreId = (int)poem.GanjoorMetreId;
string rhyme = poem.RhymeLetters;
var relatedPoems = context.GanjoorPoems.AsNoTracking().Include(p => p.Cat).ThenInclude(c => c.Poet)
.Where(p =>
p.GanjoorMetreId == metreId
&&
p.RhymeLetters == rhyme
&&
p.Id != poemId
)
.OrderBy(p => p.Cat.Poet.BirthYearInLHijri).ThenBy(p => p.Cat.PoetId);
List<GanjoorCachedRelatedPoem> ganjoorCachedRelatedPoems = new List<GanjoorCachedRelatedPoem>();
int r = 0;
int prePoetId = -1;
foreach (var relatedPoem in relatedPoems)
{
if(prePoetId != relatedPoem.Cat.PoetId)
{
r++;
GanjoorCachedRelatedPoem newRelatedPoem = new GanjoorCachedRelatedPoem()
{
PoemId = poemId,
RelationOrder = r,
PoetName = relatedPoem.Cat.Poet.Nickname,
PoetImageUrl = $"/api/ganjoor/poet/image{(await context.GanjoorCategories.Where(c => c.ParentId == null && c.PoetId == relatedPoem.Cat.PoetId).AsNoTracking().SingleAsync()).FullUrl}.gif",
FullTitle = relatedPoem.FullTitle,
FullUrl = relatedPoem.FullTitle,
PoetMorePoemsLikeThisCount = 0,
HtmlExcerpt = GetPoemHtmlExcerpt(relatedPoem.HtmlText)
};
ganjoorCachedRelatedPoems.Add(newRelatedPoem);
}
else
{
ganjoorCachedRelatedPoems[ganjoorCachedRelatedPoems.Count - 1].PoetMorePoemsLikeThisCount++;
}
}
context.GanjoorCachedRelatedPoems.AddRange(ganjoorCachedRelatedPoems);
}
/// <summary>
/// get an excerpt for the poem
/// </summary>
/// <param name="poemHtml"></param>
/// <returns></returns>
public static string GetPoemHtmlExcerpt(string poemHtml)
{
while (poemHtml.IndexOf("id=\"bn") != -1)
{
int idxbn1 = poemHtml.IndexOf(" id=\"bn");
int idxbn2 = poemHtml.IndexOf("\"", idxbn1 + " id=\"bn".Length);
poemHtml = poemHtml.Substring(0, idxbn1) + poemHtml.Substring(idxbn2 + 1);
}
poemHtml = poemHtml.Replace("<div class=\"b\">", "").Replace("<div class=\"b2\">", "").Replace("<div class=\"m1\">", "").Replace("<div class=\"m2\">", "").Replace("</div>", "");
int index = poemHtml.IndexOf("<p>");
int count = 0;
while (index != -1 && count < 5)
{
index = poemHtml.IndexOf("<p>", index + 1);
count++;
}
if (index != -1)
{
poemHtml = poemHtml.Substring(0, index);
poemHtml += "<p>[...]</p>";
}
return poemHtml;
}
public RServiceResult<bool> StartGeneratingRelatedPoemsInfo()
{
try
{
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //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("GeneratingRelatedPoemsInfo", "Query")).Result;
try
{
await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Query");
var poemIds = await context.GanjoorPoems.AsNoTracking().Where(p => string.IsNullOrEmpty(p.RhymeLetters) && p.GanjoorMetreId != null).Select(p => p.Id).ToListAsync();
await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Updating Related Poems");
int percent = 0;
for (int i = 0; i < poemIds.Count; i++)
{
if (i * 100 / poemIds.Count > percent)
{
percent++;
await jobProgressServiceEF.UpdateJob(job.Id, percent);
}
await _UpdatePoemRelatedPoemsInfoNoSaveChanges(context, poemIds[i]);
}
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
}
}
}
);
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
}
}