Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 首页Agent状态分布统计接口性能优化 #745 #1886

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,4 +233,10 @@ Long countHostInfoByMultiKeys(Collection<Long> bizIds,
* @return 删除的主机数量
*/
int deleteBizHostInfoByBizId(long bizId);
/**
* 根据业务id统计主机状态数量
* @param bizIds 业务id
* @return 状态数量
*/
List<HostStatusNumStatisticsDTO> countHostStatusNumByBizIds(List<Long> bizIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1252,6 +1253,32 @@ public List<ApplicationHostDTO> listHostsByCloudIps(Collection<String> cloudIps)
return queryHostsByCondition(conditions);
}

@Override
public List<HostStatusNumStatisticsDTO> countHostStatusNumByBizIds(List<Long> bizIds) {
List<Condition> 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<HostStatusNumStatisticsDTO> 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<ApplicationHostDTO> queryHostsByCondition(List<Condition> conditions) {
Result<Record> result =
context.select(ALL_FIELDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,40 +125,31 @@ public List<GreetingVO> listGreeting(String username) {
public AgentStatistics getAgentStatistics(String username, AppResourceScope appResourceScope) {
// 查出业务
ApplicationDTO appInfo = applicationDAO.getAppById(appResourceScope.getAppId());
Long normalNum;
Long abnormalNum;
List<Long> hostIds;
Long normalNum = 0L;
Long abnormalNum = 0L;
List<Long> 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<Long> 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<HostStatusNumStatisticsDTO> 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());
}

Expand Down