add controller's routes

This commit is contained in:
huoyo 2022-03-03 20:59:18 +08:00
parent 9dfa6389b5
commit a83ea8c9e5
9 changed files with 138 additions and 10 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.langpy</groupId>
<artifactId>ko-time</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
<name>koTime</name>
<description>koTime</description>
<licenses>

View File

@ -5,6 +5,7 @@ import cn.langpy.kotime.service.GraphService;
import cn.langpy.kotime.util.Common;
import cn.langpy.kotime.util.Context;
import cn.langpy.kotime.util.MethodType;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.util.*;
@ -39,6 +40,12 @@ public class MemoryBase implements GraphService {
}
if (!methodNodes.containsKey(methodNode.getId())) {
methodNodes.put(methodNode.getId(), methodNode);
}else {
if (methodNode.getMethodType()==MethodType.Controller && !StringUtils.isEmpty(methodNode.getRouteName())) {
MethodNode controller = methodNodes.get(methodNode.getId());
controller.setRouteName(methodNode.getRouteName());
methodNodes.put(methodNode.getId(), controller);
}
}
}
@ -137,6 +144,7 @@ public class MemoryBase implements GraphService {
methodInfo.setClassName(methodNode.getClassName());
methodInfo.setMethodName(methodNode.getMethodName());
methodInfo.setMethodType(methodNode.getMethodType());
methodInfo.setRouteName(methodNode.getRouteName());
methodInfo.setValue(relation.getAvgRunTime());
methodInfo.setAvgRunTime(relation.getAvgRunTime());
methodInfo.setMaxRunTime(relation.getMaxRunTime());
@ -168,6 +176,7 @@ public class MemoryBase implements GraphService {
methodInfo.setClassName(methodNode.getClassName());
methodInfo.setMethodName(methodNode.getMethodName());
methodInfo.setMethodType(methodNode.getMethodType());
methodInfo.setRouteName(methodNode.getRouteName());
methodInfo.setValue(relation.getAvgRunTime());
methodInfo.setAvgRunTime(relation.getAvgRunTime());
methodInfo.setMaxRunTime(relation.getMaxRunTime());
@ -233,6 +242,7 @@ public class MemoryBase implements GraphService {
methodInfo.setName(methodNode.getName());
methodInfo.setClassName(methodNode.getClassName());
methodInfo.setMethodName(methodNode.getMethodName());
methodInfo.setRouteName(methodNode.getRouteName());
methodInfo.setMethodType(methodNode.getMethodType());
methodInfo.setValue(methodRelation.getAvgRunTime());
methodInfo.setAvgRunTime(methodRelation.getAvgRunTime());
@ -282,6 +292,7 @@ public class MemoryBase implements GraphService {
rootInfo.setClassName(methodNode.getClassName());
rootInfo.setMethodName(methodNode.getMethodName());
rootInfo.setMethodType(methodNode.getMethodType());
rootInfo.setRouteName(methodNode.getRouteName());
MethodRelation methodRelation = methodRelations.values().stream().filter(relation -> relation.getTargetId().equals(methodId)).findFirst().get();
rootInfo.setValue(methodRelation.getAvgRunTime());
rootInfo.setAvgRunTime(methodRelation.getAvgRunTime());

View File

@ -12,6 +12,7 @@ public class MethodInfo implements Comparable<MethodInfo> {
private String name;
private String className;
private String methodName;
private String routeName;
private Double value = 0.0;
private Double avgRunTime = 0.0;
private Double maxRunTime = 0.0;
@ -26,6 +27,14 @@ public class MethodInfo implements Comparable<MethodInfo> {
return -this.avgRunTime.compareTo(o.getAvgRunTime());
}
public String getRouteName() {
return routeName;
}
public void setRouteName(String routeName) {
this.routeName = routeName;
}
public List<ExceptionInfo> getExceptions() {
return exceptions;
}

View File

@ -10,6 +10,7 @@ public class MethodNode{
private String name;
private String className;
private String methodName;
private String routeName;
private Double value = 0.0;
private MethodType methodType;
@ -74,6 +75,14 @@ public class MethodNode{
this.methodType = methodType;
}
public String getRouteName() {
return routeName;
}
public void setRouteName(String routeName) {
this.routeName = routeName;
}
@Override
public String toString() {
return "RunTimeNode{" +

View File

@ -20,23 +20,24 @@ public class InvokeService {
Stack<String> stack = MethodStack.get();
if (null==stack) {
MethodNode parent = new MethodNode();
parent.setId("com.langpy.kotime.Cotroller.dispatch");
parent.setClassName("Cotroller");
parent.setId("com.langpy.kotime.Controller.dispatch");
parent.setClassName("Controller");
parent.setMethodName("dispatch");
parent.setName("Cotroller.dispatch");
parent.setMethodType(MethodType.Others);
parent.setName("Controller.dispatch");
parent.setMethodType(MethodType.Dispatcher);
return parent;
}
String classMethod = stack.peek();
String[] classMethodSplit = classMethod.split("#");
String parentClassName = classMethodSplit[0];
String parentMothodName = classMethodSplit[1];
String parentMothodType = classMethodSplit[2];
MethodNode parent = new MethodNode();
parent.setId(parentClassName + "." + parentMothodName);
parent.setClassName(parentClassName);
parent.setMethodName(parentMothodName);
parent.setName(parentClassName.substring(parentClassName.lastIndexOf(".") + 1) + "." + parentMothodName);
parent.setMethodType(Common.getMethodType(parentClassName));
parent.setMethodType(Common.getMethodType(parentMothodType));
return parent;
}
@ -52,6 +53,9 @@ public class InvokeService {
runTime = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
current.setValue(runTime);
current.setMethodType(Common.getMethodType(pjp));
if (current.getMethodType()==MethodType.Controller) {
current.setRouteName(Common.getRoute(pjp));
}
return current;
}
}

View File

@ -5,8 +5,9 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Method;
import java.util.logging.Logger;
/**
@ -15,6 +16,83 @@ import java.util.logging.Logger;
public class Common {
public static Logger log = Logger.getLogger(Common.class.toString());
public static String getRoute(MethodInvocation pjp) {
Class<?> targetClass = pjp.getThis().getClass();
String[] classRoute = getRouteValue(targetClass);
if (classRoute == null) {
return null;
}
StringBuilder routes = new StringBuilder(classRoute[0]);
String[] methodRoute = getRouteValue(pjp.getMethod());
if (methodRoute[0].startsWith("/")) {
routes.append(methodRoute[0]);
} else {
routes.append("/" + methodRoute[0]);
}
return routes.toString();
}
private static String[] getRouteValue(Class<?> targetClass) {
String[] methodRoute = null;
RequestMapping methodAnnotationRequest = targetClass.getAnnotation(RequestMapping.class);
if (methodAnnotationRequest == null) {
PostMapping methodAnnotationPost = targetClass.getAnnotation(PostMapping.class);
if (methodAnnotationPost == null) {
GetMapping methodAnnotationGet = targetClass.getAnnotation(GetMapping.class);
if (methodAnnotationGet == null) {
PutMapping methodAnnotationPut = targetClass.getAnnotation(PutMapping.class);
if (methodAnnotationPut == null) {
DeleteMapping methodAnnotationDelete = targetClass.getAnnotation(DeleteMapping.class);
if (methodAnnotationDelete == null) {
return null;
} else {
methodRoute = methodAnnotationDelete.value();
}
} else {
methodRoute = methodAnnotationPut.value();
}
} else {
methodRoute = methodAnnotationGet.value();
}
} else {
methodRoute = methodAnnotationPost.value();
}
} else {
methodRoute = methodAnnotationRequest.value();
}
return methodRoute;
}
private static String[] getRouteValue(Method method) {
String[] methodRoute = null;
RequestMapping methodAnnotationRequest = method.getAnnotation(RequestMapping.class);
if (methodAnnotationRequest == null) {
PostMapping methodAnnotationPost = method.getAnnotation(PostMapping.class);
if (methodAnnotationPost == null) {
GetMapping methodAnnotationGet = method.getAnnotation(GetMapping.class);
if (methodAnnotationGet == null) {
PutMapping methodAnnotationPut = method.getAnnotation(PutMapping.class);
if (methodAnnotationPut == null) {
DeleteMapping methodAnnotationDelete = method.getAnnotation(DeleteMapping.class);
if (methodAnnotationDelete == null) {
return null;
} else {
methodRoute = methodAnnotationDelete.value();
}
} else {
methodRoute = methodAnnotationPut.value();
}
} else {
methodRoute = methodAnnotationGet.value();
}
} else {
methodRoute = methodAnnotationPost.value();
}
} else {
methodRoute = methodAnnotationRequest.value();
}
return methodRoute;
}
public static MethodType getMethodType(MethodInvocation pjp) {
Class<?> targetClass = pjp.getThis().getClass();
@ -36,6 +114,7 @@ public class Common {
return MethodType.Others;
}
}
public static MethodType getMethodType(String className) {
className = className.toLowerCase();
if (className.contains("controller")) {

View File

@ -21,7 +21,8 @@ public class MethodStack {
}else {
queue = threadMethods.get();
}
queue.add(className+"#"+methodName);
MethodType methodType = Common.getMethodType(pjp);
queue.add(className+"#"+methodName+"#"+methodType);
threadMethods.set(queue);
}

View File

@ -7,5 +7,6 @@ public enum MethodType {
Controller,
Service,
Dao,
Dispatcher,
Others;
}

View File

@ -121,9 +121,16 @@
let className = data[i]['className'];
let methodName = data[i]['methodName'];
let avgRunTime = data[i]['avgRunTime'];
let methodType = data[i]['methodType'];
let routeName = data[i]['routeName'];
let apiId = className+"."+methodName;
let color = avgRunTime>globalThreshold?'danger':'success';
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
if (methodType=='Controller' && routeName!=null && routeName!='') {
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp(<span style='font-size: 14px;font-weight: 430;color:#032b11'>"+routeName+"</span>)&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
}else{
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
}
};
element.innerHTML = html;
});
@ -261,9 +268,16 @@
let className = data[i]['className'];
let methodName = data[i]['methodName'];
let avgRunTime = data[i]['avgRunTime'];
let methodType = data[i]['methodType'];
let routeName = data[i]['routeName'];
let apiId = className+"."+methodName;
let color = avgRunTime>globalThreshold?'danger':'success';
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
if (methodType=='Controller' && routeName!=null && routeName!='') {
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp(<span style='font-size: 14px;font-weight: 430;color:#032b11'>"+routeName+"</span>)&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
}else{
html += "<li onclick=\"showMethods('"+apiId+"')\" style='color: #333;font-weight: 400;font-size: 14px;' id=\""+apiId+"\">"+ className+"#<span style='font-size: 16px;font-weight: 500;'>"+methodName+"</span>&nbsp &nbsp<span style='font-size: 10px;' class=\"uk-label uk-label-"+color+"\">平均响应 "+avgRunTime+" 毫秒</span></li>";
}
};
element.innerHTML = html;
});