EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
mertricServices.js
Go to the documentation of this file.
1/**
2 * @file Agent Metrics Data Access Service
3 * @description
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).
8 *
9 * Key features:
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
13 *
14 * Typical use cases:
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
19 *
20 * Database schema:
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
27 */
28
29const pool = require('./db');
30
31/**
32 * Retrieves the latest (most recent) metrics snapshot for a specific agent.
33 *
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.
37 * @async
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
41 * @example
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
46 */
47
48// Latest metrics (1 row)
49/**
50 *
51 * @param agent_uuid
52 */
53async function getLatestAgentMetrics(agent_uuid) {
54 const res = await pool.query(
55 `SELECT *
56 FROM agent_metrics
57 WHERE agent_uuid = $1
58 ORDER BY ts DESC
59 LIMIT 1`,
60 [agent_uuid]
61 );
62
63 return res.rows[0] || null;
64}
65
66/**
67 * Retrieves historical metrics data for a specific agent within a time window.
68 *
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.
73 * @async
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
78 * @example
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)
82 * @example
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 }))
86 */
87
88// Historical metrics (graph)
89/**
90 *
91 * @param agent_uuid
92 * @param days
93 */
94async function getAgentMetricsHistory(agent_uuid, days = 30) {
95 const res = await pool.query(
96 `SELECT *
97 FROM agent_metrics
98 WHERE agent_uuid = $1
99 AND ts > NOW() - INTERVAL '${days} days'
100 ORDER BY ts ASC`,
101 [agent_uuid]
102 );
103
104 return res.rows;
105}
106
107module.exports = {
108 getLatestAgentMetrics,
109 getAgentMetricsHistory
110};