获取访问日志数据

This commit is contained in:
ycjx 2019-06-23 18:33:34 +08:00
parent b9047e497a
commit 9e5e460b28
12 changed files with 346 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.SystemLogService;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogPageDTO;
import cn.iocoder.mall.admin.application.convert.AccessLogConvert;
import cn.iocoder.mall.admin.application.vo.log.AccessLogPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 16:42
*/
@RestController
@RequestMapping("admins/system/logs")
@Api("系统日志")
public class SystemLogController {
@Reference(validation = "true", version = "${dubbo.provider.AdminAccessLogService.version}")
private SystemLogService systemLogService;
@GetMapping("access/page")
@ApiOperation(value = "访问日志分页")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", example = "1"),
@ApiImplicitParam(name = "pageNo", value = "页码,从 1 开始", example = "1"),
@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
})
public CommonResult<AccessLogPageVo> page(@RequestParam(value = "userId", required = false) Integer userId,
@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
AccessLogPageDTO accessLogPageDTO = new AccessLogPageDTO().setUserId(userId)
.setPageNo(pageNo).setPageSize(pageSize);
// 查询分页
AccessLogPageBO result = systemLogService.getAccessLogPage(accessLogPageDTO);
// 转换结果
return success(AccessLogConvert.INSTANCE.convert(result));
}
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.mall.admin.application.convert;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.admin.api.bo.admin.AdminBO;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogBO;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.application.vo.log.AccessLogPageVo;
import cn.iocoder.mall.admin.application.vo.log.AccessLogVo;
import cn.iocoder.mall.admin.dataobject.AccessLogDO;
import cn.iocoder.mall.admin.dataobject.AdminDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 17:36
*/
@Mapper
public interface AccessLogConvert {
AccessLogConvert INSTANCE = Mappers.getMapper(AccessLogConvert.class);
@Mappings({})
AccessLogPageVo convert(AccessLogPageBO result);
@Mappings({})
AccessLogVo convert(AccessLogBO result);
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.mall.admin.application.vo.log;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 17:03
*/
@ApiModel("访问日志分页 VO")
@Data
@Accessors(chain = true)
public class AccessLogPageVo {
@ApiModelProperty(value = "访问数据")
private List<AccessLogVo> list;
@ApiModelProperty(value = "访问总数")
private Integer total;
}

View File

@ -0,0 +1,58 @@
package cn.iocoder.mall.admin.application.vo.log;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* @author:yuxj
* @descriptio
* @create:2019-06-23 17:04
*/
@ApiModel("访问日志 VO")
@Data
@Accessors(chain = true)
public class AccessLogVo {
@ApiModelProperty(value = "链路追踪编号", required = true, example = "1")
private String traceId;
@ApiModelProperty(value = "用户编号", required = true, example = "1")
private Integer userId;
@ApiModelProperty(value = "用户类型", required = true, example = "1")
private Integer userType;
@ApiModelProperty(value = "应用名", required = true, example = "1")
private String applicationName;
@ApiModelProperty(value = "访问地址", required = true, example = "1")
private String uri;
@ApiModelProperty(value = "请求参数", required = true, example = "1")
private String queryString;
@ApiModelProperty(value = "http 请求方法", required = true, example = "1")
private String method;
@ApiModelProperty(value = "User-Agent ", required = true, example = "1")
private String userAgent;
@ApiModelProperty(value = "ip", required = true, example = "1")
private String ip;
@ApiModelProperty(value = "请求时间", required = true, example = "1")
private Date startTime;
@ApiModelProperty(value = "响应时长", required = true, example = "1")
private Integer responseTime;
@ApiModelProperty(value = "错误码", required = true, example = "1")
private Integer errorCode;
}

View File

@ -1,6 +1,9 @@
package cn.iocoder.mall.admin.api; package cn.iocoder.mall.admin.api;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogPageDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO;
/** /**
@ -14,4 +17,5 @@ public interface SystemLogService {
void addExceptionLog(ExceptionLogAddDTO exceptionLogAddDTO); void addExceptionLog(ExceptionLogAddDTO exceptionLogAddDTO);
AccessLogPageBO getAccessLogPage(AccessLogPageDTO accessLogPageDTO);
} }

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.admin.api.bo.systemlog;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 17:26
*/
@Data
@Accessors(chain = true)
public class AccessLogBO implements Serializable {
private String traceId;
private Integer userId;
private Integer userType;
private String applicationName;
private String uri;
private String queryString;
private String method;
private String userAgent;
private String ip;
private Date startTime;
private Integer responseTime;
private Integer errorCode;
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.mall.admin.api.bo.systemlog;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.List;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 17:26
*/
@Data
@Accessors(chain = true)
public class AccessLogPageBO implements Serializable {
/**
* 日志数组
*/
private List<AccessLogBO> list;
/**
* 总量
*/
private Integer total;
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.mall.admin.api.dto.systemlog;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 16:53
*/
@Data
@Accessors(chain = true)
public class AccessLogPageDTO {
/**
* 用户id
*/
private Integer userId;
@NotNull(message = "页码不能为空")
private Integer pageNo;
@NotNull(message = "每页条数不能为空")
private Integer pageSize;
}

View File

@ -1,10 +1,15 @@
package cn.iocoder.mall.admin.convert; package cn.iocoder.mall.admin.convert;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogBO;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO;
import cn.iocoder.mall.admin.dataobject.AccessLogDO; import cn.iocoder.mall.admin.dataobject.AccessLogDO;
import cn.iocoder.mall.admin.dataobject.ExceptionLogDO; import cn.iocoder.mall.admin.dataobject.ExceptionLogDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -19,4 +24,9 @@ public interface AccessLogConvert {
@Mappings({}) @Mappings({})
ExceptionLogDO convert(ExceptionLogAddDTO exceptionLogAddDTO); ExceptionLogDO convert(ExceptionLogAddDTO exceptionLogAddDTO);
@Mappings({
@Mapping(source = "records", target = "list"),
})
PageResult<AccessLogBO> convert(IPage<AccessLogDO> page);
} }

View File

@ -1,10 +1,21 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.common.framework.mybatis.QueryWrapperX;
import cn.iocoder.mall.admin.api.dto.admin.AdminPageDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogPageDTO;
import cn.iocoder.mall.admin.dataobject.AccessLogDO; import cn.iocoder.mall.admin.dataobject.AccessLogDO;
import cn.iocoder.mall.admin.dataobject.AdminDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AccessLogMapper extends BaseMapper<AccessLogDO> { public interface AccessLogMapper extends BaseMapper<AccessLogDO> {
default IPage<AccessLogDO> selectPage(AccessLogPageDTO accessLogPageDTO) {
return selectPage(new Page<>(accessLogPageDTO.getPageNo(), accessLogPageDTO.getPageSize()),
new QueryWrapperX<AccessLogDO>().eqIfPresent("user_id", accessLogPageDTO.getUserId()));
}
} }

View File

@ -1,8 +1,12 @@
package cn.iocoder.mall.admin.service; package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.util.StringUtil; import cn.iocoder.common.framework.util.StringUtil;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.admin.api.SystemLogService; import cn.iocoder.mall.admin.api.SystemLogService;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogBO;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogAddDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogPageDTO;
import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO; import cn.iocoder.mall.admin.api.dto.systemlog.ExceptionLogAddDTO;
import cn.iocoder.mall.admin.convert.AccessLogConvert; import cn.iocoder.mall.admin.convert.AccessLogConvert;
import cn.iocoder.mall.admin.dao.AccessLogMapper; import cn.iocoder.mall.admin.dao.AccessLogMapper;
@ -76,4 +80,15 @@ public class SystemLogServiceImpl implements SystemLogService {
exceptionLogMapper.insert(exceptionLog); exceptionLogMapper.insert(exceptionLog);
} }
@Override
@SuppressWarnings("Duplicates")
public AccessLogPageBO getAccessLogPage(AccessLogPageDTO accessLogPageDTO) {
AccessLogPageBO accessLogPageBO = new AccessLogPageBO();
PageResult<AccessLogBO> accessLogPageBOPageResult = AccessLogConvert.INSTANCE.convert(
accessLogMapper.selectPage(accessLogPageDTO));
accessLogPageBO.setList(accessLogPageBOPageResult.getList());
accessLogPageBO.setTotal(accessLogPageBOPageResult.getTotal());
return accessLogPageBO;
}
} }

View File

@ -0,0 +1,35 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.mall.admin.api.SystemLogService;
import cn.iocoder.mall.admin.api.bo.systemlog.AccessLogPageBO;
import cn.iocoder.mall.admin.api.dto.systemlog.AccessLogPageDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author:ycjx
* @descriptio
* @create:2019-06-23 18:08
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SystemLogServiceImplTest.class)
public class SystemLogServiceImplTest {
@Autowired
private SystemLogService systemLogService;
@Test
public void getAccessLogPageTest(){
AccessLogPageDTO accessLogPageDTO = new AccessLogPageDTO();
accessLogPageDTO.setPageNo(1);
accessLogPageDTO.setPageSize(10);
AccessLogPageBO accessLogPage = systemLogService.getAccessLogPage(accessLogPageDTO);
System.out.println(accessLogPage.getTotal());
}
}