Skip to content

Commit

Permalink
refactor: 重构仪表盘查询地域分析接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Nov 4, 2024
1 parent b927470 commit e0e157f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ IPage<LogResp> selectLogPage(@Param("page") IPage<LogDO> page,
@Cached(key = "#months[0]", name = CacheConstants.DASHBOARD_KEY_PREFIX + "IP:")
List<DashboardChartCommonResp> selectListDashboardAnalysisIp(@Param("months") List<String> months);

/**
* 查询仪表盘地域分析信息
*
* @return 仪表盘地域分析信息
*/
List<DashboardChartCommonResp> selectListDashboardAnalysisGeo();

/**
* 查询仪表盘访问趋势信息
*
Expand All @@ -116,14 +123,6 @@ List<DashboardAccessTrendResp> selectListDashboardAccessTrend(@Param("startTime"
*/
List<DashboardChartCommonResp> selectListDashboardAnalysisTimeslot();

/**
* 查询仪表盘地域分析信息
*
* @param top 显示数量
* @return 仪表盘地域分析信息
*/
List<DashboardChartCommonResp> selectListDashboardAnalysisGeo(@Param("top") Integer top);

/**
* 查询仪表盘模块分析信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package top.continew.admin.system.service;

import top.continew.admin.system.model.resp.dashboard.*;
import top.continew.admin.system.model.resp.dashboard.DashboardAccessTrendResp;
import top.continew.admin.system.model.resp.dashboard.DashboardChartCommonResp;
import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp;
import top.continew.admin.system.model.resp.dashboard.DashboardOverviewCommonResp;

import java.io.IOException;
import java.util.List;

/**
Expand Down Expand Up @@ -49,6 +53,14 @@ public interface DashboardService {
*/
DashboardOverviewCommonResp getOverviewIp();

/**
* 查询地域分析信息
*
* @return 地域分析信息
* @throws IOException /
*/
List<DashboardChartCommonResp> getAnalysisGeo() throws IOException;

/**
* 查询访问趋势信息
*
Expand All @@ -64,13 +76,6 @@ public interface DashboardService {
*/
List<DashboardChartCommonResp> getAnalysisTimeslot();

/**
* 查询地域分析信息
*
* @return 地域分析信息
*/
List<DashboardChartCommonResp> getAnalysisGeo();

/**
* 查询模块分析信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
package top.continew.admin.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.*;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import top.continew.admin.system.mapper.LogMapper;
import top.continew.admin.system.model.resp.dashboard.DashboardAccessTrendResp;
Expand All @@ -28,7 +36,9 @@
import top.continew.admin.system.model.resp.dashboard.DashboardOverviewCommonResp;
import top.continew.admin.system.service.DashboardService;
import top.continew.admin.system.service.NoticeService;
import top.continew.starter.core.constant.StringConstants;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

Expand Down Expand Up @@ -78,6 +88,31 @@ public DashboardOverviewCommonResp getOverviewIp() {
return resp;
}

@Override
public List<DashboardChartCommonResp> getAnalysisGeo() throws IOException {
List<DashboardChartCommonResp> originList = logMapper.selectListDashboardAnalysisGeo();
List<DashboardChartCommonResp> list = new ArrayList<>(34);
// 获取省份数据
String chinaJson = IoUtil.readUtf8(new ClassPathResource("china.json").getInputStream());
JSONArray jsonArr = JSONUtil.parseObj(chinaJson).getJSONArray("children");
List<String> provinceList = jsonArr.stream().map(item -> {
JSONObject itemJsonObj = JSONUtil.parseObj(item);
return "%s:%s".formatted(itemJsonObj.getStr("name"), itemJsonObj.getStr("fullname"));
}).toList();
// 汇总各省份访问数据
for (String province : provinceList) {
String[] split = province.split(StringConstants.COLON);
String name = split[0];
String fullName = split[1];
long sum = originList.stream()
.filter(item -> item.getName().contains(name))
.mapToLong(DashboardChartCommonResp::getValue)
.sum();
list.add(new DashboardChartCommonResp(fullName, sum));
}
return list;
}

@Override
public List<DashboardAccessTrendResp> listAccessTrend(Integer days) {
DateTime currentDate = DateUtil.date();
Expand Down Expand Up @@ -113,12 +148,6 @@ public List<DashboardChartCommonResp> getAnalysisTimeslot() {
return list;
}

@Override
public List<DashboardChartCommonResp> getAnalysisGeo() {
List<DashboardChartCommonResp> list = logMapper.selectListDashboardAnalysisGeo(9);
return this.buildOtherPieChartData(list);
}

@Override
public List<DashboardChartCommonResp> getAnalysisModule() {
return logMapper.selectListDashboardAnalysisModule(10);
Expand Down
25 changes: 12 additions & 13 deletions continew-module-system/src/main/resources/mapper/LogMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
ORDER BY name
</select>

<select id="selectListDashboardAnalysisGeo" resultType="top.continew.admin.system.model.resp.dashboard.DashboardChartCommonResp">
SELECT
CASE
WHEN POSITION(' ' IN address) > 0 THEN SUBSTRING(address FROM 1 FOR POSITION(' ' IN address) - 1)
ELSE address
END AS name,
COUNT(*) AS value
FROM sys_log
WHERE address LIKE '中国%'
GROUP BY name
</select>

<select id="selectListDashboardAccessTrend"
resultType="top.continew.admin.system.model.resp.dashboard.DashboardAccessTrendResp">
SELECT
Expand All @@ -105,19 +117,6 @@
ORDER BY name
</select>

<select id="selectListDashboardAnalysisGeo" resultType="top.continew.admin.system.model.resp.dashboard.DashboardChartCommonResp">
SELECT
CASE
WHEN POSITION(' ' IN address) > 0 THEN SUBSTRING(address FROM 1 FOR POSITION(' ' IN address) - 1)
ELSE address
END AS name,
COUNT(DISTINCT ip) AS value
FROM sys_log
GROUP BY name
ORDER BY value DESC
LIMIT #{top}
</select>

<select id="selectListDashboardAnalysisModule"
resultType="top.continew.admin.system.model.resp.dashboard.DashboardChartCommonResp">
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.system.model.resp.dashboard.*;
import top.continew.admin.system.model.resp.dashboard.DashboardAccessTrendResp;
import top.continew.admin.system.model.resp.dashboard.DashboardChartCommonResp;
import top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp;
import top.continew.admin.system.model.resp.dashboard.DashboardOverviewCommonResp;
import top.continew.admin.system.service.DashboardService;
import top.continew.starter.core.util.validate.ValidationUtils;
import top.continew.starter.log.core.annotation.Log;

import java.io.IOException;
import java.util.List;

/**
Expand Down Expand Up @@ -72,6 +76,15 @@ public DashboardOverviewCommonResp getOverviewIp() {
return dashboardService.getOverviewIp();
}

@Operation(summary = "查询地域分析", description = "查询地域分析")
@GetMapping("/analysis/geo")
@CachePenetrationProtect
@CacheRefresh(refresh = 7200)
@Cached(key = "'GEO'", name = CacheConstants.DASHBOARD_KEY_PREFIX, cacheType = CacheType.BOTH, syncLocal = true)
public List<DashboardChartCommonResp> getAnalysisGeo() throws IOException {
return dashboardService.getAnalysisGeo();
}

@Operation(summary = "查询访问趋势信息", description = "查询访问趋势信息")
@Parameter(name = "days", description = "日期数", example = "30", in = ParameterIn.PATH)
@GetMapping("/access/trend/{days}")
Expand All @@ -92,15 +105,6 @@ public List<DashboardChartCommonResp> getAnalysisTimeslot() {
return dashboardService.getAnalysisTimeslot();
}

@Operation(summary = "查询地域分析", description = "查询地域分析")
@GetMapping("/analysis/geo")
@CachePenetrationProtect
@CacheRefresh(refresh = 7200)
@Cached(key = "'GEO'", name = CacheConstants.DASHBOARD_KEY_PREFIX, cacheType = CacheType.BOTH, syncLocal = true)
public List<DashboardChartCommonResp> getAnalysisGeo() {
return dashboardService.getAnalysisGeo();
}

@Operation(summary = "查询模块分析", description = "查询模块分析")
@GetMapping("/analysis/module")
@CachePenetrationProtect
Expand Down
1 change: 1 addition & 0 deletions continew-webapi/src/main/resources/china.json

Large diffs are not rendered by default.

0 comments on commit e0e157f

Please sign in to comment.