Files
vHost/backend/internal/store/site_preferences.go
T

98 lines
3.9 KiB
Go

package store
import (
"context"
"database/sql"
"encoding/json"
"errors"
"sea-vhost/internal/hosting"
"time"
)
func (s *Store) WebsiteSettings(ctx context.Context, id uint64) (hosting.WebsiteSettings, error) {
v := hosting.DefaultWebsite()
var raw []byte
e := s.db.QueryRowContext(ctx, "SELECT website_json FROM site_preferences WHERE site_id=?", id).Scan(&raw)
if errors.Is(e, sql.ErrNoRows) {
return v, nil
}
if e == nil && len(raw) > 0 {
e = json.Unmarshal(raw, &v)
}
return v, e
}
func (s *Store) SaveWebsiteSettings(ctx context.Context, id uint64, v hosting.WebsiteSettings) error {
raw, e := json.Marshal(v)
if e != nil {
return e
}
_, e = s.db.ExecContext(ctx, "INSERT INTO site_preferences(site_id,website_json) VALUES(?,?) ON DUPLICATE KEY UPDATE website_json=VALUES(website_json)", id, raw)
return e
}
func (s *Store) StartupCipher(ctx context.Context, id uint64) ([]byte, error) {
var v []byte
e := s.db.QueryRowContext(ctx, "SELECT startup_cipher FROM site_preferences WHERE site_id=?", id).Scan(&v)
if errors.Is(e, sql.ErrNoRows) {
e = nil
}
return v, e
}
func (s *Store) SaveStartupCipher(ctx context.Context, id uint64, v []byte) error {
_, e := s.db.ExecContext(ctx, "INSERT INTO site_preferences(site_id,startup_cipher) VALUES(?,?) ON DUPLICATE KEY UPDATE startup_cipher=VALUES(startup_cipher)", id, v)
return e
}
type HostMetric struct {
SampledAt time.Time `json:"sampledAt"`
CPUPercent float64 `json:"cpuPercent"`
CPUTicks uint64 `json:"-"`
MemoryBytes uint64 `json:"memoryBytes"`
ProcessCount uint32 `json:"processCount"`
StorageBytes uint64 `json:"storageBytes"`
DatabaseBytes uint64 `json:"databaseBytes"`
BandwidthBytes uint64 `json:"bandwidthBytes"`
}
func (s *Store) RecordHostMetric(ctx context.Context, id uint64, v HostMetric) error {
v.SampledAt = time.Now().UTC().Truncate(time.Minute)
var ticks uint64
var previous time.Time
e := s.db.QueryRowContext(ctx, "SELECT sampled_at,cpu_ticks FROM host_metrics WHERE project_id=? AND sampled_at<? ORDER BY sampled_at DESC LIMIT 1", id, v.SampledAt).Scan(&previous, &ticks)
if e == nil && v.CPUTicks >= ticks && v.SampledAt.Sub(previous) <= 10*time.Minute {
v.CPUPercent = float64(v.CPUTicks-ticks) / v.SampledAt.Sub(previous).Seconds()
}
_, e = s.db.ExecContext(ctx, `INSERT INTO host_metrics(project_id,sampled_at,cpu_percent,cpu_ticks,memory_bytes,process_count,storage_bytes,database_bytes,bandwidth_bytes) VALUES(?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE memory_bytes=VALUES(memory_bytes),process_count=VALUES(process_count),storage_bytes=VALUES(storage_bytes),database_bytes=VALUES(database_bytes),bandwidth_bytes=VALUES(bandwidth_bytes)`, id, v.SampledAt, v.CPUPercent, v.CPUTicks, v.MemoryBytes, v.ProcessCount, v.StorageBytes, v.DatabaseBytes, v.BandwidthBytes)
return e
}
func (s *Store) HostMetrics(ctx context.Context, id uint64, days int) ([]HostMetric, error) {
if days < 1 || days > 30 {
days = 1
}
rows, e := s.db.QueryContext(ctx, `SELECT sampled_at,cpu_percent,memory_bytes,process_count,storage_bytes,database_bytes,bandwidth_bytes FROM host_metrics WHERE project_id=? AND sampled_at>=? ORDER BY sampled_at`, id, time.Now().UTC().Add(-time.Duration(days)*24*time.Hour))
if e != nil {
return nil, e
}
defer rows.Close()
values := []HostMetric{}
for rows.Next() {
var v HostMetric
if e = rows.Scan(&v.SampledAt, &v.CPUPercent, &v.MemoryBytes, &v.ProcessCount, &v.StorageBytes, &v.DatabaseBytes, &v.BandwidthBytes); e != nil {
return nil, e
}
values = append(values, v)
}
if len(values) > 1440 {
step := (len(values) + 1439) / 1440
reduced := []HostMetric{}
for i := 0; i < len(values); i += step {
reduced = append(reduced, values[i])
}
values = reduced
}
return values, rows.Err()
}
func (s *Store) PruneHostMetrics(ctx context.Context) error {
_, e := s.db.ExecContext(ctx, "DELETE FROM host_metrics WHERE sampled_at<DATE_SUB(UTC_TIMESTAMP(),INTERVAL 30 DAY)")
return e
}