diff --git a/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt b/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt index 9a5f335..5dda1ea 100644 --- a/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt +++ b/src/main/kotlin/top/fatweb/api/FatWebApiApplication.kt @@ -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 /** diff --git a/src/main/kotlin/top/fatweb/api/cron/StatisticCron.kt b/src/main/kotlin/top/fatweb/api/cron/StatisticCron.kt new file mode 100644 index 0000000..d22e6e9 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/cron/StatisticCron.kt @@ -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() + }) + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/entity/system/EventLog.kt b/src/main/kotlin/top/fatweb/api/entity/system/EventLog.kt new file mode 100644 index 0000000..5d28307 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/entity/system/EventLog.kt @@ -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)" + } +} diff --git a/src/main/kotlin/top/fatweb/api/entity/system/StatisticLog.kt b/src/main/kotlin/top/fatweb/api/entity/system/StatisticLog.kt new file mode 100644 index 0000000..1ea02cd --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/entity/system/StatisticLog.kt @@ -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)" + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/mapper/system/EventLogMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/system/EventLogMapper.kt new file mode 100644 index 0000000..11177ba --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/mapper/system/EventLogMapper.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/mapper/system/StatisticLogMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/system/StatisticLogMapper.kt new file mode 100644 index 0000000..a970d8e --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/mapper/system/StatisticLogMapper.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/IEventLogService.kt b/src/main/kotlin/top/fatweb/api/service/system/IEventLogService.kt new file mode 100644 index 0000000..481c946 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/IEventLogService.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/IStatisticLogService.kt b/src/main/kotlin/top/fatweb/api/service/system/IStatisticLogService.kt new file mode 100644 index 0000000..4ad89b1 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/IStatisticLogService.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/EventLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/EventLogServiceImpl.kt new file mode 100644 index 0000000..b1e20d0 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/EventLogServiceImpl.kt @@ -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(), IEventLogService \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticLogServiceImpl.kt new file mode 100644 index 0000000..479b142 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticLogServiceImpl.kt @@ -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(), IStatisticLogService \ No newline at end of file diff --git a/src/main/resources/db/migration/sqlite/V1_0_0_231213__Add_table_'t_event_log'.sql b/src/main/resources/db/migration/sqlite/V1_0_0_231213__Add_table_'t_event_log'.sql new file mode 100644 index 0000000..a2b97a6 --- /dev/null +++ b/src/main/resources/db/migration/sqlite/V1_0_0_231213__Add_table_'t_event_log'.sql @@ -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')) -- 操作时间 +); \ No newline at end of file diff --git a/src/main/resources/db/migration/sqlite/V1_0_0_231214__Add_table_'t_statistic'.sql b/src/main/resources/db/migration/sqlite/V1_0_0_231214__Add_table_'t_statistic'.sql new file mode 100644 index 0000000..423ce81 --- /dev/null +++ b/src/main/resources/db/migration/sqlite/V1_0_0_231214__Add_table_'t_statistic'.sql @@ -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')) -- 记录时间 +) \ No newline at end of file