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 c67b55d..9aff799 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/StatisticController.kt @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation import org.springframework.web.bind.annotation.GetMapping import top.fatweb.api.annotation.BaseController 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.vo.system.* @@ -78,5 +79,6 @@ class StatisticController( */ @Operation(summary = "获取在线用户数量信息") @GetMapping("/online") - fun online(): ResponseResult = ResponseResult.success(data = statisticService.online()) + fun online(onlineInfoGetParam: OnlineInfoGetParam?): ResponseResult = + ResponseResult.success(data = statisticService.online(onlineInfoGetParam)) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/system/OnlineInfoGetParam.kt b/src/main/kotlin/top/fatweb/api/param/system/OnlineInfoGetParam.kt new file mode 100644 index 0000000..e3158d1 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/system/OnlineInfoGetParam.kt @@ -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") + } +} 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 3f1d812..750b592 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/IStatisticService.kt @@ -1,5 +1,6 @@ package top.fatweb.api.service.system +import top.fatweb.api.param.system.OnlineInfoGetParam import top.fatweb.api.vo.system.* /** @@ -55,5 +56,5 @@ interface IStatisticService { * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 */ - fun online(): OnlineInfoVo + fun online(onlineInfoGetParam: OnlineInfoGetParam?): 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 9254ca3..c8aec6f 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 @@ -5,6 +5,7 @@ import org.springframework.stereotype.Service import oshi.SystemInfo import oshi.hardware.CentralProcessor 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.ServerProperties 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 = statisticLogService.list( KtQueryWrapper(StatisticLog()) .select(StatisticLog::value, StatisticLog::recordTime) .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 { OnlineInfoVo.HistoryVo( time = it.recordTime!!, diff --git a/src/main/kotlin/top/fatweb/api/util/EventUtil.kt b/src/main/kotlin/top/fatweb/api/util/EventUtil.kt deleted file mode 100644 index 75cc2c4..0000000 --- a/src/main/kotlin/top/fatweb/api/util/EventUtil.kt +++ /dev/null @@ -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) - } - } -} \ No newline at end of file