简体   繁体   中英

Google Document AI - Inconsistent Long Running Operation's metadata JSON representation

While checking the status of Document AI - Long Running Operation (Form processor), the JSON representation of decodedOperation.metadata seems to vary during the execution. I suspect that operation response does not resolve straight away despite using then() on checkBatchProcessDocumentsProgress(operation.name) .

This behaviour does not happen using similar code for Google Speech's LROs.

Context:

At console.log line #24 of implemented code (below), as expected , decodedOperation.metadata resolves to

{
   "state":"RUNNING",
   "createTime":{
      "seconds":"1669278029",
      "nanos":500249000
   },
   "updateTime":{
      "seconds":"1669278029",
      "nanos":500249000
   }
}

Current behaviour:

At console.log line #27, decodedOperation.metadata.state returns 2 (??)

Expected behaviour:

decodedOperation.metadata.state should return RUNNING .

More details of output in the code below.

Reproduction details:

Environment: node.js 12.02

Package.json:

{
    "dependencies": {
        "@google-cloud/documentai": "latest",
    }
}

Code:

function run() {

    const documentai = require('@google-cloud/documentai').v1;

    // Create a promise on object
    let options = {
        credentials: {
            client_email: ** ** ** ,
            private_key: ** ** * ,
        },
        projectId: ** ** *
    };

    return async (async callback => {

        const client = new documentai.DocumentProcessorServiceClient(options);
        client.checkBatchProcessDocumentsProgress(properties.operation)
            .then(
                (decodedOperation) => {

                    console.log("METADATA " + JSON.stringify(decodedOperation.metadata));
                    /* logs to console:  
                        {
                           "state":"RUNNING",
                           "createTime":{
                              "seconds":"1669278029",
                              "nanos":500249000
                           },
                           "updateTime":{
                              "seconds":"1669278029",
                              "nanos":500249000
                           }
                        }
                    /// then 
                        {
                         "state":"SUCCEEDED",
                         "createTime":{
                            "seconds":"1669278029",
                            "nanos":500249000
                         },
                         "updateTime":{
                            "seconds":"1669278048",
                            "nanos":758825000
                         },
                         "individualProcessStatuses":[
                            {
                               "inputGcsSource":"gs://bucket/intake-form.pdf",
                               "status":{

                               },
                               "outputGcsDestination":"gs://bucket/ocr/7371120297544371692/0",
                               "humanReviewStatus":{
                                  "state":"SKIPPED",
                                  "stateMessage":"HumanReviewConfig is DISABLED, skipping human review."
                               }
                            }
                         ]
                      }
                      */
                    console.log("STATE " + JSON.stringify(decodedOperation.metadata.state));
                    /* log to console: 2 
                     when above is "RUNNING" */

                    /* log to console: 3
                    when above is "SUCCEEDED" */

                    if (decodedOperation.metadata.state == "SUCCEEDED") { // Never triggers as decodedOperation.metadata.state evaluates to an integer at this line

                    };
                    let response = {
                        "operationStatus": decodedOperation.metadata.state
                    };
                    callback(undefined, response);
                })
            .catch(
                (err) => {
                    callback(err);
                });
    })
} 

Update on investigation

util.inspect(decodedOperation.metadata, { showHidden: false }) returns:

BatchProcessMetadata {
   {
      "individualProcessStatuses":[
         "IndividualProcessStatus"{
            "inputGcsSource":"gs://bucketxxx/intake-form.pdf",
            "status":[
               "Status"
            ],
            "outputGcsDestination":"gs://bucketxxx/ocr/7999521463088838887/0",
            "humanReviewStatus":[
               "HumanReviewStatus"
            ]
         }
      ],
      "state":3,
      "createTime":"Timestamp"{
         "seconds":"Long"{
            "low":1670011754,
            "high":0,
            "unsigned":false
         },
         "nanos":105214000
      },
      "updateTime":"Timestamp"{
         "seconds":"Long"{
            "low":1670011773,
            "high":0,
            "unsigned":false
         },
         "nanos":489028000
      }
   }

util.inspect(decodedOperation.metadata, { showHidden: true }) returns (section of interest only):

[...] [root]: [Getter], [fullName]: [Getter] }, State: { STATE_UNSPECIFIED: 0, WAITING: 1, RUNNING: 2, SUCCEEDED: 3, CANCELLING: 4, CANCELLED: 5, FAILED: 6, '0': 'STATE_UNSPECIFIED', '1': 'WAITING', '2': 'RUNNING', '3': 'SUCCEEDED', '4': 'CANCELLING', '5': 'CANCELLED', '6': 'FAILED' }, encode: <ref *5> [Function: BatchProcessMetadata$encode] [...]

To fix this issue, you can access the string representation of the state enum value by using the documentai.v1.BatchProcessMetadata.State object. For example:

console.log("STATE " + documentai.v1.BatchProcessMetadata.State[decodedOperation.metadata.state]);

instead of

console.log("STATE " + JSON.stringify(decodedOperation.metadata.state));

Read more about it. https://cloud.google.com/php/docs/reference/cloud-document-ai/latest/V1.BatchProcessMetadata.State

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM