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

update project group api and k8s restart alarm #552

Merged
merged 2 commits into from
Jul 17, 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
Expand Up @@ -15,6 +15,7 @@ public class ProjectGroupTreeNode implements Serializable {

private Integer id;
private Integer type;
private Integer level;
private Integer relationObjectId;
private String name;
private String cnName;
Expand All @@ -23,9 +24,10 @@ public class ProjectGroupTreeNode implements Serializable {

public ProjectGroupTreeNode(){}

public ProjectGroupTreeNode(Integer id, Integer type, Integer relationObjectId, String name, String cnName,Integer parentGroupId) {
public ProjectGroupTreeNode(Integer id, Integer type, Integer level,Integer relationObjectId, String name, String cnName,Integer parentGroupId) {
this.id = id;
this.type = type;
this.level = level;
this.relationObjectId = relationObjectId;
this.name = name;
this.cnName = cnName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface HeraProjectGroupServiceApi {

Result<ProjectGroupTreeNode> getFullTree(Integer type);

Result<ProjectGroupTreeNode> getTreeByUser(String user,Integer type,String projectGroupName);
Result<ProjectGroupTreeNode> getTreeByUser(String user,Integer type,String projectGroupName,Integer level);

Result<List<HeraAppBaseInfoModel>> searchGroupApps(String user, Integer groupType, Integer projectGroupId, String appName,Integer page, Integer pageSize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void getFullTree(){

@Test
public void getTreeByUser(){
Result<ProjectGroupTreeNode> fullTree = projectGroupService.getTreeByUser("maqianli",0,null);
Result<ProjectGroupTreeNode> fullTree = projectGroupService.getTreeByUser("maqianli",0,null,null);
System.out.println(new Gson().toJson(fullTree.getData()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public List<HeraProjectGroupModel> listByIds(List<Integer> ids,Integer type,Stri
ca.andTypeEqualTo(type);
}
if(level != null){
ca.andLevelEqualTo(level);
//查询节点级数小于指定level的数据
ca.andLevelLessThan(level);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public Result<ProjectGroupTreeNode> getFullTree(Integer type) {
}

@Override
public Result<ProjectGroupTreeNode> getTreeByUser(String user, Integer type, String projectGroupName) {
return projectGroupService.getTreeByUser(user,type,projectGroupName);
public Result<ProjectGroupTreeNode> getTreeByUser(String user, Integer type, String projectGroupName,Integer level) {
return projectGroupService.getTreeByUser(user,type,projectGroupName,level);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,17 @@ public Result<ProjectGroupTreeNode> getFullTree(Integer type){
}


public Result<ProjectGroupTreeNode> getTreeByUser(String user,Integer type,String projectGroupName){
public Result<ProjectGroupTreeNode> getTreeByUser(String user,Integer type,String projectGroupName,Integer level){

HeraProjectGroupModel rootGroupNode = getRootGroupNode(type);
if(rootGroupNode == null){
log.error("getTreeByUser no root data found for type : {}",type);
return Result.fail(CommonError.NOT_EXISTS_DATA);
}

List<HeraProjectGroupModel> nodesByUser = getNodesByUser(user,type,projectGroupName,null);
List<HeraProjectGroupModel> nodesByUser = getNodesByUser(user,type,projectGroupName,level);
if(CollectionUtils.isEmpty(nodesByUser)){
return Result.fail(CommonError.NOT_EXISTS_DATA);
return Result.success();
}

Set<HeraProjectGroupModel> treeNodes = pathToRootData(nodesByUser,type);
Expand All @@ -402,7 +402,7 @@ public Result<List<HeraProjectGroupModel>> searchChildGroups(String user,Integer
List<Integer> userGroupIds = groupUserDao.listGroupIdsByUser(user);
if (CollectionUtils.isEmpty(userGroupIds)) {
log.info("searchChildGroups no group data found for user! user:{},projectGroupId:{},groupType:{}", user, projectGroupId, groupType);
return Result.fail(CommonError.NOT_EXISTS_DATA);
return Result.success();
}

if (!userGroupIds.contains(projectGroupId)) {
Expand All @@ -417,7 +417,6 @@ public Result<List<HeraProjectGroupModel>> searchChildGroups(String user,Integer
List<HeraProjectGroupModel> projectGroups = projectGroupDao.search(projectGroup, null, null);
if (CollectionUtils.isEmpty(projectGroups)) {
log.info("searchChildGroups no assign type group data found for user! user : {},projectGroupId : {},groupType : {} ", user, projectGroupId, groupType);
return Result.fail(CommonError.NOT_EXISTS_DATA);
}

return Result.success(projectGroups);
Expand All @@ -432,7 +431,7 @@ public Result<List<HeraAppBaseInfoModel>> searchGroupApps(String user,Integer gr

if (CollectionUtils.isEmpty(userGroupIds)) {
log.info("getGroupApps no group data found for user! user:{},projectGroupId:{},groupType:{},appName:{}",user,projectGroupId,groupType,appName);
return Result.fail(CommonError.NOT_EXISTS_DATA);
return Result.success();
}

if(projectGroupId != null){
Expand All @@ -448,7 +447,7 @@ public Result<List<HeraAppBaseInfoModel>> searchGroupApps(String user,Integer gr
List<HeraProjectGroupModel> projectGroupsByType = projectGroupDao.listByIds(groupIds, groupType, null,null);
if(CollectionUtils.isEmpty(projectGroupsByType)){
log.info("getGroupApps no assign type group data found for user! user:{},projectGroupId:{},groupType:{},appName:{}",user,projectGroupId,groupType,appName);
return Result.fail(CommonError.NOT_EXISTS_DATA);
return Result.success();
}

//重置为指定类型的组
Expand All @@ -457,7 +456,7 @@ public Result<List<HeraAppBaseInfoModel>> searchGroupApps(String user,Integer gr
List<Integer> appBaseInfoIds = projectGroupAppDao.getAppBaseInfoIds(groupIds);
if(CollectionUtils.isEmpty(appBaseInfoIds)){
log.info("getGroupApps no apps data found! user:{},appBaseInfoIds:{},groupType:{}",user,appBaseInfoIds,groupType);
return Result.fail(CommonError.NOT_EXISTS_DATA);
return Result.success();
}

HeraAppBaseInfoModel appBaseInfoModel = new HeraAppBaseInfoModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private List<ProjectGroupTreeNode> buildTreeChildren(Integer parentId){
List<HeraProjectGroupModel> groups = groupByParentId.getOrDefault(parentId, new ArrayList<>());

for (HeraProjectGroupModel group : groups) {
ProjectGroupTreeNode node = new ProjectGroupTreeNode(group.getId(), group.getType(), group.getRelationObjectId(),
ProjectGroupTreeNode node = new ProjectGroupTreeNode(group.getId(), group.getType(),group.getLevel(), group.getRelationObjectId(),
group.getName(), group.getCnName(),group.getParentGroupId());
List<ProjectGroupTreeNode> children = buildTreeChildren(group.getId());
node.setChildren(children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel;
import com.xiaomi.mone.app.api.model.project.group.HeraProjectGroupDataRequest;
import com.xiaomi.mone.app.api.model.project.group.HeraProjectGroupModel;
import com.xiaomi.mone.app.api.model.project.group.ProjectGroupTreeNode;
import com.xiaomi.mone.app.common.Result;
import com.xiaomi.mone.app.enums.CommonError;
Expand Down Expand Up @@ -207,7 +208,7 @@ public Result<ProjectGroupTreeNode> viewTreeByUser(HttpServletRequest request, @
}

@ResponseBody
@PostMapping("/view/project_group/app")
@PostMapping("/view/project-group/apps")
public Result<List<AppMonitor>> viewGroupApps(HttpServletRequest request, @RequestBody ProjectGroupRequest param) {

AuthUserVo userInfo = UserUtil.getUser();
Expand All @@ -225,5 +226,30 @@ public Result<List<AppMonitor>> viewGroupApps(HttpServletRequest request, @Reque
}
return projectGroupService.searchMyApps(param);
}

@ResponseBody
@PostMapping("/view/project-group/childs")
public Result<List<HeraProjectGroupModel>> searchChildGroups(HttpServletRequest request, @RequestBody ProjectGroupRequest param) {

AuthUserVo userInfo = UserUtil.getUser();
if (userInfo == null) {
return Result.fail(CommonError.UNAUTHORIZED);
}

String user = userInfo.genFullAccount();
param.setUser(user);
log.info("searchChildGroups param : {}", param);

if(param.getGroupType() == null){
log.error("searchChildGroups request param error! no group type found!");
return Result.fail(CommonError.ParamsError);
}

if(param.getProjectGroupId() == null){
log.error("searchChildGroups request param error! no projectGroupId found!");
return Result.fail(CommonError.ParamsError);
}
return projectGroupService.searchChildGroups(param);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum AlarmPresetMetrics {
k8s_mem_resource_use_rate("k8s_mem_resource_use_rate","k8s容器内存资源利用率(1d)", MetricsUnit.UNIT_PERCENT, SendAlertGroupKey.APP_INSTANCE, AlarmStrategyType.SYSTEM,InterfaceMetricTypes.basic, BasicUrlType.cn_grafana_ip_1d, "11"),

k8s_cpu_avg_use_rate("k8s_cpu_avg_use_rate","k8s容器CPU平均使用率", MetricsUnit.UNIT_PERCENT, SendAlertGroupKey.APP_INSTANCE, AlarmStrategyType.SYSTEM,InterfaceMetricTypes.basic, BasicUrlType.cn_grafana_ip_1d, "2"),
k8s_pod_restart_times("k8s_pod_restart_times","k8s-POD重启次数", MetricsUnit.UNIT_COUNT, SendAlertGroupKey.APP_INSTANCE, AlarmStrategyType.SYSTEM,InterfaceMetricTypes.basic, BasicUrlType.cn_grafana_ip_1d, "2"),
// k8s_pod_restart_times("k8s_pod_restart_times","k8s-POD重启", MetricsUnit.UNIT_COUNT, SendAlertGroupKey.APP_INSTANCE, AlarmStrategyType.SYSTEM,InterfaceMetricTypes.basic, true,BasicUrlType.cn_grafana_ip_1d, "2"),

/**
* jvm指标
Expand Down Expand Up @@ -148,6 +148,18 @@ public enum AlarmPresetMetrics {
this.hideValueConfig = hideValueConfig;
}

AlarmPresetMetrics(String code, String message, MetricsUnit unit, SendAlertGroupKey groupKey, AlarmStrategyType strategyType, InterfaceMetricTypes metricType, Boolean hideValueConfig, BasicUrlType basicUrlType, String viewPanel){
this.code = code;
this.message = message;
this.unit = unit;
this.groupKey = groupKey;
this.strategyType = strategyType;
this.metricType = metricType;
this.hideValueConfig = hideValueConfig;
this.basicUrlType = basicUrlType;
this.viewPanel = viewPanel;
}

AlarmPresetMetrics(String code, String message, String errorMetric, String totalMetric, String slowQueryMetric, String timeCostMetric, MetricsUnit unit, SendAlertGroupKey groupKey, AlarmStrategyType strategyType, InterfaceMetricTypes metricType){
this.code = code;
this.message = message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public List<AppMonitor> getData(Integer offset, Integer pageSize){
return appMonitorMapper.selectByExample(example);
}

public Long countByBaseInfoIds(List<Integer> baseInfoIds){
public Long countByBaseInfoIds(List<Integer> baseInfoIds,String user){

if(CollectionUtils.isEmpty(baseInfoIds)){
return 0l;
Expand All @@ -93,10 +93,11 @@ public Long countByBaseInfoIds(List<Integer> baseInfoIds){
AppMonitorExample.Criteria ca = example.createCriteria();
ca.andStatusEqualTo(0);
ca.andBaseInfoIdIn(baseInfoIds);
ca.andOwnerEqualTo(user);
return appMonitorMapper.countByExample(example);
}

public List<AppMonitor> getDataByBaseInfoIds(List<Integer> baseInfoIds,Integer page, Integer pageSize){
public List<AppMonitor> getDataByBaseInfoIds(List<Integer> baseInfoIds,String user,Integer page, Integer pageSize){

if(page.intValue() <=0){
page = 1;
Expand All @@ -110,6 +111,7 @@ public List<AppMonitor> getDataByBaseInfoIds(List<Integer> baseInfoIds,Integer p
AppMonitorExample.Criteria ca = example.createCriteria();
ca.andStatusEqualTo(0);
ca.andBaseInfoIdIn(baseInfoIds);
ca.andOwnerEqualTo(user);
example.setOffset((page - 1) * pageSize);
example.setLimit(pageSize);
example.setOrderByClause("id desc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,11 +1125,11 @@ public Result historyInstance(String application,Long startTime, Long endTime) {
return Result.success(rangeIps);
}

public Long countByBaseInfoId(List<Integer> baseInfoIds){
return appMonitorDao.countByBaseInfoIds(baseInfoIds);
public Long countByBaseInfoId(List<Integer> baseInfoIds,String user){
return appMonitorDao.countByBaseInfoIds(baseInfoIds,user);
}

public List<AppMonitor> searchByBaseInfoId(List<Integer> baseInfoIds,Integer page,Integer pageSize){
return appMonitorDao.getDataByBaseInfoIds(baseInfoIds,page,pageSize);
public List<AppMonitor> searchByBaseInfoId(List<Integer> baseInfoIds,String user,Integer page,Integer pageSize){
return appMonitorDao.getDataByBaseInfoIds(baseInfoIds,user,page,pageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ProjectGroupRequest implements Serializable {
Integer projectGroupId;
String projectGroupName;
String appName;
Integer level;
Integer page;
Integer pageSize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.xiaomi.mone.app.api.model.HeraAppBaseInfoModel;
import com.xiaomi.mone.app.api.model.project.group.HeraProjectGroupAppRequest;
import com.xiaomi.mone.app.api.model.project.group.HeraProjectGroupDataRequest;
import com.xiaomi.mone.app.api.model.project.group.HeraProjectGroupModel;
import com.xiaomi.mone.app.api.model.project.group.ProjectGroupTreeNode;
import com.xiaomi.mone.app.api.service.HeraAppService;
import com.xiaomi.mone.app.api.service.HeraAuthorizationApi;
Expand Down Expand Up @@ -58,14 +59,18 @@ public Result<ProjectGroupTreeNode> getFullTree(Integer type){
}

public Result<ProjectGroupTreeNode> getTreeByUser(ProjectGroupRequest request){
return projectGroupServiceApi.getTreeByUser(request.getUser(),request.getGroupType(),request.getProjectGroupName());
return projectGroupServiceApi.getTreeByUser(request.getUser(),request.getGroupType(),request.getProjectGroupName(),request.getLevel());
}

public Result<List<HeraAppBaseInfoModel>> searchGroupApps(ProjectGroupRequest request){
return projectGroupServiceApi.searchGroupApps(request.getUser(),request.getGroupType(),request.getProjectGroupId(),request.getAppName(),request.getPage(),request.getPageSize());

}

public Result<List<HeraProjectGroupModel>> searchChildGroups(ProjectGroupRequest request){
return projectGroupServiceApi.searchChildGroups(request.getUser(),request.getGroupType(),request.getProjectGroupId(),request.getPage(),request.getPageSize());
}

public Result searchMyApps(ProjectGroupRequest request){

Result<List<HeraAppBaseInfoModel>> listResult = projectGroupServiceApi.searchGroupApps(request.getUser(), request.getGroupType(), request.getProjectGroupId(), request.getAppName(), request.getPage(), request.getPageSize());
Expand All @@ -85,11 +90,11 @@ public Result searchMyApps(ProjectGroupRequest request){
List<HeraAppBaseInfoModel> data = listResult.getData();
List<Integer> baseIds = data.stream().map(t -> t.getId()).collect(Collectors.toList());
log.info("baseIds : {}",new Gson().toJson(baseIds));
Long aLong = appMonitorService.countByBaseInfoId(baseIds);
Long aLong = appMonitorService.countByBaseInfoId(baseIds,request.getUser());
pd.setTotal(aLong);

if(aLong.intValue() > 0){
List<AppMonitor> appMonitors = appMonitorService.searchByBaseInfoId(baseIds, 1, 1000);
List<AppMonitor> appMonitors = appMonitorService.searchByBaseInfoId(baseIds,request.getUser(), 1, 1000);
pd.setList(appMonitors);
pd.setPage(1);
pd.setPageSize(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,15 +838,14 @@ public String getK8sCpuAvgUsageAlarmExpr(Integer projectId,String projectName,St
public String getK8sPodRestartExpr(Integer projectId,String projectName,String op,double value,AlarmRuleData ruleData){

StringBuilder exprBuilder = new StringBuilder();
exprBuilder.append("(0 * container_last_seen{system='mione',");
exprBuilder.append("increase(kube_pod_container_restarts_record{system='mione',");
String labelProperties = getEnvLabelProperties(ruleData);
if(StringUtils.isNotBlank(labelProperties)){
exprBuilder.append(labelProperties).append(",");
}
exprBuilder.append("application='").append(projectId).append("_").append(projectName).append("'");
exprBuilder.append("}) ");
exprBuilder.append("+ on(pod,container) group_left kube_pod_container_status_restarts_total{system='mione',job='mione-kube-state'}");
exprBuilder.append(op).append(value);
String appName = projectName.replaceAll("-","_");
exprBuilder.append("application='").append(projectId).append("_").append(appName).append("'");
exprBuilder.append("}[3m]) > 0");

return exprBuilder.toString();
}
Expand Down