2017-03-07 207 views
1

我要求写一个文本或图形进度跟踪器,而rforcecom的批量更新功能加载最多10,000个批次。rforcecom.checkbatchstatus()的进度条

要设置并完成批量更新,必须创建一些对象 - 不能避免它。我真的不喜欢不得不重新运行代码来检查rforcecom.checkBatchStatus()的状态。这需要被自动化,而进度条可以看到实际进度,因为检查全局环境并不是首选,它将是一个静态“状态”更新,直到它再次运行。

下面的代码是如何设置的:

require(Rforcecom) 
## Login to Salesforce using your username and password token 
## Once ready to update records, use the following: 

job<- rforcecom.createBulkJob(session, operation = 'update',   
    object = 'custom_object__c') 
info<- rforcecom.createBulkBatch(session, jobId = job$id, data = entry, 
    batchSize = 10000) 
### Re-run this line if status(in global environment) is "In Progress" for  
### updated status 
status<- lapply(info, FUN = function(x) { 
    rforcecom.checkBatchStatus(session, jobId = x$jobId, batchId = x$id)}) 
###Once complete, check details 
details<- lapply(status, FUN = function(x){ 
    rforcecom.getBatchDetails(session, jobId = x$jobId, batchId = x$id)})  
close<- rforcecom.closeBulkJob(session, jobId = job$id) 

回答

0

要自动重新运行状态代码,使用重复循环:

repeat { 
    statements... 
    if (condition) { 
     break 
    } 
} 

然后,为了获得视觉的最新进展,在基数R中使用txtProgressBar()。对于这个特定的函数,我用两个简单的伴侣函数创建了自己的进度条函数。作为关于progressValue()的注释,rforcecom.checkBatchStatus()以列表1和子列表的形式输出。用于检查处理的记录数的子列表名称是“numberRecordsProcessed”。

progressBar<- function(x, start = 0, finish){ 
    # x is your object that is performing a function over a varying time length 
    # finish is your number of rows of data your function is processing 
    pb <- txtProgressBar(min = start, max = finish, style = 3) 
    for (i in 1:finish){ 
     i<- progressValue(x) 
     setTxtProgressBar(pb, i) 
     if (progressValue(x)/finish == 1) { 
     close(pb) 
     } 
    } 
} 

finish<- function(x){ 
    return(as.numeric(nrow(x))) 
} 

progressValue<- function(x){ 
    x=x[[1]][["numberRecordsProcessed"]] 
    return(as.numeric(x)) 
} 

现在,把它们放在一起!只要您知道自己的条件,就可以训练重复循环结束:“已完成”或“失败”。重复“状态”,它将更新处理的记录数量,这样做会更新您的进度条。当处理的记录数量等于数据中的行数时,进度条将退出,您的重复循环也会退出。

repeat { 
    status<- lapply(info, FUN = function(x){ 
     rforcecom.checkBatchStatus(session, jobId = x$jobId, batchId = x$id)}) 
    progressBar(status, finish = finish(entry)) 
    if (status[[1]][["state"]]=="Completed") { 
     break 
    } 
    if (status[[1]][["state"]]=="Failed") { 
     break 
    } 
}