|
@@ -1,15 +1,5 @@
|
|
|
package cn.com.qmth.examcloud.core.oe.student.face.starter.config;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.boot.ApplicationArguments;
|
|
|
-import org.springframework.boot.ApplicationRunner;
|
|
|
-import org.springframework.core.annotation.Order;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
@@ -19,6 +9,19 @@ import cn.com.qmth.examcloud.exchange.inner.api.SmsCloudService;
|
|
|
import cn.com.qmth.examcloud.exchange.inner.api.request.SendSmsReq;
|
|
|
import cn.com.qmth.examcloud.support.cache.CacheHelper;
|
|
|
import cn.com.qmth.examcloud.support.cache.bean.SysPropertyCacheBean;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import com.googlecode.aviator.AviatorEvaluator;
|
|
|
+import com.mysql.cj.util.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description 人脸比对预警任务
|
|
@@ -29,111 +32,128 @@ import cn.com.qmth.examcloud.support.cache.bean.SysPropertyCacheBean;
|
|
|
@Component
|
|
|
@Order(202)
|
|
|
public class ProcessFaceCompareAlarmTask implements ApplicationRunner {
|
|
|
-
|
|
|
- // 失败率预警阈值
|
|
|
- private static final double RATE_WARN_THRESHOLD = 0.5;
|
|
|
-
|
|
|
- // 总数量预警阈值
|
|
|
- private static final int COUNT_WARN_THRESHOLD = 10;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- SmsCloudService smsCloudService;
|
|
|
-
|
|
|
- private final ExamCloudLog captureLog = ExamCloudLogFactory
|
|
|
- .getLog("PROCESS_EXAM_CAPTURE_TASK_LOGGER");
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run(ApplicationArguments args) throws Exception {
|
|
|
-
|
|
|
- Thread thread = new Thread(new Runnable() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- while (true) {
|
|
|
- try {
|
|
|
- faceCompareAlarm();
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- captureLog.error("[FACE_COMPARE_ALARM.] 人脸比对预警出现异常 ", e);
|
|
|
- }
|
|
|
- // 每分钟轮循一次
|
|
|
- Util.sleep(60);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- thread.setDaemon(true);
|
|
|
- thread.start();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 人脸比对,如果有必要则发短信
|
|
|
- */
|
|
|
- private void faceCompareAlarm() {
|
|
|
- if (captureLog.isDebugEnabled()) {
|
|
|
- captureLog.debug("[FACE_COMPARE_ALARM] 进入人脸" + System.currentTimeMillis()
|
|
|
- + "....totalCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareCount() + " ,failCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
- }
|
|
|
-
|
|
|
- // 如果每分钟失败率超过50%则发短信报警,且总数不少于10次则短信报警
|
|
|
- if (ExamCaptureProcessStatisticController.getFaceCompareCount() > COUNT_WARN_THRESHOLD
|
|
|
- && ExamCaptureProcessStatisticController
|
|
|
- .getFaceCompareFailureRate() > RATE_WARN_THRESHOLD) {
|
|
|
- SysPropertyCacheBean faceCompareSmsAssemblyCodeProperty = CacheHelper
|
|
|
- .getSysProperty("capture.faceCompare.smsAssemblyCode");
|
|
|
-
|
|
|
- if (!faceCompareSmsAssemblyCodeProperty.getHasValue()) {
|
|
|
- if (captureLog.isErrorEnabled()) {
|
|
|
- captureLog.error("[FACE_COMPARE_ALARM.] 未配置人脸比对的短信模板代码,totalCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
- + ",errorCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
- }
|
|
|
- throw new StatusException("300001", "未配置人脸比对的短信模板代码");
|
|
|
- }
|
|
|
- SysPropertyCacheBean smsPhoneProperty = CacheHelper
|
|
|
- .getSysProperty("capture.sms.phones");
|
|
|
- if (!smsPhoneProperty.getHasValue()) {
|
|
|
- if (captureLog.isErrorEnabled()) {
|
|
|
- captureLog.error("[FACE_COMPARE_ALARM.] 未配置图片处理失败的通知手机号,totalCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
- + ",errorCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
- }
|
|
|
- throw new StatusException("300002", "未配置图片处理失败的通知手机号");
|
|
|
- }
|
|
|
-
|
|
|
- List<String> phoneList = Arrays
|
|
|
- .asList(smsPhoneProperty.getValue().toString().split(","));
|
|
|
-
|
|
|
- // List<String> phoneList = Arrays.asList("13717595977");
|
|
|
- SendSmsReq sendSmsReq = new SendSmsReq();
|
|
|
- sendSmsReq.setPhoneList(phoneList);
|
|
|
- sendSmsReq.setSmsAssemblyCode(faceCompareSmsAssemblyCodeProperty.getValue().toString());
|
|
|
- // sendSmsReq.setSmsAssemblyCode("FACECOMPARE");
|
|
|
-
|
|
|
- HashMap<String, String> params = new HashMap<>();
|
|
|
- params.put("totalCount",
|
|
|
- String.valueOf(ExamCaptureProcessStatisticController.getFaceCompareCount()));
|
|
|
- params.put("errorCount", String
|
|
|
- .valueOf(ExamCaptureProcessStatisticController.getFaceCompareFailedCount()));
|
|
|
- sendSmsReq.setParams(params);
|
|
|
- try {
|
|
|
- if (captureLog.isDebugEnabled()) {
|
|
|
- captureLog.debug("[FACE_COMPARE_ALARM.] 开始调用发送短信接口,totalCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
- + ",errorCount="
|
|
|
- + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
- }
|
|
|
- smsCloudService.sendSms(sendSmsReq);
|
|
|
- } catch (Exception e) {
|
|
|
- captureLog.error("[FACE_COMPARE_ALARM.] 发送短信出现异常", e);
|
|
|
- }
|
|
|
- }
|
|
|
- // 每1分钟重置一次总数量与失败数量
|
|
|
- ExamCaptureProcessStatisticController.resetAllFaceCompareCount();
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ SmsCloudService smsCloudService;
|
|
|
+
|
|
|
+ private final ExamCloudLog captureLog = ExamCloudLogFactory
|
|
|
+ .getLog("PROCESS_EXAM_CAPTURE_TASK_LOGGER");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
+
|
|
|
+ Thread thread = new Thread(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ while (true) {
|
|
|
+ try {
|
|
|
+ faceCompareAlarm();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ captureLog.error("[FACE_COMPARE_ALARM.] 人脸比对预警出现异常 ", e);
|
|
|
+ }
|
|
|
+ // 每分钟轮循一次
|
|
|
+ Util.sleep(60);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ thread.setDaemon(true);
|
|
|
+ thread.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 人脸比对,如果有必要则发短信
|
|
|
+ */
|
|
|
+ private void faceCompareAlarm() {
|
|
|
+ if (captureLog.isDebugEnabled()) {
|
|
|
+ captureLog.debug("[FACE_COMPARE_ALARM] 进入人脸" + System.currentTimeMillis()
|
|
|
+ + "....totalCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareCount() + " ,failCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认每分钟失败率超过50%则发短信报警,且总数不少于10次则短信报警
|
|
|
+ if (needSmsAlarm()) {
|
|
|
+ SysPropertyCacheBean faceCompareSmsAssemblyCodeProperty = CacheHelper
|
|
|
+ .getSysProperty("capture.faceCompare.smsAssemblyCode");
|
|
|
+
|
|
|
+ if (!faceCompareSmsAssemblyCodeProperty.getHasValue()) {
|
|
|
+ captureLog.error("[FACE_COMPARE_ALARM.] 未配置人脸比对的短信模板代码,totalCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
+ + ",errorCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
+ throw new StatusException("300001", "未配置人脸比对的短信模板代码");
|
|
|
+ }
|
|
|
+ SysPropertyCacheBean smsPhoneProperty = CacheHelper
|
|
|
+ .getSysProperty("capture.sms.phones");
|
|
|
+ if (!smsPhoneProperty.getHasValue()) {
|
|
|
+ if (captureLog.isErrorEnabled()) {
|
|
|
+ captureLog.error("[FACE_COMPARE_ALARM.] 未配置图片处理失败的通知手机号,totalCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
+ + ",errorCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
+ }
|
|
|
+ throw new StatusException("300002", "未配置图片处理失败的通知手机号");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> phoneList = Arrays
|
|
|
+ .asList(smsPhoneProperty.getValue().toString().split(","));
|
|
|
+
|
|
|
+ // List<String> phoneList = Arrays.asList("13717595977");
|
|
|
+ SendSmsReq sendSmsReq = new SendSmsReq();
|
|
|
+ sendSmsReq.setPhoneList(phoneList);
|
|
|
+ sendSmsReq.setSmsAssemblyCode(faceCompareSmsAssemblyCodeProperty.getValue().toString());
|
|
|
+ // sendSmsReq.setSmsAssemblyCode("FACECOMPARE");
|
|
|
+
|
|
|
+ HashMap<String, String> params = new HashMap<>();
|
|
|
+ params.put("totalCount",
|
|
|
+ String.valueOf(ExamCaptureProcessStatisticController.getFaceCompareCount()));
|
|
|
+ params.put("errorCount", String
|
|
|
+ .valueOf(ExamCaptureProcessStatisticController.getFaceCompareFailedCount()));
|
|
|
+ sendSmsReq.setParams(params);
|
|
|
+ try {
|
|
|
+ if (captureLog.isDebugEnabled()) {
|
|
|
+ captureLog.debug("[FACE_COMPARE_ALARM.] 开始调用发送短信接口,totalCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareCount()
|
|
|
+ + ",errorCount="
|
|
|
+ + ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
+ }
|
|
|
+ smsCloudService.sendSms(sendSmsReq);
|
|
|
+ } catch (Exception e) {
|
|
|
+ captureLog.error("[FACE_COMPARE_ALARM.] 发送短信出现异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 每1分钟重置一次总数量与失败数量
|
|
|
+ ExamCaptureProcessStatisticController.resetAllFaceCompareCount();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否需要短信报警
|
|
|
+ *
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ private boolean needSmsAlarm() {
|
|
|
+ if (ExamCaptureProcessStatisticController.getFaceCompareCount()==0){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ SysPropertyCacheBean expressionProperty = CacheHelper.getSysProperty("capture.faceCompare.expression.alarm");
|
|
|
+ if (expressionProperty.getHasValue()) {
|
|
|
+ String expression = expressionProperty.getValue().toString();
|
|
|
+ Map<String, Object> env = Maps.newHashMap();
|
|
|
+ env.put("totalCount", ExamCaptureProcessStatisticController.getFaceCompareCount());
|
|
|
+ env.put("failedCount", ExamCaptureProcessStatisticController.getFaceCompareFailedCount());
|
|
|
+ try {
|
|
|
+ return (Boolean) AviatorEvaluator.execute(expression, env, true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("300004", "failed to execute expression. expression=" + expression);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 默认每分钟失败率超过50%则发短信报警,且失败总数不少于10次则短信报警
|
|
|
+ return ExamCaptureProcessStatisticController.getFaceCompareFailedCount() > 10 &&
|
|
|
+ ExamCaptureProcessStatisticController.getFaceLivenessDetectFailurePercent() > 50;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|