Authored by BuddyJack

Fix

... ... @@ -13,6 +13,7 @@ import com.monitor.cmdb.service.ITypeInfoService;
import com.monitor.model.domain.MObjectHostInfoModel;
import com.monitor.model.domain.MObjectModel;
import com.monitor.model.page.PageResponse;
import com.monitor.model.request.DependencyRequest;
import com.monitor.model.request.HostInfoReq;
import com.monitor.model.request.MObjectHostInfoReq;
import com.monitor.model.request.MObjectInfoReq;
... ... @@ -88,19 +89,19 @@ public class MObjectInfoCtrl {
selectTag = request.getSelectGroup();
List<Map<String,String>> tags = new ArrayList<Map<String,String>>();
List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
for (HostGroup group : sourceGroupList) {
HashMap map = new HashMap();
map.put("text",group.getGroupName());
map.put("id",group.getId());
map.put("text", group.getGroupName());
map.put("id", group.getId());
tags.add(map);
groupList.add(group.getGroupName());
}
List<String> tagList=new ArrayList<>();
if(StringUtils.isNotBlank(selectTag)){
for(String str:selectTag.split(",")){
if(StringUtils.isNotBlank(str)){
List<String> tagList = new ArrayList<>();
if (StringUtils.isNotBlank(selectTag)) {
for (String str : selectTag.split(",")) {
if (StringUtils.isNotBlank(str)) {
tagList.add(str);
}
}
... ... @@ -108,11 +109,11 @@ public class MObjectInfoCtrl {
List<HostInfo> hostInfos = hostInfoService.getHostInfosByTagList(tagList);
List<Map<String,String>> ips = new ArrayList<Map<String,String>>();
List<Map<String, String>> ips = new ArrayList<Map<String, String>>();
for (HostInfo info : hostInfos) {
HashMap map = new HashMap();
map.put("text",info.getHostIp());
map.put("id",info.getHostIp());
map.put("text", info.getHostIp());
map.put("id", info.getHostIp());
ips.add(map);
ipList.add(info.getHostIp());
}
... ... @@ -286,7 +287,7 @@ public class MObjectInfoCtrl {
BaseResponse response = new BaseResponse();
try {
if (null != request&&StringUtils.isNotBlank(request.getMoHostIp())) {
if (null != request && StringUtils.isNotBlank(request.getMoHostIp())) {
if (!checkHost(request.getMoHostIp())) {
... ... @@ -296,8 +297,8 @@ public class MObjectInfoCtrl {
return response;
}
String allIps=request.getMoHostIp();
for(String ip:allIps.split(",")){
String allIps = request.getMoHostIp();
for (String ip : allIps.split(",")) {
MObjectInfo info = new MObjectInfo();
BeanUtils.copyProperties(request, info);
... ... @@ -408,8 +409,8 @@ public class MObjectInfoCtrl {
}
private boolean checkHost(String hostIps) {
if(StringUtils.isNotBlank(hostIps)){
for(String ip: hostIps.split(",")){
if (StringUtils.isNotBlank(hostIps)) {
for (String ip : hostIps.split(",")) {
if (null == hostInfoService.getHostInfoByHostIp(ip)) {
return false;
}
... ... @@ -476,4 +477,38 @@ public class MObjectInfoCtrl {
return response;
}
/**
* 查询依赖列表
* 1. 依赖服务组列表
* 2. 依赖服务列表
*/
@RequestMapping(value = "/queryDependency")
public BaseResponse queryDependency(@RequestBody DependencyRequest request) {
BaseResponse response = new BaseResponse();
response.setData(mobjectService.queryDependency(request.getSelectGroup()));
return response;
}
//查询已绑定的服务
@RequestMapping(value = "/queryBindDependency")
public BaseResponse queryBindDependency(@RequestBody DependencyRequest request) {
BaseResponse response = new BaseResponse();
response.setData(mobjectService.queryBindDependency(request.getTypeId()));
return response;
}
//更新绑定服务
@RequestMapping(value = "/updateBindDependency")
public BaseResponse updateBindDependency(@RequestBody DependencyRequest request) {
mobjectService.updateBindDependency(request.getTypeId(), request.getDependencies());
return new BaseResponse();
}
}
... ...
package com.monitor.cmdb.service;
import com.model.MObjectInfo;
import com.monitor.model.response.DependencyRep;
import java.util.List;
... ... @@ -27,4 +28,10 @@ public interface IMObjectInfoService {
List<MObjectInfo> queryMObjectsInfoByTypes(List<Integer> typeIds);
DependencyRep queryDependency(String group);
DependencyRep queryBindDependency(int typeId);
void updateBindDependency(int typeId,List<String> dependencyList);
}
... ...
package com.monitor.cmdb.service.impl;
import com.model.DependencyInfo;
import com.model.MObjectInfo;
import com.model.TypeInfo;
import com.monitor.cmdb.service.IMObjectInfoService;
import com.monitor.model.response.DependencyRep;
import com.monitor.mysql.mapper.MObjectInfoMapper;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by yoho on 2016/6/15.
*/
@Service
@EnableScheduling
public class MObjectInfoServiceImpl implements IMObjectInfoService {
@Autowired
... ... @@ -119,4 +127,103 @@ public class MObjectInfoServiceImpl implements IMObjectInfoService {
public List<MObjectInfo> queryMObjectsInfoByTypes(List<Integer> typeIds) {
return mObjectInfoMapper.selectMObjectsInfoByTypes(typeIds);
}
/**
* 根据 group 查询
*
* @param group
*/
@Override
public DependencyRep queryDependency(String group) {
DependencyRep resp = new DependencyRep();
Map<String, TypeInfo> allDependencies = queryAllDependencies();
//set groups , dependencies
HashSet<String> selectGroupSet = new HashSet<>();
List<String> selectDependencies = new ArrayList<>();
for (Map.Entry<String, TypeInfo> entry : allDependencies.entrySet()) {
String typePath = entry.getKey();
String typeGroupName = StringUtils.substring(typePath, 0, StringUtils.lastIndexOf(typePath, "/"));
selectGroupSet.add(typeGroupName);
if (StringUtils.isBlank(group)) {
selectDependencies.add(typePath);
} else if (StringUtils.startsWith(typePath, group + "/")) {
selectDependencies.add(typePath);
}
}
List<String> selectGroupList = new ArrayList<>();
selectGroupList.addAll(selectGroupSet);
Collections.sort(selectGroupList);
resp.setSelectGroups(selectGroupList);
Collections.sort(selectDependencies);
resp.setSelectDependencies(selectDependencies);
return resp;
}
/**
* 查询 绑定依赖服务
*
* @param typeId
*/
@Override
public DependencyRep queryBindDependency(int typeId) {
DependencyRep resp = new DependencyRep();
List<String> selectDependencyList = new ArrayList<>();
Map<String, TypeInfo> allDependencies = queryAllDependencies();
Map<Integer, String> dependencyId2Map = new HashMap<>();
for (Map.Entry<String, TypeInfo> entry : allDependencies.entrySet()) {
dependencyId2Map.put(entry.getValue().getTypeId(), entry.getKey());
}
List<DependencyInfo> dependencyInfos = mObjectInfoMapper.queryBindDependencyList(typeId);
for (DependencyInfo dependencyInfo : dependencyInfos) {
selectDependencyList.add(dependencyId2Map.get(dependencyInfo.getDependencyTypeId()));
}
Collections.sort(selectDependencyList);
resp.setSelectDependencies(selectDependencyList);
return resp;
}
/**
* 更新 绑定依赖服务
*
* @param typeId
* @param dependencyList
*/
@Override
public void updateBindDependency(int typeId, List<String> dependencyList) {
Map<String, TypeInfo> allDependencies = queryAllDependencies();
List<DependencyInfo> dependencyInfoList = new ArrayList<>();
for (String dependency : dependencyList) {
dependencyInfoList.add(new DependencyInfo(typeId, allDependencies.get(dependency).getTypeId()));
}
mObjectInfoMapper.deleteBindDependencyList(typeId);
mObjectInfoMapper.addBindDependencyList(dependencyInfoList);
}
public Map<String, TypeInfo> queryAllDependencies() {
//重头遍历
TypeInfo typeInfo = typeInfoService.queryTypeInfoByName("YOHO");
Map<String, TypeInfo> typesMap = queryChildTypesMap("/YOHO", typeInfo.getTypeId());
return typesMap;
}
private Map<String, TypeInfo> queryChildTypesMap(String rootPath, Integer rootId) {
Map<String, TypeInfo> childTypesMap = new HashMap<>();
List<TypeInfo> childTypes = typeInfoService.queryChildTypesInfo(rootId);
for (TypeInfo typeInfo : childTypes) {
String currentPath = rootPath + "/" + typeInfo.getTypeName();
//若为叶子节点
if (1 == typeInfo.getTypeIsLeaf()) {
childTypesMap.put(currentPath, typeInfo);
} else {
childTypesMap.putAll(queryChildTypesMap(currentPath, typeInfo.getTypeId()));
}
}
return childTypesMap;
}
}
... ...
package com.monitor.model.request;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jack on 2017/12/7.
*/
@Data
public class DependencyRequest {
//query dependency in group
String selectGroup = "";
//query dependecy binded typeId
int typeId;
//update dependency for typeId
List<String> dependencies = new ArrayList<>();
}
... ...
package com.monitor.model.response;
import lombok.Data;
import java.util.List;
/**
* Created by jack on 2017/12/7.
*/
@Data
public class DependencyRep {
List<String> selectGroups;
List<String> selectDependencies;
}
... ...
package com.model;
import lombok.Data;
import java.io.Serializable;
/**
* Created by jack on 2017/12/7.
*/
@Data
public class DependencyInfo implements Serializable {
Integer typeId;
Integer dependencyTypeId;
public DependencyInfo() {
}
public DependencyInfo(Integer typeId, Integer dependencyTypeId) {
this.typeId = typeId;
this.dependencyTypeId = dependencyTypeId;
}
}
... ...
package com.monitor.mysql.mapper;
import com.model.DependencyInfo;
import com.model.MObjectInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
... ... @@ -10,7 +12,7 @@ import java.util.List;
public interface MObjectInfoMapper {
void insertMoInfo(MObjectInfo moInfo);
void deleteMoInfo(int moId);
void deleteMoInfo(int moId);
void updateMoInfo(MObjectInfo moInfo);
... ... @@ -33,10 +35,17 @@ public interface MObjectInfoMapper {
/**
* like 模糊查询alias
* limit 1
*
* @param name alias
* @return
*/
MObjectInfo selectByFuzzyName(String name);
/* List<MObjectInfo> getHostMosInfo(int hostId);*/
List<DependencyInfo> queryBindDependencyList(int typeId);
void deleteBindDependencyList(int typeId);
void addBindDependencyList(List<DependencyInfo> dependencyInfoList);
}
... ...
... ... @@ -13,6 +13,11 @@
<id property="moUrl" column="url"></id>
</resultMap>
<resultMap id="dependencyInfoMapper" type="com.model.DependencyInfo">
<id property="typeId" column="typeId"></id>
<id property="dependencyTypeId" column="dTypeId"></id>
</resultMap>
<select id="getAllMosInfo" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT * FROM mobject_info ORDER BY id asc
</select>
... ... @@ -21,9 +26,9 @@
SELECT * FROM mobject_info WHERE type_id = #{moTypeId} order by host_ip DESC
</select>
<!-- <select id="getHostMosInfo" parameterType="int" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT * FROM mobject_info WHERE mo_hostId = #{moHostId}
</select>-->
<!-- <select id="getHostMosInfo" parameterType="int" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT * FROM mobject_info WHERE mo_hostId = #{moHostId}
</select>-->
<insert id="insertMoInfo" useGeneratedKeys="true" keyProperty="id" parameterType="com.model.MObjectInfo">
INSERT INTO mobject_info(alias,host_ip,type_id,tags,url) VALUES (#{moName},#{moHostIp},#{moTypeId},#{moTags},#{moUrl})
... ... @@ -41,21 +46,21 @@
<select id="selectMObjectsInfoByTypes" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT * FROM mobject_info
where type_id in
where type_id in
<foreach collection="list" open="(" close=")" separator="," item="item">
#{item}
</foreach>
ORDER BY id asc
ORDER BY id asc
</select>
<select id="getMoInfosByAlias" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper" parameterType="java.lang.String">
<select id="getMoInfosByAlias" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper"
parameterType="java.lang.String">
SELECT * FROM mobject_info where instr(alias, LOWER(#{alias})) &gt; 0 ORDER BY id asc
</select>
<select id="selectDistinctHostIPByTypes" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
SELECT distinct host_ip FROM mobject_info
... ... @@ -84,4 +89,21 @@
SELECT * FROM mobject_info where alias like CONCAT(#{name,jdbcType=VARCHAR},'%' ) limit 1
</select>
<select id="queryBindDependencyList" parameterType="java.lang.Integer" resultType="com.model.DependencyInfo" resultMap="dependencyInfoMapper">
select * from type_dependency_info WHERE typeId = #{typeId}
</select>
<delete id="deleteBindDependencyList" parameterType="java.lang.Integer">
DELETE from type_dependency_info WHERE typeId = #{typeId}
</delete>
<insert id="addBindDependencyList" parameterType="java.util.List">
INSERT INTO type_dependency_info (typeId, dTypeId) VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.typeId},#{item.dependencyTypeId})
</foreach>
</insert>
</mapper>
\ No newline at end of file
... ...