215 lines
5.5 KiB
Go
215 lines
5.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// TrackedDatabase represents a monitored database
|
|
type TrackedDatabase struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
DSN string `json:"dsn"`
|
|
AddedAt int64 `json:"added_at"`
|
|
}
|
|
|
|
// AlertRule represents a user-configured alert threshold
|
|
type AlertRule struct {
|
|
ID int64 `json:"id"`
|
|
DBName string `json:"db_name"` // empty = all databases
|
|
Metric string `json:"metric"`
|
|
Operator string `json:"operator"`
|
|
Threshold float64 `json:"threshold"`
|
|
Enabled bool `json:"enabled"`
|
|
CooldownMin int `json:"cooldown_min"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
}
|
|
|
|
// NotificationChannel represents a notification target
|
|
type NotificationChannel struct {
|
|
ID int64 `json:"id"`
|
|
ChannelType string `json:"channel_type"`
|
|
Config json.RawMessage `json:"config"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
// TelegramConfig holds Telegram-specific settings
|
|
type TelegramConfig struct {
|
|
BotToken string `json:"bot_token"`
|
|
ChatID string `json:"chat_id"`
|
|
}
|
|
|
|
// ---- Tracked Databases CRUD ----
|
|
|
|
func (s *Store) ListTrackedDatabases() ([]TrackedDatabase, error) {
|
|
rows, err := s.db.Query("SELECT id, name, dsn, added_at FROM tracked_databases ORDER BY added_at ASC")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var dbs []TrackedDatabase
|
|
for rows.Next() {
|
|
var d TrackedDatabase
|
|
if err := rows.Scan(&d.ID, &d.Name, &d.DSN, &d.AddedAt); err != nil {
|
|
continue
|
|
}
|
|
dbs = append(dbs, d)
|
|
}
|
|
return dbs, rows.Err()
|
|
}
|
|
|
|
func (s *Store) AddTrackedDatabase(name, dsn string) (int64, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
res, err := s.db.Exec(
|
|
"INSERT INTO tracked_databases (name, dsn, added_at) VALUES (?, ?, ?)",
|
|
name, dsn, time.Now().Unix(),
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (s *Store) RemoveTrackedDatabase(id int64) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
_, err := s.db.Exec("DELETE FROM tracked_databases WHERE id = ?", id)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) GetTrackedDatabase(id int64) (*TrackedDatabase, error) {
|
|
var d TrackedDatabase
|
|
err := s.db.QueryRow(
|
|
"SELECT id, name, dsn, added_at FROM tracked_databases WHERE id = ?", id,
|
|
).Scan(&d.ID, &d.Name, &d.DSN, &d.AddedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
return &d, err
|
|
}
|
|
|
|
// ---- Alert Rules CRUD ----
|
|
|
|
func (s *Store) ListAlertRules() ([]AlertRule, error) {
|
|
rows, err := s.db.Query(
|
|
"SELECT id, db_name, metric, operator, threshold, enabled, cooldown_min, created_at FROM alert_rules ORDER BY created_at ASC",
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var rules []AlertRule
|
|
for rows.Next() {
|
|
var r AlertRule
|
|
var enabled int
|
|
if err := rows.Scan(&r.ID, &r.DBName, &r.Metric, &r.Operator, &r.Threshold, &enabled, &r.CooldownMin, &r.CreatedAt); err != nil {
|
|
continue
|
|
}
|
|
r.Enabled = enabled == 1
|
|
rules = append(rules, r)
|
|
}
|
|
return rules, rows.Err()
|
|
}
|
|
|
|
func (s *Store) AddAlertRule(dbName, metric, operator string, threshold float64, cooldownMin int) (int64, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
res, err := s.db.Exec(
|
|
"INSERT INTO alert_rules (db_name, metric, operator, threshold, enabled, cooldown_min, created_at) VALUES (?, ?, ?, ?, 1, ?, ?)",
|
|
dbName, metric, operator, threshold, cooldownMin, time.Now().Unix(),
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (s *Store) UpdateAlertRule(id int64, dbName, metric, operator string, threshold float64, enabled bool, cooldownMin int) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
enabledInt := 0
|
|
if enabled {
|
|
enabledInt = 1
|
|
}
|
|
_, err := s.db.Exec(
|
|
"UPDATE alert_rules SET db_name=?, metric=?, operator=?, threshold=?, enabled=?, cooldown_min=? WHERE id=?",
|
|
dbName, metric, operator, threshold, enabledInt, cooldownMin, id,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) DeleteAlertRule(id int64) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
_, err := s.db.Exec("DELETE FROM alert_rules WHERE id = ?", id)
|
|
return err
|
|
}
|
|
|
|
// ---- Notification Channels CRUD ----
|
|
|
|
func (s *Store) GetNotificationChannel(channelType string) (*NotificationChannel, error) {
|
|
var c NotificationChannel
|
|
var configStr string
|
|
var enabled int
|
|
err := s.db.QueryRow(
|
|
"SELECT id, channel_type, config_json, enabled FROM notification_channels WHERE channel_type = ?",
|
|
channelType,
|
|
).Scan(&c.ID, &c.ChannelType, &configStr, &enabled)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Config = json.RawMessage(configStr)
|
|
c.Enabled = enabled == 1
|
|
return &c, nil
|
|
}
|
|
|
|
func (s *Store) SaveNotificationChannel(channelType string, config json.RawMessage, enabled bool) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
enabledInt := 0
|
|
if enabled {
|
|
enabledInt = 1
|
|
}
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO notification_channels (channel_type, config_json, enabled) VALUES (?, ?, ?)
|
|
ON CONFLICT(channel_type) DO UPDATE SET config_json=excluded.config_json, enabled=excluded.enabled`,
|
|
channelType, string(config), enabledInt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) ListNotificationChannels() ([]NotificationChannel, error) {
|
|
rows, err := s.db.Query("SELECT id, channel_type, config_json, enabled FROM notification_channels")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var channels []NotificationChannel
|
|
for rows.Next() {
|
|
var c NotificationChannel
|
|
var configStr string
|
|
var enabled int
|
|
if err := rows.Scan(&c.ID, &c.ChannelType, &configStr, &enabled); err != nil {
|
|
continue
|
|
}
|
|
c.Config = json.RawMessage(configStr)
|
|
c.Enabled = enabled == 1
|
|
channels = append(channels, c)
|
|
}
|
|
return channels, rows.Err()
|
|
}
|