opt:1.更新后台管理首页看板功能

This commit is contained in:
2026-03-20 15:31:04 +08:00
parent bbd09035a8
commit 9498380c6f
12 changed files with 257 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
package org.nl.monitor.dashboard.controller;
import jakarta.annotation.Resource;
import org.nl.monitor.dashboard.service.DashboardService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
/**
* @author dsh
* 2026/3/20
*/
@RestController
@RequestMapping("/dashboard")
public class DashboardController {
@Resource
private DashboardService dashboardService;
@GetMapping("/queryDashboardData")
public ResponseEntity<Object> queryDashboardData(@RequestParam String timeRange) {
return new ResponseEntity<>(dashboardService.queryDashboardData(timeRange), HttpStatus.OK);
}
}

View File

@@ -0,0 +1,41 @@
package org.nl.monitor.dashboard.dto;
import lombok.Data;
/**
* @author dsh
* 2026/3/20
*/
@Data
public class DashboardDataDto {
/**
* 总任务数
*/
private Long totalTasks;
/**
* 执行中的任务数
*/
private Long executingTasks;
/**
* 已完成的任务数
*/
private Long completedTasks;
/**
* 已取消的任务数
*/
private Long canceledTasks;
/**
* 完成率 (%)
*/
private Double completionRate;
/**
* 取消率 (%)
*/
private Double cancellationRate;
}

View File

@@ -0,0 +1,16 @@
package org.nl.monitor.dashboard.param;
import lombok.Data;
/**
* @author dsh
* 2026/3/20
*/
@Data
public class DashboardQueryParam {
/**
* 时间范围类型: today(今天), week(一周), month(一个月)
*/
private String timeRange;
}

View File

@@ -0,0 +1,17 @@
package org.nl.monitor.dashboard.service;
import org.nl.response.WebResponse;
/**
* @author dsh
* 2026/3/20
*/
public interface DashboardService {
/**
* 查询看板数据
* @param timeRange 时间范围: today(今天), week(一周), month(一个月)
* @return 看板数据
*/
WebResponse queryDashboardData(String timeRange);
}

View File

@@ -0,0 +1,80 @@
package org.nl.monitor.dashboard.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.nl.api.task.api.TaskAPI;
import org.nl.config.language.LangProcess;
import org.nl.exception.BadRequestException;
import org.nl.monitor.dashboard.dto.DashboardDataDto;
import org.nl.monitor.dashboard.service.DashboardService;
import org.nl.response.WebResponse;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
/**
* @author dsh
* 2026/3/20
*/
@Slf4j
@Service
public class DashboardServiceImpl implements DashboardService {
@Resource
private TaskAPI taskAPI;
@Override
public WebResponse queryDashboardData(String timeRange) {
if (StrUtil.isBlank(timeRange)) {
throw new BadRequestException(LangProcess.msg("param_is_null"));
}
// 获取时间范围
String[] timeRange_arr = getTimeRange(timeRange);
String startTime = timeRange_arr[0];
String endTime = timeRange_arr[1];
// 从数据库查询指定时间范围内的任务统计数据
Map<String, Object> statisticsData = taskAPI.queryTasksByTimeRange(startTime, endTime);
// 构建返回数据
DashboardDataDto dto = new DashboardDataDto();
dto.setTotalTasks(((Number) statisticsData.get("totalTasks")).longValue());
dto.setExecutingTasks(((Number) statisticsData.get("executingTasks")).longValue());
dto.setCompletedTasks(((Number) statisticsData.get("completedTasks")).longValue());
dto.setCanceledTasks(((Number) statisticsData.get("canceledTasks")).longValue());
dto.setCompletionRate(((Number) statisticsData.get("completionRate")).doubleValue());
dto.setCancellationRate(((Number) statisticsData.get("cancellationRate")).doubleValue());
return WebResponse.requestParamOk(dto);
}
/**
* 根据时间范围获取开始和结束时间
* @return [startTime, endTime]
*/
private String[] getTimeRange(String timeRange) {
Date now = new Date();
Date startDate;
Date endDate = now;
switch (timeRange) {
case "today":
startDate = DateUtil.beginOfDay(now);
break;
case "week":
startDate = DateUtil.offsetDay(DateUtil.beginOfDay(now), -7);
break;
case "month":
startDate = DateUtil.offsetDay(DateUtil.beginOfDay(now), -30);
break;
default:
throw new BadRequestException("Invalid timeRange: " + timeRange);
}
return new String[]{DateUtil.formatDateTime(startDate), DateUtil.formatDateTime(endDate)};
}
}