waiting to send a report until critical dataset updates - hope this helps somebody out.

WHM
WHM 🟠

We needed to wait until a certain, slow running dataset had finished before sending out reports. With lots of help from Domo, here is what we came up with. I am trying to scrape the interal info, hopefully I do not scrape too much.

 

This is a powershell script we call from a bat file scheduled in Windows scheduler. 

you need

     a developer token

     client and secret for your instance

     $url is your domo instance name e.g. "modo" for modo.domo.com

     the group id you want to send to - this can be found in domostats group dataset.

     the dataset id of the dataset that is your dependency (in the url of the dataset)

    the report id you want to send - get this by going to the scheduled report (in chrome) right click and hit inspect to bring up the debugger. the network tab will show the api call that is made when you click "send now" to send yourself the report.

 

This code waits on the open orders datset then sends three reports. We have seen network errors happen while making any of these calls so that is the reason for all the checking of wait status. if we send two reports and one fails, I do not want to resend the two that already went.

 

I hope people find this useful and appreciate any suggested changes to make it better. 

 

 

$Waitexec = $True
$WaitMgr = $True
$WaitDNO = $True
$NeedToken = $true
$url = "https://$instance.domo.com"
$AllProtocols = [System.Net.SecurityProtocolType]'Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

$Cnct_Cntr = 0

function Get-Dataset($datasetId, $Authentication) {

$headers = @{
Accept = 'text/csv'
Authorization = $Authentication.access_token
}
Write-Host "https://api.domo.com/v1/datasets/$datasetId/data?includeHeader=true&fileName=dump.csv" -Headers $headers
try {
$csv = Invoke-RestMethod -Method Get -Uri "https://api.domo.com/v1/datasets/$datasetId/data?includeHeader=true&fileName=dump.csv" -Headers $headers
$csv = $csv -replace '\\N', $null

$data = ConvertFrom-Csv $csv
# Write-Host "must have Successfully Read CSV File"
}
catch {
Write-Host "Failed to Read CSV File"

$result = $PSItem.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$data = $reader.ReadToEnd()
Write-Host $data
}
return $data
}

function Get-Authorization($clientID, $clientSecret) {
$basicAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $clientID, $clientSecret)));
$authenticationHeader = @{
Authorization = ("Basic {0}" -f $basicAuth)
}
#Write-Host "getting token"
While( $NeedToken -eq $true -and $Cnct_Cntr -lt 100){
try {
$data = Invoke-RestMethod -Method Post -Uri "https://api.domo.com/oauth/token?grant_type=client_credentials&scope=data%20user" -Headers $authenticationHeader
$data.access_token = "bearer $($data.access_token)"
# Write-Host "Got Token"
$NeedToken = $false
}
catch {
#$ErrorActionPreference = Stop #this does not recognize "Stop"
Write-Host "Failed to get Token"
Write-Host $_.ErrorDetails
Write-Host $_.Exception
Write-Host $_.FullyQualifiedErrorId
$Cnct_Cntr++
Write-Host $Cnct_Cntr
Start-Sleep -Seconds 10
}
}
return $data
}

Write-Host "go get auth token"

#Write-Host $Authentication
Get-Date -Format g
$now = Get-Date -Format g

 

While($Waitexec -eq $true -or $WaitMgr -eq $true -or $WaitDNO -eq $true){
# moved authentication token inside loop.... since it may be checking for hours, we need a new token.
$Authentication = Get-Authorization -clientID $apiKey -clientSecret $apiToken
$NeedToken = $true

# Write-Host "have token - getting dataflow metadata"
$headers = @{
Accept = 'application/json'
Authorization = $Authentication.access_token
}
# check the status of the dataset. if it updated between 5 minutes and an hour ago, send the report.
#keep checking every 5 minutes.
$dataset = Invoke-RestMethod -Method Get -Uri "https://api.domo.com/v1/datasets/$Current_Open_Orders" -Headers $headers

$info = $dataset.name
$Now = GET-DATE
$info = $info + ", " + $Now
$Ran = [DateTime] $dataset.updatedAt
$info = $info + ", " + $Ran
$info = $info + ", " + ( $Now - $Ran ).TotalMinutes
Write-Host $info
$Time_sense = ( $Now - $Ran ).TotalMinutes
if( $Time_sense -gt 5 -and $Time_sense -lt 60 -and $Waitexec -eq $true -and $WaitMgr -eq $true ){
$Waitexec = $false
$WaitMgr = $false
$WaitDNO = $false
}
###if( ($Time_sense -gt 280)){$wait = $false } #just for testing
if($Waitexec -eq $true -and $WaitMgr -eq $true -and $WaitDNO -eq $true ){
Start-Sleep -Seconds 300
}
else
{
if($Waitexec -eq $false){
try {


$Body = '[{"email":null,"type":"GROUP","value":"group_id_of_recipients"}]'

$token = @{
"cache-control"= "no-cache";
"Content-Type" = "application/json";
"X-DOMO-Developer-Token" = "developer_token"
}
$Report = Invoke-RestMethod -Method Post -Uri "$url/api/content/v1/reportschedules/views/report_id_captured from debugger/sendNow" -ContentType 'application/json;charset=UTF-8' -Body $Body -Headers $token

Write-Host "Executive Report Sent"


}
catch{
Write-Host $_.ErrorDetails
Write-Host $_.Exception
Write-Host $_.FullyQualifiedErrorId
Write-Host "Report failed to send, staying in the loop"
$Waitexec = $true #report did not send for some reason.
}
}#end exec check

if($WaitMgr -eq $false){
try {

$token = @{
"cache-control"= "no-cache";
"Content-Type" = "application/json";
"X-DOMO-Developer-Token" = "developer_token"
}

#SSC MANAGER REPORT
$Body = '[{"email":null,"type":"GROUP","value":"groupid_for_recipients"}]'
$Report = Invoke-RestMethod -Method Post -Uri "$url/api/content/v1/reportschedules/views/report_id_captured from debugger/sendNow" -ContentType 'application/json;charset=UTF-8' -Body $Body -Headers $token

Write-Host "SSC Manager Report Sent"
}
catch{
Write-Host $_.ErrorDetails
Write-Host $_.Exception
Write-Host $_.FullyQualifiedErrorId
Write-Host "Report failed to send, staying in the loop"
$WaitMgr = $true #report did not send for some reason.
}
}#end SSC Manager check

if($WaitDNO -eq $false){
try {

$token = @{
"cache-control"= "no-cache";
"Content-Type" = "application/json";
"X-DOMO-Developer-Token" = "developertoken"
}

#SSC MANAGER REPORT
$Body = '[{"email":null,"type":"GROUP","value":"group_id_of_recipients"}]'
$Report = Invoke-RestMethod -Method Post -Uri "$url/api/content/v1/reportschedules/views/report_id_captured from debugger/sendNow" -ContentType 'application/json;charset=UTF-8' -Body $Body -Headers $token

Write-Host "Daily New Orders Sent"
}
catch{
Write-Host $_.ErrorDetails
Write-Host $_.Exception
Write-Host $_.FullyQualifiedErrorId
Write-Host "Report failed to send, staying in the loop"
$WaitDNO = $true #report did not send for some reason.
}
}#end DailyNew Order

}#end of Else

}# End While


Get-Date -Format g
Write-Host "Done!"

Best Answer

  • WHM
    WHM 🟠
    Answer ✓

    see original as its a complete solution, thanks!

Answers

This discussion has been closed.