This commit is contained in:
default
2018-11-01 16:18:11 +08:00
parent 553487baf1
commit 63974e7ad6
12 changed files with 1317 additions and 2 deletions

View File

@@ -0,0 +1,264 @@
package com.nms.interceptor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.nms.model.SyncDbInfo;
import com.nms.thread.SyncSlaveToMasterThread;
public class SyncMissionResultStatisticalInterceptor implements Interceptor{
private Logger logger =Logger.getLogger(this.getClass());
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Java Date 类型数据格式化格式
@Override
public void intercept(Invocation inv) {
try{
logger.info("---------SyncMissionResultStatisticalInterceptor拦截器拦截开始------------");
//创建一个任务id集合 存储任务结果改变的任务id 这些任务统一走新的统计方法 修改mission_state_table状态
//新增或者修改的结果可能是多条 但是任务id相同 用set进行去重
Set<Long> missionIds =new HashSet<Long>();
//同步前 查询出最后任务结果数据信息ID 用于查出新任务结果数据或者修改的数据信息
SyncSlaveToMasterThread target = inv.getTarget();
SyncDbInfo syncDbInfo = target.getSyncDbInfo();
String url = "jdbc:mysql://" + syncDbInfo.get("ip") + ":" + syncDbInfo.get("port") + "/"
+ syncDbInfo.get("database_name")+"?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&rewriteBatchedStatements=true";
logger.info("当前数据库连接为 "+url);
Record beforeInsertMissionResultTable1 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table1' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeInsertMissionResultTable1));
Record beforeInsertMissionResultTable4 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table4' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeInsertMissionResultTable4));
Record beforeInsertMissionResultTable6 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table6' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeInsertMissionResultTable6));
Record beforeUpdateMissionResultTable1 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table1' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeUpdateMissionResultTable1));
Record beforeUpdateMissionResultTable4 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table4' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeUpdateMissionResultTable4));
Record beforeUpdateMissionResultTable6 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table6' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(beforeUpdateMissionResultTable6));
inv.invoke();
Record afterInsertMissionResultTable1 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table1' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable1));
Record afterInsertMissionResultTable4 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table4' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable4));
Record afterInsertMissionResultTable6 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table6' and event=1 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable6));
if(null!=beforeInsertMissionResultTable1&&null!=afterInsertMissionResultTable1) {
List<Record> find = Db.use(url.toString()).find("select * from mission_result_table1 where id>= ? and id <= ? ",beforeInsertMissionResultTable1.get("last_id"),afterInsertMissionResultTable1.get("last_id"));
if(null!=find && find.size()>0) {
for (Record record : find) {
missionIds.add(record.getLong("mission_id"));
}
}
}
if(null!=beforeInsertMissionResultTable4&&null!=afterInsertMissionResultTable4) {
List<Record> find = Db.use(url.toString()).find("select * from mission_result_table4 where id>= ? and id <= ? ",beforeInsertMissionResultTable4.get("last_id"),afterInsertMissionResultTable4.get("last_id"));
if(null!=find && find.size()>0) {
for (Record record : find) {
missionIds.add(record.getLong("mission_id"));
}
}
}
if(null!=beforeInsertMissionResultTable6&&null!=afterInsertMissionResultTable6) {
List<Record> find = Db.use(url.toString()).find("select * from mission_result_table6 where id>= ? and id <= ? ",beforeInsertMissionResultTable6.get("last_id"),afterInsertMissionResultTable6.get("last_id"));
if(null!=find && find.size()>0) {
for (Record record : find) {
missionIds.add(record.getLong("mission_id"));
}
}
}
Record afterUpdateMissionResultTable1 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table1' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable1));
Record afterUpdateMissionResultTable4 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table4' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable4));
Record afterUpdateMissionResultTable6 = Db.use(url.toString()).findFirst("select * from table_sync_info where table_name='mission_result_table6' and event=2 and db_id=-1");
logger.info("获取mission_result_table1表中最后一次同步id的数据信息为 "+JSON.toJSONString(afterInsertMissionResultTable6));
if(null!=beforeUpdateMissionResultTable1&&null!=afterUpdateMissionResultTable1) {
List<Long> updateIds = new ArrayList<Long>();
List<Record> updateDatas = Db.use(url.toString()).find("select * from table_event_log where table_name='mission_result_table1' and id>= ? and id <= ?",beforeUpdateMissionResultTable1.get("last_id"),afterUpdateMissionResultTable1.get("last_id"));
StringBuilder handleStr=new StringBuilder();
if(null!=updateDatas && updateDatas.size()>0) {
for (int i=0;i<updateDatas.size();i++) {
updateIds.add(updateDatas.get(i).getLong("target_id"));
if(i==0) {
handleStr.append("?");
}else {
handleStr.append(",?");
}
}
List<Record> updateDataInfos = Db.use(url.toString())
.find(" select mission_id from mission_result_table1 where id in (" + handleStr + ") ",
updateIds.toArray());
if (updateDataInfos != null && updateDataInfos.size() > 0) {
for (Record record : updateDataInfos) {
missionIds.add(record.getLong("mission_id"));
}
}
}
}
if(null!=beforeUpdateMissionResultTable4&&null!=afterUpdateMissionResultTable4) {
List<Long> updateIds = new ArrayList<Long>();
List<Record> updateDatas = Db.use(url.toString()).find("select * from table_event_log where table_name='mission_result_table4' and id>= ? and id <= ?",beforeUpdateMissionResultTable4.get("last_id"),afterUpdateMissionResultTable4.get("last_id"));
StringBuilder handleStr=new StringBuilder();
if(null!=updateDatas && updateDatas.size()>0) {
for (int i=0;i<updateDatas.size();i++) {
updateIds.add(updateDatas.get(i).getLong("target_id"));
if(i==0) {
handleStr.append("?");
}else {
handleStr.append(",?");
}
}
List<Record> updateDataInfos = Db.use(url.toString())
.find(" select mission_id from mission_result_table4 where id in (" + handleStr + ") ",
updateIds.toArray());
if (updateDataInfos != null && updateDataInfos.size() > 0) {
for (Record record : updateDataInfos) {
missionIds.add(record.getLong("mission_id"));
}
}
}
}
if(null!=beforeUpdateMissionResultTable6&&null!=afterUpdateMissionResultTable6) {
List<Long> updateIds = new ArrayList<Long>();
List<Record> updateDatas = Db.use(url.toString()).find("select * from table_event_log where table_name='mission_result_table6' and id>= ? and id <= ?",beforeUpdateMissionResultTable1.get("last_id"),afterUpdateMissionResultTable1.get("last_id"));
StringBuilder handleStr=new StringBuilder();
if(null!=updateDatas && updateDatas.size()>0) {
for (int i=0;i<updateDatas.size();i++) {
updateIds.add(updateDatas.get(i).getLong("target_id"));
if(i==0) {
handleStr.append("?");
}else {
handleStr.append(",?");
}
}
List<Record> updateDataInfos = Db.use(url.toString())
.find(" select mission_id from mission_result_table6 where id in (" + handleStr + ") ",
updateIds.toArray());
if (updateDataInfos != null && updateDataInfos.size() > 0) {
for (Record record : updateDataInfos) {
missionIds.add(record.getLong("mission_id"));
}
}
}
}
logger.info("即将进行统计的所有任务的mission_id为:"+JSON.toJSONString(missionIds));
//根据统计结果更新mission_state_talbe表对应任务的状态
if(missionIds.size()>0) {
for (Long missionId : missionIds) {
StatisticalHandle(missionId);
}
}
logger.info("--------SyncMissionResultStatisticalInterceptor拦截器拦截结束------------");
}catch(Exception e){
e.printStackTrace();
logger.error("SyncMissionResultStatisticalInterceptor拦截器内部程序出现异常信息",e);
}
}
public void StatisticalHandle(Long missionId) {
logger.info("根据当前任务id为:"+missionId+"开始统计");
// mission_state_table 状态值
Integer status = null;
List<Record> results = Db.use().find("select result from (\r\n" +
"(select result,mission_id from mission_result_table1 mrt) union all \r\n" +
"(select result,mission_id from mission_result_table4 mrt) union all \r\n" +
"(select result,mission_id from mission_result_table6 mrt)\r\n" +
") t \r\n" +
"left join mission_state_table mst on mst.mission_id = t.mission_id \r\n" +
"where mst.is_loop = 0 and mst.mission_id=?",missionId);
boolean noThree=true;
if(null!=results&&results.size()>0) {
for (Record record : results) {
if(record.getInt("result")==3) {
noThree=false;
}
}
}
// 判断任务结果有没有状态值为3的 如果有 则任务状态为在下发
Record result = Db.use().findFirst("select t.mission_id,t.ok,t.fail,t.total from (\r\n" +
"(select mrt.mission_id,sum(CASE mrt.result when 0 THEN 1 ELSE 0 end) ok,sum(CASE mrt.result when 1 then 1 when -1 then 1 else 0 end) fail,count(mrt.seq_id) total from mission_result_table1 mrt group by mrt.mission_id) union all \r\n" +
"(select mrt.mission_id,sum(CASE mrt.result when 0 THEN 1 ELSE 0 end) ok,sum(CASE mrt.result when 1 then 1 when -1 then 1 else 0 end) fail,count(mrt.seq_id) total from mission_result_table4 mrt group by mrt.mission_id) union all \r\n" +
"(select mrt.mission_id,sum(CASE mrt.result when 0 THEN 1 ELSE 0 end) ok,sum(CASE mrt.result when 1 then 1 when -1 then 1 else 0 end) fail,count(mrt.seq_id) total from mission_result_table6 mrt group by mrt.mission_id)\r\n" +
") t \r\n" +
"left join mission_state_table mst on mst.mission_id = t.mission_id \r\n" +
"where mst.is_loop = 0 and t.mission_id = ?",missionId);
if(null!=result) {
Integer okCount = result.getInt("ok");
Integer failCount =result.getInt("fail");
Integer total =result.getInt("total");
if(okCount+failCount==total) {
if(failCount==0) {
status=30;
}else if(okCount==0) {
status=31;
}else {
status=32;
}
}
if(!noThree) {
status=2;
}
logger.info("统计完成 修改mission_state_table状态为:? 总个数:?执行数:? 成功:? 失败:?");
String missionStateAutoDesc=null;
if(okCount+failCount==total) {
missionStateAutoDesc=format.format(System.currentTimeMillis())+" i18n_sserver.UpgradeService.sql.complate_n81i "+total+" i18n_sserver.UpgradeService.sql.executeNode_n81i "+okCount+" i18n_sserver.UpgradeService.sql.failed_n81i "+failCount;
}
String missionStateDesc="\r\n" +
"i18n_server.UpgradeService.sql.total_n81i "+total+" i18n_server.UpgradeService.sql.executeNode2_n81i</br> "+(total-okCount-failCount)+" i18n_server.UpgradeService.sql.unexecute_n81i</br>"+(okCount+failCount)+" i18n_server.UpgradeService.sql.execute_n81i【i18n_server.UpgradeService.sql.success_n81i "+okCount+" i18n_sserver.UpgradeService.sql.failed_n81i "+failCount+"";
Record missionStateTableResult =new Record();
missionStateTableResult.set("mission_id", missionId);
missionStateTableResult.set("mission_state", status);
missionStateTableResult.set("mission_state_desc", missionStateDesc);
if(missionStateAutoDesc!=null) {
missionStateTableResult.set("auto_desc", missionStateAutoDesc);
}
Db.use("masterDataSource").update("mission_state_table","mission_id",missionStateTableResult);
}
logger.info("修改mission_state_table信息完成");
}
}

View File

@@ -0,0 +1,187 @@
package com.nms.interceptor;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.nms.model.SetInfo;
import com.nms.model.SyncDbInfo;
import com.nms.socket.SocketClientServeice;
import com.nms.thread.SyncThread;
/**
* 数据同步完成后针对新监测数据 进行dc socket通讯
* @author default
*
*/
public class SyncSocketInterceptor implements Interceptor{
private Logger logger =Logger.getLogger(this.getClass());
/**
* 监控设置信息变更请求命令
*/
private String WEB_NOTICE_SET_INFO_ALERT = "char:setInfoAlert";
@Override
public void intercept(Invocation inv) {
try{
logger.info("--------数据同步前 SyncSocketInterceptor拦截器拦截------------");
//同步前 查询出最后监测数据信息ID 用于查出新监测数据或者修改的数据信息
SyncThread target = inv.getTarget();
SyncDbInfo syncDbInfo = target.getSyncDbInfo();
String url = "jdbc:mysql://" + syncDbInfo.get("ip") + ":" + syncDbInfo.get("port") + "/"
+ syncDbInfo.get("database_name")+"?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&rewriteBatchedStatements=true";
logger.info("当前数据库连接为 "+url);
Record detectionSetInfoTableInfo = Db.use("masterDataSource").findFirst("select * from table_sync_info where table_name='detection_set_info' and event=1 and db_id=?",syncDbInfo.getId());
logger.info("获取detectionSetInfo表中最后一次同步id的数据信息为 "+JSON.toJSONString(detectionSetInfoTableInfo));
Record detectionSetInfoTableUpdateInfo = Db.use("masterDataSource").findFirst("select * from table_sync_info where table_name='detection_set_info' and event=2 and db_id=?",syncDbInfo.getId());
logger.info("获取detectionSetInfo表中最后一次同步修改id的数据信息为 "+JSON.toJSONString(detectionSetInfoTableUpdateInfo));
//查询所有修改数据信息
List<Record> datas = Db.use("masterDataSource")
.find("select * from table_event_log where table_name = '" + detectionSetInfoTableUpdateInfo.getStr("table_name")
+ "' and id > " + detectionSetInfoTableUpdateInfo.getLong("last_id") + " and event = "
+ detectionSetInfoTableUpdateInfo.getInt("event") + " order by id asc ");
List<Long> updateIds = new ArrayList<Long>();
StringBuilder handleStr=new StringBuilder();
if (datas != null && datas.size() > 0) {
for (int i = 0; i < datas.size(); i++) {
updateIds.add(datas.get(i).getLong("target_id"));
if(i==0) {
handleStr.append("?");
}else {
handleStr.append(",?");
}
}
}
List<Record> detectionSetUpdateInfos=null;
Map<Long,Record> map=new HashMap<Long,Record>();
//查询修改前数据的信息
if(updateIds.size()>0) {
detectionSetUpdateInfos= Db.use(url).find("select m.*,cti.id CHECK_TYPE_INFO_ID,cti.check_type_name CHECK_TYPE_INFO_NAME from detection_set_info m left join check_type_info cti on m.check_type_id=cti.id where m.id in ("+handleStr.toString()+")",updateIds.toArray());
if(null!=detectionSetUpdateInfos&&detectionSetUpdateInfos.size()>0) {
for(Record detectionSetInfo:detectionSetUpdateInfos) {
map.put(detectionSetInfo.getLong("ID"),detectionSetInfo);
}
}
}
inv.invoke();
//获取当前分库连接的dc的IP信息
final String serverIp = PropKit.use("socket.properties").get("db"+syncDbInfo.get("ip"));
logger.info("获取socket连接ip信息为"+serverIp);
//根据数据库分库dc ip信息获取数据库相关实体信息
final Record serverTableInfo = Db.findFirst("select * from server_table where server_ip = ? and server_state=0 ",serverIp);
// 查询出 修改或者更新的监测配置信息 如果没有的话不进行任何操作
if(detectionSetInfoTableInfo!=null) {
List<Record> detectionSetInfos = Db.use(url).find("select m.*,cti.id CHECK_TYPE_INFO_ID,cti.check_type_name CHECK_TYPE_INFO_NAME from detection_set_info m left join check_type_info cti on m.check_type_id=cti.id where m.id > ? ",detectionSetInfoTableInfo.getLong("last_id"));
if(null!=detectionSetInfos&&detectionSetInfos.size()>0) {
for(Record detectionSetInfo:detectionSetInfos) {
SetInfo o=null;
SetInfo n = new SetInfo();
n.setId(detectionSetInfo.getLong("ID"));
n.setCheckTypeId(detectionSetInfo.getLong("CHECK_TYPE_INFO_ID"));
n.setCheckTypeName(detectionSetInfo.getStr("CHECK_TYPE_INFO_Name"));
n.setProcessIden(detectionSetInfo.getStr("PROCESS_IDEN"));
n.setNodeGroupsId(detectionSetInfo.getStr("NODE_GROUPS_ID"));
n.setNodeIpsId(detectionSetInfo.getStr("NODE_IPS_ID"));
n.setCheckWay(detectionSetInfo.getStr("CHECK_WAY"));
final JSONObject jObject = new JSONObject();
jObject.put("old", o);
jObject.put("new", n);
new Thread(new Runnable(){
@Override
public void run() {
try {
new SocketClientServeice(serverIp).sendInfoToServer(WEB_NOTICE_SET_INFO_ALERT,jObject.toString());
logger.info("…………………………………………socket通信发送修改监测数据完毕………………………………………………");
} catch (Exception e) {
Record eventRecordLibrary=new Record();
eventRecordLibrary.set("record_command", WEB_NOTICE_SET_INFO_ALERT);
eventRecordLibrary.set("record_content", jObject.toString());
eventRecordLibrary.set("record_type", "W2S");
eventRecordLibrary.set("state", 1l);
eventRecordLibrary.set("nmsserver_id", serverTableInfo.get("id"));
eventRecordLibrary.set("create_time", new Date());
Db.save("event_record_library", eventRecordLibrary);
logger.error("Monitoring setting to change communication anomalies: " + e.getMessage());
}
}
}).start();
}
}
}
if(null!=detectionSetUpdateInfos&&detectionSetUpdateInfos.size()>0) {
//查询出已经修改的监测配置数据
List<Record> detectionSetInfos = Db.use(url).find("select m.*,cti.id CHECK_TYPE_INFO_ID,cti.check_type_name CHECK_TYPE_INFO_NAME from detection_set_info m left join check_type_info cti on m.check_type_id=cti.id where m.id in ("+handleStr.toString()+")",updateIds.toArray());
if(null!=detectionSetInfos&&detectionSetInfos.size()>0) {
for (Record detectionSetInfo : detectionSetInfos) {
SetInfo o=null;
Record old = map.get(detectionSetInfo.get("ID"));
if(old!=null) {
o = new SetInfo();
o.setId(detectionSetInfo.getLong("ID"));
o.setCheckTypeId(detectionSetInfo.getLong("CHECK_TYPE_INFO_ID"));
o.setCheckTypeName(detectionSetInfo.getStr("CHECK_TYPE_INFO_Name"));
o.setProcessIden(detectionSetInfo.getStr("PROCESS_IDEN"));
o.setNodeGroupsId(detectionSetInfo.getStr("NODE_GROUPS_ID"));
o.setNodeIpsId(detectionSetInfo.getStr("NODE_IPS_ID"));
o.setCheckWay(detectionSetInfo.getStr("CHECK_WAY"));
}
SetInfo n = new SetInfo();
n.setId(detectionSetInfo.getLong("ID"));
n.setCheckTypeId(detectionSetInfo.getLong("CHECK_TYPE_INFO_ID"));
n.setCheckTypeName(detectionSetInfo.getStr("CHECK_TYPE_INFO_Name"));
n.setProcessIden(detectionSetInfo.getStr("PROCESS_IDEN"));
n.setNodeGroupsId(detectionSetInfo.getStr("NODE_GROUPS_ID"));
n.setNodeIpsId(detectionSetInfo.getStr("NODE_IPS_ID"));
n.setCheckWay(detectionSetInfo.getStr("CHECK_WAY"));
final JSONObject jObject = new JSONObject();
jObject.put("old", o);
jObject.put("new", n);
new Thread(new Runnable(){
@Override
public void run() {
try {
new SocketClientServeice(serverIp).sendInfoToServer(WEB_NOTICE_SET_INFO_ALERT,jObject.toString());
logger.info("…………………………………………socket通信发送修改监测数据完毕………………………………………………");
} catch (Exception e) {
Record eventRecordLibrary=new Record();
eventRecordLibrary.set("record_command", WEB_NOTICE_SET_INFO_ALERT);
eventRecordLibrary.set("record_content", jObject.toString());
eventRecordLibrary.set("record_type", "W2S");
eventRecordLibrary.set("state", 1l);
eventRecordLibrary.set("nmsserver_id", serverTableInfo.get("id"));
eventRecordLibrary.set("create_time", new Date());
Db.save("event_record_library", eventRecordLibrary);
logger.error("Monitoring setting to change communication anomalies: " + e.getMessage());
}
}
}).start();
}
}
}
logger.info("--------数据同步前 SyncSocketInterceptor拦截器拦截完毕------------");
}catch(Exception e){
e.printStackTrace();
logger.error("syncDataInterceptor socket通信拦截器内部程序出现异常信息"+e.getMessage());
logger.error("syncDataInterceptor socket通信拦截器内部程序出现异常信息",e);
}
}
}