Add get online info api

This commit is contained in:
2023-12-18 18:02:21 +08:00
parent e3d31bcc38
commit 0f5d1fad4b
4 changed files with 60 additions and 5 deletions

View File

@@ -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<StorageInfoVo> = 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<OnlineInfoVo> = ResponseResult.success(data = statisticService.online())
}

View File

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

View File

@@ -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<OnlineInfoVo.HistoryVo> = 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
)
}
}

View File

@@ -0,0 +1,13 @@
package top.fatweb.api.vo.system
import java.time.LocalDateTime
data class OnlineInfoVo(
val current: Long,
val history: List<HistoryVo>
) {
data class HistoryVo (
val time: LocalDateTime,
val record: String
)
}