Rename statistic to statistics

This commit is contained in:
2023-12-20 11:32:47 +08:00
parent b2b4ac5302
commit 60353906ad
14 changed files with 83 additions and 66 deletions

View File

@@ -1,24 +1,25 @@
package top.fatweb.api.controller.system package top.fatweb.api.controller.system
import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Operation
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import top.fatweb.api.annotation.BaseController import top.fatweb.api.annotation.BaseController
import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.param.system.ActiveInfoGetParam import top.fatweb.api.param.system.ActiveInfoGetParam
import top.fatweb.api.param.system.OnlineInfoGetParam import top.fatweb.api.param.system.OnlineInfoGetParam
import top.fatweb.api.service.system.IStatisticService import top.fatweb.api.service.system.IStatisticsService
import top.fatweb.api.vo.system.* import top.fatweb.api.vo.system.*
/** /**
* Statistic controller * Statistics controller
* *
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
* @see IStatisticService * @see IStatisticsService
*/ */
@BaseController(path = ["/system/statistic"], name = "统计接口", description = "系统信息统计相关接口") @BaseController(path = ["/system/statistics"], name = "统计接口", description = "系统信息统计相关接口")
class StatisticController( class StatisticsController(
private val statisticService: IStatisticService private val statisticService: IStatisticsService
) { ) {
/** /**
* Get software information * Get software information
@@ -31,6 +32,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取软件信息") @Operation(summary = "获取软件信息")
@GetMapping("/software") @GetMapping("/software")
@PreAuthorize("hasAnyAuthority('system:statistics:query:base')")
fun software(): ResponseResult<SoftwareInfoVo> = ResponseResult.success(data = statisticService.software()) fun software(): ResponseResult<SoftwareInfoVo> = ResponseResult.success(data = statisticService.software())
/** /**
@@ -44,6 +46,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取硬件信息") @Operation(summary = "获取硬件信息")
@GetMapping("/hardware") @GetMapping("/hardware")
@PreAuthorize("hasAnyAuthority('system:statistics:query:base')")
fun hardware(): ResponseResult<HardwareInfoVo> = ResponseResult.success(data = statisticService.hardware()) fun hardware(): ResponseResult<HardwareInfoVo> = ResponseResult.success(data = statisticService.hardware())
/** /**
@@ -57,6 +60,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取 CPU 信息") @Operation(summary = "获取 CPU 信息")
@GetMapping("/cpu") @GetMapping("/cpu")
@PreAuthorize("hasAnyAuthority('system:statistics:query:real')")
fun cpu(): ResponseResult<CpuInfoVo> = ResponseResult.success(data = statisticService.cpu()) fun cpu(): ResponseResult<CpuInfoVo> = ResponseResult.success(data = statisticService.cpu())
/** /**
@@ -70,6 +74,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取存储信息") @Operation(summary = "获取存储信息")
@GetMapping("/storage") @GetMapping("/storage")
@PreAuthorize("hasAnyAuthority('system:statistics:query:real')")
fun storage(): ResponseResult<StorageInfoVo> = ResponseResult.success(data = statisticService.storage()) fun storage(): ResponseResult<StorageInfoVo> = ResponseResult.success(data = statisticService.storage())
/** /**
@@ -80,6 +85,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取在线用户数量信息") @Operation(summary = "获取在线用户数量信息")
@GetMapping("/online") @GetMapping("/online")
@PreAuthorize("hasAnyAuthority('system:statistics:query:usage')")
fun online(onlineInfoGetParam: OnlineInfoGetParam?): ResponseResult<OnlineInfoVo> = fun online(onlineInfoGetParam: OnlineInfoGetParam?): ResponseResult<OnlineInfoVo> =
ResponseResult.success(data = statisticService.online(onlineInfoGetParam)) ResponseResult.success(data = statisticService.online(onlineInfoGetParam))
@@ -91,6 +97,7 @@ class StatisticController(
*/ */
@Operation(summary = "获取用户活跃信息") @Operation(summary = "获取用户活跃信息")
@GetMapping("/active") @GetMapping("/active")
@PreAuthorize("hasAnyAuthority('system:statistics:query:usage')")
fun active(activeInfoGetParam: ActiveInfoGetParam): ResponseResult<ActiveInfoVo> = fun active(activeInfoGetParam: ActiveInfoGetParam): ResponseResult<ActiveInfoVo> =
ResponseResult.success(data = statisticService.active(activeInfoGetParam)) ResponseResult.success(data = statisticService.active(activeInfoGetParam))
} }

View File

@@ -2,20 +2,20 @@ package top.fatweb.api.cron
import org.springframework.scheduling.annotation.Scheduled import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import top.fatweb.api.entity.system.StatisticLog import top.fatweb.api.entity.system.StatisticsLog
import top.fatweb.api.properties.SecurityProperties import top.fatweb.api.properties.SecurityProperties
import top.fatweb.api.service.system.IStatisticLogService import top.fatweb.api.service.system.IStatisticsLogService
import top.fatweb.api.util.RedisUtil import top.fatweb.api.util.RedisUtil
@Component @Component
class StatisticCron( class StatisticsCron(
private val redisUtil: RedisUtil, private val redisUtil: RedisUtil,
private val statisticLogService: IStatisticLogService private val statisticsLogService: IStatisticsLogService
) { ) {
@Scheduled(cron = "0 * * * * *") @Scheduled(cron = "0 * * * * *")
fun onlineUserCount() { fun onlineUserCount() {
statisticLogService.save(StatisticLog().apply { statisticsLogService.save(StatisticsLog().apply {
key = StatisticLog.KeyItem.ONLINE_USERS_COUNT key = StatisticsLog.KeyItem.ONLINE_USERS_COUNT
value = redisUtil.keys("${SecurityProperties.jwtIssuer}_login_*") value = redisUtil.keys("${SecurityProperties.jwtIssuer}_login_*")
.distinctBy { Regex("FatWeb_login_(.*):.*").matchEntire(it)?.groupValues?.getOrNull(1) }.size.toString() .distinctBy { Regex("FatWeb_login_(.*):.*").matchEntire(it)?.groupValues?.getOrNull(1) }.size.toString()
}) })

View File

@@ -10,13 +10,13 @@ import java.io.Serializable
import java.time.LocalDateTime import java.time.LocalDateTime
/** /**
* Statistic log entity * Statistics log entity
* *
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
*/ */
@TableName("t_statistic_log") @TableName("t_statistics_log")
class StatisticLog : Serializable { class StatisticsLog : Serializable {
enum class KeyItem(@field:EnumValue @field:JsonValue val code: String) { enum class KeyItem(@field:EnumValue @field:JsonValue val code: String) {
ONLINE_USERS_COUNT("ONLINE_USER_COUNT") ONLINE_USERS_COUNT("ONLINE_USER_COUNT")
} }
@@ -59,6 +59,6 @@ class StatisticLog : Serializable {
var recordTime: LocalDateTime?= null var recordTime: LocalDateTime?= null
override fun toString(): String { override fun toString(): String {
return "StatisticLog(id=$id, key=$key, value=$value, recordTime=$recordTime)" return "StatisticsLog(id=$id, key=$key, value=$value, recordTime=$recordTime)"
} }
} }

View File

@@ -24,7 +24,7 @@ class SysLog : Serializable {
* @since 1.0.0 * @since 1.0.0
*/ */
enum class LogType(@field:EnumValue @field:JsonValue val code: String) { enum class LogType(@field:EnumValue @field:JsonValue val code: String) {
INFO("INFO"), ERROR("ERROR"), LOGIN("LOGIN"), LOGOUT("LOGOUT"), REGISTER("REGISTER"), STATISTIC("STATISTIC"), API( INFO("INFO"), ERROR("ERROR"), LOGIN("LOGIN"), LOGOUT("LOGOUT"), REGISTER("REGISTER"), STATISTICS("STATISTICS"), API(
"API" "API"
) )
} }

View File

@@ -72,7 +72,7 @@ class SysLogInterceptor(
it.startsWith("/login") -> SysLog.LogType.LOGIN it.startsWith("/login") -> SysLog.LogType.LOGIN
it.startsWith("/logout") -> SysLog.LogType.LOGOUT it.startsWith("/logout") -> SysLog.LogType.LOGOUT
it.startsWith("/register") -> SysLog.LogType.REGISTER it.startsWith("/register") -> SysLog.LogType.REGISTER
it.startsWith("/system/statistic/") -> SysLog.LogType.STATISTIC it.startsWith("/system/statistics/") -> SysLog.LogType.STATISTICS
it.startsWith("/api/") -> SysLog.LogType.API it.startsWith("/api/") -> SysLog.LogType.API
else -> SysLog.LogType.INFO else -> SysLog.LogType.INFO
} }

View File

@@ -2,7 +2,7 @@ package top.fatweb.api.mapper.system
import com.baomidou.mybatisplus.core.mapper.BaseMapper import com.baomidou.mybatisplus.core.mapper.BaseMapper
import org.apache.ibatis.annotations.Mapper import org.apache.ibatis.annotations.Mapper
import top.fatweb.api.entity.system.StatisticLog import top.fatweb.api.entity.system.StatisticsLog
@Mapper @Mapper
interface StatisticLogMapper : BaseMapper<StatisticLog> interface StatisticsLogMapper : BaseMapper<StatisticsLog>

View File

@@ -1,6 +0,0 @@
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,6 @@
package top.fatweb.api.service.system
import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.system.StatisticsLog
interface IStatisticsLogService : IService<StatisticsLog>

View File

@@ -5,12 +5,12 @@ import top.fatweb.api.param.system.OnlineInfoGetParam
import top.fatweb.api.vo.system.* import top.fatweb.api.vo.system.*
/** /**
* Statistic service interface * Statistics service interface
* *
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
*/ */
interface IStatisticService { interface IStatisticsService {
/** /**
* Get software information * Get software information
* *

View File

@@ -1,12 +0,0 @@
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

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.StatisticsLog
import top.fatweb.api.mapper.system.StatisticsLogMapper
import top.fatweb.api.service.system.IStatisticsLogService
@DS("sqlite")
@Service
class StatisticsLogServiceImpl : ServiceImpl<StatisticsLogMapper, StatisticsLog>(), IStatisticsLogService

View File

@@ -6,14 +6,14 @@ import org.springframework.stereotype.Service
import oshi.SystemInfo import oshi.SystemInfo
import oshi.hardware.CentralProcessor import oshi.hardware.CentralProcessor
import top.fatweb.api.entity.system.EventLog import top.fatweb.api.entity.system.EventLog
import top.fatweb.api.entity.system.StatisticLog import top.fatweb.api.entity.system.StatisticsLog
import top.fatweb.api.param.system.ActiveInfoGetParam import top.fatweb.api.param.system.ActiveInfoGetParam
import top.fatweb.api.param.system.OnlineInfoGetParam import top.fatweb.api.param.system.OnlineInfoGetParam
import top.fatweb.api.properties.SecurityProperties import top.fatweb.api.properties.SecurityProperties
import top.fatweb.api.properties.ServerProperties import top.fatweb.api.properties.ServerProperties
import top.fatweb.api.service.system.IEventLogService import top.fatweb.api.service.system.IEventLogService
import top.fatweb.api.service.system.IStatisticLogService import top.fatweb.api.service.system.IStatisticsLogService
import top.fatweb.api.service.system.IStatisticService import top.fatweb.api.service.system.IStatisticsService
import top.fatweb.api.util.ByteUtil import top.fatweb.api.util.ByteUtil
import top.fatweb.api.util.RedisUtil import top.fatweb.api.util.RedisUtil
import top.fatweb.api.vo.system.* import top.fatweb.api.vo.system.*
@@ -25,17 +25,17 @@ import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** /**
* Statistic service implement * Statistics service implement
* *
* @author FatttSnake, fatttsnake@gmail.com * @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0 * @since 1.0.0
*/ */
@Service @Service
class StatisticServiceImpl( class StatisticsServiceImpl(
private val redisUtil: RedisUtil, private val redisUtil: RedisUtil,
private val statisticLogService: IStatisticLogService, private val statisticsLogService: IStatisticsLogService,
private val eventLogService: IEventLogService private val eventLogService: IEventLogService
) : IStatisticService { ) : IStatisticsService {
private val systemProperties: Properties = System.getProperties() private val systemProperties: Properties = System.getProperties()
private val systemInfo: SystemInfo = SystemInfo() private val systemInfo: SystemInfo = SystemInfo()
private val runtime: Runtime = Runtime.getRuntime() private val runtime: Runtime = Runtime.getRuntime()
@@ -160,13 +160,13 @@ class StatisticServiceImpl(
) )
override fun online(onlineInfoGetParam: OnlineInfoGetParam?): OnlineInfoVo { override fun online(onlineInfoGetParam: OnlineInfoGetParam?): OnlineInfoVo {
val history: List<OnlineInfoVo.HistoryVo> = statisticLogService.list( val history: List<OnlineInfoVo.HistoryVo> = statisticsLogService.list(
KtQueryWrapper(StatisticLog()) KtQueryWrapper(StatisticsLog())
.select(StatisticLog::value, StatisticLog::recordTime) .select(StatisticsLog::value, StatisticsLog::recordTime)
.eq(StatisticLog::key, StatisticLog.KeyItem.ONLINE_USERS_COUNT) .eq(StatisticsLog::key, StatisticsLog.KeyItem.ONLINE_USERS_COUNT)
.between( .between(
onlineInfoGetParam?.scope != OnlineInfoGetParam.Scope.ALL, onlineInfoGetParam?.scope != OnlineInfoGetParam.Scope.ALL,
StatisticLog::recordTime, StatisticsLog::recordTime,
LocalDateTime.now(ZoneOffset.UTC).run { LocalDateTime.now(ZoneOffset.UTC).run {
when (onlineInfoGetParam?.scope) { when (onlineInfoGetParam?.scope) {
OnlineInfoGetParam.Scope.DAY -> minusDays(1) OnlineInfoGetParam.Scope.DAY -> minusDays(1)

View File

@@ -14,6 +14,7 @@ insert into t_power (id, type_id)
(1040000, 2), (1040000, 2),
(1510000, 2), (1510000, 2),
(1520000, 2), (1520000, 2),
(1530000, 2),
(1010100, 3), (1010100, 3),
(1010200, 3), (1010200, 3),
(1010300, 3), (1010300, 3),
@@ -29,7 +30,8 @@ insert into t_power (id, type_id)
(1040100, 3), (1040100, 3),
(1510100, 3), (1510100, 3),
(1520100, 3), (1520100, 3),
(1520300, 3), (1530100, 3),
(1530300, 3),
(1010101, 4), (1010101, 4),
(1010102, 4), (1010102, 4),
(1010103, 4), (1010103, 4),
@@ -56,10 +58,13 @@ insert into t_power (id, type_id)
(1030402, 4), (1030402, 4),
(1040103, 4), (1040103, 4),
(1510101, 4), (1510101, 4),
(1510102, 4),
(1510103, 4),
(1520101, 4), (1520101, 4),
(1520102, 4), (1530101, 4),
(1520301, 4), (1530102, 4),
(1520302, 4) (1530301, 4),
(1530302, 4)
as new_value as new_value
on duplicate key update type_id = new_value.type_id; on duplicate key update type_id = new_value.type_id;
@@ -73,8 +78,9 @@ insert into t_menu (id, name, url, parent_id, module_id)
(1020000, '角色管理', '/system/role', 1990000, 1000000), (1020000, '角色管理', '/system/role', 1990000, 1000000),
(1030000, '用户组管理', '/system/group', 1990000, 1000000), (1030000, '用户组管理', '/system/group', 1990000, 1000000),
(1040000, '权限管理', '/system/power', 1990000, 1000000), (1040000, '权限管理', '/system/power', 1990000, 1000000),
(1510000, '日志管理', '/system/log', 1990000, 1000000), (1510000, '系统概况', '/system/statistics', 1990000, 1000000),
(1520000, '系统设置', '/system/settings', 1990000, 1000000) as new_value (1520000, '日志管理', '/system/log', 1990000, 1000000),
(1530000, '系统设置', '/system/settings', 1990000, 1000000) as new_value
on duplicate key update name =new_value.name, on duplicate key update name =new_value.name,
url =new_value.url, url =new_value.url,
parent_id =new_value.parent_id; parent_id =new_value.parent_id;
@@ -95,7 +101,8 @@ insert into t_func(id, name, menu_id, parent_id)
(1040100, '查询', 1040000, null), (1040100, '查询', 1040000, null),
(1510100, '查询', 1510000, null), (1510100, '查询', 1510000, null),
(1520100, '查询', 1520000, null), (1520100, '查询', 1520000, null),
(1520300, '修改', 1520000, null) as new_value (1530100, '查询', 1530000, null),
(1530300, '修改', 1530000, null) as new_value
on duplicate key update name = new_value.name, on duplicate key update name = new_value.name,
menu_id = new_value.menu_id, menu_id = new_value.menu_id,
parent_id = new_value.parent_id; parent_id = new_value.parent_id;
@@ -126,11 +133,14 @@ insert into t_operation(id, name, code, func_id)
(1030401, '单个', 'system:group:delete:one', 1030400), (1030401, '单个', 'system:group:delete:one', 1030400),
(1030402, '多个', 'system:group:delete:multiple', 1030400), (1030402, '多个', 'system:group:delete:multiple', 1030400),
(1040103, '列表', 'system:power:query:list', 1040100), (1040103, '列表', 'system:power:query:list', 1040100),
(1510101, '全部', 'system:log:query:all', 1510100), (1510101, '使用情况', 'system:statistics:query:usage', 1510100),
(1520101, '基础', 'system:settings:query:base', 1520100), (1510102, '基础信息', 'system:statistics:query:base', 1510100),
(1520102, '邮件', 'system:settings:query:mail', 1520100), (1510103, '实时信息', 'system:statistics:query:real', 1510100),
(1520301, '基础', 'system:settings:modify:base', 1520300), (1520101, '全部', 'system:log:query:all', 1520100),
(1520302, '邮件', 'system:settings:modify:mail', 1520300) as new_value (1530101, '基础', 'system:settings:query:base', 1530100),
(1530102, '邮件', 'system:settings:query:mail', 1530100),
(1530301, '基础', 'system:settings:modify:base', 1530300),
(1530302, '邮件', 'system:settings:modify:mail', 1530300) as new_value
on duplicate key update name=new_value.name, on duplicate key update name=new_value.name,
code=new_value.code, code=new_value.code,
func_id=new_value.func_id; func_id=new_value.func_id;

View File

@@ -1,6 +1,6 @@
drop table if exists t_statistic_log; drop table if exists t_statistics_log;
create table if not exists t_statistic_log -- 统计日志表 create table if not exists t_statistics_log -- 统计日志表
( (
id bigint not null primary key, id bigint not null primary key,
key varchar(50) not null, -- 记录键 key varchar(50) not null, -- 记录键