|
@@ -12,6 +12,8 @@ import cn.com.ctop.alarm.modules.mapper.AlarmEventSendMapper;
|
|
|
import cn.com.ctop.alarm.modules.mapper.AlarmMetricValueMapper;
|
|
|
import cn.com.ctop.alarm.modules.mapper.AlarmRuleCampaignRelMapper;
|
|
|
import cn.com.ctop.alarm.modules.service.IAlarmEventSendService;
|
|
|
+import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
|
|
|
import cn.com.ctop.common.module.service.IMessageTemplate;
|
|
|
import cn.com.ctop.common.module.service.ISendMessageService;
|
|
|
import cn.com.ctop.common.module.utils.Check;
|
|
@@ -19,9 +21,11 @@ import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaign;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroup;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCampaignMapper;
|
|
|
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouGroupMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.xxl.job.core.enums.NoEn;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jeecg.common.util.db.DataSourceCachePool;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.HashOperations;
|
|
@@ -40,6 +44,7 @@ import java.util.Map;
|
|
|
* @version V1.0
|
|
|
* @date 2020-10-29
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class AlarmEventSendServiceImpl extends ServiceImpl<AlarmEventSendMapper, AlarmEventSend> implements IAlarmEventSendService {
|
|
|
|
|
@@ -68,6 +73,12 @@ public class AlarmEventSendServiceImpl extends ServiceImpl<AlarmEventSendMapper,
|
|
|
private KuaiShouCampaignMapper kuaiShouCampaignMapper;
|
|
|
|
|
|
@Autowired
|
|
|
+ private IKuaiShouUpdateService kuaiShouUpdateService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICtopOauthTokenService oauthTokenService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private KuaiShouGroupMapper kuaiShouGroupMapper;
|
|
|
|
|
|
private static JSONObject alarmValue;
|
|
@@ -80,7 +91,11 @@ public class AlarmEventSendServiceImpl extends ServiceImpl<AlarmEventSendMapper,
|
|
|
* @throws
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
- private JSONObject getAlarmValue() {
|
|
|
+ @Override
|
|
|
+ public JSONObject getAlarmValue(String type) {
|
|
|
+ if (NoEn.NO1.valueStr().equals(type)) {
|
|
|
+ alarmValue = null;
|
|
|
+ }
|
|
|
if (Check.isNull(alarmValue)) {
|
|
|
alarmValue = new JSONObject();
|
|
|
List<AlarmMetricValue> valueList = alarmMetricValueMapper.selectList(null);
|
|
@@ -154,124 +169,139 @@ public class AlarmEventSendServiceImpl extends ServiceImpl<AlarmEventSendMapper,
|
|
|
* @author ZHAOXA
|
|
|
*/
|
|
|
private void matchRules(AlarmEventRule rule, JSONObject cost, String level) {
|
|
|
- //目标值
|
|
|
- String goal = cost.getString(rule.getMetricValueCode());
|
|
|
- if (Check.isNull(goal)) {
|
|
|
- log.warn("规则中指标不匹配");
|
|
|
- return;
|
|
|
- }
|
|
|
- //总消耗
|
|
|
- BigDecimal totalCost = cost.getBigDecimal("totalCost");
|
|
|
- BigDecimal value = new BigDecimal(goal);
|
|
|
- List<AlarmEventMetric> metrics = rule.getAlarmEventMetrics();
|
|
|
- if (Check.isNull(metrics)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- //value配置数据
|
|
|
- JSONObject alarmValue = getAlarmValue();
|
|
|
- boolean flag = true;
|
|
|
- StringBuffer _depletion = new StringBuffer();
|
|
|
- StringBuffer _roi = new StringBuffer();
|
|
|
- StringBuffer _value = new StringBuffer();
|
|
|
- for (AlarmEventMetric metric : metrics) {
|
|
|
- if (flag) {
|
|
|
- //阈值
|
|
|
- BigDecimal threshold = new BigDecimal(metric.getThreshold());
|
|
|
- String judgeMethod = metric.getJudgeMethod();
|
|
|
- String metricValue = metric.getMetricValueCode();
|
|
|
- JSONObject valObj = alarmValue.getJSONObject(metricValue);
|
|
|
- //模板
|
|
|
- String msgTemplate = valObj.getString("msgTemplate");
|
|
|
- //单位 元、%
|
|
|
- String msgValueUnit = valObj.getString("msgValueUnit");
|
|
|
- //阈值类型 1-字符串,2-数字(BigDecimal),3-百分数',
|
|
|
- String showValue = getThreshold(valObj.getString("msgValueType"), threshold, msgValueUnit);
|
|
|
- String judgment = "低于";
|
|
|
- //大于等于
|
|
|
- if ("GT_EQ".equals(judgeMethod)) {
|
|
|
- judgment = "高于";
|
|
|
- //判断阈值类型(cost-成本;depletion-消耗;roi)
|
|
|
- if (metricValue.contains("_depletion")) {
|
|
|
- flag = totalCost.compareTo(threshold) >= 0;
|
|
|
- getMsgTemplate(_depletion, msgTemplate, judgment, "消耗值", showValue);
|
|
|
- } else if (metricValue.contains("_roi")) {
|
|
|
- flag = value.compareTo(threshold) >= 0;
|
|
|
- getMsgTemplate(_roi, msgTemplate, judgment, "ROI", showValue);
|
|
|
- } else {
|
|
|
- flag = value.compareTo(threshold) >= 0;
|
|
|
- getMsgTemplate(_value, msgTemplate, judgment, "阈值", showValue);
|
|
|
- }
|
|
|
- //小于等于
|
|
|
- } else if ("LT_EQ".equals(judgeMethod)) {
|
|
|
- if (metricValue.contains("_depletion")) {
|
|
|
- flag = totalCost.compareTo(threshold) <= 0;
|
|
|
- getMsgTemplate(_depletion, msgTemplate, judgment, "消耗值", showValue);
|
|
|
- } else if (metricValue.contains("_roi")) {
|
|
|
- flag = value.compareTo(threshold) <= 0;
|
|
|
- getMsgTemplate(_roi, msgTemplate, judgment, "ROI", showValue);
|
|
|
- } else {
|
|
|
- flag = value.compareTo(threshold) <= 0;
|
|
|
- getMsgTemplate(_value, msgTemplate, judgment, "阈值", showValue);
|
|
|
- }
|
|
|
- //介于
|
|
|
- } else if ("BETWEEN".equals(judgeMethod)) {
|
|
|
- BigDecimal minThreshold = new BigDecimal(metric.getMinThreshold());
|
|
|
- String minshowValue = getThreshold(valObj.getString("msgValueType"), minThreshold, msgValueUnit);
|
|
|
- if (metricValue.contains("_depletion")) {
|
|
|
- flag = (totalCost.compareTo(threshold) > 0) || (totalCost.compareTo(minThreshold) < 0);
|
|
|
- if (totalCost.compareTo(threshold) > 0) {
|
|
|
- judgment = "高于";
|
|
|
+ try {
|
|
|
+ //目标值
|
|
|
+ String goal = cost.getString(rule.getMetricValueCode());
|
|
|
+ if (Check.isNull(goal)) {
|
|
|
+ log.warn("规则中指标不匹配");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //总消耗
|
|
|
+ BigDecimal totalCost = cost.getBigDecimal("totalCost");
|
|
|
+ BigDecimal value = new BigDecimal(goal);
|
|
|
+ List<AlarmEventMetric> metrics = rule.getAlarmEventMetrics();
|
|
|
+ if (Check.isNull(metrics)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //value配置数据
|
|
|
+ JSONObject alarmValue = getAlarmValue(NoEn.NO0.valueStr());
|
|
|
+ boolean flag = true;
|
|
|
+ StringBuffer _depletion = new StringBuffer();
|
|
|
+ StringBuffer _roi = new StringBuffer();
|
|
|
+ StringBuffer _value = new StringBuffer();
|
|
|
+ for (AlarmEventMetric metric : metrics) {
|
|
|
+ if (flag) {
|
|
|
+ //阈值
|
|
|
+ BigDecimal threshold = new BigDecimal(metric.getThreshold());
|
|
|
+ String judgeMethod = metric.getJudgeMethod();
|
|
|
+ String metricValue = metric.getMetricValueCode();
|
|
|
+ JSONObject valObj = alarmValue.getJSONObject(metricValue);
|
|
|
+ //模板
|
|
|
+ String msgTemplate = valObj.getString("msgTemplate");
|
|
|
+ //单位 元、%
|
|
|
+ String msgValueUnit = valObj.getString("msgValueUnit");
|
|
|
+ //阈值类型 1-字符串,2-数字(BigDecimal),3-百分数',
|
|
|
+ String showValue = getThreshold(valObj.getString("msgValueType"), threshold, msgValueUnit);
|
|
|
+ String judgment = "低于";
|
|
|
+ //大于等于
|
|
|
+ if ("GT_EQ".equals(judgeMethod)) {
|
|
|
+ judgment = "高于";
|
|
|
+ //判断阈值类型(cost-成本;depletion-消耗;roi)
|
|
|
+ if (metricValue.contains("_depletion")) {
|
|
|
+ flag = totalCost.compareTo(threshold) >= 0;
|
|
|
+ getMsgTemplate(_depletion, msgTemplate, judgment, "消耗值", showValue);
|
|
|
+ } else if (metricValue.contains("_roi")) {
|
|
|
+ flag = value.compareTo(threshold) >= 0;
|
|
|
+ getMsgTemplate(_roi, msgTemplate, judgment, "ROI", showValue);
|
|
|
+ } else {
|
|
|
+ flag = value.compareTo(threshold) >= 0;
|
|
|
+ getMsgTemplate(_value, msgTemplate, judgment, "阈值", showValue);
|
|
|
}
|
|
|
- getMsgTemplate(_depletion, msgTemplate, judgment, "消耗区间", minshowValue.concat("-").concat(showValue));
|
|
|
- } else if (metricValue.contains("_roi")) {
|
|
|
- flag = (value.compareTo(threshold) > 0) || (value.compareTo(minThreshold) < 0);
|
|
|
- if (value.compareTo(threshold) > 0) {
|
|
|
- judgment = "高于";
|
|
|
+ //小于等于
|
|
|
+ } else if ("LT_EQ".equals(judgeMethod)) {
|
|
|
+ if (metricValue.contains("_depletion")) {
|
|
|
+ flag = totalCost.compareTo(threshold) <= 0;
|
|
|
+ getMsgTemplate(_depletion, msgTemplate, judgment, "消耗值", showValue);
|
|
|
+ } else if (metricValue.contains("_roi")) {
|
|
|
+ flag = value.compareTo(threshold) <= 0;
|
|
|
+ getMsgTemplate(_roi, msgTemplate, judgment, "ROI", showValue);
|
|
|
+ } else {
|
|
|
+ flag = value.compareTo(threshold) <= 0;
|
|
|
+ getMsgTemplate(_value, msgTemplate, judgment, "阈值", showValue);
|
|
|
}
|
|
|
- getMsgTemplate(_roi, msgTemplate, judgment, "ROI区间", minshowValue.concat("-").concat(showValue));
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- flag = (value.compareTo(threshold) > 0) || (value.compareTo(minThreshold) < 0);
|
|
|
- if (value.compareTo(threshold) > 0) {
|
|
|
- judgment = "高于";
|
|
|
+ //介于
|
|
|
+ } else if ("BETWEEN".equals(judgeMethod)) {
|
|
|
+ BigDecimal minThreshold = new BigDecimal(metric.getMinThreshold());
|
|
|
+ String minshowValue = getThreshold(valObj.getString("msgValueType"), minThreshold, msgValueUnit);
|
|
|
+ if (metricValue.contains("_depletion")) {
|
|
|
+ flag = (totalCost.compareTo(threshold) > 0) || (totalCost.compareTo(minThreshold) < 0);
|
|
|
+ if (totalCost.compareTo(threshold) > 0) {
|
|
|
+ judgment = "高于";
|
|
|
+ }
|
|
|
+ getMsgTemplate(_depletion, msgTemplate, judgment, "消耗区间", minshowValue.concat("-").concat(showValue));
|
|
|
+ } else if (metricValue.contains("_roi")) {
|
|
|
+ flag = (value.compareTo(threshold) > 0) || (value.compareTo(minThreshold) < 0);
|
|
|
+ if (value.compareTo(threshold) > 0) {
|
|
|
+ judgment = "高于";
|
|
|
+ }
|
|
|
+ getMsgTemplate(_roi, msgTemplate, judgment, "ROI区间", minshowValue.concat("-").concat(showValue));
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ flag = (value.compareTo(threshold) > 0) || (value.compareTo(minThreshold) < 0);
|
|
|
+ if (value.compareTo(threshold) > 0) {
|
|
|
+ judgment = "高于";
|
|
|
+ }
|
|
|
+ getMsgTemplate(_value, msgTemplate, judgment, "阈值区间", minshowValue.concat("-").concat(showValue));
|
|
|
}
|
|
|
- getMsgTemplate(_value, msgTemplate, judgment, "阈值区间", minshowValue.concat("-").concat(showValue));
|
|
|
+ //等于
|
|
|
+ } else if ("EQ".equals(judgeMethod)) {
|
|
|
+ judgment = "等于";
|
|
|
+ flag = value.compareTo(threshold) == 0;
|
|
|
+ String name = StatusEnum.getEnumByCode(threshold.toString()).getName();
|
|
|
+ getMsgTemplate(_value, msgTemplate, judgment, "阈值", name);
|
|
|
+ } else {
|
|
|
+ log.warn("缺失判断条件");
|
|
|
+ break;
|
|
|
}
|
|
|
- //等于
|
|
|
- } else if ("EQ".equals(judgeMethod)) {
|
|
|
- judgment = "等于";
|
|
|
- flag = value.compareTo(threshold) == 0;
|
|
|
- String name = StatusEnum.getEnumByCode(threshold.toString()).getName();
|
|
|
- getMsgTemplate(_value, msgTemplate, judgment, "阈值", name);
|
|
|
} else {
|
|
|
- log.warn("缺失判断条件");
|
|
|
break;
|
|
|
}
|
|
|
- } else {
|
|
|
- break;
|
|
|
}
|
|
|
- }
|
|
|
- //规则满足,产生预警
|
|
|
- if (flag) {
|
|
|
- String detail = getDetail(_depletion, _roi, _value);
|
|
|
- String msg = getMsg(level, detail, cost, rule);
|
|
|
- if (!Check.isNull(msg)) {
|
|
|
- if ("SEND".equals(rule.getProcessMethod())) {
|
|
|
- msg += "请您及时查看并调整!";
|
|
|
- } else {
|
|
|
- msg += "现已被关停,请您及时查看并调整!";
|
|
|
+ //规则满足,产生预警
|
|
|
+ if (flag) {
|
|
|
+ String detail = getDetail(_depletion, _roi, _value);
|
|
|
+ String msg = getMsg(level, detail, cost, rule);
|
|
|
+ if (!Check.isNull(msg)) {
|
|
|
+ sendMessageService.sendMessage(rule.getUserId(), msg);
|
|
|
+ AlarmEventSend send = new AlarmEventSend();
|
|
|
+ send.setAlarmDetail(msg);
|
|
|
+ send.setAlarmStatus(NoEn.NO1.valueStr());
|
|
|
+ send.setMetricValueCode(rule.getMetricValueCode());
|
|
|
+ send.setEventRuleId(rule.getEventRuleId());
|
|
|
+ send.setAlarmCondition(rule.getMetricValueName().concat("预警:").concat(detail.replace("<br/>", ";")));
|
|
|
+ send.setUserId(rule.getUserId());
|
|
|
+ alarmEventSendMapper.insert(send);
|
|
|
}
|
|
|
- sendMessageService.sendMessage(rule.getUserId(), msg);
|
|
|
- AlarmEventSend send = new AlarmEventSend();
|
|
|
- send.setAlarmDetail(msg);
|
|
|
- send.setAlarmStatus(NoEn.NO1.valueStr());
|
|
|
- send.setMetricValueCode(rule.getMetricValueCode());
|
|
|
- send.setEventRuleId(rule.getEventRuleId());
|
|
|
- send.setAlarmCondition(rule.getMetricValueName().concat("预警:").concat(detail.replace("<br/>", ";")));
|
|
|
- send.setUserId(rule.getUserId());
|
|
|
- alarmEventSendMapper.insert(send);
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("匹配规则异常", e);
|
|
|
}
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean shutDown(AlarmEventRule rule, String level) {
|
|
|
+ CtopOauthToken token = oauthTokenService.getTokenByAccountId(rule.getAccountId());
|
|
|
+ Map<String, Object> updateMap = new HashMap<>();
|
|
|
+ if (!Check.isNull(token)) {
|
|
|
+ if ("CAMPAIGN".equals(level)) {
|
|
|
+ updateMap = kuaiShouUpdateService.updateCampaignStatus(token.getAccessToken(), rule.getAccountId(), rule.getCampaignId(), NoEn.NO2.valueInt(), rule.getUserId());
|
|
|
+ log.info("---------规则关停快手计划:{}", rule.getCampaignId());
|
|
|
+ } else if ("UNIT".equals(level)) {
|
|
|
+ updateMap = kuaiShouUpdateService.updateUnitStatus(token.getAccessToken(), rule.getAccountId(), rule.getUnitId(), NoEn.NO2.valueInt(), rule.getUserId());
|
|
|
+ log.info("---------规则关停快手组:{}", rule.getUnitId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (boolean) updateMap.get("success");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -335,12 +365,22 @@ public class AlarmEventSendServiceImpl extends ServiceImpl<AlarmEventSendMapper,
|
|
|
if ("ACCOUNT".equals(level)) {
|
|
|
header.append(detail);
|
|
|
} else if ("CAMPAIGN".equals(level)) {
|
|
|
- header.append("计划ID:").append(rule.getCampaignId()).append("计划名称为:").append(rule.getCampaignName()).append("<br/>").append(detail);
|
|
|
+ header.append("计划ID:").append(rule.getCampaignId()).append(",计划名称为:").append(rule.getCampaignName()).append("<br/>").append(detail);
|
|
|
} else {
|
|
|
header.append("计划ID:").append(rule.getCampaignId()).append(",计划名称为:").append(rule.getCampaignName()).append("<br/>")
|
|
|
.append("组ID:").append(rule.getUnitId()).append(",组名称为:").append(rule.getUnitName()).append("<br/>").append(detail);
|
|
|
}
|
|
|
- return header.toString();
|
|
|
+ String msg = null;
|
|
|
+ if ("PAUSE".equals(rule.getProcessMethod())) {
|
|
|
+ header.append("现已被关停,请您及时查看并调整!");
|
|
|
+ if (shutDown(rule, level)) {
|
|
|
+ msg = header.toString();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ header.append("请您及时查看并调整!");
|
|
|
+ msg = header.toString();
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
}
|
|
|
|
|
|
|