Skip to content

Implement monitor statistics API with flexible time range queries#2

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-monitor-metrics-endpoint
Draft

Implement monitor statistics API with flexible time range queries#2
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-monitor-metrics-endpoint

Conversation

Copy link

Copilot AI commented Feb 5, 2026

Adds /api/monitor/getByMillRange endpoint to query historical system metrics (CPU, memory, disk, network) within a time range specified by millisecond timestamps.

Implementation

  • Entity & Persistence: SysMainMetrics entity with MyBatis Plus mapper using custom aggregation SQL that groups metrics by configurable time intervals
  • Service Layer: SysMetricsStorageService automatically calculates grouping intervals (60s to 86400s) based on query range to optimize data volume
  • Controller: Accepts parameters via query string (?startMills=X&endMills=Y) or JSON body, merging both sources with body taking precedence
  • Error Handling: Returns 400 for missing parameters, 500 for query failures

Example Usage

// GET with query params
GET /api/monitor/getByMillRange?startMills=1707110400000&endMills=1707114000000

// POST with JSON body
POST /api/monitor/getByMillRange
{
  "startMills": 1707110400000,
  "endMills": 1707114000000
}

Additional Files

  • Database schema: migration/monitor/create_sys_main_metrics_table.sql
  • API documentation: docs/MONITOR_STATISTICS_API.md
  • Configuration: MyBatis mapper XML location added to application.properties

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • mybatis.org
    • Triggering command: `/opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/FoxNAServer/.codeql-scratch/dbs/java/working/files-to-index76624110352171325.list --sourceArchiveDir=/home/REDACTED/work/FoxNAServer/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/FoxNAServer/.codeql-scratch/dbs/java/trap/java e and controller

Co-authored-by: ProgramCX <124671913+ProgramCX@users.noreply.g-s k/_temp/ghcca-node/node/bin/git` (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

补充完整getRecentStatistics

The user has attached the following files from their workspace:

  • src/main/java/cn/programcx/foxnaserver/api/monitor/MonitorStatisticsController.java

TITLE: Fixing monitor metrics endpoint mapping and request parameter handling

USER INTENT: Resolve errors when calling /api/monitor/getByMillRange by properly mapping the endpoint and handling required parameters (startMills, endMills).

TASK DESCRIPTION:

  • Ensure the monitor statistics endpoint is reachable under /api/monitor/getByMillRange.
  • Handle requests that may provide startMills and endMills via query parameters (GET) or JSON body (POST).
  • Return clear error messaging if required parameters are missing.

EXISTING:

  • 404 errors were occurring for /api/monitor/getByMillRange.
  • Updated the controller to include a base path with @RequestMapping("/api/monitor").
  • Added parameter binding for startMills/endMills.

PENDING:

  • Verify endpoint now responds correctly when parameters are supplied via query string or JSON body.
  • If client still omits both values, it will return a 400 with a descriptive message; ensure the client supplies them.

CODE STATE:

  • src/main/java/cn/programcx/foxnaserver/api/monitor/MonitorStatisticsController.java
@Slf4j
@RestController
@RequestMapping("/api/monitor")
@ResponseBody
public class MonitorStatisticsController {

    @Autowired
    private SysMetricsStorageService sysMetricsStorageService;

    @RequestMapping(value = "/getByMillRange", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<?> getRecentStatisticsByMillRange(
            @RequestParam(value = "startMills", required = false) Long startMills,
            @RequestParam(value = "endMills", required = false) Long endMills,
            @RequestBody(required = false) MetricsRangeRequest payload) {
        if (payload != null) {
            if (startMills == null) startMills = payload.getStartMills();
            if (endMills == null) endMills = payload.getEndMills();
        }
        if (startMills == null || endMills == null) {
            return ResponseEntity.badRequest()
                .body("startMills and endMills are required via query params or JSON body");
        }
        LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startMills), ZoneId.systemDefault());
        LocalDateTime endTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(endMills), ZoneId.systemDefault());
        try {
            List<SysMainMetrics> sysMainMetrics = sysMetricsStorageService.selectRecentMetrics(startTime, endTime);
            return ResponseEntity.ok(sysMainMetrics);
        } catch (Exception e) {
            log.info("获取最近监控数据失败:{}", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("获取最近监控数据失败! ");
        }
    }

    @Getter
    private static class MetricsRangeRequest {
        private Long startMills;
        private Long endMills;
    }
}
  • src/main/java/cn/programcx/foxnaserver/service/monitor/SysMetricsStorageService.java
public List<SysMainMetrics> selectRecentMetrics(LocalDateTime startTime, LocalDateTime endTime) {
    int groupSeconds = calculateGroupSeconds(startTime, endTime);
    log.info("正在查询时间范围为 {} 到 {} 的系统指标,分组秒数为 {}", startTime, endTime, groupSeconds);
    return sysMainMetricsMapper.selectRecentMetrics(startTime, endTime, groupSeconds);
}

RELEVANT CODE/DOCUMENTATION SNIPPETS:

  • Endpoint accepts both GET and POST:
    • GET example: /api/monitor/getByMillRange?startMills=1707110400000&endMills=1707114000000
    • POST example (JSON): { "startMills": 1707110400000, "endMills": 1707114000000 }
  • If both startMills and endMills are missing, returns HTTP 400 with a descriptive message.

OTHER NOTES:

  • The 400 error seen was due to missing required parameters; the updated controller now allows optional params and body payload, combining them if provided. Ensure the client supplies one or both sources.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 5, 2026 09:10
… controller

Co-authored-by: ProgramCX <124671913+ProgramCX@users.noreply.github.com>
Co-authored-by: ProgramCX <124671913+ProgramCX@users.noreply.github.com>
…stics

Co-authored-by: ProgramCX <124671913+ProgramCX@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix monitor metrics endpoint mapping and request parameter handling Implement monitor statistics API with flexible time range queries Feb 5, 2026
Copilot AI requested a review from ProgramCX February 5, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants