Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

...

Introduction

  • User might be interested to automatically receive reports about past job chain executions with orders.the JS7 - Order History that state what workflows have been executed for what date and time and the respective execution result.
    • Such reports include similar
    • The reports include the same information as available from the JOC Cockpit's Order History view for orders.
    • The reports are provided as Excel® files similar to what is available for export from the JOC Cockpit Order History view.
  • The report Such reports can be scheduled e.g. , for example on a daily basis or more frequently to report about past order , to provide ongoing information about completed orders and workflow execution results.

Use Cases

Report the Order History from a

...

Job

Order History reports can be automated by by JS7 jobs. The following PowerShell modules are used The PowerShell CLI is used by jobs to create reports. Two modules are applied for this purpose:

The Get-JobSchedulerOrderHistoryJS7OrderHistory cmdlet is used to retrieve order history Order History items and to forward them to the ImportExcel module within a job. Two flavors of the job are available for Windows and Linux. The difference is not about the handling of cmdlets or parameters but due to the fact that PowerShell is invoked differently on Windows and Linux. For Windows environments usually PowerShell is available with the OS, for Linux the job has to call pwsh to invoke the PowerShell.

Find a sample report: jobscheduler_reporting.xlsx

The job is the same for Windows and Unix.

Find a sample report for download that includes the report with its Daily-Plan worksheet: jobscheduler_reporting.xlsx

First Line of the Job

The only difference between platforms is the way how PowerShell is invoked with the first line of the job.

Code Block
languagebash
titleShebang for PowerShell with Unix
#!/usr/bin/env pwsh

Explanation:

  • Use of a shebang allows to invoke the pwsh PowerShell executable.


Code Block
languagebash
titleShebang for PowerShell with Windows
@@findstr/v "^@@f.*&" "%~f0"|pwsh.exe -&goto:eof

Explanation:

  • Credits for the Windows shebang replacement to How to run a PowerShell script within a Windows batch file
  • If you consider this shebang replacement somewhat cryptic then add it to JS7 - Script Includes that are easily referenced from a shell job, for example by using ##!include pwsh
  • The PowerShell executable pwsh.exe is available starting from PowerShell 6.0. PowerShell releases 5.x use the executable powershell.exe that can be specified accordingly with the shebang.

Job Implementation

Please consider that the below job is an example that has Please consider that below jobs are examples that have to be adjusted for your environment.

...

Download: report_order_history_windows.job.xmljdOrderHistoryReport.workflow.json


Code Block
languagepowershell
titleOrder History Report (Windows version)Daily Plan Report Job
linenumberstrue
collapsetrue
<?xml version="1.0" encoding="UTF-8" ?>
<job title="Report Order History" process_class="agent_windows">
  <script language="powershell"><![CDATA[
Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/ImportExcel;
Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/JobScheduler;

Connect-JS -Url $JOCCockpitUrl -Credential $JOCCockpitCredential@@findstr/v "^@@f.*&" "%~f0"|pwsh.exe -&goto:eof

Import-Module ImportExcel
Import-Module JS7

$credentials = ( New-Object -typename System.Management.Automation.PSCredential -ArgumentList 'root', ( 'root' | ConvertTo-SecureString -AsPlainText -Force) )
Connect-JS7 -Url $env:JS7_JOC_URL -Credentials $credentials -Id $env:JS7_CONTROLLER_ID | Out-Null;

# Dates in local time timezonezone, output includes local date format
Get-JSOrderHistory -Timezone (Get-Timezone) -DateFromRelativeDateFrom (Get-Date).AddDays(-3).ToUniversalTime()-3d `
                |  Select-Object -Property @{name="JobSchedulerController ID"; expression={$_.jobschedulerIdcontrollerId}}, `
                                           @{name="OrderHistory ID"; expression={$_.orderIdhistoryId}}, `
                                           @{name="JobOrder ChainID"; expression={$_.jobChainorderId}}, `
                                           @{name="Order Status"; expression={$_.state._text}}, `
                                           @{name="StartOrder TimePosition"; expression={ Get-Date $_.startTime $_.position}}, `
                                           @{name="Workflow"; expression={$_.workflow}}, `
                                           @{name="History Status"; expression={$_.state._text}}, `
                                           @{name="EndPlanned Start Time"; expression={ Get-Date $_.endTimeplannedTime }}, `
                                           @{name="Duration (sec.)Start Time"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").SecondsGet-Date $_.startTime}}, `
                                           @{name="End Time"; expression={ Get-Date $_.endTime }}, `
                                           @{name="NodeDuration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.node.endTime)").Seconds }} `
                | Export-Excel -Path /tmp/jobscheduler_reporting.xlsx -WorksheetName "Order-History" -ClearSheet;
				
Write-Output ".. report created: /tmp/jobscheduler_reporting.xls";
]]></script>
  <run_time/>
</job>xlsx"				

Explanations

  • Line 2-31: The job is executed with a Windows Agent that is assigned by a process class. The job is of type "powershell" and will use the Powershell version provided with the server.and makes use of the PowerShell shebang for Windows, see above explanation.
  • Line 3-4Line 4-5: The required PowerShell modules are imported. They could be installed with any location in the file system
  • Line 6-7: The Connect-JSJS7 cmdlet is used to authenticate with the JOC Cockpit JS7 REST Web Service API. The required URL and credentials are arguments for -Url , -Credentials and -Id can specified in a PowerShell profile, see PowerShell CLI 1.2 - Use Cases - Credentials Managementnumber of ways:
  • Line 10: For better readability of the report the start types of jobs are mapped to a textual representation (single start, cyclic start etc.).
  • Line 13: The Get-JS7DailyPlanOrder cmdlet is invokedLine 10: The Get-JSOrderHistory cmdlet is called 
    • with the parameter -Timezone to specify to which timezone time zone date values in the report should be converted. The parameter value -Timezone (Get-Timezone) specifies that the timezone time zone of the Agent's server is used. Otherwise specify the desired timezone e.g. time zone, for example like this: -Timezone (Get-Timezone -Id 'GMT Standard Time'). Without using this parameter any date values are stored as UTC dates to the report.
    • optionally with additional parameters, e.g. for example to specify the date or date range for which the report is created  A value -DateFrom (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-7).ToUniversalTime()RelativeDateTo +7d specifies that the report should cover the last next 7 days (from until midnight). Keep in mind that dates have to be specified for the UTC timezonetime zone. Without this parameter the report will be created for the last next day.
    • see the Get-JSOrderHistory cmdlet JS7DailyPlanOrder cmdlet for a full parameter reference.
  • Line 1114-1826: From the output of the Get-JSOrderHistory JS7DailyPlanOrder cmdlet a number of properties are selected and and are specified for the sequence in which they should occur in the report. 
    • To add more speaking column headers the property names are mapped to a more readable textual representation.
    • Consider the handling of date formats in lines 15, 16line 21-25. Use of the Get-Date cmdlet converts the output format of dates (not the timezonetime zone) to the default format that is in place on the Agent's server. Without using the Get-Date cmdlet any date values will be stored to the report in ISO format, e.g. 2020-12-31 10:11:12+02:00 for a date in the European central timezone time zone that is UTC+1 in winter time and UTC+2 in summer time.
    • Lines 17 introduces 23, 26 introduce a new property, a calculated duration. From the start time and end time values of a planned start and optionally of a past start the difference in seconds is calculated and is forwarded to the report.
  • Line 1927: The list of properties per order history Daily Plan item is piped to the Export-Excel cmdlet that is available with the ImportExcel PowerShell Module. The report file name is specified and optionally the worksheet. For a full list of parameters see the ImportExcel PowerShell Module.

Linux Version


Download: report_order_history_linux.job.xmll

Code Block
languagepowershell
titleOrder History Report (Linux Windows version)
linenumberstrue
collapsetrue
<?xml version="1.0" encoding="UTF-8" ?>
<job title="Report Order History" process_class="agent_linuxwindows">
  <script language="shellpowershell"><![CDATA[
pwsh -NoLogo -NonInteractive -Command '& {
    . $env:SCHEDULER_DATA/config/powershell/JobScheduler.PowerShell_profile.ps1;
    Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/ImportExcel;
    Import-Module $env:SCHEDULER_DATA/config/powershell/Modules/JobScheduler;

    Connect-JS -Url $JOCCockpitUrl -Credential $JOCCockpitCredential | Out-Null;

    # Dates in local timezone, output includes local date format;
    Get-JSOrderHistory -Timezone (Get-Timezone) -DateFrom (Get-Date).AddDays(-73).ToUniversalTime() `
                |  Select-Object -Property @{name="JobScheduler ID"; expression={$_.jobschedulerId}}, `
                                           @{name="Order ID"; expression={$_.orderId}}, `
                                           @{name="Job Chain"; expression={$_.jobChain}}, `
                                           @{name="Status"; expression={$_.state._text}}, `
                                           @{name="Start Time"; expression={ Get-Date $_.startTime }}, `
                                           @{name="End Time"; expression={ Get-Date $_.endTime }}, `
                                           @{name="Duration (sec.)"; expression={ (New-Timespan -Start "$($_.startTime)" -End "$($_.endTime)").Seconds }}, `
                                           @{name="Node"; expression={$_.node}} `
                | Export-Excel -Path /tmp/jobscheduler_reporting.xlsx -WorksheetName "Order-History" -ClearSheet;
				
    Write-Output ".. report created: /tmp/jobscheduler_reporting.xls";
}'
]]></script>
  <run_time/>
</job>
</job>

Explanations

Explanation:

  • Line 2-3: The job is executed with a Windows Agent that is assigned by a process class. The job is of type "powershell" and will use the Powershell version provided with the server.
  • Line 4-5: The required PowerShell modules are imported. They could be installed with any location in the file system
  • Line 7: The Connect-JS cmdlet is used to authenticate with the JOC Cockpit REST Web Service. The required URL and credentials are specified in a PowerShell profile, see PowerShell CLI 1.2 - Use Cases - Credentials Management
  • Line 10: The Get-JSOrderHistory cmdlet is called 
    • with the parameter -Timezone to specify to which timezone date values in the report should be converted. The parameter value -Timezone (Get-Timezone) specifies that the timezone of the Agent's server is used. Otherwise specify the desired timezone e.g. like this: -Timezone (Get-Timezone -Id 'GMT Standard Time'). Without using this parameter any date values are stored as UTC dates to the report.
    • optionally with additional parameters, e.g. to specify the date range for which the report is created  A value -DateFrom (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-7).ToUniversalTime() specifies that the report should cover the last 7 days (from midnight). Keep in mind that dates have to be specified for the UTC timezone. Without this parameter the report will be created for the last day.
    • see the Get-JSOrderHistory cmdlet for a full parameter reference.
  • Line 11-18: From the output of the Get-JSOrderHistory cmdlet a number of properties are selected and and are specified for the sequence in which they should occur in the report. 
    • To add more speaking column headers the property names are mapped to a more readable textual representation.
    • Consider the handling of date formats in lines 15, 16. Use of the Get-Date cmdlet converts the output format of dates (not the timezone) to the default format that is in place on the Agent's server. Without using the Get-Date cmdlet any date values will be stored to the report in ISO format, e.g. 2020-12-31 10:11:12+02:00 for a date in the European central timezone that is UTC+1 in winter time and UTC+2 in summer time.
    • Lines 17 introduces a new property, a calculated duration. From the start time and end time values of a past start the difference in seconds is calculated and is forwarded to the report.
  • Line 19: The list of properties per order history item is piped to the Export-Excel cmdlet that is available with the ImportExcel PowerShell Module. The report file name is specified and optionally the worksheet. For a full list of parameters see the ImportExcel PowerShell Module
  • Basically the same explanations as for the Windows version of the job apply.
  • Line 4: The PowerShell has to be invoked with pwsh. Consider that any subsequent PowerShell commands are quoted within a string that starts with line 4 and that ends with line 24.
    • As the string is using a single quote all subsequent PowerShell commands make use of double quotes when required.
    • You could apply a different quoting style, however, quotes have to be consistent.
  • Line 5: As an example a PowerShell profile is invoked that provides the variables for URL and credentials to access the JOC Cockpit REST Web Service. Such profiles can be stored in different locations and can be invoked automatically by pwsh on startup.