Add event log and statistic log
This commit is contained in:
@@ -3,6 +3,7 @@ package top.fatweb.api
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
import org.springframework.boot.runApplication
|
import org.springframework.boot.runApplication
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement
|
import org.springframework.transaction.annotation.EnableTransactionManagement
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -15,6 +16,7 @@ import java.util.*
|
|||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
|
@EnableScheduling
|
||||||
class FatWebApiApplication
|
class FatWebApiApplication
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
23
src/main/kotlin/top/fatweb/api/cron/StatisticCron.kt
Normal file
23
src/main/kotlin/top/fatweb/api/cron/StatisticCron.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package top.fatweb.api.cron
|
||||||
|
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import top.fatweb.api.entity.system.StatisticLog
|
||||||
|
import top.fatweb.api.properties.SecurityProperties
|
||||||
|
import top.fatweb.api.service.system.IStatisticLogService
|
||||||
|
import top.fatweb.api.util.RedisUtil
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class StatisticCron(
|
||||||
|
private val redisUtil: RedisUtil,
|
||||||
|
private val statisticLogService: IStatisticLogService
|
||||||
|
) {
|
||||||
|
@Scheduled(cron = "0 * * * * *")
|
||||||
|
fun onlineUserCount() {
|
||||||
|
statisticLogService.save(StatisticLog().apply {
|
||||||
|
key = StatisticLog.KeyItem.ONLINE_USERS_COUNT
|
||||||
|
value = redisUtil.keys("${SecurityProperties.jwtIssuer}_login_*")
|
||||||
|
.distinctBy { Regex("FatWeb_login_(.*):.*").matchEntire(it)?.groupValues?.getOrNull(1) }.size.toString()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/main/kotlin/top/fatweb/api/entity/system/EventLog.kt
Normal file
64
src/main/kotlin/top/fatweb/api/entity/system/EventLog.kt
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package top.fatweb.api.entity.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.EnumValue
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue
|
||||||
|
import java.io.Serializable
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event log entity
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableName("t_event_log")
|
||||||
|
class EventLog : Serializable {
|
||||||
|
enum class Event(@field:EnumValue @field:JsonValue val code: String) {
|
||||||
|
LOGIN("LOGIN"), LOGOUT("LOGOUT"), REGISTER("REGISTER"), API("API")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableId("id")
|
||||||
|
var id: Long? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableField("event")
|
||||||
|
var event: Event? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operate user ID
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableField("operate_user_id")
|
||||||
|
var operateUserId: Long? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operate time
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||||
|
@TableField("operate_time")
|
||||||
|
var operateTime: LocalDateTime? = null
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "EventLog(id=$id, event=$event, operateUserId=$operateUserId, operateTime=$operateTime)"
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/main/kotlin/top/fatweb/api/entity/system/StatisticLog.kt
Normal file
64
src/main/kotlin/top/fatweb/api/entity/system/StatisticLog.kt
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package top.fatweb.api.entity.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.EnumValue
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue
|
||||||
|
import java.io.Serializable
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Statistic log entity
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableName("t_statistic_log")
|
||||||
|
class StatisticLog : Serializable {
|
||||||
|
enum class KeyItem(@field:EnumValue @field:JsonValue val code: String) {
|
||||||
|
ONLINE_USERS_COUNT("ONLINE_USER_COUNT")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ID
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableId("id")
|
||||||
|
var id: Long? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableField("key")
|
||||||
|
var key: KeyItem? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@TableField("value")
|
||||||
|
var value: String? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record time
|
||||||
|
*
|
||||||
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||||
|
@TableField("record_time")
|
||||||
|
var recordTime: LocalDateTime?= null
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "StatisticLog(id=$id, key=$key, value=$value, recordTime=$recordTime)"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package top.fatweb.api.mapper.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
||||||
|
import org.apache.ibatis.annotations.Mapper
|
||||||
|
import top.fatweb.api.entity.system.EventLog
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
interface EventLogMapper : BaseMapper<EventLog>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package top.fatweb.api.mapper.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
||||||
|
import org.apache.ibatis.annotations.Mapper
|
||||||
|
import top.fatweb.api.entity.system.StatisticLog
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
interface StatisticLogMapper : BaseMapper<StatisticLog>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package top.fatweb.api.service.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService
|
||||||
|
import top.fatweb.api.entity.system.EventLog
|
||||||
|
|
||||||
|
interface IEventLogService : IService<EventLog>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package top.fatweb.api.service.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService
|
||||||
|
import top.fatweb.api.entity.system.StatisticLog
|
||||||
|
|
||||||
|
interface IStatisticLogService : IService<StatisticLog>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package top.fatweb.api.service.system.impl
|
||||||
|
|
||||||
|
import com.baomidou.dynamic.datasource.annotation.DS
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import top.fatweb.api.entity.system.EventLog
|
||||||
|
import top.fatweb.api.mapper.system.EventLogMapper
|
||||||
|
import top.fatweb.api.service.system.IEventLogService
|
||||||
|
|
||||||
|
@DS("sqlite")
|
||||||
|
@Service
|
||||||
|
class EventLogServiceImpl : ServiceImpl<EventLogMapper, EventLog>(), IEventLogService
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package top.fatweb.api.service.system.impl
|
||||||
|
|
||||||
|
import com.baomidou.dynamic.datasource.annotation.DS
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import top.fatweb.api.entity.system.StatisticLog
|
||||||
|
import top.fatweb.api.mapper.system.StatisticLogMapper
|
||||||
|
import top.fatweb.api.service.system.IStatisticLogService
|
||||||
|
|
||||||
|
@DS("sqlite")
|
||||||
|
@Service
|
||||||
|
class StatisticLogServiceImpl : ServiceImpl<StatisticLogMapper, StatisticLog>(), IStatisticLogService
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
drop table if exists t_event_log;
|
||||||
|
|
||||||
|
create table if not exists t_event_log -- 事件日志表
|
||||||
|
(
|
||||||
|
id bigint not null primary key,
|
||||||
|
event varchar(50) not null, -- 事件,
|
||||||
|
operate_user_id bigint not null, -- 操作用户
|
||||||
|
operate_time datetime not null default (strftime('%Y-%m-%d %H:%M:%f','now')) -- 操作时间
|
||||||
|
);
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
drop table if exists t_statistic_log;
|
||||||
|
|
||||||
|
create table if not exists t_statistic_log -- 统计日志表
|
||||||
|
(
|
||||||
|
id bigint not null primary key,
|
||||||
|
key varchar(50) not null, -- 记录键
|
||||||
|
value varchar(100) not null, -- 记录值
|
||||||
|
record_time datetime not null default (strftime('%Y-%m-%d %H:%M:%f', 'now')) -- 记录时间
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user