Customizing Information Content and Access > Configuring and Customizing KPIs > Calculating KPIs Using Job Orders Rather Than Shifts
Calculating KPIs Using Job Orders Rather Than Shifts
As a customization, you can calculate KPIs using job orders rather than shifts as the period of time. This calculates the KPIs over the duration of a job order, from start to completion, when that time period can cover a span of multiple shifts.
* 
KPIs can be calculated using either shifts for the time period or using job orders for the time period. This customization changes the calculation of all KPIs to use job orders.
Once this customization is in place, each time KPIs are calculated the system retrieves all running job orders. Running job orders are those for which both of the following statements are true:
The current time is after the actual start time of the job order.
The actual end time for the job order is not defined.
For each job order retrieved, all assets referencing the UID of the job order are found. The current KPI values for those assets are calculated based on the actual start of the job order and the current time.
To customize KPI calculation to use job orders, complete the following steps:
Customizing the KPI Manager
1. Create a new KPI manager Thing extending the PTC.SCA.SCO.KPIManagerThingTemplate.
a. Click New and select Thing.
b. Enter a Name for the Thing.
c. For the Base Thing Template, select PTC.SCA.SCO.KPIManagerThingTemplate or a Thing Template that extends PTC.SCA.SCO.KPIManagerThingTemplate.
d. Click Save to save the new Thing Template.
2. Edit the KPIManager defined for the system.
a. Navigate to the launch point configuration Thing (PTC.Factory.C_LaunchPointConfigurationThing_[ReleaseVersion]).
b. Under Configuration, in the ManagerConfigurationSettings table, click Edit icon to edit the KPIManager. Change the Value to the KPI manager Thing created in step 1, and click Set.
c. Click Save to save the updates to the launch point configuration Thing.
3. On the new KPI manager Thing, create a service to retrieve the currently running job orders.
a. Under Services, click Add.
b. For Name, enter GetRunningJobOrders.
c. For Output, select INFOTABLE from the drop-down menu. For Data Shape, search for and select PTC.SCA.SCO.JobOrder.
d. Enter the following code in the service editor:

var filters = {
"filters": {
"filters": [
{
"dataShapeName": "PTC.SCA.SCO.JobOrder",
"fieldName": "ActualStartTime",
"type": "LE",
"value": new Date()
},
{
"dataShapeName": "PTC.SCA.SCO.JobOrder",
"fieldName": "ActualEndTime",
"type": "MISSINGVALUE"
}

],
"type": "AND"
},
"sorts": [
{
"fieldName": "ActualStartTime",
"isAscending": true
}
]
}
// result: THINGNAME
var productionOrder = Things["PTC.Factory.LaunchPointConfigurationThing"].GetProductionOrderManagerThingName();

// result: INFOTABLE dataShape: "PTC.SCA.SCO.JobOrder"
var result = Things[productionOrder].GetJobOrders({
filter: filters /* JSON */,
offset: -1 /* INTEGER */,
limit: 99999999 /* INTEGER */
});
e. Click Save and Continue.
4. On the new KPI manager Thing, create a service to retrieve all equipment Things for which KPIs are calculated, that are related to this job order.
a. Under Services, click Add.
b. For Name, enter GetKPIElementsFromJobOrder.
c. For Inputs, click Add. For Name, enter JobOrderUID.
d. For Output, select INFOTABLE from the drop-down menu.
e. Enter the following code in the service editor:
//Calculate all elements value
//Retrieve all KPIElement things
var elementThingShape = "PTC.SCA.SCO.KPIElementThingShape";
var maxItems = 300;
var filter = {
"filters" : {
"type" : "EQ",
"fieldName" : "JobOrderUID",
"value" : JobOrderUID
}
};


var tmpResult = Resources["SearchFunctions"].SearchThings({
searchExpression: undefined /* STRING */,
modelTags: undefined /* TAGS */,
networks: undefined /* JSON */,
types: {"items":["Thing"]} /* JSON */,
thingTemplates: undefined /* JSON */,
thingShapes: {"items":["PTC.SCA.SCO.KPIElementThingShape"]} /* JSON */,
aspects: undefined /* JSON */,
excludedAspects: undefined /* JSON */,
identifierSearchExpression: undefined /* STRING */,
query: filter /* QUERY */,
maxItems: maxItems /* NUMBER */,
maxSearchItems: maxItems /* NUMBER */
});

var result = tmpResult.getRow(0).thingResults;
f. Click Save and Continue.
5. On the new KPI manager Thing, find and override the GetKPIElementsFromTimeInfo service.
6. Replace the content in the service editor with the following code:
var jobOrderUID = TimeInfo.getRow(0).UID+"";
var result = me.GetKPIElementsFromJobOrder({
JobOrderUID: jobOrderUID /* STRING */
});
7. Click Done and then click Save.
Customizing the KPI Calculation Scheduler
Complete the following steps to customize the KPI calculation scheduler:
1. Navigate to the PTC.SCA.SCO.KPIScheduler.
2. Under Services, override the GetTimeInfos service.
3. Replace the content in the service editor with the following code:
var params = {
infoTableName : "InfoTable",
dataShapeName : "PTC.SCA.SCO.TimeInfo"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(PTC.SCA.SCO.TimeInfo)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);


//Retrieve All JobOrders
var jobOrders =Things[Things["PTC.Factory.LaunchPointConfigurationThing"].GetKPIManagerThingName()].GetRunningJobOrders();

var tableLength = jobOrders.rows.length;
for (var x=0; x < tableLength; x++) {
var row = jobOrders.rows[x];
var uid = row.UID;
//Your code here
// PTC.SCA.SCO.ShiftInfo entry object
var newEntry = new Object();
newEntry.UID = uid; // LONG
newEntry.StartTimeDate = row.ActualStartTime; // DATETIME
newEntry.GeneratedID = 'JobOrder-'+uid; // STRING [Primary Key]
//newEntry.EndTimeDate = undefined; // DATETIME
newEntry.SiteUID = undefined; // LONG
result.AddRow(newEntry);
}
4. Click Done and then click Save.
Associate Job Orders to Equipment
Associate a job order to an individual piece of equipment by setting the JobOrderUID property on the equipment Thing to the UID of the job order. When the KPIs are calculated for the piece of equipment, the job order reference is stored as part of the historical data for the KPI.
To associate a piece of equipment, such as an asset or line, with a job order:
1. In ThingWorx Composer, navigate to the Thing for the piece of equipment. By default, the Thing name in ThingWorx Composer is the equipment name shown inThingWorx Apps, prefixed by the equipment type. For example, for an asset named 1-2_GantryRobot, the Thing name is Asset_1-2_GantryRobot.
2. Under Properties and Alerts, set the value for the JobOrderUID property to the UID of the appropriate job order.
* 
The UID of a job order is returned in the CreateJobOrders service output when a job order is created. You can also use the GetJobOrders service to retrieve the job orders in the database, including their UIDs. Both services are found on the default production order manager (PTC.SCA.SCO.DefaultProductionOrderManager).
3. Click Save to save the updates to the Thing.
For more information on job orders, see Job Order Schema.
Starting a Job Order
Start a job order by setting the ActualStartTime property on the job order.
To set the start time a job order:
1. In ThingWorx Composer, navigate to the default production order manager (PTC.SCA.SCO.DefaultProductionOrderManager).
2. Under Services, execute the UpdateJobOrders service.
3. In the JobOrders input parameter table, click Add and provide the UID of the job order to be updated. Enter the new value for the ActualStartTime field. Leave other fields blank. Click Add to add the updated information to the input parameter table.
Multiple job orders can be updated by adding them individually to the JobOrders input parameter table.
4. Click Execute.
Closing the Job Order and Trigger Final KPI Calculation
Close a job order by setting the ActualEndTime property on the job order. When a job order is closed, a final KPI calculation must be triggered. After the final KPI calculation is performed, the association between the job order and the piece of equipment can be removed.
The following procedure creates a service that closes the job order by setting the end time, triggers the final KPI calculation, and cleans up the association between the job order and equipment.
1. On the new KPI manager, under Services, click Add.
2. For Name, enter CompleteJobOrder.
3. Enter the following code in the service editor:
// result: INFOTABLE dataShape: "PTC.SCA.SCO.JobOrder"
var jobOrderManager = Things[Things["PTC.Factory.LaunchPointConfigurationThing"].GetProductionOrderManagerThingName()];

// result: INFOTABLE dataShape: "PTC.SCA.SCO.JobOrder"
var jobOrder = Things["PTC.SCA.SCO.DefaultProductionOrderManager"].GetJobOrder({
UID: jobOrderUID /* STRING */
});

//Update end Date
jobOrder.getRow(0).ActualEndTime = new Date();
var updatedJobOrder = jobOrderManager.UpdateJobOrders({
JobOrders: jobOrder /* INFOTABLE */
});

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(PTC.SCA.SCO.TimeInfo)
var timeInfo = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape( {infoTableName : "InfoTable", dataShapeName : "PTC.SCA.SCO.TimeInfo"
});


var jobOrder = updatedJobOrder.rows[0];
var uid = jobOrder.UID;
// PTC.SCA.SCO.ShiftInfo entry object
var newEntry = new Object();
newEntry.UID = uid; // LONG
newEntry.StartTimeDate = jobOrder.ActualStartTime; // DATETIME
newEntry.EndTimeDate = jobOrder.ActualEndTime; // DATETIME
newEntry.GeneratedID = 'JobOrder-'+uid; // STRING [Primary Key]
newEntry.SiteUID = undefined; // LONG
timeInfo.AddRow(newEntry);


Things["PTC.SCA.SCO.KPIScheduler"].CalculateKPIsForTimeInfo({
TimeInfo: timeInfo /* INFOTABLE */
});


// result: INFOTABLE dataShape: ""
var result = Things[Things["PTC.Factory.LaunchPointConfigurationThing"].GetKPIManagerThingName()].GetKPIElementsFromJobOrder({
JobOrderUID: jobOrderUID /* STRING */
});

//Remove job order reference from the thing
var tableLength = result.rows.length;
for (var x=0; x < tableLength; x++) {
var row = result.rows[x];
//Your code here
Things[row.name].JobOrderUID = undefined;
}
4. Click Done, then click Save.
Was this helpful?