From 7996ae5b6309ea8fb225b447d44718f9eccf2105 Mon Sep 17 00:00:00 2001 From: lijiahang Date: Fri, 27 Dec 2024 13:54:45 +0800 Subject: [PATCH] =?UTF-8?q?:hammer:=20=E5=9F=BA=E7=A1=80=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/entity/StatisticsRange.java | 148 ++++++++++++++++++ .../entity/chart/BarSingleChartData.java | 50 ++++++ .../common/entity/chart/LineChartData.java | 54 +++++++ .../entity/chart/LineSingleChartData.java | 53 +++++++ .../common/entity/chart/PieChartData.java | 50 ++++++ .../controller/InfraStatisticsController.java | 65 ++++++++ .../module/infra/dao/OperatorLogDAO.java | 15 ++ .../cache/InfraStatisticsCacheKeyDefine.java | 49 ++++++ .../infra/entity/po/OperatorLogCountPO.java | 58 +++++++ .../entity/vo/InfraWorkplaceStatisticsVO.java | 73 +++++++++ .../infra/service/InfraStatisticsService.java | 52 ++++++ .../infra/service/SystemMessageService.java | 8 + .../service/SystemUserManagementService.java | 8 + .../impl/InfraStatisticsServiceImpl.java | 139 ++++++++++++++++ .../impl/SystemMessageServiceImpl.java | 11 ++ .../impl/SystemUserManagementServiceImpl.java | 7 + .../resources/mapper/OperatorLogMapper.xml | 16 ++ 17 files changed, 856 insertions(+) create mode 100644 orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/StatisticsRange.java create mode 100644 orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/BarSingleChartData.java create mode 100644 orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineChartData.java create mode 100644 orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineSingleChartData.java create mode 100644 orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/PieChartData.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/InfraStatisticsController.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/InfraStatisticsCacheKeyDefine.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/po/OperatorLogCountPO.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/InfraWorkplaceStatisticsVO.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/InfraStatisticsService.java create mode 100644 orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/InfraStatisticsServiceImpl.java diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/StatisticsRange.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/StatisticsRange.java new file mode 100644 index 00000000..ae60b873 --- /dev/null +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/StatisticsRange.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.common.entity; + +import cn.orionsec.kit.lang.utils.collect.Lists; +import cn.orionsec.kit.lang.utils.time.DateStream; +import cn.orionsec.kit.lang.utils.time.Dates; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 统计区间枚举 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 14:02 + */ +public enum StatisticsRange { + + /** + * 当天 + */ + TODAY { + @Override + public Date getRangeEndTime(Date startTime) { + return DateStream.of(startTime) + .dayEnd() + .date(); + } + + @Override + public List getDateRanges(Date startTime) { + return Lists.singleton(Dates.format(startTime, Dates.YMD)); + } + }, + + /** + * 日视图 + */ + DAY { + @Override + public Date getRangeEndTime(Date startTime) { + return DateStream.of(startTime) + .dayEnd() + .date(); + } + + @Override + public List getDateRanges(Date startTime) { + return Lists.singleton(Dates.format(startTime, Dates.YMD)); + } + }, + + /** + * 周视图 + */ + WEEK { + @Override + public Date getRangeEndTime(Date startTime) { + return DateStream.of(startTime) + .addDay(7) + .dayEnd() + .date(); + } + + @Override + public List getDateRanges(Date startTime) { + return Arrays.stream(Dates.getIncrementDayDates(startTime, 1, 7)) + .map(s -> Dates.format(s, Dates.YMD)) + .collect(Collectors.toList()); + } + }, + + /** + * 月视图 + */ + MONTH { + @Override + public Date getRangeEndTime(Date startTime) { + return DateStream.of(startTime) + .addMonth(1) + .dayEnd() + .date(); + } + + @Override + public List getDateRanges(Date startTime) { + int monthLastDay = Dates.getMonthLastDay(startTime); + return Arrays.stream(Dates.getIncrementDayDates(startTime, 1, monthLastDay - 1)) + .map(s -> Dates.format(s, Dates.YMD)) + .collect(Collectors.toList()); + } + }, + + ; + + /** + * 获取区间结束时间 + * + * @param startTime startTime + * @return end + */ + public abstract Date getRangeEndTime(Date startTime); + + /** + * 获取时间区间 + * + * @param startTime startTime + * @return ranges + */ + public abstract List getDateRanges(Date startTime); + + public static StatisticsRange of(String type) { + if (type == null) { + return TODAY; + } + for (StatisticsRange value : values()) { + if (value.name().equals(type)) { + return value; + } + } + return TODAY; + } + +} diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/BarSingleChartData.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/BarSingleChartData.java new file mode 100644 index 00000000..3a2fc865 --- /dev/null +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/BarSingleChartData.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.common.entity.chart; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + +/** + * 柱状图图单系列数据 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 13:42 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "BarSingleChartData", description = "柱状图图单系列数据") +public class BarSingleChartData { + + @Schema(description = "数据") + private Map data; + +} diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineChartData.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineChartData.java new file mode 100644 index 00000000..ededf971 --- /dev/null +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineChartData.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.common.entity.chart; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +/** + * 折线图多系列数据 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 13:41 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "LineChartData", description = "折线图多系列数据") +public class LineChartData { + + @Schema(description = "x轴") + private List x; + + @Schema(description = "数据") + private Map> data; + +} diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineSingleChartData.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineSingleChartData.java new file mode 100644 index 00000000..47a00d47 --- /dev/null +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/LineSingleChartData.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.common.entity.chart; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * 折线图单系列数据 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 13:42 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "LineSingleChartData", description = "折线图单系列数据") +public class LineSingleChartData { + + @Schema(description = "x轴") + private List x; + + @Schema(description = "数据") + private List data; + +} diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/PieChartData.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/PieChartData.java new file mode 100644 index 00000000..615cfea6 --- /dev/null +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/entity/chart/PieChartData.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.framework.common.entity.chart; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + +/** + * 饼图数据 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 13:42 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "PieChartData", description = "饼图数据") +public class PieChartData { + + @Schema(description = "数据") + private Map data; + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/InfraStatisticsController.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/InfraStatisticsController.java new file mode 100644 index 00000000..443ac409 --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/InfraStatisticsController.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.dromara.visor.framework.log.core.annotation.IgnoreLog; +import org.dromara.visor.framework.log.core.enums.IgnoreLogMode; +import org.dromara.visor.framework.web.core.annotation.RestWrapper; +import org.dromara.visor.module.infra.entity.vo.InfraWorkplaceStatisticsVO; +import org.dromara.visor.module.infra.service.InfraStatisticsService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * infra - 统计服务 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 16:07 + */ +@Tag(name = "infra - 统计服务") +@Slf4j +@Validated +@RestWrapper +@RestController +@RequestMapping("/infra/statistics") +public class InfraStatisticsController { + + @Resource + private InfraStatisticsService infraStatisticsService; + + @IgnoreLog(IgnoreLogMode.RET) + @GetMapping("/get-workplace") + @Operation(summary = "查询工作台统计信息") + public InfraWorkplaceStatisticsVO getWorkplaceStatisticsData() { + return infraStatisticsService.getWorkplaceStatisticsData(); + } + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/dao/OperatorLogDAO.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/dao/OperatorLogDAO.java index cc7eb389..96dfc343 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/dao/OperatorLogDAO.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/dao/OperatorLogDAO.java @@ -24,9 +24,12 @@ package org.dromara.visor.module.infra.dao; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import org.dromara.visor.framework.mybatis.core.mapper.IMapper; import org.dromara.visor.module.infra.entity.domain.OperatorLogDO; +import org.dromara.visor.module.infra.entity.po.OperatorLogCountPO; +import java.util.Date; import java.util.List; /** @@ -63,4 +66,16 @@ public interface OperatorLogDAO extends IMapper { return this.delete(wrapper); } + /** + * 查询操作日志数量 + * + * @param userId userId + * @param startTime startTime + * @param endTime endTime + * @return rows + */ + List selectOperatorLogCount(@Param("userId") Long userId, + @Param("startTime") Date startTime, + @Param("endTime") Date endTime); + } diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/InfraStatisticsCacheKeyDefine.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/InfraStatisticsCacheKeyDefine.java new file mode 100644 index 00000000..96cae039 --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/InfraStatisticsCacheKeyDefine.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.define.cache; + +import cn.orionsec.kit.lang.define.cache.key.CacheKeyBuilder; +import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine; +import cn.orionsec.kit.lang.define.cache.key.struct.RedisCacheStruct; +import org.dromara.visor.module.infra.entity.vo.InfraWorkplaceStatisticsVO; + +import java.util.concurrent.TimeUnit; + +/** + * infra 模块统计缓存 key + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 16:10 + */ +public interface InfraStatisticsCacheKeyDefine { + + CacheKeyDefine WORKPLACE_DATA = new CacheKeyBuilder() + .key("data:statistics:infra-workplace:{}:{}") + .desc("基建模块工作台统计 ${userId} ${time}") + .type(InfraWorkplaceStatisticsVO.class) + .struct(RedisCacheStruct.STRING) + .timeout(10, TimeUnit.MINUTES) + .build(); + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/po/OperatorLogCountPO.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/po/OperatorLogCountPO.java new file mode 100644 index 00000000..9e948fe2 --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/po/OperatorLogCountPO.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.entity.po; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 操作日志数量 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 16:24 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "OperatorLogCountPO", description = "操作日志数量") +public class OperatorLogCountPO implements Serializable { + + private static final long serialVersionUID = 1L; + + @Schema(description = "操作日期") + private String operatorDate; + + @Schema(description = "结果") + private Integer result; + + @Schema(description = "操作次数") + private Integer count; + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/InfraWorkplaceStatisticsVO.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/InfraWorkplaceStatisticsVO.java new file mode 100644 index 00000000..17ab0135 --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/InfraWorkplaceStatisticsVO.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.entity.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.dromara.visor.framework.common.entity.chart.LineSingleChartData; + +import java.util.Date; +import java.util.List; + +/** + * 基建模块工作台统计响应 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 17:38 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "InfraWorkplaceStatisticsVO", description = "基建模块工作台统计响应") +public class InfraWorkplaceStatisticsVO { + + @Schema(description = "userId") + private Long userId; + + @Schema(description = "用户名") + private String username; + + @Schema(description = "花名") + private String nickname; + + @Schema(description = "未读消息数量") + private Integer unreadMessageCount; + + @Schema(description = "上次登录时间") + private Date lastLoginTime; + + @Schema(description = "当前登录会话数量") + private Integer userSessionCount; + + @Schema(description = "系统操作数量图表") + private LineSingleChartData operatorChart; + + @Schema(description = "用户登录日志") + private List loginHistoryList; + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/InfraStatisticsService.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/InfraStatisticsService.java new file mode 100644 index 00000000..eb827c38 --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/InfraStatisticsService.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.service; + +import org.dromara.visor.framework.common.entity.chart.LineSingleChartData; +import org.dromara.visor.module.infra.entity.vo.InfraWorkplaceStatisticsVO; + +/** + * 基建模块统计服务 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 16:52 + */ +public interface InfraStatisticsService { + + /** + * 查询工作台统计信息 + * + * @return data + */ + InfraWorkplaceStatisticsVO getWorkplaceStatisticsData(); + + /** + * 获取用户操作日志图表 + * + * @param userId userId + * @return data + */ + LineSingleChartData getUserOperatorLogChart(Long userId); + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemMessageService.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemMessageService.java index 9186996e..899cf679 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemMessageService.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemMessageService.java @@ -62,6 +62,14 @@ public interface SystemMessageService { */ Map getSystemMessageCount(Boolean queryUnread); + /** + * 查询未读消息数量 + * + * @param receiverId receiverId + * @return count + */ + Integer getUnreadSystemMessageCount(Long receiverId); + /** * 查询是否有未读消息 * diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemUserManagementService.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemUserManagementService.java index e9d66e9f..bf201087 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemUserManagementService.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemUserManagementService.java @@ -36,6 +36,14 @@ import java.util.List; */ public interface SystemUserManagementService { + /** + * 获取用户会话数量 + * + * @param userId userId + * @return count + */ + Integer getUserSessionCount(Long userId); + /** * 获取用户会话列表 * diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/InfraStatisticsServiceImpl.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/InfraStatisticsServiceImpl.java new file mode 100644 index 00000000..44b7782b --- /dev/null +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/InfraStatisticsServiceImpl.java @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.service.impl; + +import cn.orionsec.kit.lang.utils.time.Dates; +import lombok.extern.slf4j.Slf4j; +import org.dromara.visor.framework.common.entity.StatisticsRange; +import org.dromara.visor.framework.common.entity.chart.LineSingleChartData; +import org.dromara.visor.framework.redis.core.utils.RedisStrings; +import org.dromara.visor.framework.security.core.utils.SecurityUtils; +import org.dromara.visor.module.infra.dao.OperatorLogDAO; +import org.dromara.visor.module.infra.dao.SystemUserDAO; +import org.dromara.visor.module.infra.define.cache.InfraStatisticsCacheKeyDefine; +import org.dromara.visor.module.infra.entity.domain.SystemUserDO; +import org.dromara.visor.module.infra.entity.po.OperatorLogCountPO; +import org.dromara.visor.module.infra.entity.vo.InfraWorkplaceStatisticsVO; +import org.dromara.visor.module.infra.entity.vo.LoginHistoryVO; +import org.dromara.visor.module.infra.service.InfraStatisticsService; +import org.dromara.visor.module.infra.service.OperatorLogService; +import org.dromara.visor.module.infra.service.SystemMessageService; +import org.dromara.visor.module.infra.service.SystemUserManagementService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 基建模块统计服务实现类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/12/23 16:52 + */ +@Slf4j +@Service +public class InfraStatisticsServiceImpl implements InfraStatisticsService { + + @Resource + private SystemUserDAO systemUserDAO; + + @Resource + private OperatorLogDAO operatorLogDAO; + + @Resource + private OperatorLogService operatorLogService; + + @Resource + private SystemMessageService systemMessageService; + + @Resource + private SystemUserManagementService systemUserManagementService; + + @Override + public InfraWorkplaceStatisticsVO getWorkplaceStatisticsData() { + Long userId = SecurityUtils.getLoginUserId(); + // 读取缓存 + String cacheKey = InfraStatisticsCacheKeyDefine.WORKPLACE_DATA.format(userId, Dates.current(Dates.YMD2)); + InfraWorkplaceStatisticsVO data = RedisStrings.getJson(cacheKey, InfraStatisticsCacheKeyDefine.WORKPLACE_DATA); + if (data == null) { + // 查询用户操作日志图表 + LineSingleChartData operatorChart = this.getUserOperatorLogChart(userId); + data = InfraWorkplaceStatisticsVO.builder() + .userId(userId) + .operatorChart(operatorChart) + .build(); + // 设置缓存 + RedisStrings.setJson(cacheKey, InfraStatisticsCacheKeyDefine.WORKPLACE_DATA, data); + } + // 查询用户信息 + SystemUserDO user = systemUserDAO.of() + .createWrapper() + .select(SystemUserDO::getId, + SystemUserDO::getUsername, + SystemUserDO::getNickname, + SystemUserDO::getLastLoginTime) + .eq(SystemUserDO::getId, userId) + .then() + .getOne(); + data.setUsername(user.getUsername()); + data.setNickname(user.getNickname()); + data.setLastLoginTime(user.getLastLoginTime()); + // 查询未读消息数量 + Integer unreadMessageCount = systemMessageService.getUnreadSystemMessageCount(userId); + data.setUnreadMessageCount(unreadMessageCount); + // 查询当前登录会话数量 + Integer userSessionCount = systemUserManagementService.getUserSessionCount(userId); + data.setUserSessionCount(userSessionCount); + // 查询用户登录日志 + List loginHistoryList = operatorLogService.getLoginHistory(user.getUsername(), 10); + data.setLoginHistoryList(loginHistoryList); + return data; + } + + @Override + public LineSingleChartData getUserOperatorLogChart(Long userId) { + Date endTime = new Date(); + Date startTime = Dates.stream() + .clearHms() + .addDay(-6) + .get(); + List rangeDays = StatisticsRange.WEEK.getDateRanges(startTime); + // 查询操作数量 + Map countMap = operatorLogDAO.selectOperatorLogCount(userId, startTime, endTime) + .stream() + .collect(Collectors.toMap(OperatorLogCountPO::getOperatorDate, OperatorLogCountPO::getCount)); + // 构建每天的数据 + List data = rangeDays.stream() + .map(s -> countMap.getOrDefault(s, 0)) + .collect(Collectors.toList()); + return LineSingleChartData.builder() + .x(rangeDays) + .data(data) + .build(); + } + +} diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemMessageServiceImpl.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemMessageServiceImpl.java index 3e4c7c85..55c8f4f8 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemMessageServiceImpl.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemMessageServiceImpl.java @@ -114,6 +114,17 @@ public class SystemMessageServiceImpl implements SystemMessageService { Functions.right())); } + @Override + public Integer getUnreadSystemMessageCount(Long receiverId) { + return systemMessageDAO.of() + .createWrapper() + .eq(SystemMessageDO::getReceiverId, receiverId) + .eq(SystemMessageDO::getStatus, MessageStatusEnum.UNREAD.getStatus()) + .then() + .count() + .intValue(); + } + @Override public Boolean checkHasUnreadMessage() { // 查询 diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemUserManagementServiceImpl.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemUserManagementServiceImpl.java index 84e6ae6a..7b5c0a6e 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemUserManagementServiceImpl.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemUserManagementServiceImpl.java @@ -62,6 +62,13 @@ public class SystemUserManagementServiceImpl implements SystemUserManagementServ @Resource private SystemUserDAO systemUserDAO; + @Override + public Integer getUserSessionCount(Long userId) { + // 扫描缓存 + Set keys = RedisStrings.scanKeys(UserCacheKeyDefine.LOGIN_TOKEN.format(userId, "*")); + return Lists.size(keys); + } + @Override public List getUserSessionList(Long userId) { // 扫描缓存 diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/OperatorLogMapper.xml b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/OperatorLogMapper.xml index b62f145c..5c16918d 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/OperatorLogMapper.xml +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/OperatorLogMapper.xml @@ -26,9 +26,25 @@ + + + + + + + id, user_id, username, trace_id, address, location, user_agent, risk_level, module, type, log_info, extra, result, error_message, return_value, duration, start_time, end_time, create_time, deleted + +