dokploy/apps/monitoring/database/containers.go
Mauricio Siu 74a0f5e992
Feat/monitoring (#1267) Cloud Version
* feat: add start monitoring remote servers

* reafctor: update

* refactor: update

* refactor: update

* refactor: update

* refactor: update

* refactor: update

* refactor: update

* refactor:

* refactor: add metrics

* feat: add disk monitoring

* refactor: translate to english

* refacotor: add stats

* refactor: remove color

* feat: add log server metrics

* refactor: remove unused deps

* refactor: add origin

* refactor: add logs

* refactor: update

* feat: add series monitoring

* refactor: add system monitoring

* feat: add benchmark to optimize data

* refactor: update fn

* refactor: remove comments

* refactor: update

* refactor: exclude items

* feat: add refresh rate

* feat: add monitoring remote servers

* refactor: update

* refactor: remove unsued volumes

* refactor: update monitoring

* refactor: add more presets

* feat: add container metrics

* feat: add docker monitoring

* refactor: update conversion

* refactor: remove unused code

* refactor: update

* refactor: add docker compose logs

* refactor: add docker cli

* refactor: add install curl

* refactor: add get update

* refactor: add monitoring remote servers

* refactor: add containers config

* feat: add container specification

* refactor: update path

* refactor: add server filter

* refactor: simplify logic

* fix: verify if file exist before get stats

* refactor: update

* refactor: remove unused deps

* test: add test for containers

* refactor: update

* refactor add memory collector

* refactor: update

* refactor: update

* refactor: update

* refactor: remove

* refactor: add memory

* refactor: add server memory usage

* refactor: change memory

* refactor: update

* refactor: update

* refactor: add container metrics

* refactor: comment code

* refactor: mount proc bind

* refactor: change interval with node cron

* refactor: remove opening file

* refactor: use streams

* refactor: remove unused ws

* refactor: disable live when is all

* refactor: add sqlite

* refactor: update

* feat: add golang benchmark

* refactor: update go

* refactor: update dockerfile

* refactor: update db

* refactor: add env

* refactor: separate logic

* refactor: split logic

* refactor: update logs

* refactor: update dockerfile

* refactor: hide .env

* refactor: update

* chore: hide ,.ebnv

* refactor: add end angle

* refactor: update

* refactor: update

* refactor: update

* refactor: update

* refactor: update

* refactor: update monitoring

* refactor: add mount db

* refactor: add metrics and url callback

* refactor: add middleware

* refactor: add threshold property

* feat: add memory and cpu threshold notification

* feat: send notifications to the server

* feat: add metrics for dokploy server

* refactor: add dokploy server to monitoring

* refactor: update methods

* refactor: add admin to useeffect

* refactor: stop monitoring containers if elements are 0

* refactor: cancel request if appName is empty

* refactor: reuse methods

* chore; add feat monitoring

* refactor: set base url

* refactor: adjust monitoring

* refactor: delete migrations

* feat: add columns

* fix: add missing flag

* refactor: add free metrics

* refactor: add paid monitoring

* refactor: update methods

* feat: improve ui

* feat: add container stats

* refactor: add all container metrics

* refactor: add color primary

* refactor: change default rate limiting refresher

* refactor: update retention days

* refactor: use json instead of individual properties

* refactor: lint

* refactor: pass json env

* refactor: update

* refactor: delete

* refactor: update

* refactor: fix types

* refactor: add retention days

* chore: add license

* refactor: create db

* refactor: update path

* refactor: update setup

* refactor: update

* refactor: create files

* refactor: update

* refactor: delete

* refactor: update

* refactor: update token metrics

* fix: typechecks

* refactor: setup web server

* refactor: update error handling and add monitoring

* refactor: add local storage save

* refactor: add spacing

* refactor: update

* refactor: upgrade drizzle

* refactor: delete

* refactor: uppgrade drizzle kit

* refactor: update search with jsonB

* chore: upgrade drizzle

* chore: update packages

* refactor: add missing type

* refactor: add serverType

* refactor: update url

* refactor: update

* refactor: update

* refactor: hide monitoring on self hosted

* refactor: update server

* refactor: update

* refactor: update

* refactor: pin node version
2025-02-02 14:08:06 -06:00

161 lines
4.1 KiB
Go

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"`
}