Add event log and statistic log

This commit is contained in:
2023-12-14 18:29:24 +08:00
parent c2e5248aa0
commit ed1124380b
12 changed files with 223 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package top.fatweb.api
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.transaction.annotation.EnableTransactionManagement
import java.io.File
import java.util.*
@@ -15,6 +16,7 @@ import java.util.*
*/
@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
class FatWebApiApplication
/**

View 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()
})
}
}

View 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)"
}
}

View 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)"
}
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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

View File

@@ -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