Skip to main content

This article explains how to use the OpCon API and PowerShell to calculate the average runtime of a job based on historical data and update the Max Runtime field accordingly. This is useful for ensuring that job monitoring thresholds are accurate and reflective of actual performance.

Overview

The Max Runtime field in OpCon can be used to trigger alerts when a job exceeds its expected duration. However, this field is not automatically updated by OpCon. By using the OpCon API, you can automate the process of calculating average runtimes and updating this field.

Step-by-Step Process

1. Retrieve Historical Job Data

Use the OpCon API to retrieve job history for a specific job over a defined date range. Example PowerShell code:

$startDate = (Get-Date).AddDays(-7).ToString('yyyy-MM-dd')
$endDate = (Get-Date).ToString('yyyy-MM-dd')
$url = "$baseUrl/jobhistory?startDate=$startDate&endDate=$endDate&jobName=MyJob"
$history = Invoke-RestMethod -Uri $url -Headers $headers -Method Get

2. Calculate Average Runtime

Sum the durations and calculate the average. Then add a buffer (e.g., 25%) to determine the new Max Runtime.

$totalDuration = 0
foreach ($entry in $history) {
    $totalDuration += $entry.duration
}
$average = $totalDuration / $history.Count
$maxRuntime = [math]::Ceiling($average * 1.25)

 3. Retrieve and Update the Job

Fetch the job definition, update the Max Runtime for each frequency, and send the updated job back to the API.

$job = Invoke-RestMethod -Uri "$baseUrl/jobs/$jobId" -Headers $headers -Method Get
foreach ($freq in $job.frequencies) {
    $freq.jobExecution.maxRuntime = $maxRuntime
}
$body = $job | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "$baseUrl/jobs/$jobId" -Headers $headers -Method Put -Body $body -ContentType 'application/json'

Notes

  • Ensure your API token is valid and has sufficient permissions.
  • Adjust the buffer percentage based on your operational tolerance.
  • Consider scheduling this script to run periodically to keep Max Runtime values up to date.

     

Be the first to reply!