Commit 63acf7d88ff4c25a73838fbfb78d26da91f901dc

Authored by 云中非
1 parent 7953e6ab

feat: 日志管理接口

  1 +package org.thingsboard.server.controller.yunteng;
  2 +
  3 +import com.baomidou.mybatisplus.core.metadata.IPage;
  4 +import io.swagger.annotations.Api;
  5 +import io.swagger.annotations.ApiOperation;
  6 +import lombok.RequiredArgsConstructor;
  7 +import org.apache.commons.lang3.StringUtils;
  8 +import org.springframework.security.access.prepost.PreAuthorize;
  9 +import org.springframework.web.bind.annotation.*;
  10 +import org.thingsboard.server.common.data.audit.ActionType;
  11 +import org.thingsboard.server.common.data.audit.AuditLog;
  12 +import org.thingsboard.server.common.data.exception.ThingsboardException;
  13 +import org.thingsboard.server.common.data.yunteng.dto.SysLogDTO;
  14 +import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
  15 +import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
  16 +import org.thingsboard.server.controller.BaseController;
  17 +import org.thingsboard.server.dao.model.ModelConstants;
  18 +import org.thingsboard.server.dao.yunteng.entities.SysLogEntity;
  19 +import org.thingsboard.server.dao.yunteng.service.YtSysLogService;
  20 +
  21 +import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*;
  22 +
  23 +@RestController
  24 +@RequestMapping("api/yt/log")
  25 +@Api(tags = {"日志管理"})
  26 +@RequiredArgsConstructor
  27 +public class YtSyslogController extends BaseController {
  28 +
  29 + private final YtSysLogService logService;
  30 +
  31 + @GetMapping("{entityId}")
  32 + @PreAuthorize("@check.checkPermissions({},{})")
  33 + @ApiOperation("详情")
  34 + public SysLogDTO detail(@PathVariable("entityId") String entityId)
  35 + throws ThingsboardException {
  36 + return logService.detail(entityId);
  37 + }
  38 +
  39 + @GetMapping(value="exception",params = {PAGE_SIZE, PAGE})
  40 + @ApiOperation("异常日志列表")
  41 + @PreAuthorize("@check.checkPermissions({},{})")
  42 + public YtPageData<SysLogDTO> pageExceptions(
  43 + @RequestParam(PAGE_SIZE) int pageSize,
  44 + @RequestParam(PAGE) int page,
  45 + @RequestParam(value = "actionType", required = false) ActionType actionType,
  46 + @RequestParam(value = ORDER_FILED, required = false) String orderBy,
  47 + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
  48 + throws ThingsboardException {
  49 +
  50 +
  51 + if(StringUtils.isEmpty(orderBy)){
  52 + orderBy = ModelConstants.CREATED_TIME_PROPERTY;
  53 + }
  54 + IPage<SysLogEntity> pageInfrom = logService.getPage(page, pageSize, orderBy, orderType);
  55 + return logService.exceptionPage(pageInfrom, null,null
  56 + ,getCurrentUser().isPtTenantAdmin()?getCurrentUser().getCurrentTenantId():null
  57 + ,getCurrentUser().isCustomerUser()?getCurrentUser().getCurrentUserId():null
  58 + ,actionType);
  59 + }
  60 +
  61 + @GetMapping(value="operate",params = {PAGE_SIZE, PAGE})
  62 + @ApiOperation("操作日志列表")
  63 + @PreAuthorize("@check.checkPermissions({},{})")
  64 + public YtPageData<SysLogDTO> pageOperate(
  65 + @RequestParam(PAGE_SIZE) int pageSize,
  66 + @RequestParam(PAGE) int page,
  67 + @RequestParam(value = "actionType", required = false) ActionType actionType,
  68 + @RequestParam(value = ORDER_FILED, required = false) String orderBy,
  69 + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
  70 + throws ThingsboardException {
  71 + if(StringUtils.isEmpty(orderBy)){
  72 + orderBy = ModelConstants.CREATED_TIME_PROPERTY;
  73 + }
  74 + IPage<SysLogEntity> pageInfrom = logService.getPage(page, pageSize, orderBy, orderType);
  75 + return logService.operatePage(pageInfrom, null,null
  76 + ,getCurrentUser().isPtTenantAdmin()?getCurrentUser().getCurrentTenantId():null
  77 + ,getCurrentUser().isCustomerUser()?getCurrentUser().getCurrentUserId():null
  78 + ,actionType);
  79 + }
  80 +
  81 + @GetMapping(value="user",params = {PAGE_SIZE, PAGE})
  82 + @ApiOperation("登录登出列表")
  83 + @PreAuthorize("@check.checkPermissions({},{})")
  84 + public YtPageData<SysLogDTO> pageLogin(
  85 + @RequestParam(PAGE_SIZE) int pageSize,
  86 + @RequestParam(PAGE) int page,
  87 + @RequestParam(value = "actionType", required = false) ActionType actionType,
  88 + @RequestParam(value = ORDER_FILED, required = false) String orderBy,
  89 + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
  90 + throws ThingsboardException {
  91 +
  92 +
  93 + if(StringUtils.isEmpty(orderBy)){
  94 + orderBy = ModelConstants.CREATED_TIME_PROPERTY;
  95 + }
  96 + IPage<SysLogEntity> pageInfrom = logService.getPage(page, pageSize, orderBy, orderType);
  97 + return logService.loginPage(pageInfrom, null,null
  98 + ,getCurrentUser().isPtTenantAdmin()?getCurrentUser().getCurrentTenantId():null
  99 + ,getCurrentUser().isCustomerUser()?getCurrentUser().getCurrentUserId():null
  100 + ,actionType);
  101 + }
  102 +
  103 +
  104 +
  105 +
  106 +
  107 +}
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.common.data.yunteng.dto;
  17 +
  18 +
  19 +import com.fasterxml.jackson.databind.JsonNode;
  20 +import lombok.Data;
  21 +import lombok.EqualsAndHashCode;
  22 +import org.thingsboard.server.common.data.EntityType;
  23 +import org.thingsboard.server.common.data.audit.ActionStatus;
  24 +import org.thingsboard.server.common.data.audit.ActionType;
  25 +
  26 +import java.util.UUID;
  27 +
  28 +
  29 +@EqualsAndHashCode(callSuper = true)
  30 +@Data
  31 +public class SysLogDTO extends BaseDTO {
  32 +
  33 +
  34 + private long createdTime;
  35 +
  36 + private EntityType entityType;
  37 +
  38 + private UUID tenantId;
  39 + private String tenantName;
  40 +
  41 + private UUID customerId;
  42 + private String customerName;
  43 +
  44 + private UUID entityId;
  45 + private String entityName;
  46 +
  47 + private UUID userId;
  48 + private String userName;
  49 +
  50 +
  51 + private ActionType actionType;
  52 +
  53 + private JsonNode actionData;
  54 +
  55 + private ActionStatus actionStatus;
  56 +
  57 +
  58 + private String actionFailureDetails;
  59 +
  60 +
  61 +}
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.yunteng.entities;
  17 +
  18 +import com.baomidou.mybatisplus.annotation.TableField;
  19 +import com.baomidou.mybatisplus.annotation.TableName;
  20 +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
  21 +import com.fasterxml.jackson.databind.JsonNode;
  22 +import lombok.Data;
  23 +import lombok.EqualsAndHashCode;
  24 +import org.hibernate.annotations.Type;
  25 +import org.hibernate.annotations.TypeDef;
  26 +import org.thingsboard.server.common.data.EntityType;
  27 +import org.thingsboard.server.common.data.audit.ActionStatus;
  28 +import org.thingsboard.server.common.data.audit.ActionType;
  29 +import org.thingsboard.server.common.data.audit.AuditLog;
  30 +import org.thingsboard.server.common.data.id.*;
  31 +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO;
  32 +import org.thingsboard.server.dao.model.BaseSqlEntity;
  33 +import org.thingsboard.server.dao.model.ModelConstants;
  34 +import org.thingsboard.server.dao.util.mapping.JsonStringType;
  35 +
  36 +import javax.persistence.*;
  37 +import java.util.UUID;
  38 +
  39 +import static org.thingsboard.server.dao.model.ModelConstants.*;
  40 +
  41 +@Data
  42 +@EqualsAndHashCode(callSuper = true)
  43 +@TableName(ModelConstants.AUDIT_LOG_COLUMN_FAMILY_NAME)
  44 +public class SysLogEntity extends BaseEntity {
  45 +
  46 +
  47 + private UUID tenantId;
  48 +
  49 + private long createdTime;
  50 + private UUID customerId;
  51 +
  52 + private EntityType entityType;
  53 +
  54 +
  55 + private UUID entityId;
  56 +
  57 +
  58 + private String entityName;
  59 +
  60 +
  61 + private UUID userId;
  62 +
  63 + private String userName;
  64 +
  65 +
  66 + private ActionType actionType;
  67 +
  68 + @TableField(typeHandler = JacksonTypeHandler.class)
  69 + private JsonNode actionData;
  70 +
  71 + private ActionStatus actionStatus;
  72 +
  73 +
  74 + private String actionFailureDetails;
  75 +
  76 +
  77 +}
  1 +package org.thingsboard.server.dao.yunteng.impl;
  2 +
  3 +import com.baomidou.mybatisplus.core.conditions.Wrapper;
  4 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5 +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6 +import com.baomidou.mybatisplus.core.metadata.IPage;
  7 +import lombok.RequiredArgsConstructor;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import org.springframework.stereotype.Service;
  10 +import org.thingsboard.server.common.data.EntityType;
  11 +import org.thingsboard.server.common.data.StringUtils;
  12 +import org.thingsboard.server.common.data.audit.ActionType;
  13 +import org.thingsboard.server.common.data.audit.AuditLog;
  14 +import org.thingsboard.server.common.data.yunteng.dto.SysLogDTO;
  15 +import org.thingsboard.server.common.data.yunteng.dto.YtOpinionDTO;
  16 +import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
  17 +import org.thingsboard.server.dao.yunteng.entities.SysLogEntity;
  18 +import org.thingsboard.server.dao.yunteng.entities.YtOpinionEntity;
  19 +import org.thingsboard.server.dao.yunteng.mapper.YtSysLogMapper;
  20 +import org.thingsboard.server.dao.yunteng.service.AbstractBaseService;
  21 +import org.thingsboard.server.dao.yunteng.service.YtSysLogService;
  22 +
  23 +import java.util.ArrayList;
  24 +import java.util.List;
  25 +import java.util.UUID;
  26 +
  27 +@Slf4j
  28 +@Service
  29 +@RequiredArgsConstructor
  30 +public class YtSysLogServiceImpl extends AbstractBaseService<YtSysLogMapper, SysLogEntity>
  31 + implements YtSysLogService {
  32 +
  33 +
  34 + @Override
  35 + public SysLogDTO detail(String entityId) {
  36 + return baseMapper.detailById(entityId);
  37 + }
  38 +
  39 + @Override
  40 + public YtPageData<SysLogDTO> exceptionPage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType) {
  41 + IPage<SysLogDTO> page =
  42 + baseMapper.getPageDatasMatched(pageInfrom, tenantId,customerId,startTime,endTime,EntityType.RUNNING_EXCEPTION,actionType);
  43 + return getPageData(page, SysLogDTO.class);
  44 + }
  45 +
  46 +
  47 + @Override
  48 + public YtPageData<SysLogDTO> operatePage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType) {
  49 + List<EntityType> entityTypes = new ArrayList<>();
  50 + entityTypes.add(EntityType.RUNNING_EXCEPTION);
  51 + entityTypes.add(EntityType.USER);
  52 +
  53 + IPage<SysLogDTO> page =
  54 + baseMapper.getPageDatasNot(pageInfrom, tenantId,customerId,startTime,endTime,actionType, entityTypes);
  55 + return getPageData(page, SysLogDTO.class);
  56 + }
  57 +
  58 + @Override
  59 + public YtPageData<SysLogDTO> loginPage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType) {
  60 + IPage<SysLogDTO> page =
  61 + baseMapper.getPageDatasMatched(pageInfrom, tenantId,customerId,startTime,endTime,EntityType.USER,actionType);
  62 + return getPageData(page, SysLogDTO.class);
  63 + }
  64 +}
  1 +package org.thingsboard.server.dao.yunteng.mapper;
  2 +
  3 +import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4 +import com.baomidou.mybatisplus.core.metadata.IPage;
  5 +import org.apache.ibatis.annotations.Mapper;
  6 +import org.apache.ibatis.annotations.Param;
  7 +import org.thingsboard.server.common.data.EntityType;
  8 +import org.thingsboard.server.common.data.audit.ActionType;
  9 +import org.thingsboard.server.common.data.audit.AuditLog;
  10 +import org.thingsboard.server.common.data.rpc.RpcStatus;
  11 +import org.thingsboard.server.common.data.yunteng.dto.SysLogDTO;
  12 +import org.thingsboard.server.common.data.yunteng.dto.YtRpcRecordDTO;
  13 +import org.thingsboard.server.dao.yunteng.entities.SysLogEntity;
  14 +import org.thingsboard.server.dao.yunteng.entities.YtOpinionEntity;
  15 +
  16 +import java.util.List;
  17 +
  18 +/**
  19 + * @author Administrator
  20 + */
  21 +@Mapper
  22 +public interface YtSysLogMapper extends BaseMapper<SysLogEntity> {
  23 + IPage<SysLogDTO> getPageDatasMatched(IPage<?> page, @Param("tenantId") String tenantId, @Param("customerId") String customerId
  24 + , @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("entityType") EntityType entityType, @Param("actionType") ActionType actionType);
  25 + IPage<SysLogDTO> getPageDatasNot(IPage<?> page, @Param("tenantId") String tenantId, @Param("customerId") String customerId
  26 + , @Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("actionType") ActionType actionType, @Param("entityType") List<EntityType> entityType);
  27 +
  28 +
  29 +
  30 +
  31 + SysLogDTO detailById( @Param("entityId") String entityId);
  32 +}
  1 +package org.thingsboard.server.dao.yunteng.service;
  2 +
  3 +import com.baomidou.mybatisplus.core.metadata.IPage;
  4 +import org.thingsboard.server.common.data.audit.ActionType;
  5 +import org.thingsboard.server.common.data.audit.AuditLog;
  6 +import org.thingsboard.server.common.data.yunteng.dto.SysLogDTO;
  7 +import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
  8 +import org.thingsboard.server.dao.yunteng.entities.SysLogEntity;
  9 +import org.thingsboard.server.dao.yunteng.entities.YtOpinionEntity;
  10 +
  11 +public interface YtSysLogService extends BaseService<SysLogEntity> {
  12 +
  13 +
  14 + /**
  15 + * 日志详细信息
  16 + *
  17 + * @param entityId 日志ID
  18 + * @return
  19 + */
  20 + SysLogDTO detail(String entityId);
  21 +
  22 + /**
  23 + * 异常日志列表
  24 + *
  25 + * @param pageInfrom 分页配置信息
  26 + * @param startTime 时间过滤窗口起点
  27 + * @param endTime 时间过滤窗口终点
  28 + * @param tenantId 租户ID,超级管理员该值为null
  29 + * @param customerId 客户ID,租户管理员该值为null
  30 + * @param actionType 日志类型
  31 + * @return
  32 + */
  33 + YtPageData<SysLogDTO> exceptionPage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType);
  34 +
  35 + /**
  36 + * 操作日志列表
  37 + *
  38 + * @param pageInfrom 分页配置信息
  39 + * @param startTime 时间过滤窗口起点
  40 + * @param endTime 时间过滤窗口终点
  41 + * @param tenantId 租户ID,超级管理员该值为null
  42 + * @param customerId 客户ID,租户管理员该值为null
  43 + * @param actionType 日志类型
  44 + * @return
  45 + */
  46 + YtPageData<SysLogDTO> operatePage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType);
  47 +
  48 + /**
  49 + * 登录日志列表
  50 + *
  51 + * @param pageInfrom 分页配置信息
  52 + * @param startTime 时间过滤窗口起点
  53 + * @param endTime 时间过滤窗口终点
  54 + * @param tenantId 租户ID,超级管理员该值为null
  55 + * @param customerId 客户ID,租户管理员该值为null
  56 + * @param actionType 日志类型
  57 + * @return
  58 + */
  59 + YtPageData<SysLogDTO> loginPage(IPage<SysLogEntity> pageInfrom, Long startTime, Long endTime, String tenantId, String customerId, ActionType actionType);
  60 +
  61 +}
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +
  4 +<mapper namespace="org.thingsboard.server.dao.yunteng.mapper.YtSysLogMapper">
  5 + <resultMap id="logDto" type="org.thingsboard.server.common.data.yunteng.dto.SysLogDTO">
  6 + <result property="id" column="id"/>
  7 + <result property="entityType" column="entity_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
  8 +
  9 + <result property="entityId" column="entity_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/>
  10 + <result property="entityName" column="entity_name" />
  11 + <result property="tenantId" column="tenant_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/>
  12 + <result property="tenantName" column="tenant_name"/>
  13 + <result property="customerId" column="customer_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/>
  14 + <result property="customerName" column="customer_name"/>
  15 + <result property="userId" column="user_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/>
  16 + <result property="userName" column="user_name" />
  17 +
  18 + <result property="actionData" column="action_data" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
  19 + <result property="actionType" column="action_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
  20 + <result property="actionStatus" column="action_status" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
  21 + <result property="actionFailureDetails" column="action_failure_details"/>
  22 + <result property="createdTime" column="created_time"/>
  23 +
  24 + </resultMap>
  25 +
  26 + <sql id="baseColumn">
  27 + base.id, base.tenant_id, base.created_time, base.customer_id, base.entity_type, base.entity_id, base.entity_name, base.user_id, base.user_name, base.action_type, base.action_status
  28 + ,ten.title tenant_name
  29 + </sql>
  30 + <sql id="detailColumn">
  31 + <include refid="baseColumn"></include>
  32 + , base.action_data,base.action_failure_details
  33 + </sql>
  34 +
  35 + <select id="getPageDatasMatched" resultMap="logDto">
  36 + SELECT <include refid="baseColumn"/>
  37 + FROM audit_log base
  38 + LEFT JOIN tenant ten ON base.tenant_id = ten.id
  39 + <where>
  40 + base.entity_type = #{entityType}
  41 + <if test="tenantId !=null and tenantId !=''">
  42 + AND base.tenant_id = #{tenantId}::uuid
  43 + </if>
  44 + <if test="customerId !=null ">
  45 + AND base.customer_id = #{customerId}::uuid
  46 + </if>
  47 + <if test="startTime !=null">
  48 + AND base.created_time >= #{startTime}
  49 + </if>
  50 + <if test="endTime !=null">
  51 + AND base.created_time &lt; #{endTime}
  52 + </if>
  53 + <if test="actionType !=null">
  54 + AND base.action_type = #{actionType}
  55 + </if>
  56 + </where>
  57 + </select>
  58 +
  59 +
  60 + <select id="getPageDatasNot" resultMap="logDto">
  61 + SELECT <include refid="baseColumn"/>
  62 + FROM audit_log base
  63 + LEFT JOIN tenant ten ON base.tenant_id = ten.id
  64 + <where>
  65 + base.entity_type
  66 + NOT IN
  67 + <foreach collection="entityType" item="item" open="(" separator="," close=")">
  68 + #{item}
  69 + </foreach>
  70 + <if test="tenantId !=null and tenantId !=''">
  71 + AND base.tenant_id = #{tenantId}::uuid
  72 + </if>
  73 + <if test="customerId !=null ">
  74 + AND base.customer_id = #{customerId}::uuid
  75 + </if>
  76 + <if test="startTime !=null">
  77 + AND base.created_time >= #{startTime}
  78 + </if>
  79 + <if test="endTime !=null">
  80 + AND base.created_time &lt; #{endTime}
  81 + </if>
  82 + <if test="actionType !=null">
  83 + AND base.action_type = #{actionType}
  84 + </if>
  85 + </where>
  86 + </select>
  87 +
  88 + <select id="detailById" resultMap="logDto">
  89 + SELECT <include refid="detailColumn"/>
  90 + FROM audit_log base
  91 + LEFT JOIN tenant ten ON base.tenant_id = ten.id
  92 + <where>
  93 + base.id = #{entityId}::uuid
  94 +
  95 + </where>
  96 + </select>
  97 +
  98 +</mapper>