Add scope option to online info api
This commit is contained in:
@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation
|
|||||||
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.OnlineInfoGetParam
|
||||||
import top.fatweb.api.service.system.IStatisticService
|
import top.fatweb.api.service.system.IStatisticService
|
||||||
import top.fatweb.api.vo.system.*
|
import top.fatweb.api.vo.system.*
|
||||||
|
|
||||||
@@ -78,5 +79,6 @@ class StatisticController(
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "获取在线用户数量信息")
|
@Operation(summary = "获取在线用户数量信息")
|
||||||
@GetMapping("/online")
|
@GetMapping("/online")
|
||||||
fun online(): ResponseResult<OnlineInfoVo> = ResponseResult.success(data = statisticService.online())
|
fun online(onlineInfoGetParam: OnlineInfoGetParam?): ResponseResult<OnlineInfoVo> =
|
||||||
|
ResponseResult.success(data = statisticService.online(onlineInfoGetParam))
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package top.fatweb.api.param.system
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.EnumValue
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue
|
||||||
|
|
||||||
|
data class OnlineInfoGetParam(
|
||||||
|
val scope: Scope = Scope.WEAK
|
||||||
|
) {
|
||||||
|
enum class Scope(@field:EnumValue @field:JsonValue val code: String) {
|
||||||
|
DAY("DAY"),
|
||||||
|
|
||||||
|
WEAK("WEAK"),
|
||||||
|
|
||||||
|
MONTH("MONTH"),
|
||||||
|
|
||||||
|
QUARTER("QUARTER"),
|
||||||
|
|
||||||
|
YEAR("YEAR"),
|
||||||
|
|
||||||
|
TWO_YEARS("TWO_YEARS"),
|
||||||
|
|
||||||
|
THREE_YEARS("THREE_YEARS"),
|
||||||
|
|
||||||
|
FIVE_YEARS("FIVE_YEARS"),
|
||||||
|
|
||||||
|
ALL("ALL")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package top.fatweb.api.service.system
|
package top.fatweb.api.service.system
|
||||||
|
|
||||||
|
import top.fatweb.api.param.system.OnlineInfoGetParam
|
||||||
import top.fatweb.api.vo.system.*
|
import top.fatweb.api.vo.system.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,5 +56,5 @@ interface IStatisticService {
|
|||||||
* @author FatttSnake, fatttsnake@gmail.com
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
fun online(): OnlineInfoVo
|
fun online(onlineInfoGetParam: OnlineInfoGetParam?): OnlineInfoVo
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ 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.StatisticLog
|
import top.fatweb.api.entity.system.StatisticLog
|
||||||
|
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.IStatisticLogService
|
import top.fatweb.api.service.system.IStatisticLogService
|
||||||
@@ -151,11 +152,28 @@ class StatisticServiceImpl(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun online(): OnlineInfoVo {
|
override fun online(onlineInfoGetParam: OnlineInfoGetParam?): OnlineInfoVo {
|
||||||
val history: List<OnlineInfoVo.HistoryVo> = statisticLogService.list(
|
val history: List<OnlineInfoVo.HistoryVo> = statisticLogService.list(
|
||||||
KtQueryWrapper(StatisticLog())
|
KtQueryWrapper(StatisticLog())
|
||||||
.select(StatisticLog::value, StatisticLog::recordTime)
|
.select(StatisticLog::value, StatisticLog::recordTime)
|
||||||
.eq(StatisticLog::key, StatisticLog.KeyItem.ONLINE_USERS_COUNT)
|
.eq(StatisticLog::key, StatisticLog.KeyItem.ONLINE_USERS_COUNT)
|
||||||
|
.between(
|
||||||
|
onlineInfoGetParam?.scope != OnlineInfoGetParam.Scope.ALL,
|
||||||
|
StatisticLog::recordTime,
|
||||||
|
LocalDateTime.now(ZoneOffset.UTC).run {
|
||||||
|
when (onlineInfoGetParam?.scope) {
|
||||||
|
OnlineInfoGetParam.Scope.DAY -> minusDays(1)
|
||||||
|
OnlineInfoGetParam.Scope.MONTH -> minusMonths(1)
|
||||||
|
OnlineInfoGetParam.Scope.QUARTER -> minusMonths(3)
|
||||||
|
OnlineInfoGetParam.Scope.YEAR -> minusYears(1)
|
||||||
|
OnlineInfoGetParam.Scope.TWO_YEARS -> minusYears(2)
|
||||||
|
OnlineInfoGetParam.Scope.THREE_YEARS -> minusYears(3)
|
||||||
|
OnlineInfoGetParam.Scope.FIVE_YEARS -> minusYears(5)
|
||||||
|
else -> minusWeeks(1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
LocalDateTime.now(ZoneOffset.UTC)
|
||||||
|
)
|
||||||
).map {
|
).map {
|
||||||
OnlineInfoVo.HistoryVo(
|
OnlineInfoVo.HistoryVo(
|
||||||
time = it.recordTime!!,
|
time = it.recordTime!!,
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package top.fatweb.api.util
|
|
||||||
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import org.springframework.stereotype.Component
|
|
||||||
import top.fatweb.api.entity.system.EventLog
|
|
||||||
import top.fatweb.api.service.system.IEventLogService
|
|
||||||
|
|
||||||
@Component
|
|
||||||
class EventUtil(
|
|
||||||
private val eventLogService: IEventLogService
|
|
||||||
) {
|
|
||||||
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
|
||||||
|
|
||||||
fun record(event: EventLog.Event) {
|
|
||||||
try {
|
|
||||||
eventLogService.save(EventLog().apply {
|
|
||||||
this.event = event
|
|
||||||
operateUserId = WebUtil.getLoginUserId() ?: -1
|
|
||||||
})
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("Cannot record event!!!", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user