diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/HostStatusNumStatisticsDTO.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/HostStatusNumStatisticsDTO.java new file mode 100644 index 0000000000..9ed8de69a0 --- /dev/null +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/model/dto/HostStatusNumStatisticsDTO.java @@ -0,0 +1,29 @@ +package com.tencent.bk.job.common.model.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * host状态数量统计 + */ +@Data +@EqualsAndHashCode +@NoArgsConstructor +@AllArgsConstructor +public class HostStatusNumStatisticsDTO { + public static final String KEY_AGENT_ALIVE = "gseAgentAlive"; + public static final String KEY_HOST_NUM = "hostNum"; + + /** + * 主机Agent状态 + */ + private int gseAgentAlive; + + /** + * 主机数量 + */ + private int hostNum; + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java index 9fe2e804f3..50b262a289 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/ApplicationHostDAO.java @@ -29,6 +29,7 @@ import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.model.dto.HostSimpleDTO; +import com.tencent.bk.job.common.model.dto.HostStatusNumStatisticsDTO; import java.util.Collection; import java.util.List; @@ -232,4 +233,10 @@ Long countHostInfoByMultiKeys(Collection bizIds, * @return 删除的主机数量 */ int deleteBizHostInfoByBizId(long bizId); + /** + * 根据业务id统计主机状态数量 + * @param bizIds 业务id + * @return 状态数量 + */ + List countHostStatusNumByBizIds(List bizIds); } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java index e7f061e0e5..4e0979653b 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/ApplicationHostDAOImpl.java @@ -34,6 +34,7 @@ import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; import com.tencent.bk.job.common.model.dto.HostSimpleDTO; +import com.tencent.bk.job.common.model.dto.HostStatusNumStatisticsDTO; import com.tencent.bk.job.common.model.dto.ResourceScope; import com.tencent.bk.job.common.util.StringUtil; import com.tencent.bk.job.common.util.TagUtils; @@ -1252,6 +1253,32 @@ public List listHostsByCloudIps(Collection cloudIps) return queryHostsByCondition(conditions); } + @Override + public List countHostStatusNumByBizIds(List bizIds) { + List conditions = new ArrayList<>(); + if (bizIds != null) { + conditions.add(HostTopo.HOST_TOPO.APP_ID.in(bizIds)); + } + var query = context.select( + TABLE.IS_AGENT_ALIVE.as(HostStatusNumStatisticsDTO.KEY_AGENT_ALIVE), + DSL.countDistinct(TABLE.HOST_ID).as(HostStatusNumStatisticsDTO.KEY_HOST_NUM) + ).from(TABLE) + .leftJoin(HostTopo.HOST_TOPO).on(TABLE.HOST_ID.eq(HostTopo.HOST_TOPO.HOST_ID)) + .where(conditions) + .groupBy(TABLE.IS_AGENT_ALIVE); + val records = query.fetch(); + List countList = new ArrayList<>(); + if (records.size() > 0) { + records.forEach(record -> { + HostStatusNumStatisticsDTO statisticsDTO = new HostStatusNumStatisticsDTO(); + statisticsDTO.setHostNum(Integer.valueOf(record.get(HostStatusNumStatisticsDTO.KEY_HOST_NUM).toString())); + statisticsDTO.setGseAgentAlive(Integer.valueOf(record.get(HostStatusNumStatisticsDTO.KEY_AGENT_ALIVE).toString())); + countList.add(statisticsDTO); + }); + } + return countList; + } + private List queryHostsByCondition(List conditions) { Result result = context.select(ALL_FIELDS) diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/IndexServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/IndexServiceImpl.java index 9a31a47e73..3401a8cb5e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/IndexServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/IndexServiceImpl.java @@ -33,6 +33,7 @@ import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.common.model.dto.ApplicationDTO; import com.tencent.bk.job.common.model.dto.ApplicationHostDTO; +import com.tencent.bk.job.common.model.dto.HostStatusNumStatisticsDTO; import com.tencent.bk.job.common.model.vo.HostInfoVO; import com.tencent.bk.job.common.util.JobContextUtil; import com.tencent.bk.job.common.util.PageUtil; @@ -124,40 +125,31 @@ public List listGreeting(String username) { public AgentStatistics getAgentStatistics(String username, AppResourceScope appResourceScope) { // 查出业务 ApplicationDTO appInfo = applicationDAO.getAppById(appResourceScope.getAppId()); - Long normalNum; - Long abnormalNum; - List hostIds; + Long normalNum = 0L; + Long abnormalNum = 0L; + List bizIds; if (appInfo.isBiz()) { // 普通业务 - hostIds = hostTopoDAO.listHostIdByBizIds( - Collections.singletonList(Long.valueOf(appResourceScope.getId())) - ); + bizIds = Collections.singletonList(Long.valueOf(appResourceScope.getId())); } else if (appInfo.isAllBizSet()) { // 全业务 // 不根据主机ID过滤 - hostIds = null; + bizIds = null; } else if (appInfo.isBizSet()) { // 业务集 // 查出业务集下所有子业务 - List subBizIds = appInfo.getSubBizIds(); - // 查出所有子业务下属主机ID - hostIds = hostTopoDAO.listHostIdByBizIds(subBizIds); + bizIds = appInfo.getSubBizIds(); } else { throw new InternalException("Ilegal appInfo:" + appInfo, ErrorCode.INTERNAL_ERROR); } - // 查主机数量 - normalNum = applicationHostDAO.countHostByIdAndStatus( - hostIds, - AgentStatusEnum.ALIVE - ); - abnormalNum = applicationHostDAO.countHostByIdAndStatus( - hostIds, - AgentStatusEnum.NOT_ALIVE - ); - abnormalNum += applicationHostDAO.countHostByIdAndStatus( - hostIds, - AgentStatusEnum.UNKNOWN - ); + List statisticsDTOS = applicationHostDAO.countHostStatusNumByBizIds(bizIds); + for (HostStatusNumStatisticsDTO statisticsDTO : statisticsDTOS) { + if (statisticsDTO.getGseAgentAlive() == AgentStatusEnum.ALIVE.getValue()) { + normalNum += statisticsDTO.getHostNum(); + } else { + abnormalNum += statisticsDTO.getHostNum(); + } + } return new AgentStatistics(normalNum.intValue(), abnormalNum.intValue()); }