Authored by qinchao

优化代码

... ... @@ -18,6 +18,10 @@
<artifactId>monitor-service-common</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-cmdb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
... ...
... ... @@ -6,6 +6,7 @@ import com.monitor.cloudtools.model.CommTestResponse;
import com.monitor.cloudtools.service.CloudToolService;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.cloudtools.util.HttpConnectUtil;
import com.monitor.cmdb.service.impl.TypeInfoServiceImpl;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.*;
import org.apache.commons.collections.CollectionUtils;
... ... @@ -51,6 +52,9 @@ public class CloudToolServiceImpl implements CloudToolService {
@Autowired
private InternalDomainHistoryMapper internalDomainHistoryMapper;
@Autowired
private TypeInfoServiceImpl typeInfoService;
/**
... ... @@ -345,7 +349,7 @@ public class CloudToolServiceImpl implements CloudToolService {
for(String typeName:typeNames.split("-")){
mobjectPaths_array[mobjectPaths_array.length-1]=typeName;
TypeInfo typeInfo = getTypeByMobjectPathArray(mobjectPaths_array);
TypeInfo typeInfo = typeInfoService.getTypeByMobjectPathArray(mobjectPaths_array);
if(typeInfo!=null){
List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(typeInfo.getTypeId());
List<String> existsHostIps=new ArrayList<String>();
... ... @@ -451,7 +455,7 @@ public class CloudToolServiceImpl implements CloudToolService {
String typeName=mobjectPaths_array[mobjectPaths_array.length-1];
//1:获取的type ,用来做type_id
TypeInfo typeInfo = getTypeByMobjectPathArray(mobjectPaths_array);
TypeInfo typeInfo = typeInfoService.getTypeByMobjectPathArray(mobjectPaths_array);
if(typeInfo==null){
res.setData("无法获取typeinfo信息:"+mobjectPaths);
return res;
... ... @@ -516,7 +520,7 @@ public class CloudToolServiceImpl implements CloudToolService {
String typeName=mobjectPaths_array[mobjectPaths_array.length-1];
//1:获取的type ,用来做type_id
TypeInfo typeInfo =getTypeByMobjectPathArray(mobjectPaths_array);
TypeInfo typeInfo =typeInfoService.getTypeByMobjectPathArray(mobjectPaths_array);
if(typeInfo==null){
res.setData("无法获取typeinfo信息:"+mobjectPaths);
return res;
... ... @@ -566,48 +570,6 @@ public class CloudToolServiceImpl implements CloudToolService {
return res;
}
private TypeInfo getTypeByMobjectPathArray(String[] mobjectPaths_array){
String typeName=mobjectPaths_array[mobjectPaths_array.length-1];
List<TypeInfo> typeInfoList=mTypeInfoMapper.selectAllTypeInfoByName(typeName);
if(typeInfoList==null||typeInfoList.size()<=0){
return null;
}
if(typeInfoList.size()==1){
return typeInfoList.get(0);
}else{
//// 名字多于一个的时候,就要根据路径查询
String topName=mobjectPaths_array[0];
List<TypeInfo> typeInfoList_top=mTypeInfoMapper.selectAllTypeInfoByName(topName);
if(typeInfoList_top==null||typeInfoList_top.size()!=1){
logger.error("typeinfo配置错误:存在零或者多条"+topName);
return null;
}
int parentTypeID=typeInfoList_top.get(0).getTypeId();
TypeInfo targetTypeInfo=null;
//循环从1开始
for(int i=1;i<mobjectPaths_array.length;i++){
List<TypeInfo> typeInfoList_tmp=mTypeInfoMapper.selectAllTypeInfoByName(mobjectPaths_array[i]);
if(typeInfoList_tmp==null||typeInfoList_tmp.size()<=0){
logger.error("typeinfo配置错误:没有配置"+typeName);
return null;
}
for(TypeInfo typeInfo:typeInfoList_tmp){
if(parentTypeID==typeInfo.getTypeParentId()){
parentTypeID=typeInfo.getTypeId();
if(i==mobjectPaths_array.length-1){
//最后一次循环
targetTypeInfo=typeInfo;
}
break;
}
}
}
return targetTypeInfo;
}
}
public BaseResponse updateNginxConfig(AutoScalingInfoReq infoReq){
BaseResponse response=new BaseResponse();
String commond = "ssh master@172.31.16.167 sh /home/master/nginx-sync/update-java-nginx.sh";
... ...
... ... @@ -321,7 +321,7 @@ public class TypeInfoCtrl {
BaseResponse response = new BaseResponse();
try {
allTypeInfo = typeInfoService.getChildById();
allTypeInfo = typeInfoService.getZkTree();
if (null != allTypeInfo) {
... ...
... ... @@ -24,6 +24,10 @@ public interface ITypeInfoService {
TypeInfo queryTypeInfoByName(String typeName);
List<TypeInfo> getChildById();
List<TypeInfo> getZkTree();
//根据路径数据查询配置
TypeInfo getTypeByMobjectPathArray(String[] mobjectPaths_array);
}
... ...
package com.monitor.cmdb.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.model.TypeInfo;
import com.monitor.cmdb.service.ITypeInfoService;
import com.monitor.mysql.mapper.MObjectInfoMapper;
import com.monitor.mysql.mapper.MTypeInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/6/15.
... ... @@ -17,6 +18,8 @@ import com.monitor.mysql.mapper.MTypeInfoMapper;
@Service
public class TypeInfoServiceImpl implements ITypeInfoService {
public static final Logger logger = LoggerFactory.getLogger(TypeInfoServiceImpl.class);
@Autowired
MTypeInfoMapper mTypeInfoMapper;
... ... @@ -88,9 +91,10 @@ public class TypeInfoServiceImpl implements ITypeInfoService {
}
@Override
public List<TypeInfo> getChildById() {
public List<TypeInfo> getZkTree() {
List<TypeInfo> list= new ArrayList<TypeInfo>();
TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("zookeeper");
//YOHO,middleware,zookeeper
TypeInfo typeInfo=this.getTypeByMobjectPathArray("YOHO,middleware,zookeeper".split(","));
typeInfo.setTypeParentId(0);
list.add(typeInfo);
List<TypeInfo> listInfo=mTypeInfoMapper.getChildTypesInfo(typeInfo.getTypeId());
... ... @@ -104,6 +108,49 @@ public class TypeInfoServiceImpl implements ITypeInfoService {
return list;
}
@Override
public TypeInfo getTypeByMobjectPathArray(String[] mobjectPaths_array){
String typeName=mobjectPaths_array[mobjectPaths_array.length-1];
List<TypeInfo> typeInfoList=mTypeInfoMapper.selectAllTypeInfoByName(typeName);
if(typeInfoList==null||typeInfoList.size()<=0){
return null;
}
if(typeInfoList.size()==1){
return typeInfoList.get(0);
}else{
//// 名字多于一个的时候,就要根据路径查询
String topName=mobjectPaths_array[0];
List<TypeInfo> typeInfoList_top=mTypeInfoMapper.selectAllTypeInfoByName(topName);
if(typeInfoList_top==null||typeInfoList_top.size()!=1){
logger.error("typeinfo配置错误:存在零或者多条"+topName);
return null;
}
int parentTypeID=typeInfoList_top.get(0).getTypeId();
TypeInfo targetTypeInfo=null;
//循环从1开始
for(int i=1;i<mobjectPaths_array.length;i++){
List<TypeInfo> typeInfoList_tmp=mTypeInfoMapper.selectAllTypeInfoByName(mobjectPaths_array[i]);
if(typeInfoList_tmp==null||typeInfoList_tmp.size()<=0){
logger.error("typeinfo配置错误:没有配置"+typeName);
return null;
}
for(TypeInfo typeInfo:typeInfoList_tmp){
if(parentTypeID==typeInfo.getTypeParentId()){
parentTypeID=typeInfo.getTypeId();
if(i==mobjectPaths_array.length-1){
//最后一次循环
targetTypeInfo=typeInfo;
}
break;
}
}
}
return targetTypeInfo;
}
}
}
... ...