Move table t_sys_log to sqlite

This commit is contained in:
2023-12-12 17:55:23 +08:00
parent 19d20a0f09
commit e1e661988a
13 changed files with 70 additions and 103 deletions

View File

@@ -1,48 +1,19 @@
drop table if exists t_user;
create table if not exists t_user
(
id bigint not null primary key,
username varchar(20) not null comment '用户名',
password char(70) not null comment '密码',
locking int not null comment '锁定',
expiration datetime comment '过期时间',
credentials_expiration datetime comment '认证过期时间',
enable int not null comment '启用',
last_login_time datetime comment '上次登录时间',
last_login_ip varchar(128) comment '上次登录 IP',
create_time datetime not null default (utc_timestamp()) comment '创建时间',
update_time datetime not null default (utc_timestamp()) comment '修改时间',
deleted bigint not null default 0,
version int not null default 0,
constraint t_user_unique unique (username, deleted)
) comment '用户表';
insert into t_user (id, username, password, locking, enable) value (0, 'admin',
'$2a$10$3wDGdzTZlC..7eY6u2XM5u78xUQo0z5Sj5yOpneD4QJ0q/TA5TY0S',
0, 1);
drop table if exists t_sys_log; drop table if exists t_sys_log;
create table t_sys_log create table t_sys_log -- 系统日志表
( (
id bigint not null, id bigint not null,
log_type varchar(50) not null comment '日志类型', log_type varchar(50) not null, -- 日志类型
operate_user_id bigint not null comment '操作用户', operate_user_id bigint not null, -- 操作用户
operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间', operate_time datetime(3) not null default (strftime('%Y-%m-%d %H:%M:%f','now')), -- 操作时间
request_uri varchar(500) default null comment '请求 URI', request_uri varchar(500) default null, -- 请求 URI
request_method varchar(10) default null comment '请求方式', request_method varchar(10) default null, -- 请求方式
request_params text comment '请求参数', request_params text, -- 请求参数
request_ip varchar(20) not null comment '请求 IP', request_ip varchar(20) not null, -- 请求 IP
request_server_address varchar(50) not null comment '请求服务器地址', request_server_address varchar(50) not null, -- 请求服务器地址
is_exception char(1) default null comment '是否异常', exception int not null default 0, -- 是否异常
exception_info text comment '异常信息', exception_info text, -- 异常信息
start_time datetime(3) not null comment '开始时间', start_time datetime(3) not null, -- 开始时间
end_time datetime(3) not null comment '结束时间', end_time datetime(3) not null, -- 结束时间
execute_time bigint default null comment '执行时间', execute_time bigint default null, -- 执行时间
user_agent varchar(500) default null comment '用户代理', user_agent varchar(500) default null -- 用户代理
primary key (id) using btree, );
key idx_sys_log_log_type (log_type) using btree,
key idx_sys_log_operate_user_id (operate_user_id) using btree,
key idx_sys_log_is_exception (is_exception) using btree,
key idx_sys_log_operate_time (operate_time) using btree
) comment '系统日志表';

View File

@@ -153,7 +153,7 @@
<dependency> <dependency>
<groupId>org.xerial</groupId> <groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version> <version>3.44.1.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>

View File

@@ -34,6 +34,26 @@ fun main(args: Array<String>) {
} }
} }
if (!File("data/db").isDirectory) {
if (!File("data/db").mkdir()) {
logger.error("Can not create directory 'data/db', please try again later.")
return
}
}
if (!File("data/db/sqlite.db").isFile || File("data/db/sqlite.db").inputStream()
.use { it.readNBytes(15).toString(Charsets.UTF_8) != "SQLite format 3" }
) {
logger.warn("The 'data/db/sqlite.db' database is lost or damaged, recreating...")
if (File("data/db/sqlite.db").exists() && !File("data/db/sqlite.db").delete()) {
logger.error("Can not recreate database 'data/db/sqlite.db', please try again later.")
}
FatWebApiApplication::class.java.getResourceAsStream("/db/sqlite.db")?.let {
File("data/db/sqlite.db").writeBytes(it.readAllBytes())
}
}
if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) { if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) {
runApplication<FatWebApiApplication>(*args) runApplication<FatWebApiApplication>(*args)
} else { } else {

View File

@@ -7,7 +7,6 @@ import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import top.fatweb.api.converter.system.SysLogConverter
import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseCode
import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.param.system.SysLogGetParam import top.fatweb.api.param.system.SysLogGetParam
@@ -44,9 +43,7 @@ class SysLogController(
@PreAuthorize("hasAnyAuthority('system:log:query:all')") @PreAuthorize("hasAnyAuthority('system:log:query:all')")
fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult<PageVo<SysLogVo>> { fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult<PageVo<SysLogVo>> {
return ResponseResult.success( return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo( ResponseCode.DATABASE_SELECT_SUCCESS, data = sysLogService.getPage(sysLogGetParam)
sysLogService.getPage(sysLogGetParam)
)
) )
} }
} }

View File

@@ -23,7 +23,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
* @param logType List of log types * @param logType List of log types
* @param requestMethod List of request methods * @param requestMethod List of request methods
* @param searchRequestUrl Request URL to search for * @param searchRequestUrl Request URL to search for
* @param searchRegex Use regex
* @param searchStartTime Start time to search for * @param searchStartTime Start time to search for
* @param searchEndTime end time to search for * @param searchEndTime end time to search for
* @return System log in page * @return System log in page
@@ -38,7 +37,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
logType: List<String>?, logType: List<String>?,
requestMethod: List<String>?, requestMethod: List<String>?,
searchRequestUrl: String?, searchRequestUrl: String?,
searchRegex: Boolean,
searchStartTime: LocalDateTime?, searchStartTime: LocalDateTime?,
searchEndTime: LocalDateTime? searchEndTime: LocalDateTime?
): IPage<SysLog> ): IPage<SysLog>

View File

@@ -51,8 +51,10 @@ data class SysLogGetParam(
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
*/ */
/*
@Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"], defaultValue = "false") @Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"], defaultValue = "false")
val searchRegex: Boolean = false, val searchRegex: Boolean = false,
*/
/** /**
* Start time to search for * Start time to search for

View File

@@ -1,5 +1,6 @@
package top.fatweb.api.service.permission.impl package top.fatweb.api.service.permission.impl
import com.baomidou.dynamic.datasource.annotation.DS
import com.baomidou.mybatisplus.core.metadata.OrderItem import com.baomidou.mybatisplus.core.metadata.OrderItem
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
@@ -47,6 +48,7 @@ import java.time.ZoneOffset
* @see User * @see User
* @see IUserService * @see IUserService
*/ */
@DS("master")
@Service @Service
class UserServiceImpl( class UserServiceImpl(
private val passwordEncoder: PasswordEncoder, private val passwordEncoder: PasswordEncoder,

View File

@@ -1,9 +1,10 @@
package top.fatweb.api.service.system package top.fatweb.api.service.system
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.service.IService import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.system.SysLog import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.param.system.SysLogGetParam import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogVo
/** /**
* System log service interface * System log service interface
@@ -22,8 +23,8 @@ interface ISysLogService : IService<SysLog> {
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
* @see SysLogGetParam * @see SysLogGetParam
* @see IPage * @see PageVo
* @see SysLog * @see SysLogVo
*/ */
fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo>
} }

View File

@@ -1,15 +1,19 @@
package top.fatweb.api.service.system.impl package top.fatweb.api.service.system.impl
import com.baomidou.mybatisplus.core.metadata.IPage import com.baomidou.dynamic.datasource.annotation.DS
import com.baomidou.mybatisplus.core.metadata.OrderItem import com.baomidou.mybatisplus.core.metadata.OrderItem
import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import top.fatweb.api.converter.system.SysLogConverter
import top.fatweb.api.entity.system.SysLog import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.mapper.system.SysLogMapper import top.fatweb.api.mapper.system.SysLogMapper
import top.fatweb.api.param.system.SysLogGetParam import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.service.permission.IUserService
import top.fatweb.api.service.system.ISysLogService import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.util.PageUtil import top.fatweb.api.util.PageUtil
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogVo
/** /**
* System log service implement * System log service implement
@@ -21,21 +25,29 @@ import top.fatweb.api.util.PageUtil
* @see SysLog * @see SysLog
* @see ISysLogService * @see ISysLogService
*/ */
@DS("sqlite")
@Service @Service
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService { class SysLogServiceImpl(
override fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> { private val userService: IUserService
) : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
override fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo> {
val sysLogPage = Page<SysLog>(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20) val sysLogPage = Page<SysLog>(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20)
PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time")) PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time"))
return baseMapper.selectPage( val sysLogIPage = baseMapper.selectPage(
sysLogPage, sysLogPage,
sysLogGetParam?.logType?.split(","), sysLogGetParam?.logType?.split(","),
sysLogGetParam?.requestMethod?.split(","), sysLogGetParam?.requestMethod?.split(","),
sysLogGetParam?.searchRequestUrl, sysLogGetParam?.searchRequestUrl,
sysLogGetParam?.searchRegex ?: false,
sysLogGetParam?.searchStartTime, sysLogGetParam?.searchStartTime,
sysLogGetParam?.searchEndTime sysLogGetParam?.searchEndTime
) )
sysLogIPage.records.forEach {
it.operateUsername =
it.operateUserId?.let { it1 -> userService.getOne(it1)?.username }
}
return SysLogConverter.sysLogPageToSysLogPageVo(sysLogIPage)
} }
} }

View File

@@ -14,10 +14,10 @@ spring:
master: master:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
local: sqlite:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.sqlite.JDBC driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:db/sqlite.db url: jdbc:sqlite:data/db/sqlite.db?date_string_format=yyyy-MM-dd'T'HH:mm:ss.SSS
jackson: jackson:

View File

@@ -1,24 +0,0 @@
drop table if exists t_sys_log;
create table t_sys_log
(
id bigint not null,
log_type varchar(50) not null comment '日志类型',
operate_user_id bigint not null comment '操作用户',
operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间',
request_uri varchar(500) default null comment '请求 URI',
request_method varchar(10) default null comment '请求方式',
request_params text comment '请求参数',
request_ip varchar(20) not null comment '请求 IP',
request_server_address varchar(50) not null comment '请求服务器地址',
exception int not null default 0 comment '是否异常',
exception_info text comment '异常信息',
start_time datetime(3) not null comment '开始时间',
end_time datetime(3) not null comment '结束时间',
execute_time bigint default null comment '执行时间',
user_agent varchar(500) default null comment '用户代理',
primary key (id) using btree,
key idx_sys_log_log_type (log_type) using btree,
key idx_sys_log_operate_user_id (operate_user_id) using btree,
key idx_sys_log_exception (exception) using btree,
key idx_sys_log_operate_time (operate_time) using btree
) comment '系统日志表';

Binary file not shown.

View File

@@ -16,10 +16,8 @@
t_sys_log.start_time as sys_log_start_time, t_sys_log.start_time as sys_log_start_time,
t_sys_log.end_time as sys_log_end_time, t_sys_log.end_time as sys_log_end_time,
t_sys_log.execute_time as sys_log_execute_time, t_sys_log.execute_time as sys_log_execute_time,
t_sys_log.user_agent as sys_log_user_agent, t_sys_log.user_agent as sys_log_user_agent
t_user.username as sys_log_operate_username
from t_sys_log from t_sys_log
left join t_user on t_user.id = t_sys_log.operate_user_id
<where> <where>
<foreach collection="logType" item="item" index="index" open="and t_sys_log.log_type in (" separator="," <foreach collection="logType" item="item" index="index" open="and t_sys_log.log_type in (" separator=","
close=")" nullable="true"> close=")" nullable="true">
@@ -30,17 +28,9 @@
#{item} #{item}
</foreach> </foreach>
<if test="searchRequestUrl != null"> <if test="searchRequestUrl != null">
<choose> and t_sys_log.request_server_address || t_sys_log.request_uri ||
<when test="searchRegex == true"> case when length(t_sys_log.request_params) != 0 then '?' || t_sys_log.request_params else '' end like
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri), '%'||'mail'||'%'
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) regexp #{searchRequestUrl}
</when>
<otherwise>
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri),
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) like concat('%',
#{searchRequestUrl}, '%')
</otherwise>
</choose>
</if> </if>
<if test="searchStartTime != null and searchEndTime != null"> <if test="searchStartTime != null and searchEndTime != null">
and t_sys_log.start_time between #{searchStartTime} and #{searchEndTime} and t_sys_log.start_time between #{searchStartTime} and #{searchEndTime}
@@ -64,7 +54,5 @@
<result property="endTime" column="sys_log_end_time"/> <result property="endTime" column="sys_log_end_time"/>
<result property="executeTime" column="sys_log_execute_time"/> <result property="executeTime" column="sys_log_execute_time"/>
<result property="userAgent" column="sys_log_user_agent"/> <result property="userAgent" column="sys_log_user_agent"/>
<result property="operateUsername" column="sys_log_operate_username"/>
</resultMap> </resultMap>
</mapper> </mapper>