From 25431a033c649fb704419cca6a3ac1f75ac1ae45 Mon Sep 17 00:00:00 2001 From: Jackie Han Date: Wed, 10 Aug 2022 12:49:07 -0700 Subject: [PATCH] Fix undefine expect value for feature attribution (#310) Signed-off-by: Jackie Han Signed-off-by: Jackie Han --- server/routes/ad.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/server/routes/ad.ts b/server/routes/ad.ts index 912aea0a..42d71cb4 100644 --- a/server/routes/ad.ts +++ b/server/routes/ad.ts @@ -1009,8 +1009,7 @@ export default class AdService { ? toFixedNumberForAnomaly(Number.parseFloat(featureData.data)) : 0, name: featureData.feature_name, - expectedValue: result._source.anomaly_grade > 0 - ? this.getExpectedValue(result, featureData.feature_id) : featureData.data + expectedValue: this.getExpectedValue(result, featureData) }); }); }); @@ -1135,22 +1134,26 @@ export default class AdService { ? toFixedNumberForAnomaly(Number.parseFloat(featureData.data)) : 0, name: featureData.feature_name, - expectedValue: rawResult._source.anomaly_grade > 0 - ? this.getExpectedValue(rawResult, featureData.feature_id) - : featureData.data + expectedValue: this.getExpectedValue(rawResult, featureData) }; }); return featureResult; }; - getExpectedValue = (rawResult: any, featureId: string) => { - const expectedValueList = rawResult._source.expected_values; - if (expectedValueList.length > 0) { - expectedValueList[0].value_list.forEach((expect: any) => { - if (expect.feature_id === featureId) { - return expect.data; - } - }) + getExpectedValue = (rawResult: any, featureData: any) => { + let expectedValue = featureData.data != null && featureData.data !== 'NaN' + ? toFixedNumberForAnomaly(Number.parseFloat(featureData.data)) + : 0; + if (rawResult._source.anomaly_grade > 0) { + const expectedValueList = rawResult._source.expected_values; + if (expectedValueList?.length > 0) { + expectedValueList[0].value_list.forEach((expect: any) => { + if (expect.feature_id === featureData.feature_id) { + expectedValue = expect.data; + } + }) + } } + return expectedValue } }