File size: 4,158 Bytes
b80fc11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
package database
import (
"encoding/json"
"fmt"
"strings"
)
func (db *DB) InitContainerMetricsTable() error {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS container_metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
container_id TEXT NOT NULL,
container_name TEXT NOT NULL,
metrics_json TEXT NOT NULL
)
`)
if err != nil {
return fmt.Errorf("error creating container_metrics table: %v", err)
}
// Crear índices para mejorar el rendimiento
_, err = db.Exec(`CREATE INDEX IF NOT EXISTS idx_container_metrics_timestamp ON container_metrics(timestamp)`)
if err != nil {
return fmt.Errorf("error creating timestamp index: %v", err)
}
_, err = db.Exec(`CREATE INDEX IF NOT EXISTS idx_container_metrics_name ON container_metrics(container_name)`)
if err != nil {
return fmt.Errorf("error creating name index: %v", err)
}
return nil
}
func (db *DB) SaveContainerMetric(metric *ContainerMetric) error {
metricsJSON, err := json.Marshal(metric)
if err != nil {
return fmt.Errorf("error marshaling metrics: %v", err)
}
_, err = db.Exec(`
INSERT INTO container_metrics (timestamp, container_id, container_name, metrics_json)
VALUES (?, ?, ?, ?)
`, metric.Timestamp, metric.ID, metric.Name, string(metricsJSON))
return err
}
func (db *DB) GetLastNContainerMetrics(containerName string, limit int) ([]ContainerMetric, error) {
name := strings.TrimPrefix(containerName, "/")
parts := strings.Split(name, "-")
if len(parts) > 1 {
containerName = strings.Join(parts[:len(parts)-1], "-")
}
query := `
WITH recent_metrics AS (
SELECT metrics_json
FROM container_metrics
WHERE container_name LIKE ? || '%'
ORDER BY timestamp DESC
LIMIT ?
)
SELECT metrics_json FROM recent_metrics ORDER BY json_extract(metrics_json, '$.timestamp') ASC
`
rows, err := db.Query(query, containerName, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var metrics []ContainerMetric
for rows.Next() {
var metricsJSON string
err := rows.Scan(&metricsJSON)
if err != nil {
return nil, err
}
var metric ContainerMetric
if err := json.Unmarshal([]byte(metricsJSON), &metric); err != nil {
return nil, err
}
metrics = append(metrics, metric)
}
return metrics, nil
}
func (db *DB) GetAllMetricsContainer(containerName string) ([]ContainerMetric, error) {
name := strings.TrimPrefix(containerName, "/")
parts := strings.Split(name, "-")
if len(parts) > 1 {
containerName = strings.Join(parts[:len(parts)-1], "-")
}
query := `
WITH recent_metrics AS (
SELECT metrics_json
FROM container_metrics
WHERE container_name LIKE ? || '%'
ORDER BY timestamp DESC
)
SELECT metrics_json FROM recent_metrics ORDER BY json_extract(metrics_json, '$.timestamp') ASC
`
rows, err := db.Query(query, containerName)
if err != nil {
return nil, err
}
defer rows.Close()
var metrics []ContainerMetric
for rows.Next() {
var metricsJSON string
err := rows.Scan(&metricsJSON)
if err != nil {
return nil, err
}
var metric ContainerMetric
if err := json.Unmarshal([]byte(metricsJSON), &metric); err != nil {
return nil, err
}
metrics = append(metrics, metric)
}
return metrics, nil
}
type ContainerMetric struct {
Timestamp string `json:"timestamp"`
CPU float64 `json:"CPU"`
Memory MemoryMetric `json:"Memory"`
Network NetworkMetric `json:"Network"`
BlockIO BlockIOMetric `json:"BlockIO"`
Container string `json:"Container"`
ID string `json:"ID"`
Name string `json:"Name"`
}
type MemoryMetric struct {
Percentage float64 `json:"percentage"`
Used float64 `json:"used"`
Total float64 `json:"total"`
UsedUnit string `json:"usedUnit"`
TotalUnit string `json:"totalUnit"`
}
type NetworkMetric struct {
Input float64 `json:"input"`
Output float64 `json:"output"`
InputUnit string `json:"inputUnit"`
OutputUnit string `json:"outputUnit"`
}
type BlockIOMetric struct {
Read float64 `json:"read"`
Write float64 `json:"write"`
ReadUnit string `json:"readUnit"`
WriteUnit string `json:"writeUnit"`
}
|