From a83ea8c9e501cd8a67b96bec6cdbfb75ab71853b Mon Sep 17 00:00:00 2001
From: huoyo <1729913829@qq.com>
Date: Thu, 3 Mar 2022 20:59:18 +0800
Subject: [PATCH] add controller's routes
---
pom.xml | 2 +-
.../cn/langpy/kotime/data/MemoryBase.java | 11 +++
.../cn/langpy/kotime/model/MethodInfo.java | 9 +++
.../cn/langpy/kotime/model/MethodNode.java | 9 +++
.../langpy/kotime/service/InvokeService.java | 14 ++--
.../java/cn/langpy/kotime/util/Common.java | 81 ++++++++++++++++++-
.../cn/langpy/kotime/util/MethodStack.java | 3 +-
.../cn/langpy/kotime/util/MethodType.java | 1 +
src/main/resources/kotime.html | 18 ++++-
9 files changed, 138 insertions(+), 10 deletions(-)
diff --git a/pom.xml b/pom.xml
index 33df3f3..ca8df37 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
cn.langpy
ko-time
- 2.0.6
+ 2.0.7
koTime
koTime
diff --git a/src/main/java/cn/langpy/kotime/data/MemoryBase.java b/src/main/java/cn/langpy/kotime/data/MemoryBase.java
index 997a409..71d13cb 100644
--- a/src/main/java/cn/langpy/kotime/data/MemoryBase.java
+++ b/src/main/java/cn/langpy/kotime/data/MemoryBase.java
@@ -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());
diff --git a/src/main/java/cn/langpy/kotime/model/MethodInfo.java b/src/main/java/cn/langpy/kotime/model/MethodInfo.java
index 09221e7..75a98c5 100644
--- a/src/main/java/cn/langpy/kotime/model/MethodInfo.java
+++ b/src/main/java/cn/langpy/kotime/model/MethodInfo.java
@@ -12,6 +12,7 @@ public class MethodInfo implements Comparable {
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 {
return -this.avgRunTime.compareTo(o.getAvgRunTime());
}
+ public String getRouteName() {
+ return routeName;
+ }
+
+ public void setRouteName(String routeName) {
+ this.routeName = routeName;
+ }
+
public List getExceptions() {
return exceptions;
}
diff --git a/src/main/java/cn/langpy/kotime/model/MethodNode.java b/src/main/java/cn/langpy/kotime/model/MethodNode.java
index 37c215b..5d168c2 100644
--- a/src/main/java/cn/langpy/kotime/model/MethodNode.java
+++ b/src/main/java/cn/langpy/kotime/model/MethodNode.java
@@ -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{" +
diff --git a/src/main/java/cn/langpy/kotime/service/InvokeService.java b/src/main/java/cn/langpy/kotime/service/InvokeService.java
index c99a8be..7d3ee7c 100644
--- a/src/main/java/cn/langpy/kotime/service/InvokeService.java
+++ b/src/main/java/cn/langpy/kotime/service/InvokeService.java
@@ -20,23 +20,24 @@ public class InvokeService {
Stack 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;
}
}
diff --git a/src/main/java/cn/langpy/kotime/util/Common.java b/src/main/java/cn/langpy/kotime/util/Common.java
index 73f0f8c..73f6af2 100644
--- a/src/main/java/cn/langpy/kotime/util/Common.java
+++ b/src/main/java/cn/langpy/kotime/util/Common.java
@@ -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")) {
diff --git a/src/main/java/cn/langpy/kotime/util/MethodStack.java b/src/main/java/cn/langpy/kotime/util/MethodStack.java
index 57471c1..eb42e25 100644
--- a/src/main/java/cn/langpy/kotime/util/MethodStack.java
+++ b/src/main/java/cn/langpy/kotime/util/MethodStack.java
@@ -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);
}
diff --git a/src/main/java/cn/langpy/kotime/util/MethodType.java b/src/main/java/cn/langpy/kotime/util/MethodType.java
index c4ca615..4ac6298 100644
--- a/src/main/java/cn/langpy/kotime/util/MethodType.java
+++ b/src/main/java/cn/langpy/kotime/util/MethodType.java
@@ -7,5 +7,6 @@ public enum MethodType {
Controller,
Service,
Dao,
+ Dispatcher,
Others;
}
diff --git a/src/main/resources/kotime.html b/src/main/resources/kotime.html
index 3076663..ed84139 100644
--- a/src/main/resources/kotime.html
+++ b/src/main/resources/kotime.html
@@ -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 += ""+ className+"#"+methodName+"   平均响应 "+avgRunTime+" 毫秒";
+ if (methodType=='Controller' && routeName!=null && routeName!='') {
+ html += ""+ className+"#"+methodName+" ("+routeName+")   平均响应 "+avgRunTime+" 毫秒";
+ }else{
+ html += ""+ className+"#"+methodName+"   平均响应 "+avgRunTime+" 毫秒";
+ }
};
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 += ""+ className+"#"+methodName+"   平均响应 "+avgRunTime+" 毫秒";
+
+ if (methodType=='Controller' && routeName!=null && routeName!='') {
+ html += ""+ className+"#"+methodName+" ("+routeName+")   平均响应 "+avgRunTime+" 毫秒";
+ }else{
+ html += ""+ className+"#"+methodName+"   平均响应 "+avgRunTime+" 毫秒";
+ }
};
element.innerHTML = html;
});