You've already forked FrameTour-BE
refactor(stats): 移除统计追踪模块相关代码
- 删除 StatsBiz 业务类 - 移除 TraceController 控制器及其接口实现 - 删除 AddTraceReq 数据传输对象 - 移除 StatsEntity 和 StatsRecordEntity 实体类 - 移除 StatsInterceptor 拦截器 - 删除 StatsMapper 和 StatsRecordMapper 数据访问接口 - 移除 StatsService 服务接口及 StatsServiceImpl 实现类 - 删除 StatsUtil 工具类
This commit is contained in:
@@ -3,7 +3,6 @@ package com.ycwl.basic.config;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import com.ycwl.basic.interceptor.AuthInterceptor;
|
import com.ycwl.basic.interceptor.AuthInterceptor;
|
||||||
import com.ycwl.basic.stats.interceptor.StatsInterceptor;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -25,11 +24,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebMvcConfig implements WebMvcConfigurer {
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AuthInterceptor authInterceptor;
|
private AuthInterceptor authInterceptor;
|
||||||
@Autowired
|
|
||||||
private StatsInterceptor statsInterceptor;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
@@ -37,8 +33,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
|||||||
// 拦截除指定接口外的所有请求,通过判断 注解 来决定是否需要做登录验证
|
// 拦截除指定接口外的所有请求,通过判断 注解 来决定是否需要做登录验证
|
||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/api-docs", "/doc.html/**", "/error", "/");
|
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/api-docs", "/doc.html/**", "/error", "/");
|
||||||
registry.addInterceptor(statsInterceptor)
|
|
||||||
.addPathPatterns("/api/mobile/**");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.biz;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class StatsBiz {
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.controller;
|
|
||||||
|
|
||||||
import com.ycwl.basic.annotation.IgnoreToken;
|
|
||||||
import com.ycwl.basic.stats.dto.AddTraceReq;
|
|
||||||
import com.ycwl.basic.stats.service.StatsService;
|
|
||||||
import com.ycwl.basic.stats.util.StatsUtil;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/trace/v1")
|
|
||||||
public class TraceController {
|
|
||||||
@Autowired
|
|
||||||
private StatsService statsService;
|
|
||||||
@IgnoreToken
|
|
||||||
@PostMapping("/start")
|
|
||||||
public void startTrace(HttpServletRequest request, HttpServletResponse response) {
|
|
||||||
String traceId = request.getHeader("traceId");
|
|
||||||
if (traceId == null || traceId.isEmpty()) {
|
|
||||||
traceId = StatsUtil.createUuid();
|
|
||||||
response.setHeader("Set-TraceId", traceId);
|
|
||||||
}
|
|
||||||
statsService.addStats(traceId, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@IgnoreToken
|
|
||||||
@PostMapping("/add")
|
|
||||||
public void addTrace(HttpServletRequest request, HttpServletResponse response, @RequestBody AddTraceReq req) {
|
|
||||||
String traceId = request.getHeader("traceId");
|
|
||||||
if (traceId == null || traceId.isEmpty()) {
|
|
||||||
traceId = StatsUtil.createUuid();
|
|
||||||
response.setHeader("Set-TraceId", traceId);
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(req.getParams())) {
|
|
||||||
req.setParams(null);
|
|
||||||
}
|
|
||||||
statsService.addRecord(traceId, req.getAction(), req.getIdentifier(), req.getParams());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class AddTraceReq {
|
|
||||||
private String action;
|
|
||||||
private String identifier;
|
|
||||||
private String params;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.entity;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("t_stats")
|
|
||||||
public class StatsEntity {
|
|
||||||
@TableId(value = "id", type = IdType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
private String traceId;
|
|
||||||
private Long memberId;
|
|
||||||
private Date createTime;
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.entity;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("t_stats_record")
|
|
||||||
public class StatsRecordEntity {
|
|
||||||
@TableId(value = "id", type = IdType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
private String traceId;
|
|
||||||
private String action;
|
|
||||||
private String identifier;
|
|
||||||
private String params;
|
|
||||||
private Date createTime;
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.interceptor;
|
|
||||||
|
|
||||||
import com.ycwl.basic.annotation.IgnoreLogReq;
|
|
||||||
import com.ycwl.basic.constant.BaseContextHandler;
|
|
||||||
import com.ycwl.basic.stats.service.StatsService;
|
|
||||||
import com.ycwl.basic.stats.util.StatsUtil;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Lazy;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.method.HandlerMethod;
|
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class StatsInterceptor implements HandlerInterceptor {
|
|
||||||
@Lazy
|
|
||||||
@Autowired
|
|
||||||
private StatsService statsService;
|
|
||||||
|
|
||||||
// 在请求处理前执行
|
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
||||||
if (!(handler instanceof HandlerMethod)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
||||||
// request.setAttribute("startTime", System.currentTimeMillis());
|
|
||||||
// String requestURI = request.getRequestURI();
|
|
||||||
// String method = request.getMethod();
|
|
||||||
String traceId = request.getHeader("traceId");
|
|
||||||
if (StringUtils.isEmpty(traceId)) {
|
|
||||||
return true;
|
|
||||||
// traceId = StatsUtil.createUuid();
|
|
||||||
// response.setHeader("Set-TraceId", traceId);
|
|
||||||
// statsService.addStats(traceId, null);
|
|
||||||
}
|
|
||||||
// if (handlerMethod.getMethodAnnotation(IgnoreLogReq.class) == null) {
|
|
||||||
// statsService.addRecord(traceId, "REQUEST",method + " " + requestURI, null);
|
|
||||||
// }
|
|
||||||
if (StringUtils.isNotEmpty(BaseContextHandler.getUserId())) {
|
|
||||||
statsService.updateStats(traceId, Long.valueOf(BaseContextHandler.getUserId()));
|
|
||||||
}
|
|
||||||
// 返回 true 继续后续流程,false 终止请求
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 控制器方法执行后,渲染视图前
|
|
||||||
@Override
|
|
||||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求完全完成后执行(无论是否异常)
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
||||||
// long startTime = (Long) request.getAttribute("startTime");
|
|
||||||
// long endTime = System.currentTimeMillis();
|
|
||||||
// System.out.println("【AfterCompletion】请求结束,耗时:" + (endTime - startTime) + "ms");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.mapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import com.ycwl.basic.stats.entity.StatsEntity;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface StatsMapper extends BaseMapper<StatsEntity> {
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.mapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import com.ycwl.basic.stats.entity.StatsRecordEntity;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface StatsRecordMapper extends BaseMapper<StatsRecordEntity> {
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.service;
|
|
||||||
|
|
||||||
public interface StatsService {
|
|
||||||
void addStats(String traceId, Long memberId);
|
|
||||||
|
|
||||||
void updateStats(String traceId, Long memberId);
|
|
||||||
|
|
||||||
void addRecord(String traceId, String action, String identifier, String params);
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.service.impl;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
||||||
import com.ycwl.basic.stats.entity.StatsEntity;
|
|
||||||
import com.ycwl.basic.stats.entity.StatsRecordEntity;
|
|
||||||
import com.ycwl.basic.stats.mapper.StatsMapper;
|
|
||||||
import com.ycwl.basic.stats.mapper.StatsRecordMapper;
|
|
||||||
import com.ycwl.basic.stats.service.StatsService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Lazy;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class StatsServiceImpl implements StatsService {
|
|
||||||
@Lazy
|
|
||||||
@Autowired
|
|
||||||
private StatsMapper statsMapper;
|
|
||||||
|
|
||||||
@Lazy
|
|
||||||
@Autowired
|
|
||||||
private StatsRecordMapper statsRecordMapper;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addStats(String traceId, Long memberId) {
|
|
||||||
StatsEntity entity = new StatsEntity();
|
|
||||||
entity.setTraceId(traceId);
|
|
||||||
entity.setMemberId(memberId);
|
|
||||||
entity.setCreateTime(new Date());
|
|
||||||
statsMapper.insert(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateStats(String traceId, Long memberId) {
|
|
||||||
StatsEntity entity = new StatsEntity();
|
|
||||||
entity.setMemberId(memberId);
|
|
||||||
LambdaQueryWrapper<StatsEntity> queryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
queryWrapper.eq(StatsEntity::getTraceId, traceId);
|
|
||||||
statsMapper.update(entity, queryWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addRecord(String traceId, String action, String identifier, String params) {
|
|
||||||
StatsRecordEntity entity = new StatsRecordEntity();
|
|
||||||
entity.setTraceId(traceId);
|
|
||||||
entity.setAction(action);
|
|
||||||
entity.setIdentifier(identifier);
|
|
||||||
entity.setParams(params);
|
|
||||||
entity.setCreateTime(new Date());
|
|
||||||
statsRecordMapper.insert(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.ycwl.basic.stats.util;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class StatsUtil {
|
|
||||||
public static String createUuid() {
|
|
||||||
return UUID.randomUUID().toString().replace("-", "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user