查询实例总数

develop
guyuliang 2024-08-24 19:29:02 +08:00
parent 1f83038120
commit a816dffe8c
1 changed files with 33 additions and 22 deletions

View File

@ -70,6 +70,7 @@ import com.megatron.framework.lock.AutoCloseLock;
import com.megatron.framework.lock.LockFactory; import com.megatron.framework.lock.LockFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;
/** /**
* @author * @author
@ -437,58 +438,68 @@ public class ModuleServiceImpl implements ModuleService {
//用countDownLatch是为了加快速度 //用countDownLatch是为了加快速度
CountDownLatch countDownLatch = new CountDownLatch(rootGroups.size()); CountDownLatch countDownLatch = new CountDownLatch(rootGroups.size());
Map<String,Object> resultMap = new HashMap<>(); Map<String, Object> resultMap = new HashMap<>();
for (ModuleGroup rootGroup : rootGroups) { for (ModuleGroup rootGroup : rootGroups) {
//获取每个模型的所有实例总数 //获取每个模型的所有实例总数
getInstaceCount(rootGroup,resultMap,countDownLatch); getInstaceCount(rootGroup, resultMap, countDownLatch, context.getTarget());
} }
countDownLatch.await(); countDownLatch.await();
//查询云主机总数 //查询云主机总数
Pager pager = new Pager(); Pager pager = new Pager();
pager.setPage(1); pager.setPage(1);
pager.setRows(Integer.MAX_VALUE); pager.setRows(Integer.MAX_VALUE);
List<Param> paramList = new ArrayList<>(); List<Param> paramList = new ArrayList<>();
Param param = new Param(); Param param = new Param();
param.setParam(MapTools.simpleMap("isTemplate",false)); param.setParam(MapTools.simpleMap("isTemplate", false));
param.setSign(Sign.EQ); param.setSign(Sign.EQ);
paramList.add(param); paramList.add(param);
pager.setParams(paramList); pager.setParams(paramList);
GeneralResult<JSONObject> result = cmpInternalService.list(pager.toMap()); GeneralResult<JSONObject> result = cmpInternalService.list(pager.toMap());
if(result.isFailed()){ if (result.isFailed()) {
log.info("list:{}", JSON.toJSONString(result)); log.info("list:{}", JSON.toJSONString(result));
return new GeneralResult(false,"查询失败"); return new GeneralResult(false, "查询失败");
} }
resultMap.put("vm",result.getData().getInteger("total")); resultMap.put("vm", result.getData().getInteger("total"));
return new GeneralResult<>(true, resultMap,"查询成功"); return new GeneralResult<>(true, resultMap, "查询成功");
} catch (Exception e) { } catch (Exception e) {
log.info("查询失败", e); log.info("查询失败", e);
return new GeneralResult<>(false, "查询失败"); return new GeneralResult<>(false, "查询失败");
} }
} }
private void getInstaceCount(ModuleGroup rootGroup, Map<String, Object> resultMap, CountDownLatch countDownLatch) { private void getInstaceCount(ModuleGroup rootGroup, Map<String, Object> resultMap, CountDownLatch countDownLatch, Long userId) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
//获取当前ModuleGroup下所有的modle try {
Pager pager = new Pager(); //获取当前ModuleGroup下所有的modle
GeneralResult<GridBean<CiModule>> modelList = getModelList(pager); Pager pager = new Pager();
if (modelList.isFailed()) { GeneralResult<GridBean<CiModule>> modelList = getModelList(pager);
log.info("modelList:{}", JSON.toJSONString(modelList)); if (modelList.isFailed()) {
countDownLatch.countDown(); log.info("modelList:{}", JSON.toJSONString(modelList));
return; countDownLatch.countDown();
return;
}
Integer total = 0;
for (CiModule model : modelList.getData().getRows()) {
//获取每个模型下所有的实例总数
String table = CmdbUtils.CMDB_INSTANCE + model.getCode().toLowerCase();
String valueTable = CmdbUtils.CMDB_INSTANCE_VALUE + model.getCode().toLowerCase();
List<Param> paramList = new ArrayList<>();
Param param = new Param();
param.setParam(MapTools.simpleMap("moduleId", model.getId()));
boolean adminRole = instanceService.checkAdminRole(userId);
int count = instanceRepository.count((List<Param>) param, table, valueTable, userId, adminRole);
total+=count;
}
resultMap.put(rootGroup.getCode(), total);
} catch (Exception e) {
log.info("查询失败", e);
} }
for (CiModule model : modelList.getData().getRows()) {
//获取每个模型下所有的实例总数
String table = CmdbUtils.CMDB_INSTANCE + model.getCode().toLowerCase();
String valueTable = CmdbUtils.CMDB_INSTANCE_VALUE + model.getCode().toLowerCase();
}
resultMap.put("","");
countDownLatch.countDown(); countDownLatch.countDown();
} }
}).start(); }).start();