diff --git a/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt b/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt index a279220..c67b55d 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt @@ -5,10 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping import top.fatweb.api.annotation.BaseController import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.service.system.IStatisticService -import top.fatweb.api.vo.system.CpuInfoVo -import top.fatweb.api.vo.system.HardwareInfoVo -import top.fatweb.api.vo.system.SoftwareInfoVo -import top.fatweb.api.vo.system.StorageInfoVo +import top.fatweb.api.vo.system.* /** * Statistic controller @@ -72,4 +69,14 @@ class StatisticController( @Operation(summary = "获取存储信息") @GetMapping("/storage") fun storage(): ResponseResult = ResponseResult.success(data = statisticService.storage()) + + /** + * Get the number of online users information + * + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ + @Operation(summary = "获取在线用户数量信息") + @GetMapping("/online") + fun online(): ResponseResult = ResponseResult.success(data = statisticService.online()) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt b/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt index 7f37b35..3f1d812 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt @@ -48,4 +48,12 @@ interface IStatisticService { * @see StorageInfoVo */ fun storage(): StorageInfoVo + + /** + * Get the number of online users information + * + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ + fun online(): OnlineInfoVo } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticServiceImpl.kt index fb141d9..9254ca3 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/StatisticServiceImpl.kt @@ -1,11 +1,16 @@ package top.fatweb.api.service.system.impl +import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import org.springframework.stereotype.Service import oshi.SystemInfo import oshi.hardware.CentralProcessor +import top.fatweb.api.entity.system.StatisticLog +import top.fatweb.api.properties.SecurityProperties import top.fatweb.api.properties.ServerProperties +import top.fatweb.api.service.system.IStatisticLogService import top.fatweb.api.service.system.IStatisticService import top.fatweb.api.util.ByteUtil +import top.fatweb.api.util.RedisUtil import top.fatweb.api.vo.system.* import java.time.LocalDateTime import java.time.ZoneOffset @@ -19,7 +24,10 @@ import java.util.concurrent.TimeUnit * @since 1.0.0 */ @Service -class StatisticServiceImpl : IStatisticService { +class StatisticServiceImpl( + private val redisUtil: RedisUtil, + private val statisticLogService: IStatisticLogService +) : IStatisticService { private val systemProperties: Properties = System.getProperties() private val systemInfo: SystemInfo = SystemInfo() private val runtime: Runtime = Runtime.getRuntime() @@ -142,4 +150,23 @@ class StatisticServiceImpl : IStatisticService { ) } ) + + override fun online(): OnlineInfoVo { + val history: List = statisticLogService.list( + KtQueryWrapper(StatisticLog()) + .select(StatisticLog::value, StatisticLog::recordTime) + .eq(StatisticLog::key, StatisticLog.KeyItem.ONLINE_USERS_COUNT) + ).map { + OnlineInfoVo.HistoryVo( + time = it.recordTime!!, + record = it.value!! + ) + } + + return OnlineInfoVo( + current = redisUtil.keys("${SecurityProperties.jwtIssuer}_login_*") + .distinctBy { Regex("FatWeb_login_(.*):.*").matchEntire(it)?.groupValues?.getOrNull(1) }.size.toLong(), + history = history + ) + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/vo/system/OnlineInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/OnlineInfoVo.kt new file mode 100644 index 0000000..9164ff3 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/system/OnlineInfoVo.kt @@ -0,0 +1,13 @@ +package top.fatweb.api.vo.system + +import java.time.LocalDateTime + +data class OnlineInfoVo( + val current: Long, + val history: List +) { + data class HistoryVo ( + val time: LocalDateTime, + val record: String + ) +}