2 * @file Agent Metrics Data Access Service
4 * Provides database query functions for retrieving agent performance metrics data.
5 * This module serves as the data access layer for the agent_metrics table, supporting
6 * both real-time monitoring (latest metrics) and historical analysis (time-series data
7 * for graphs and trend analysis).
10 * - **Latest metrics**: Retrieve most recent performance snapshot for an agent
11 * - **Historical data**: Time-series queries for graphing and analysis
12 * - **Configurable time windows**: Historical queries support custom day ranges
15 * - Dashboard widgets showing current agent status (CPU, memory, disk, network)
16 * - Performance graphs over time (30-day default, configurable)
17 * - Alerting based on metric thresholds
18 * - Capacity planning and trend analysis
21 * - Table: `agent_metrics`
22 * - Key columns: agent_uuid, ts (timestamp), cpu_percent, memory_percent, disk_percent, network_io
23 * - Indexed on: agent_uuid, ts for efficient time-series queries
24 * @module services/metricServices
25 * @requires ./db - PostgreSQL database pool
26 * @note This file is misnamed as "mertricServices.js" (typo) but kept for compatibility
29const pool = require('./db');
32 * Retrieves the latest (most recent) metrics snapshot for a specific agent.
34 * Returns the single most recent metrics row from the agent_metrics table, containing
35 * performance data like CPU usage, memory usage, disk space, network I/O, and timestamp.
36 * Used for real-time agent status displays and dashboards.
38 * @function getLatestAgentMetrics
39 * @param {string} agent_uuid - Unique identifier of the agent (UUID format)
40 * @returns {Promise<object | null>} Latest metrics object with columns from agent_metrics table, or null if no metrics found
42 * const latest = await getLatestAgentMetrics('550e8400-e29b-41d4-a716-446655440000');
43 * console.log(latest.cpu_percent); // e.g., 45.2
44 * console.log(latest.memory_percent); // e.g., 78.5
45 * console.log(latest.ts); // e.g., 2025-01-15T10:30:00.000Z
48// Latest metrics (1 row)
53async function getLatestAgentMetrics(agent_uuid) {
54 const res = await pool.query(
63 return res.rows[0] || null;
67 * Retrieves historical metrics data for a specific agent within a time window.
69 * Returns time-series array of all metrics data points within the specified number of
70 * days, ordered chronologically (oldest to newest). Used for generating performance
71 * graphs, trend analysis, and historical reporting. Default 30-day window provides
72 * sufficient data for monthly performance review.
74 * @function getAgentMetricsHistory
75 * @param {string} agent_uuid - Unique identifier of the agent (UUID format)
76 * @param {number} [days=30] - Number of days of historical data to retrieve (default: 30)
77 * @returns {Promise<object[]>} Array of metrics objects ordered by timestamp (ascending), empty array if no data
79 * // Get 30 days of history (default)
80 * const history = await getAgentMetricsHistory('550e8400-e29b-41d4-a716-446655440000');
81 * console.log(history.length); // e.g., 43200 (30 days * 24 hours * 60 minutes if sampled per minute)
83 * // Get last 7 days only
84 * const weekHistory = await getAgentMetricsHistory('550e8400-e29b-41d4-a716-446655440000', 7);
85 * // Use for graphing: weekHistory.map(m => ({ time: m.ts, cpu: m.cpu_percent }))
88// Historical metrics (graph)
94async function getAgentMetricsHistory(agent_uuid, days = 30) {
95 const res = await pool.query(
99 AND ts > NOW() - INTERVAL '${days} days'
108 getLatestAgentMetrics,
109 getAgentMetricsHistory