This commit is contained in:
2021-12-22 18:51:22 +00:00
parent 14ce0bbf5d
commit 1756f27b5a
30 changed files with 164 additions and 58 deletions

View File

@@ -383,7 +383,9 @@ namespace AyaNova.Biz
public async Task<string> RenderReport(DataListReportRequest reportRequest, string apiUrl)
{
var log = AyaNova.Util.ApplicationLogging.CreateLogger("ReportBiz::RenderReport");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport called");
#endif
//Customer User Report?
bool RequestIsCustomerWorkOrderReport = false;
if (reportRequest.ReportId == -100)
@@ -398,6 +400,9 @@ namespace AyaNova.Biz
}
//get report, vet security, see what we need before init in case of issue
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport get report from db");
#endif
var report = await ct.Report.FirstOrDefaultAsync(z => z.Id == reportRequest.ReportId);
if (report == null)
{
@@ -436,12 +441,18 @@ namespace AyaNova.Biz
reportRequest.IncludeWoItemDescendants = report.IncludeWoItemDescendants;
//Get data
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport GetReportData");
#endif
var ReportData = await GetReportData(reportRequest, RequestIsCustomerWorkOrderReport);//###### TODO: SLOW NEEDS TIMEOUT HERE ONCE IT WORKS IS SUPPORTED
//if GetReportData errored then will return null so need to return that as well here
if (ReportData == null)
{
return null;
}
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport got report data");
#endif
//initialization
log.LogDebug("Initializing report rendering system");
@@ -461,7 +472,7 @@ namespace AyaNova.Biz
var lo = new LaunchOptions { Headless = true };
if (!AutoDownloadChromium)
{
lo.ExecutablePath = ServerBootConfig.AYANOVA_REPORT_RENDER_BROWSER_PATH;
lo.ExecutablePath = ServerBootConfig.AYANOVA_REPORT_RENDER_BROWSER_PATH;
/*
troubleshooting links:
https://developers.google.com/web/tools/puppeteer/troubleshooting
@@ -480,6 +491,9 @@ namespace AyaNova.Biz
}
else
{
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::calling browserfetcher");
#endif
log.LogDebug($"Windows: Calling browserFetcher download async now:");
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
}
@@ -500,11 +514,14 @@ namespace AyaNova.Biz
//API DOCS http://www.puppeteersharp.com/api/index.html
log.LogDebug($"Launching headless Browser now:");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::launching browser now");
#endif
using (var browser = await Puppeteer.LaunchAsync(lo))
using (var page = await browser.NewPageAsync())
{
//track this process so it can be cancelled if it times out
ReportRenderManager.AddProcess(browser.Process.Id);
ReportRenderManager.AddProcess(browser.Process.Id, log);
page.DefaultTimeout = 0;//infinite timeout as we are controlling how long the process can live for with the reportprocessmanager
try
{
@@ -623,17 +640,25 @@ namespace AyaNova.Biz
//prePareData / preRender
var ReportDataObject = $"{{ ayReportData:{ReportData}, ayReportMetaData:{reportMeta}, ayClientMetaData:{clientMeta}, ayServerMetaData:{serverMeta} }}";
log.LogDebug($"Calling ayPreRender...");
//PRE_RENDER WITH TIMEOUT
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - preRender");
#endif
//PRE_RENDER (WITH TIMEOUT???)
await page.WaitForExpressionAsync($"ayPreRender({ReportDataObject})");
//compile the template
log.LogDebug($"Calling Handlebars.compile...");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - compiling handlebars template");
#endif
var compileScript = $"Handlebars.compile(`{report.Template}`)(PreParedReportDataObject);";
var compiledHTML = await page.EvaluateExpressionAsync<string>(compileScript);
//render report as HTML
log.LogDebug($"Setting page content to compiled HTML");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - settting html on page contents");
#endif
await page.SetContentAsync(compiledHTML);
//add style (after page or it won't work)
@@ -650,6 +675,9 @@ namespace AyaNova.Biz
//Set PDF options
//https://pptr.dev/#?product=Puppeteer&version=v5.3.0&show=api-pagepdfoptions
log.LogDebug($"Resolving PDF Options from report settings");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - settting pdf options");
#endif
var PdfOptions = new PdfOptions() { };
PdfOptions.DisplayHeaderFooter = report.DisplayHeaderFooter;
@@ -726,7 +754,9 @@ namespace AyaNova.Biz
//render to pdf and return
log.LogDebug($"Calling render page contents to PDF");
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - rendering to pdf");
#endif
await page.PdfAsync(outputFullPath, PdfOptions);//###### TODO: SLOW NEEDS TIMEOUT HERE ONCE SUPPORTED, open bug: https://github.com/hardkoded/puppeteer-sharp/issues/1710
log.LogDebug($"Completed, returning results");
@@ -748,6 +778,9 @@ namespace AyaNova.Biz
}
finally
{
#if (DEBUG)
log.LogInformation($"DBG: ReportBiz::RenderReport - closing browser");
#endif
log.LogDebug($"Closing browser");
await browser.CloseAsync();
ReportRenderManager.RemoveProcess(browser.Process.Id, log);