teacup/report.xqm

132 lines
4.5 KiB
Plaintext

(:~
: Report library.
:
: @author Rave Technologies, https://www.rave-tech.com/, 2017
:)
module namespace report = 'http://www.rave-tech.com/bloomsbury/report';
import module namespace config = 'http://www.rave-tech.com/bloomsbury/config' at './module/config.xqm';
import module namespace contents = 'http://www.rave-tech.com/bloomsbury/contents' at 'content.xqm';
import module namespace blcommon = 'http://www.rave-tech.com/bloomsbury/common' at 'common.xqm';
declare namespace pipeline = 'http://cms.bloomsbury.com/pipeline';
(:~
: Update check-in information into the report
: @param $outputFileName File name to match of the report
: @param $jobID ID of the job to ingest report
: @param $cid Content ID
: @return empty sequence
:)
declare %updating function report:update-checkin-info(
$outputFileName as xs:string,
$jobID as xs:string,
$cid as xs:string,
$ctype as xs:string
)
{
insert node <check-in>Yes</check-in> as last into db:open(blcommon:get-db-name($ctype),fn:concat($config:JobDir,$jobID,'.xml'))/job/job-info/lock/file[fn:contains(fn:tokenize(uri,'/')[fn:last()],fn:replace($outputFileName,'.docx','.xml'))]
[fn:tokenize(uri,'/')[4]=$cid]
};
(:~
: Append error report to the specific job.
: @param $jobID ID of the job to update status
: @param $stepReport The step information to record
: @return empty sequence
:)
declare %updating function report:update(
$jobID as xs:string,
$stepReport as element(step),
$ctype as xs:string
)
{
insert node $stepReport as last into db:open(blcommon:get-db-name($ctype),fn:concat($config:JobDir,$jobID,'.xml'))/job/ingestion-report/steps
};
(:~
: Append final status of the report.
: @param $jobID ID of the job to update status
: @param $ignoreValidation Ignore validation if error happens
: @param $contentID ID of the content
: @return empty sequence
:)
declare %updating function report:update-final-status(
$jobID as xs:string,
$ignoreValidation as xs:string,
$contentID as xs:string,
$ctype as xs:string
)
{
let $jobXml := db:open(blcommon:get-db-name($ctype),fn:concat($config:JobDir,$jobID,'.xml'))
return
if($jobXml/job/ingestion-report/status[.='InProgress'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/*)
then
if($ignoreValidation='true')
then replace node $jobXml/job/ingestion-report/status with <status>SuccessWithWarnings</status>
else
(
replace node $jobXml/job/ingestion-report/status with <status>Failure</status>
,
(: delete all ingested content, as some files has error :)
db:delete(blcommon:get-db-name($ctype),fn:concat($config:ContentDir,$contentID))
)
else replace node $jobXml/job/ingestion-report/status with <status>Success</status>
(:,
admin:write-log(fn:concat('[',$jobID,'][***************************Job Finished***************************]')):)
)
else ()
};
(:~
: Append finished time of the report.
: @param $jobID ID of the job to update status
: @return empty sequence
:)
declare %updating function report:update-final-time(
$jobID as xs:string,
$ctype as xs:string
)
{
let $jobXml := db:open(blcommon:get-db-name($ctype),fn:concat($config:JobDir,$jobID,'.xml'))
return
if($jobXml/job/ingestion-report/finished-on[.=''])
then replace node $jobXml/job/ingestion-report/finished-on with <finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>
else ()
};
(:~
: Get Ingest or Publish report
: @param $id ID of the report
: @result element(result)
:)
declare
%rest:path("/reports/{$id=.+}")
%rest:GET
%rest:header-param("Authorization", "{$authorization}", "none")
%rest:consumes("application/xml", "text/xml")
function report:info(
$id as xs:string,
$authorization as xs:string
)
{
config:session-check($authorization),
try
{
let $job := db:open($config:CoreDatabase,$config:JobDir)/job[job-info/id=$id]
return
if($job)
then <result><status>Success</status><message>Report is available</message>{$job}</result>
else <result><status>Failure</status><message>Report is unavailable</message></result>
}
catch *
{
admin:write-log("[Report][Error: " || $err:description || "]"),
admin:write-log("[Report][Error: " || $err:additional || "]"),
<result><status>Error</status><message>Error generated. Please check system log</message></result>
}
};