mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-07 17:28:27 +08:00
add the redis support for saving data
This commit is contained in:
parent
e941d97020
commit
a1450e6761
21
pom.xml
21
pom.xml
@ -29,7 +29,8 @@
|
|||||||
<spring-web.version>5.2.11.RELEASE</spring-web.version>
|
<spring-web.version>5.2.11.RELEASE</spring-web.version>
|
||||||
<spring-webmvc.version>5.3.6</spring-webmvc.version>
|
<spring-webmvc.version>5.3.6</spring-webmvc.version>
|
||||||
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
|
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
|
||||||
<spring-redis.version>2.6.4</spring-redis.version>
|
<spring-redis.version>2.6.2</spring-redis.version>
|
||||||
|
<fastjson.version>2.0.9</fastjson.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -69,16 +70,16 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.javassist</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>javassist</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
<version>3.26.0-GA</version>
|
<version>${spring-redis.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>${fastjson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- <dependency>-->
|
|
||||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
|
||||||
<!-- <artifactId>spring-boot-starter-data-redis</artifactId>-->
|
|
||||||
<!-- <version>${spring-redis.version}</version>-->
|
|
||||||
<!-- <scope>provided</scope>-->
|
|
||||||
<!-- </dependency>-->
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<developers>
|
<developers>
|
||||||
<developer>
|
<developer>
|
||||||
|
|||||||
@ -22,12 +22,22 @@ public class DefaultConfig {
|
|||||||
private Boolean paramAnalyse;
|
private Boolean paramAnalyse;
|
||||||
private String saver;
|
private String saver;
|
||||||
private String dataSource;
|
private String dataSource;
|
||||||
|
|
||||||
|
private String redisTemplate;
|
||||||
private Integer threadNum;
|
private Integer threadNum;
|
||||||
private String contextPath;
|
private String contextPath;
|
||||||
private Boolean authEnable;
|
private Boolean authEnable;
|
||||||
private String userName;
|
private String userName;
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
public String getRedisTemplate() {
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRedisTemplate(String redisTemplate) {
|
||||||
|
this.redisTemplate = redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
public String getLanguage() {
|
public String getLanguage() {
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,13 +15,17 @@ import org.springframework.context.ApplicationContext;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* zhangchang
|
* zhangchang
|
||||||
@ -62,7 +66,6 @@ public class LoadConfig {
|
|||||||
public void initConfig() {
|
public void initConfig() {
|
||||||
DefaultConfig config = new DefaultConfig();
|
DefaultConfig config = new DefaultConfig();
|
||||||
config.setLogEnable(defaultConfig.getLogEnable() == null ? logEnable : defaultConfig.getLogEnable());
|
config.setLogEnable(defaultConfig.getLogEnable() == null ? logEnable : defaultConfig.getLogEnable());
|
||||||
// config.setPointcut(defaultConfig.getPointcut() == null ? pointcut : defaultConfig.getPointcut());
|
|
||||||
config.setLogLanguage(defaultConfig.getLogLanguage() == null ? logLanguage : defaultConfig.getLogLanguage());
|
config.setLogLanguage(defaultConfig.getLogLanguage() == null ? logLanguage : defaultConfig.getLogLanguage());
|
||||||
config.setThreshold(defaultConfig.getThreshold() == null ? timeThreshold : defaultConfig.getThreshold());
|
config.setThreshold(defaultConfig.getThreshold() == null ? timeThreshold : defaultConfig.getThreshold());
|
||||||
config.setExceptionEnable(defaultConfig.getExceptionEnable() == null ? exceptionEnable : defaultConfig.getExceptionEnable());
|
config.setExceptionEnable(defaultConfig.getExceptionEnable() == null ? exceptionEnable : defaultConfig.getExceptionEnable());
|
||||||
@ -73,20 +76,8 @@ public class LoadConfig {
|
|||||||
config.setThreadNum(defaultConfig.getThreadNum() == null ? 2 : defaultConfig.getThreadNum());
|
config.setThreadNum(defaultConfig.getThreadNum() == null ? 2 : defaultConfig.getThreadNum());
|
||||||
config.setAuthEnable(defaultConfig.getAuthEnable() == null ? false : defaultConfig.getAuthEnable());
|
config.setAuthEnable(defaultConfig.getAuthEnable() == null ? false : defaultConfig.getAuthEnable());
|
||||||
config.setParamAnalyse(defaultConfig.getParamAnalyse() == null ? true : defaultConfig.getParamAnalyse());
|
config.setParamAnalyse(defaultConfig.getParamAnalyse() == null ? true : defaultConfig.getParamAnalyse());
|
||||||
try {
|
configDataSource(config);
|
||||||
DataSource dataSource = applicationContext.getBean(DataSource.class);
|
configRedisTemplate(config);
|
||||||
Context.setDataSource(dataSource);
|
|
||||||
}catch (NoUniqueBeanDefinitionException e){
|
|
||||||
if (StringUtils.isEmpty(config.getDataSource())) {
|
|
||||||
log.warning("kotime=>No unique bean of type 'DataSource' available,you can define it by `ko-time.data-source=xxx`");
|
|
||||||
}else {
|
|
||||||
DataSource dataSource = applicationContext.getBean(config.getDataSource(),DataSource.class);
|
|
||||||
Context.setDataSource(dataSource);
|
|
||||||
}
|
|
||||||
}catch (NoSuchBeanDefinitionException e){
|
|
||||||
log.warning("kotime=>No qualifying bean of type 'DataSource' available,but you can ignore it if your KoTime saver is `ko-time.saver=memory`");
|
|
||||||
}
|
|
||||||
|
|
||||||
Context.setConfig(config);
|
Context.setConfig(config);
|
||||||
String[] names = applicationContext.getBeanNamesForType(GraphService.class);
|
String[] names = applicationContext.getBeanNamesForType(GraphService.class);
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
@ -100,7 +91,7 @@ public class LoadConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (null == Context.getSaver()) {
|
if (null == Context.getSaver()) {
|
||||||
throw new DataBaseException("error `ko-time.saver="+config.getSaver()+"`, and you can only choose an option in {memory,database} for `ko-time.saver=`!");
|
throw new DataBaseException("error `ko-time.saver=" + config.getSaver() + "`, and you can only choose an option in {memory,database,redis} for `ko-time.saver=`!");
|
||||||
}
|
}
|
||||||
log.info("kotime=>loading config");
|
log.info("kotime=>loading config");
|
||||||
|
|
||||||
@ -112,6 +103,48 @@ public class LoadConfig {
|
|||||||
initMethodHandlers();
|
initMethodHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void configDataSource(DefaultConfig config) {
|
||||||
|
if (!"database".equals(config.getSaver())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
DataSource dataSource = applicationContext.getBean(DataSource.class);
|
||||||
|
Context.setDataSource(dataSource);
|
||||||
|
} catch (NoUniqueBeanDefinitionException e) {
|
||||||
|
if (StringUtils.isEmpty(config.getDataSource())) {
|
||||||
|
log.warning("kotime=>No unique bean of type 'DataSource' available,you can define it by `ko-time.data-source=xxx`");
|
||||||
|
} else {
|
||||||
|
DataSource dataSource = applicationContext.getBean(config.getDataSource(), DataSource.class);
|
||||||
|
Context.setDataSource(dataSource);
|
||||||
|
}
|
||||||
|
} catch (NoSuchBeanDefinitionException e) {
|
||||||
|
log.warning("kotime=>No qualifying bean of type 'DataSource' available,but you can ignore it if your KoTime saver is `ko-time.saver=memory`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void configRedisTemplate(DefaultConfig config) {
|
||||||
|
if (!"redis".equals(config.getSaver())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||||
|
Context.setStringRedisTemplate(redisTemplate);
|
||||||
|
} catch (NoUniqueBeanDefinitionException e) {
|
||||||
|
if (!StringUtils.isEmpty(config.getRedisTemplate())) {
|
||||||
|
StringRedisTemplate redisTemplate = applicationContext.getBean(config.getRedisTemplate(), StringRedisTemplate.class);
|
||||||
|
Context.setStringRedisTemplate(redisTemplate);
|
||||||
|
} else {
|
||||||
|
Map<String, StringRedisTemplate> beansOfType = applicationContext.getBeansOfType(StringRedisTemplate.class);
|
||||||
|
log.warning("kotime=>No unique bean of type 'StringRedisTemplate' available,you can define it by `ko-time.redis-template=xxx`,and you can choose a name in "+beansOfType.keySet().stream().collect(Collectors.toList()));
|
||||||
|
log.warning("kotime=>Now the firsr was be set.");
|
||||||
|
Context.setStringRedisTemplate(beansOfType.values().stream().collect(Collectors.toList()).get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (NoSuchBeanDefinitionException e) {
|
||||||
|
log.warning("kotime=>No qualifying bean of type 'StringRedisTemplate' available,but you can ignore it if your KoTime saver is `ko-time.saver=memory`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void initMethodHandlers() {
|
public void initMethodHandlers() {
|
||||||
String[] names = applicationContext.getBeanNamesForType(InvokedHandler.class);
|
String[] names = applicationContext.getBeanNamesForType(InvokedHandler.class);
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.langpy.kotime.config;
|
||||||
|
|
||||||
|
import cn.langpy.kotime.util.Context;
|
||||||
|
import cn.langpy.kotime.util.KoUtil;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SaveResourceConfig implements CommandLineRunner{
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
DataSource dataSource = KoUtil.getDataSource();
|
||||||
|
if (null!=dataSource) {
|
||||||
|
Context.setDataSource(dataSource);
|
||||||
|
}
|
||||||
|
StringRedisTemplate redisTemplate = KoUtil.getStringRedisTemplate();
|
||||||
|
if (null!=redisTemplate) {
|
||||||
|
Context.setStringRedisTemplate(redisTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
KoUtil.clearCaches();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,6 +5,13 @@ import cn.langpy.kotime.service.GraphService;
|
|||||||
import cn.langpy.kotime.util.Common;
|
import cn.langpy.kotime.util.Common;
|
||||||
import cn.langpy.kotime.util.Context;
|
import cn.langpy.kotime.util.Context;
|
||||||
import cn.langpy.kotime.util.MethodType;
|
import cn.langpy.kotime.util.MethodType;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -18,25 +25,21 @@ import static java.util.stream.Collectors.toList;
|
|||||||
/**
|
/**
|
||||||
* zhangchang
|
* zhangchang
|
||||||
*/
|
*/
|
||||||
//@Component("redis")
|
@Lazy
|
||||||
|
@Component("redis")
|
||||||
public class RedisBase implements GraphService {
|
public class RedisBase implements GraphService {
|
||||||
|
|
||||||
private volatile static Map<String, MethodNode> methodNodes;
|
private static final String methodPre = "KO_METHODS:";
|
||||||
|
private static final String exceptionPre = "KO_EXCEPTIONS:";
|
||||||
|
private static final String methodRelationPre = "KO_METHODRES:";
|
||||||
|
private static final String exceptionRelationPre = "KO_EXCEPTIONRES:";
|
||||||
|
private static final String paramValueMetricMapPre = "KO_PARAM:";
|
||||||
|
|
||||||
private volatile static Map<String, ExceptionNode> exceptions;
|
private static StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
private volatile static Map<String, MethodRelation> methodRelations;
|
public RedisBase() {
|
||||||
|
redisTemplate = Context.getStringRedisTemplate();
|
||||||
|
|
||||||
private volatile static Map<String, ExceptionRelation> exceptionRelations;
|
|
||||||
|
|
||||||
private volatile static Map<String, Map<String, ParamMetric>> paramValueMetricMap;
|
|
||||||
|
|
||||||
static {
|
|
||||||
methodNodes = new HashMap<>();
|
|
||||||
exceptions = new HashMap<>();
|
|
||||||
methodRelations = new HashMap<>();
|
|
||||||
exceptionRelations = new HashMap<>();
|
|
||||||
paramValueMetricMap = new HashMap<String, Map<String, ParamMetric>>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -44,13 +47,14 @@ public class RedisBase implements GraphService {
|
|||||||
if (null == methodNode) {
|
if (null == methodNode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!methodNodes.containsKey(methodNode.getId())) {
|
String key = methodPre + methodNode.getId();
|
||||||
methodNodes.put(methodNode.getId(), methodNode);
|
if (!redisTemplate.hasKey(key)) {
|
||||||
|
insert(key, methodNode);
|
||||||
} else {
|
} else {
|
||||||
if (methodNode.getMethodType() == MethodType.Controller && !StringUtils.isEmpty(methodNode.getRouteName())) {
|
if (methodNode.getMethodType() == MethodType.Controller && !StringUtils.isEmpty(methodNode.getRouteName())) {
|
||||||
MethodNode controller = methodNodes.get(methodNode.getId());
|
MethodNode controller = query(key, MethodNode.class);
|
||||||
controller.setRouteName(methodNode.getRouteName());
|
controller.setRouteName(methodNode.getRouteName());
|
||||||
methodNodes.put(methodNode.getId(), controller);
|
insert(key, controller);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,10 +62,11 @@ public class RedisBase implements GraphService {
|
|||||||
|
|
||||||
public void addParamAnalyse(String methodId, Parameter[] names, Object[] values, double v) {
|
public void addParamAnalyse(String methodId, Parameter[] names, Object[] values, double v) {
|
||||||
String paramsKey = Common.getPramsStr(names, values);
|
String paramsKey = Common.getPramsStr(names, values);
|
||||||
if (paramValueMetricMap.containsKey(methodId)) {
|
String key = paramValueMetricMapPre + methodId;
|
||||||
Map<String, ParamMetric> paramMetricMap = paramValueMetricMap.get(methodId);
|
if (redisTemplate.hasKey(key)) {
|
||||||
|
Map<String, JSONObject> paramMetricMap = query(key, Map.class);
|
||||||
if (paramMetricMap.containsKey(paramsKey)) {
|
if (paramMetricMap.containsKey(paramsKey)) {
|
||||||
ParamMetric paramMetric = paramMetricMap.get(paramsKey);
|
ParamMetric paramMetric = paramMetricMap.get(paramsKey).toJavaObject(ParamMetric.class);
|
||||||
BigDecimal bg = BigDecimal.valueOf((paramMetric.getAvgRunTime() + v) / 2.0);
|
BigDecimal bg = BigDecimal.valueOf((paramMetric.getAvgRunTime() + v) / 2.0);
|
||||||
double avg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
double avg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||||
paramMetric.setAvgRunTime(avg);
|
paramMetric.setAvgRunTime(avg);
|
||||||
@ -71,22 +76,16 @@ public class RedisBase implements GraphService {
|
|||||||
if (v < paramMetric.getMinRunTime()) {
|
if (v < paramMetric.getMinRunTime()) {
|
||||||
paramMetric.setMinRunTime(v);
|
paramMetric.setMinRunTime(v);
|
||||||
}
|
}
|
||||||
} else {
|
insert(key, paramMetricMap);
|
||||||
ParamMetric paramMetric = new ParamMetric();
|
|
||||||
paramMetric.setMaxRunTime(v);
|
|
||||||
paramMetric.setAvgRunTime(v);
|
|
||||||
paramMetric.setMaxRunTime(v);
|
|
||||||
paramMetricMap.put(paramsKey, paramMetric);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ParamMetric paramMetric = new ParamMetric();
|
ParamMetric paramMetric = new ParamMetric();
|
||||||
paramMetric.setMaxRunTime(v);
|
paramMetric.setMaxRunTime(v);
|
||||||
paramMetric.setAvgRunTime(v);
|
paramMetric.setAvgRunTime(v);
|
||||||
paramMetric.setMaxRunTime(v);
|
paramMetric.setMaxRunTime(v);
|
||||||
|
|
||||||
Map<String, ParamMetric> paramMetricMap = new HashMap<>();
|
Map<String, ParamMetric> paramMetricMap = new HashMap<>();
|
||||||
paramMetricMap.put(paramsKey, paramMetric);
|
paramMetricMap.put(paramsKey, paramMetric);
|
||||||
paramValueMetricMap.put(methodId, paramMetricMap);
|
insert(key, paramMetricMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +98,7 @@ public class RedisBase implements GraphService {
|
|||||||
if (sourceMethodNode.getId().equals(targetMethodNode.getId())) {
|
if (sourceMethodNode.getId().equals(targetMethodNode.getId())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (methodRelations.containsKey(targetMethodNode.getId()+sourceMethodNode.getId())) {
|
if (redisTemplate.hasKey(methodRelationPre + targetMethodNode.getId() + sourceMethodNode.getId())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
MethodRelation methodRelation = new MethodRelation();
|
MethodRelation methodRelation = new MethodRelation();
|
||||||
@ -109,9 +108,10 @@ public class RedisBase implements GraphService {
|
|||||||
methodRelation.setAvgRunTime(targetMethodNode.getValue());
|
methodRelation.setAvgRunTime(targetMethodNode.getValue());
|
||||||
methodRelation.setMaxRunTime(targetMethodNode.getValue());
|
methodRelation.setMaxRunTime(targetMethodNode.getValue());
|
||||||
methodRelation.setMinRunTime(targetMethodNode.getValue());
|
methodRelation.setMinRunTime(targetMethodNode.getValue());
|
||||||
MethodRelation old = methodRelations.get(methodRelation.getId());
|
String key = methodRelationPre + methodRelation.getId();
|
||||||
|
MethodRelation old = query(key, MethodRelation.class);
|
||||||
if (null == old) {
|
if (null == old) {
|
||||||
methodRelations.put(methodRelation.getId(), methodRelation);
|
insert(key, methodRelation);
|
||||||
return methodRelation;
|
return methodRelation;
|
||||||
} else {
|
} else {
|
||||||
BigDecimal bg = BigDecimal.valueOf((methodRelation.getAvgRunTime() + old.getAvgRunTime()) / 2.0);
|
BigDecimal bg = BigDecimal.valueOf((methodRelation.getAvgRunTime() + old.getAvgRunTime()) / 2.0);
|
||||||
@ -119,6 +119,7 @@ public class RedisBase implements GraphService {
|
|||||||
old.setAvgRunTime(avg);
|
old.setAvgRunTime(avg);
|
||||||
old.setMaxRunTime(methodRelation.getMaxRunTime() > old.getMaxRunTime() ? methodRelation.getMaxRunTime() : old.getMaxRunTime());
|
old.setMaxRunTime(methodRelation.getMaxRunTime() > old.getMaxRunTime() ? methodRelation.getMaxRunTime() : old.getMaxRunTime());
|
||||||
old.setMinRunTime(methodRelation.getMinRunTime() < old.getMinRunTime() ? methodRelation.getMinRunTime() : old.getMinRunTime());
|
old.setMinRunTime(methodRelation.getMinRunTime() < old.getMinRunTime() ? methodRelation.getMinRunTime() : old.getMinRunTime());
|
||||||
|
insert(key, old);
|
||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,9 +131,10 @@ public class RedisBase implements GraphService {
|
|||||||
exceptionRelation.setSourceId(sourceMethodNode.getId());
|
exceptionRelation.setSourceId(sourceMethodNode.getId());
|
||||||
exceptionRelation.setTargetId(exceptionNode.getId());
|
exceptionRelation.setTargetId(exceptionNode.getId());
|
||||||
exceptionRelation.setLocation(exceptionNode.getValue());
|
exceptionRelation.setLocation(exceptionNode.getValue());
|
||||||
ExceptionRelation old = exceptionRelations.get(exceptionRelation.getId());
|
String key = exceptionRelationPre + exceptionRelation.getId();
|
||||||
|
ExceptionRelation old = query(key, ExceptionRelation.class);
|
||||||
if (null == old) {
|
if (null == old) {
|
||||||
exceptionRelations.put(exceptionRelation.getId(), exceptionRelation);
|
insert(key, exceptionRelation);
|
||||||
return exceptionRelation;
|
return exceptionRelation;
|
||||||
} else {
|
} else {
|
||||||
return old;
|
return old;
|
||||||
@ -141,18 +143,20 @@ public class RedisBase implements GraphService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addExceptionNode(ExceptionNode exceptionNode) {
|
public void addExceptionNode(ExceptionNode exceptionNode) {
|
||||||
if (!exceptions.containsKey(exceptionNode.getId())) {
|
String key = exceptionPre + exceptionNode.getId();
|
||||||
exceptions.put(exceptionNode.getId(), exceptionNode);
|
if (!redisTemplate.hasKey(key)) {
|
||||||
|
insert(key, exceptionNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ExceptionInfo> getExceptions(String methodId) {
|
public List<ExceptionInfo> getExceptions(String methodId) {
|
||||||
List<ExceptionInfo> exceptionInfos = new ArrayList<>();
|
List<ExceptionInfo> exceptionInfos = new ArrayList<>();
|
||||||
List<ExceptionRelation> relations = exceptionRelations.values().stream().filter(exceptionRelation -> exceptionRelation.getSourceId().equals(methodId)).collect(toList());
|
List<ExceptionRelation> searchs = searchList(exceptionRelationPre, ExceptionRelation.class);
|
||||||
|
List<ExceptionRelation> relations = searchs.stream().filter(exceptionRelation -> exceptionRelation.getSourceId().equals(methodId)).collect(toList());
|
||||||
for (ExceptionRelation relation : relations) {
|
for (ExceptionRelation relation : relations) {
|
||||||
String exceptionId = relation.getTargetId();
|
String exceptionId = relation.getTargetId();
|
||||||
ExceptionNode exceptionNode = exceptions.get(exceptionId);
|
ExceptionNode exceptionNode = query(exceptionPre + exceptionId, ExceptionNode.class);
|
||||||
ExceptionInfo exceptionInfo = new ExceptionInfo();
|
ExceptionInfo exceptionInfo = new ExceptionInfo();
|
||||||
exceptionInfo.setId(exceptionNode.getId());
|
exceptionInfo.setId(exceptionNode.getId());
|
||||||
exceptionInfo.setName(exceptionNode.getName());
|
exceptionInfo.setName(exceptionNode.getName());
|
||||||
@ -168,16 +172,21 @@ public class RedisBase implements GraphService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ExceptionNode> getExceptions() {
|
public List<ExceptionNode> getExceptions() {
|
||||||
return exceptions.values().stream().distinct().collect(toList());
|
List<ExceptionNode> searchs = searchList(exceptionPre, ExceptionNode.class);
|
||||||
|
return searchs.stream().distinct().collect(toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<MethodInfo> getControllers() {
|
public List<MethodInfo> getControllers() {
|
||||||
List<MethodInfo> methodInfos = new ArrayList<>();
|
List<MethodInfo> methodInfos = new ArrayList<>();
|
||||||
for (MethodNode methodNode : methodNodes.values()) {
|
List<MethodNode> smethodNodes = searchList(methodPre, MethodNode.class);
|
||||||
|
|
||||||
|
for (MethodNode methodNode : smethodNodes) {
|
||||||
if (MethodType.Controller == methodNode.getMethodType()) {
|
if (MethodType.Controller == methodNode.getMethodType()) {
|
||||||
String id = methodNode.getId();
|
String id = methodNode.getId();
|
||||||
Optional<MethodRelation> relations = methodRelations.values().stream().filter(methodRelation -> methodRelation.getTargetId().equals(id)).findFirst();
|
List<MethodRelation> smethodRelations = searchList(methodRelationPre, MethodRelation.class);
|
||||||
|
|
||||||
|
Optional<MethodRelation> relations = smethodRelations.stream().filter(methodRelation -> methodRelation.getTargetId().equals(id)).findFirst();
|
||||||
MethodRelation relation = null;
|
MethodRelation relation = null;
|
||||||
if (relations.isPresent()) {
|
if (relations.isPresent()) {
|
||||||
relation = relations.get();
|
relation = relations.get();
|
||||||
@ -205,17 +214,24 @@ public class RedisBase implements GraphService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, ParamMetric> getMethodParamGraph(String methodId) {
|
public Map<String, ParamMetric> getMethodParamGraph(String methodId) {
|
||||||
Map<String, ParamMetric> paramMetricMap = paramValueMetricMap.get(methodId);
|
String key = paramValueMetricMapPre + methodId;
|
||||||
return paramMetricMap;
|
Map<String, ParamMetric> res = new HashMap<>();
|
||||||
|
Map<String, JSONObject> paramMetric = query(key, Map.class);
|
||||||
|
for (String name : paramMetric.keySet()) {
|
||||||
|
res.put(name, paramMetric.get(name).toJavaObject(ParamMetric.class));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<MethodInfo> searchMethods(String question) {
|
public List<MethodInfo> searchMethods(String question) {
|
||||||
List<MethodInfo> methodInfos = new ArrayList<>();
|
List<MethodInfo> methodInfos = new ArrayList<>();
|
||||||
for (MethodNode methodNode : methodNodes.values()) {
|
List<MethodNode> smethodNodes = searchList(methodPre, MethodNode.class);
|
||||||
|
List<MethodRelation> methodRelationList = searchList(methodRelationPre, MethodRelation.class);
|
||||||
|
for (MethodNode methodNode : smethodNodes) {
|
||||||
if (methodNode.getName().toLowerCase().contains(question.toLowerCase())) {
|
if (methodNode.getName().toLowerCase().contains(question.toLowerCase())) {
|
||||||
String id = methodNode.getId();
|
String id = methodNode.getId();
|
||||||
Optional<MethodRelation> relations = methodRelations.values().stream().filter(methodRelation -> methodRelation.getTargetId().equals(id)).findFirst();
|
Optional<MethodRelation> relations = methodRelationList.stream().filter(methodRelation -> methodRelation.getTargetId().equals(id)).findFirst();
|
||||||
MethodRelation relation = null;
|
MethodRelation relation = null;
|
||||||
if (relations.isPresent()) {
|
if (relations.isPresent()) {
|
||||||
relation = relations.get();
|
relation = relations.get();
|
||||||
@ -244,7 +260,8 @@ public class RedisBase implements GraphService {
|
|||||||
@Override
|
@Override
|
||||||
public List<String> getCondidates(String question) {
|
public List<String> getCondidates(String question) {
|
||||||
List<String> methodInfos = new ArrayList<>();
|
List<String> methodInfos = new ArrayList<>();
|
||||||
for (MethodNode methodNode : methodNodes.values()) {
|
List<MethodNode> smethodNodes = searchList(methodPre, MethodNode.class);
|
||||||
|
for (MethodNode methodNode : smethodNodes) {
|
||||||
if (methodNode.getName().toLowerCase().contains(question.toLowerCase())) {
|
if (methodNode.getName().toLowerCase().contains(question.toLowerCase())) {
|
||||||
if (!methodInfos.contains(methodNode.getName())) {
|
if (!methodInfos.contains(methodNode.getName())) {
|
||||||
methodInfos.add(methodNode.getName());
|
methodInfos.add(methodNode.getName());
|
||||||
@ -260,11 +277,12 @@ public class RedisBase implements GraphService {
|
|||||||
@Override
|
@Override
|
||||||
public List<ExceptionInfo> getExceptionInfos(String exceptionId) {
|
public List<ExceptionInfo> getExceptionInfos(String exceptionId) {
|
||||||
List<ExceptionInfo> exceptionInfos = new ArrayList<>();
|
List<ExceptionInfo> exceptionInfos = new ArrayList<>();
|
||||||
for (ExceptionRelation relation : exceptionRelations.values()) {
|
List<ExceptionRelation> sexceptionRelations = searchList(exceptionRelationPre, ExceptionRelation.class);
|
||||||
|
for (ExceptionRelation relation : sexceptionRelations) {
|
||||||
if (relation.getTargetId().equals(exceptionId)) {
|
if (relation.getTargetId().equals(exceptionId)) {
|
||||||
String sourceMethodId = relation.getSourceId();
|
String sourceMethodId = relation.getSourceId();
|
||||||
MethodNode methodNode = methodNodes.get(sourceMethodId);
|
MethodNode methodNode = query(methodPre + sourceMethodId, MethodNode.class);
|
||||||
ExceptionNode exceptionNode = exceptions.get(exceptionId);
|
ExceptionNode exceptionNode = query(exceptionPre + exceptionId, ExceptionNode.class);
|
||||||
|
|
||||||
ExceptionInfo exceptionInfo = new ExceptionInfo();
|
ExceptionInfo exceptionInfo = new ExceptionInfo();
|
||||||
exceptionInfo.setId(exceptionNode.getId());
|
exceptionInfo.setId(exceptionNode.getId());
|
||||||
@ -285,10 +303,11 @@ public class RedisBase implements GraphService {
|
|||||||
@Override
|
@Override
|
||||||
public List<MethodInfo> getChildren(String methodId) {
|
public List<MethodInfo> getChildren(String methodId) {
|
||||||
List<MethodInfo> methodInfos = new ArrayList<>();
|
List<MethodInfo> methodInfos = new ArrayList<>();
|
||||||
for (MethodRelation methodRelation : methodRelations.values()) {
|
List<MethodRelation> methodRelationList = searchList(methodRelationPre, MethodRelation.class);
|
||||||
|
for (MethodRelation methodRelation : methodRelationList) {
|
||||||
if (methodRelation.getSourceId().equals(methodId)) {
|
if (methodRelation.getSourceId().equals(methodId)) {
|
||||||
String targetMethodId = methodRelation.getTargetId();
|
String targetMethodId = methodRelation.getTargetId();
|
||||||
MethodNode methodNode = methodNodes.get(targetMethodId);
|
MethodNode methodNode = query(methodPre + targetMethodId, MethodNode.class);
|
||||||
MethodInfo methodInfo = new MethodInfo();
|
MethodInfo methodInfo = new MethodInfo();
|
||||||
methodInfo.setId(methodNode.getId());
|
methodInfo.setId(methodNode.getId());
|
||||||
methodInfo.setName(methodNode.getName());
|
methodInfo.setName(methodNode.getName());
|
||||||
@ -338,14 +357,16 @@ public class RedisBase implements GraphService {
|
|||||||
@Override
|
@Override
|
||||||
public MethodInfo getTree(String methodId) {
|
public MethodInfo getTree(String methodId) {
|
||||||
MethodInfo rootInfo = new MethodInfo();
|
MethodInfo rootInfo = new MethodInfo();
|
||||||
MethodNode methodNode = methodNodes.get(methodId);
|
MethodNode methodNode = query(methodPre + methodId, MethodNode.class);
|
||||||
rootInfo.setId(methodNode.getId());
|
rootInfo.setId(methodNode.getId());
|
||||||
rootInfo.setName(methodNode.getName());
|
rootInfo.setName(methodNode.getName());
|
||||||
rootInfo.setClassName(methodNode.getClassName());
|
rootInfo.setClassName(methodNode.getClassName());
|
||||||
rootInfo.setMethodName(methodNode.getMethodName());
|
rootInfo.setMethodName(methodNode.getMethodName());
|
||||||
rootInfo.setMethodType(methodNode.getMethodType());
|
rootInfo.setMethodType(methodNode.getMethodType());
|
||||||
rootInfo.setRouteName(methodNode.getRouteName());
|
rootInfo.setRouteName(methodNode.getRouteName());
|
||||||
MethodRelation methodRelation = methodRelations.values().stream().filter(relation -> relation.getTargetId().equals(methodId)).findFirst().get();
|
List<MethodRelation> methodRelationList = searchList(methodRelationPre, MethodRelation.class);
|
||||||
|
|
||||||
|
MethodRelation methodRelation = methodRelationList.stream().filter(relation -> relation.getTargetId().equals(methodId)).findFirst().get();
|
||||||
rootInfo.setValue(methodRelation.getAvgRunTime());
|
rootInfo.setValue(methodRelation.getAvgRunTime());
|
||||||
rootInfo.setAvgRunTime(methodRelation.getAvgRunTime());
|
rootInfo.setAvgRunTime(methodRelation.getAvgRunTime());
|
||||||
rootInfo.setMaxRunTime(methodRelation.getMaxRunTime());
|
rootInfo.setMaxRunTime(methodRelation.getMaxRunTime());
|
||||||
@ -374,4 +395,42 @@ public class RedisBase implements GraphService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void insert(String key, Object o) {
|
||||||
|
redisTemplate.opsForValue().set(key, toJson(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T query(String key, Class<T> c) {
|
||||||
|
Object o = redisTemplate.opsForValue().get(key);
|
||||||
|
if (o == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return toObject(o.toString(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> List<T> searchList(String pre, Class<T> c) {
|
||||||
|
List<T> res = new ArrayList<>();
|
||||||
|
Set<String> keys = redisTemplate.keys(pre + "*");
|
||||||
|
if (keys == null || keys.size() == 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
List<String> objects = redisTemplate.opsForValue().multiGet(keys);
|
||||||
|
if (objects == null || objects.size() == 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
for (Object object : objects) {
|
||||||
|
res.add(toObject(object.toString(), c));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String toJson(Object o) {
|
||||||
|
return JSON.toJSONString(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T toObject(String str, Class<T> c) {
|
||||||
|
return JSONObject.parseObject(str, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package cn.langpy.kotime.util;
|
|||||||
import cn.langpy.kotime.config.DefaultConfig;
|
import cn.langpy.kotime.config.DefaultConfig;
|
||||||
import cn.langpy.kotime.handler.InvokedHandler;
|
import cn.langpy.kotime.handler.InvokedHandler;
|
||||||
import cn.langpy.kotime.service.GraphService;
|
import cn.langpy.kotime.service.GraphService;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -16,6 +17,7 @@ public class Context {
|
|||||||
private static DefaultConfig config;
|
private static DefaultConfig config;
|
||||||
private static List<InvokedHandler> invokedHandlers;
|
private static List<InvokedHandler> invokedHandlers;
|
||||||
private static DataSource dataSource;
|
private static DataSource dataSource;
|
||||||
|
private static StringRedisTemplate stringRedisTemplate;
|
||||||
private static GraphService saver;
|
private static GraphService saver;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@ -26,6 +28,14 @@ public class Context {
|
|||||||
invokedHandlers = new ArrayList<>();
|
invokedHandlers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static StringRedisTemplate getStringRedisTemplate() {
|
||||||
|
return stringRedisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
|
||||||
|
Context.stringRedisTemplate = stringRedisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
public static void setConfig(DefaultConfig koTimeConfig) {
|
public static void setConfig(DefaultConfig koTimeConfig) {
|
||||||
config = koTimeConfig;
|
config = koTimeConfig;
|
||||||
}
|
}
|
||||||
@ -50,6 +60,7 @@ public class Context {
|
|||||||
Context.dataSource = dataSource;
|
Context.dataSource = dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static GraphService getSaver() {
|
public static GraphService getSaver() {
|
||||||
return saver;
|
return saver;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package cn.langpy.kotime.util;
|
package cn.langpy.kotime.util;
|
||||||
|
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -8,6 +11,8 @@ public class KoUtil {
|
|||||||
|
|
||||||
private final static List<Integer> choices = randomSecretIndexs();
|
private final static List<Integer> choices = randomSecretIndexs();
|
||||||
|
|
||||||
|
private static Map<String,Object> caches = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nothing to introduce for this, that everyone knows!
|
* nothing to introduce for this, that everyone knows!
|
||||||
*/
|
*/
|
||||||
@ -39,24 +44,29 @@ public class KoUtil {
|
|||||||
* set a Datasource for saving of kotime data
|
* set a Datasource for saving of kotime data
|
||||||
* note: this Datasource will not affect project's datasource
|
* note: this Datasource will not affect project's datasource
|
||||||
*/
|
*/
|
||||||
public void setDataSource(DataSource dataSource) {
|
public static void setDataSource(DataSource dataSource) {
|
||||||
//I will write it, and do not rush me!
|
caches.put("dataSource",dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataSource getDataSource() {
|
||||||
|
return (DataSource)caches.get("dataSource");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set a RedisTemplate for saving of kotime data
|
* set a RedisTemplate for saving of kotime data
|
||||||
* note: you can choose one between setRedisTemplate and setJedisPool to save data
|
* note: you can choose one between setRedisTemplate and setJedisPool to save data
|
||||||
*/
|
*/
|
||||||
public void setRedisTemplate() {
|
public static void setStringRedisTemplate(RedisTemplate redisTemplate) {
|
||||||
//I will write it, and do not rush me!
|
caches.put("redisTemplate",redisTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static StringRedisTemplate getStringRedisTemplate() {
|
||||||
* set a RedisTemplate for saving of kotime data
|
return (StringRedisTemplate)caches.get("redisTemplate");
|
||||||
* note: you can choose one between setRedisTemplate and setJedisPool to save data
|
}
|
||||||
*/
|
|
||||||
public void setJedisPool() {
|
|
||||||
//I will write it, and do not rush me!
|
public static void clearCaches() {
|
||||||
|
caches.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user