用户访问数量
This commit is contained in:
@@ -6,14 +6,20 @@ import com.banben.modules.admin.support.AdminAccess;
|
||||
import com.banben.modules.auth.dto.LoginLogQuery;
|
||||
import com.banben.modules.auth.entity.LoginLog;
|
||||
import com.banben.modules.auth.service.LoginLogService;
|
||||
import com.banben.modules.auth.vo.AccessDimensionItem;
|
||||
import com.banben.modules.auth.vo.AccessOverview;
|
||||
import com.banben.modules.auth.vo.AccessTrendPoint;
|
||||
import com.banben.modules.auth.vo.LoginLogSummary;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台管理 - 登录日志。
|
||||
*/
|
||||
@@ -39,4 +45,32 @@ public class LoginLogController {
|
||||
adminAccess.require();
|
||||
return Result.success(loginLogService.summary());
|
||||
}
|
||||
|
||||
@Operation(summary = "网络访问概览:历史/本月/本周/今日 的人次与人数")
|
||||
@GetMapping("/access-overview")
|
||||
public Result<AccessOverview> accessOverview() {
|
||||
adminAccess.require();
|
||||
return Result.success(loginLogService.accessOverview());
|
||||
}
|
||||
|
||||
@Operation(summary = "网络访问趋势:按 小时/日/周/月/年 分桶(可指定时段)")
|
||||
@GetMapping("/access-trend")
|
||||
public Result<List<AccessTrendPoint>> accessTrend(
|
||||
@RequestParam(defaultValue = "hour") String granularity,
|
||||
@RequestParam(required = false) String start,
|
||||
@RequestParam(required = false) String end) {
|
||||
adminAccess.require();
|
||||
return Result.success(loginLogService.accessTrend(granularity, start, end));
|
||||
}
|
||||
|
||||
@Operation(summary = "网络访问维度统计:按 IP/单位/账号/类型 分组(可指定时段)")
|
||||
@GetMapping("/access-dimension")
|
||||
public Result<List<AccessDimensionItem>> accessDimension(
|
||||
@RequestParam(defaultValue = "ip") String dimension,
|
||||
@RequestParam(required = false) String start,
|
||||
@RequestParam(required = false) String end,
|
||||
@RequestParam(defaultValue = "20") int limit) {
|
||||
adminAccess.require();
|
||||
return Result.success(loginLogService.accessDimension(dimension, start, end, limit));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,4 +121,140 @@ public class LoginLogServiceImpl implements LoginLogService {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// ==================== 网络访问统计 ====================
|
||||
|
||||
@Override
|
||||
public AccessOverview accessOverview() {
|
||||
AccessOverview o = new AccessOverview();
|
||||
try {
|
||||
long[] history = countPair(null);
|
||||
o.setHistoryCount(history[0]);
|
||||
o.setHistoryUsers(history[1]);
|
||||
|
||||
long[] month = countPair(LocalDate.now().withDayOfMonth(1).atStartOfDay());
|
||||
o.setMonthCount(month[0]);
|
||||
o.setMonthUsers(month[1]);
|
||||
|
||||
long[] week = countPair(LocalDate.now().with(DayOfWeek.MONDAY).atStartOfDay());
|
||||
o.setWeekCount(week[0]);
|
||||
o.setWeekUsers(week[1]);
|
||||
|
||||
long[] today = countPair(LocalDate.now().atStartOfDay());
|
||||
o.setTodayCount(today[0]);
|
||||
o.setTodayUsers(today[1]);
|
||||
} catch (Exception e) {
|
||||
log.warn("统计网络访问概览失败:{}", e.getMessage());
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AccessTrendPoint> accessTrend(String granularity, String start, String end) {
|
||||
String expr = granularityExpr(granularity);
|
||||
QueryWrapper<LoginLog> w = new QueryWrapper<>();
|
||||
w.select(expr + " AS bucket", "COUNT(*) AS cnt", "COUNT(DISTINCT user_id) AS users");
|
||||
applyRange(w, start, end);
|
||||
w.groupBy(expr).orderByAsc("bucket");
|
||||
List<AccessTrendPoint> list = new ArrayList<>();
|
||||
try {
|
||||
for (Map<String, Object> m : logMapper.selectMaps(w)) {
|
||||
Object b = m.get("bucket");
|
||||
list.add(new AccessTrendPoint(b == null ? "" : b.toString(),
|
||||
toLong(m.get("cnt")), toLong(m.get("users"))));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("统计网络访问趋势失败:{}", e.getMessage());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AccessDimensionItem> accessDimension(String dimension, String start, String end, int limit) {
|
||||
String col = "ip";
|
||||
if ("org".equals(dimension)) {
|
||||
col = "org_name";
|
||||
} else if ("account".equals(dimension)) {
|
||||
col = "username";
|
||||
} else if ("type".equals(dimension)) {
|
||||
col = "online";
|
||||
}
|
||||
QueryWrapper<LoginLog> w = new QueryWrapper<>();
|
||||
w.select(col + " AS name", "COUNT(*) AS cnt", "COUNT(DISTINCT user_id) AS users");
|
||||
// 字符串维度排除空值;type 维度(online 为整数)仅排除 NULL,避免 integer <> '' 报错
|
||||
if ("type".equals(dimension)) {
|
||||
w.isNotNull(col);
|
||||
} else {
|
||||
w.isNotNull(col).ne(col, "");
|
||||
}
|
||||
applyRange(w, start, end);
|
||||
w.groupBy(col).orderByDesc("cnt").last("LIMIT " + Math.max(1, limit));
|
||||
List<AccessDimensionItem> list = new ArrayList<>();
|
||||
try {
|
||||
for (Map<String, Object> m : logMapper.selectMaps(w)) {
|
||||
Object raw = m.get("name");
|
||||
String name = raw == null ? "未知" : raw.toString();
|
||||
if ("type".equals(dimension)) {
|
||||
name = "1".equals(name) ? "在线" : "0".equals(name) ? "离线" : name;
|
||||
}
|
||||
list.add(new AccessDimensionItem(name, toLong(m.get("cnt")), toLong(m.get("users"))));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("统计网络访问维度失败:{}", e.getMessage());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/** 人次 + 去重人数(按给定起始时间)。 */
|
||||
private long[] countPair(LocalDateTime ge) {
|
||||
QueryWrapper<LoginLog> w = new QueryWrapper<>();
|
||||
w.select("COUNT(*) AS c", "COUNT(DISTINCT user_id) AS u");
|
||||
if (ge != null) {
|
||||
w.ge("login_time", ge);
|
||||
}
|
||||
List<Map<String, Object>> maps = logMapper.selectMaps(w);
|
||||
long c = 0, u = 0;
|
||||
if (!maps.isEmpty()) {
|
||||
Map<String, Object> m = maps.get(0);
|
||||
c = toLong(m.get("c"));
|
||||
u = toLong(m.get("u"));
|
||||
}
|
||||
return new long[]{c, u};
|
||||
}
|
||||
|
||||
/** 时间分桶表达式(PostgreSQL)。 */
|
||||
private String granularityExpr(String g) {
|
||||
if ("day".equals(g)) {
|
||||
return "to_char(login_time, 'YYYY-MM-DD')";
|
||||
}
|
||||
if ("week".equals(g)) {
|
||||
return "to_char(date_trunc('week', login_time), 'YYYY-MM-DD')";
|
||||
}
|
||||
if ("month".equals(g)) {
|
||||
return "to_char(login_time, 'YYYY-MM')";
|
||||
}
|
||||
if ("year".equals(g)) {
|
||||
return "to_char(login_time, 'YYYY')";
|
||||
}
|
||||
// 默认按小时(含日期,避免跨天合并)
|
||||
return "to_char(login_time, 'YYYY-MM-DD HH24')";
|
||||
}
|
||||
|
||||
/** 时间区间条件:start 含当天 00:00,end 含当天 23:59:59。 */
|
||||
private void applyRange(QueryWrapper<LoginLog> w, String start, String end) {
|
||||
if (StrUtil.isNotBlank(start)) {
|
||||
w.ge("login_time", start.trim());
|
||||
}
|
||||
if (StrUtil.isNotBlank(end)) {
|
||||
String e = end.trim();
|
||||
if (e.length() == 10) {
|
||||
e = e + " 23:59:59";
|
||||
}
|
||||
w.le("login_time", e);
|
||||
}
|
||||
}
|
||||
|
||||
private long toLong(Object value) {
|
||||
return value instanceof Number number ? number.longValue() : 0L;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user