Example: Rework Ratio KPI
This example creates a new KPI named Rework Ratio (ISO_22400). The rework ratio is the relationship between the rework quantity (RQ) and produced quantity (PQ) for a work unit, product, production order, or defect type:
RQ/PQ
The rework quantity (RQ) KPI element is created as part of this example.
1. Create a new Thing Shape named ACME_CORP.KPI.ReworkRatioThingShape with the following properties:
ReworkRatio_currentValue,with Base Type=Number
ReworkRatio_lastCalculatedTime, with Base Type=DateTime
ReworkRatio_unitOfMeasure, with Base Type=String
Ensure that the Persistent checkbox is selected for each of these properties.
2. Implement the following services on the ACME_CORP.KPI.ReworkRatioThingShape Thing Shape:
Get_ReworkRatio_CurrentValue
Get_ReworkRatio_ThresholdValues
Get_ReworkRatio_Trend
ReworkRatio_Calculate
Set_ReworkRatio_CurrentValue
3. On the Thing Template of each equipment type for which this KPI is to be calculated, override the GetKPINames service. Add the ACME_CORP.KPI.ReworkRatioThingShape Thing Shape, as in the following example code:
var kpiInfoJSON = new Object();
// JSON of KPI information we want to test if they are implemented on "me"
// For custom KPI, please add into this JSON and following given format
// Warning the order is important
kpiInfoJSON.kpiInfoArray = [
{kpiThingShapeName: 'PTC.SCA.SCO.AvailabilityThingShape', kpiName: 'Availability'},
{kpiThingShapeName: 'PTC.SCA.SCO.QualityRatioThingShape', kpiName: 'QualityRatio'},
{kpiThingShapeName: 'PTC.SCA.SCO.EffectivenessThingShape', kpiName: 'Effectiveness'},
{kpiThingShapeName: 'PTC.SCA.SCO.OEEThingShape', kpiName: 'OEE'},
{kpiThingShapeName: 'ACME_CORP.KPI.ReworkRatioThingShape', kpiName: 'ReworkRatio'}
];
var result = Things["PTC.SCA.SCO.DefaultKPIManager"].GetImplementedKPIsOnThing({
thingName: me.name,
kpiInfo: kpiInfoJSON
});
4. Declare the thresholds for the Rework Ratio KPI. On the Configuration page of the PTC.SCA.SCO.DefaultKPIManager Thing, add a new configuration table.
a. On the PTC.SCA.SCO.DefaultKPIManager Thing, add a configuration table with the following values:
Table NameReworkRatioThresholdValues
Data ShapePTC.SCA.SCO.KPIThresholdValues
Allow Multiple Rows—Ensure that this checkbox is selected.
b. Add rows to the ReworkRatioThresholdValues configuration table for the desired value ranges, similar to the following image:
Screenshot of the ReworkRatioThresholdValues configuration table created as part of this example
5. Define the rework quantity (RQ) KPI element.
a. Create a property on the ACME_CORP.KPI.ReworkRatioThingShape Thing Shape named reworkQuantity_RQ, with a Base Type of Number, and the Persistent checkbox selected.
b. Create a new service on the ACME_CORP.KPI.ReworkRatioThingShape Thing Shape named reworkQuantity_RQ_Calculate with the following code:
function isUndefinedOrNull(value){
return (value === null || typeof(value) === "undefined" );
}
result = me.reworkQuantity_RQ;
//Register first value to cache
me.RegisterValueToKPICache({
key: "reworkQuantity_RQ_firstValue" /* STRING */,
value: result, /* NUMBER */
TimeInfo : TimeInfo
});
c. On the Thing Template of each equipment type for which this KPI is to be calculated, override the GetKPIElementNames service. Add the new property to the list. For example:
result = "actualProductionTime_APT,goodQuantity_GQ,plannedBusyTime_PBT,plannedRunTimePerItem_PRI,producedQuantity_PQ,reworkQuantity_RQ";
d. Update the ReworkRatio_Calculate service created in step 2 to include the new rework quantity (RQ) KPI element. For example:
var customLogger = logger.getLoggerContext().getLogger(logger.getName()+".com.ptc.sca.sco.KPICalculation.ReworkRatio");
var reworkRatio = me.reworkQuantity_RQ;
var producedQuantity = me.producedQuantity_PQ;
if (TimeInfo.getRowCount() > 0){
var row = TimeInfo.getRow(0);
var startTime = row.StartTimeDate;
var generatedID= row.GeneratedID;
// result from service GetKPIElementStartValue: INFOTABLE dataShape: "PTC.SCA.SCO.KPIElementValue"
var reworkQuantity_RQ_firstValue = me.GetKPICacheValue({
generatedID: generatedID/* STRING */,
name: "reworkQuantity_RQ_firstValue" /* STRING */
});
var producedQuantity_PQ_firstValue = me.GetKPICacheValue({
generatedID: generatedID/* STRING */,
name: "producedQuantity_PQ_firstValue" /* STRING */
});
var producedTotalQuantity = 0;
var producedReworkQuantity = 0;
if ( (typeof producedQuantity_PQ_firstValue !== 'undefined') && (typeof reworkQuantity_RQ_firstValue !== 'undefined') ){
producedTotalQuantity = (producedQuantity - producedQuantity_PQ_firstValue);
producedReworkQuantity = (reworkRatio - reworkQuantity_RQ_firstValue);
if ( producedTotalQuantity === 0 ){
// Until items are produced, rework ratio cannot be measured and will default to 1
// with producedReworkQuantity greater than 0, then the service will return 0.
reworkRatio = producedReworkQuantity > 0 ? 0: 1;
}
else{
reworkRatio = producedReworkQuantity / producedTotalQuantity;
}

customLogger.debug("ReworkRatio_Calculate: produced quantity value when time info started - last produced quantity :"+ producedQuantity_PQ_firstValue+"-"+producedQuantity);
customLogger.debug("ReworkRatio_Calculate: rework quantity value when time info started - last good quantity: "+ reworkQuantity_RQ_firstValue+"-"+reworkRatio);
customLogger.debug("ReworkRatio_Calculate: produced Rework Quantity / produced total quantity = quality ratio: " +producedReworkQuantity+ " / " + producedTotalQuantity + " = " + reworkRatio);
}
else{
customLogger.warn("ReworkRatio_Calculate for "+me.name+" could not retrieve the good quantity or produced quantity at the start of the time info.");
}
}
else{
customLogger.warn("ReworkRatio_Calculate for "+me.name+" could not retrieve the current time info. ReworkRatio = 0.");
}
var result = reworkRatio;
me.Set_ReworkRatio_CurrentValue({
value: result
});
var now = new Date();
me.ReworkRatio_lastCalculatedTime = now;

Was this helpful?