Skip to content

Commit

Permalink
refactor: optimize query of monitorInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
霄鸿 committed Jun 3, 2024
1 parent 3272941 commit 03c255b
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.common.dao.converter;

import io.holoinsight.server.common.dao.entity.MonitorInstance;
import io.holoinsight.server.common.dao.entity.dto.MonitorInstanceDTO;
import io.holoinsight.server.common.dao.transformer.MonitorInstanceCfgMapper;
import org.mapstruct.Mapper;

import java.util.List;

/**
* @author jsy1001de
* @version 1.0: MonitorInstanceConfigConverter.java, Date: 2024-06-03 Time: 11:05
*/
@Mapper(componentModel = "spring", uses = {MonitorInstanceCfgMapper.class})
public interface MonitorInstanceConfigConverter {
MonitorInstanceDTO doToDTO(MonitorInstance monitorInstance);

MonitorInstance dtoToDO(MonitorInstanceDTO monitorInstanceDTO);

List<MonitorInstanceDTO> dosToDTOs(Iterable<MonitorInstance> monitorInstances);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.common.dao.entity.dto;

import io.holoinsight.server.common.dao.entity.MonitorInstanceCfg;
import lombok.Data;

import java.util.Date;

/**
* @author jsy1001de
* @version 1.0: MonitorInstanceDTO.java, Date: 2024-06-03 Time: 11:04
*/
@Data
public class MonitorInstanceDTO {

private Long id;

/**
* 创建时间
*/
private Date gmtCreate;

/**
* 修改时间
*/
private Date gmtModified;

/**
* 实例标识
*/
private String instance;

private String instanceName;
private String instanceType;
private String instanceInfo;

/**
* 实例类型
*/
private String type;

/**
* 租户
*/
private String tenant;

/**
* 租户名称
*/
private String tenantName;

/**
* 工作空间
*/
private String workspace;

/**
* 工作空间名称
*/
private String workspaceName;

/**
* 计量状态
*/
private int meterState;

/**
* 计费状态
*/
private int billingState;

/**
* 扩展配置
*/
private MonitorInstanceCfg config;

/**
* 软删除标记
*/
private boolean deleted;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package io.holoinsight.server.common.dao.transformer;

import com.google.gson.reflect.TypeToken;
import io.holoinsight.server.common.J;
import io.holoinsight.server.common.dao.entity.MonitorInstanceCfg;

import java.lang.reflect.Type;

/**
* @author jsy1001de
* @version 1.0: MonitorInstanceCfgMapper.java, Date: 2024-06-03 Time: 11:07
*/
public class MonitorInstanceCfgMapper {
public static String asString(MonitorInstanceCfg map) {
if (map != null) {
return J.toJson(map);
}
return null;
}

public static MonitorInstanceCfg asObj(String map) {
if (map != null) {
Type t = new TypeToken<MonitorInstanceCfg>() {}.getType();
return J.fromJson(map, t);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import io.holoinsight.server.common.dao.entity.MetaDataDictValue;
import io.holoinsight.server.common.dao.entity.MonitorInstance;
import io.holoinsight.server.common.dao.entity.MonitorInstanceCfg;
import io.holoinsight.server.common.dao.entity.dto.MonitorInstanceDTO;
import io.holoinsight.server.common.service.MonitorInstanceService;
import io.holoinsight.server.common.service.SuperCacheService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

Expand All @@ -37,15 +39,15 @@ public class ProductCtlServiceImpl implements ProductCtlService {

protected static final String RS_DELIMITER = "_";

@Scheduled(initialDelay = 10000L, fixedDelay = 30000L)
@Scheduled(initialDelay = 10000L, fixedDelay = 60000L)
private void refresh() {
List<MonitorInstance> monitorInstances = monitorInstanceService.list();

StopWatch stopWatch = StopWatch.createStarted();
List<MonitorInstanceDTO> monitorInstances = monitorInstanceService.listAllValid();

Map<String, Set<String>> dbProductClosed = new HashMap<>();
for (MonitorInstance monitorInstance : monitorInstances) {
MonitorInstanceCfg cfg =
J.get().fromJson(monitorInstance.getConfig(), MonitorInstanceCfg.class);
cfg.getClosed().forEach((code, closed) -> {
for (MonitorInstanceDTO monitorInstance : monitorInstances) {
monitorInstance.getConfig().getClosed().forEach((code, closed) -> {
if (closed) {
dbProductClosed.computeIfAbsent(monitorInstance.getInstance(), k -> new HashSet<>())
.add(code);
Expand All @@ -71,8 +73,9 @@ private void refresh() {
// This catch statement is intentionally empty
}

log.info("[product_ctl] refresh closed products, switchOn={}, closed={}, metricWhiteList={}",
switchOn, productClosed, metricWhitePrefixes);
log.info(
"[product_ctl] refresh closed products, switchOn={}, closed={}, cost={}, metricWhiteList={}",
switchOn, productClosed.size(), stopWatch.getTime(), metricWhitePrefixes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

import com.baomidou.mybatisplus.extension.service.IService;
import io.holoinsight.server.common.dao.entity.MonitorInstance;
import io.holoinsight.server.common.dao.entity.dto.MonitorInstanceDTO;

import java.util.List;

public interface MonitorInstanceService extends IService<MonitorInstance> {

List<MonitorInstance> listValid();

List<MonitorInstanceDTO> listAllValid();

List<MonitorInstance> queryByInstance(String instance);

List<MonitorInstance> queryByWorkspace(String workspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.holoinsight.server.common.dao.converter.MonitorInstanceConfigConverter;
import io.holoinsight.server.common.dao.entity.MonitorInstance;
import io.holoinsight.server.common.dao.entity.dto.MonitorInstanceDTO;
import io.holoinsight.server.common.dao.mapper.MonitorInstanceMapper;
import io.holoinsight.server.common.service.MonitorInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

Expand All @@ -19,13 +22,25 @@
public class MonitorInstanceServiceImpl extends ServiceImpl<MonitorInstanceMapper, MonitorInstance>
implements MonitorInstanceService {


@Autowired
private MonitorInstanceConfigConverter monitorInstanceConfigConverter;


@Override
public List<MonitorInstance> listValid() {
Map<String, Object> cols = new HashMap<>();
cols.put("deleted", 0);
return listByMap(cols);
}

@Override
public List<MonitorInstanceDTO> listAllValid() {
Map<String, Object> cols = new HashMap<>();
cols.put("deleted", 0);
return monitorInstanceConfigConverter.dosToDTOs(listByMap(cols));
}

@Override
public List<MonitorInstance> queryByInstance(String instance) {
Map<String, Object> cols = new HashMap<>();
Expand Down

0 comments on commit 03c255b

Please sign in to comment.