2017-09-04 85 views
3

您好我正在進行攝入和驗證,但我想記錄攝取和驗證的開始時間和結束時間。下面是我的代碼,我做錯了,請建議。如何在BaseX中記錄任務開始時間和任務結束時間

let $pipelineXml := 
<pipeline id='a111' name='ACH-export' xmlns='http://cms.bloomsbury.com/pipeline'> 
    <transform href='/transforms/docbook2xml.xsl'/> 
    <validate href='/validation/taxonomy.sch' failOnError='true'/> 
</pipeline> 
let $reportChunk := <report> 
          <info> 
           <id>{$pipelineXml//@id}</id> 
           <name>{$pipelineXml//@name}</name> 
           <submitted-on>{fn:current-dateTime()}</submitted-on> 
           <user>{user:current()}</user> 
          </info> 
          <ingestion-report> 
           <steps/> 
          </ingestion-report> 
         </report> 
let $startTime := fn:current-dateTime() 
let $validate := validate:rng-report($pipelineXml, bin:decode-string(db:retrieve($config:Database, fn:concat($config:ValidationDir,$config:PipelineRelaxNG)),'UTF-8'), fn:true()) 
return 
if($validate/status='valid') 
     then 
    (

     admin:write-log("[" || "][Pipeline is valid as per Relax NG : " || $pipelineXml//@id || "]") 
      , 
      let $appendReport := let $updateReport := <step> 
                  <type>RELAX NG Validation</type> 
                  <started-on>{$startTime,prof:sleep(20000)}</started-on> 
                  <completed-on>{fn:current-dateTime()}</completed-on> 
                  <status>Pass</status> 
                  <error></error> 
                 </step> 
            return 
            copy $target := $reportChunk 
            modify insert node $updateReport as last into $target/ingestion-report/steps 
            return $target 


        return $appendReport 
    ) 
    else "error" 

回答

3

您好達門德拉庫馬爾辛格

current-Time()功能是所謂的確定性函數,其轉換爲:

[定義]這是保證一個函數,以產生·相同· 在單個執行範圍內重複調用的結果•如果顯式和隱式參數相同,則稱爲 確定性。 https://www.w3.org/TR/xpath-functions-3/#dt-deterministic

話雖這麼說:你startTimeendTime是相同的。

不過,你有幾種可能性,根據您的實際需求:

  1. 可以使用prof:current-ns(這是一個非確定型函數)來獲取一個時間戳,並使用該值來計算你的時間:

http://docs.basex.org/wiki/Profiling_Module#prof:current-ns

let $ns1 := prof:current-ns() 
return (
    (: process to measure :) 
    (1 to 1000000)[. = 0], 
    let $ns2 := prof:current-ns() 
    let $ms := ((($ns2 - $ns1) idiv 10000) div 100) 
    return $ms || ' ms' 
) 

或者你可以使用內置的計時功能prof:time它記錄執行所需的時間:

http://docs.basex.org/wiki/Profiling_Module#prof:time

你可以寫類似:

let $rng := <element name="addressBook" xmlns="http://relaxng.org/ns/structure/1.0"> 
    <zeroOrMore> 
    <element name="card"> 
     <element name="name"> 
     <text/> 
     </element> 
     <element name="email"> 
     <text/> 
     </element> 
    </element> 
    </zeroOrMore> 
</element> 

let $doc := <addressBook>{ 
for $i in 1 to 1000 
    return <cards> 
    <name>John Smith</name> 
    <email>[email protected]</email> 
    </cards> 
} 
</addressBook> 


let $report  := prof:time(validate:rng-report($doc, $rng), true(), 'RNG Validation ') 
let $report-size := prof:time(count($report//*) , true(), 'Counting ') 
return $report 

得到以下結果:

Profiling result in the GUI


旁註:最新的bin:decode-string(db:retrieve…)部分爲? 您可能想用db:open('…path-to-file…')替換它,並將Relax-NG模式存儲爲XML文件而不是二進制文件?

+0

謝謝您的回覆Michael,但我的要求是存儲時間格式爲 2017-09-04T17:17:14.989 + 05:30 2017-09-04T17:17:14.989 + 05: 30只有這樣我才能實現這個在當前的代碼,如果你在這個非常讚賞的幫助 –

+1

我得到的解決方案感謝指南邁克爾:我已經取代只有FN:當前日期時間調整-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms())) –

+0

好的:-)如果有幫助,你可以考慮接受我的答案https://meta.stackexchange.com/questions/5234 /如何-不接受-的回答工作 – michael

相關問題