gva-pms/server/plugin/monitor/service/monitor_config.go

173 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"fmt"
"sync"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/monitor/model"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/monitor/model/request"
)
var (
MonitorCache = make(map[string]map[string]bool) // 缓存结构: table -> field -> exists
CacheMutex sync.RWMutex
ConfigList = []model.MonitorConfig{}
)
var MonitorConfig = new(MC)
type MC struct{}
// CreateMonitorConfig 创建监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) CreateMonitorConfig(ctx context.Context, MC *model.MonitorConfig) (err error) {
var num int64
if MC.BusinessDB == nil {
MC.BusinessDB = new(string)
}
err = global.GVA_DB.Model(&model.MonitorConfig{}).Where("business_db = ? AND database = ? AND db_table = ?", *MC.BusinessDB, *MC.Database, *MC.Table).Count(&num).Error
if err != nil {
return err
}
if num > 0 {
return fmt.Errorf("当前【业务库%s-数据库%s-表名%s】已存在", *MC.BusinessDB, *MC.Database, *MC.Table)
}
err = global.GVA_DB.Create(MC).Error
go s.InitMonitor()
return err
}
// DeleteMonitorConfig 删除监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) DeleteMonitorConfig(ctx context.Context, ID string) (err error) {
err = global.GVA_DB.Delete(&model.MonitorConfig{}, "id = ?", ID).Error
return err
}
// DeleteMonitorConfigByIds 批量删除监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) DeleteMonitorConfigByIds(ctx context.Context, IDs []string) (err error) {
err = global.GVA_DB.Delete(&[]model.MonitorConfig{}, "id in ?", IDs).Error
return err
}
// UpdateMonitorConfig 更新监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) UpdateMonitorConfig(ctx context.Context, MC model.MonitorConfig) (err error) {
var num int64
if MC.BusinessDB == nil {
MC.BusinessDB = new(string)
}
err = global.GVA_DB.Model(&model.MonitorConfig{}).Where("id <> ? AND business_db = ? AND database = ? AND db_table = ?", MC.ID, *MC.BusinessDB, *MC.Database, *MC.Table).Count(&num).Error
if err != nil {
return err
}
if num > 0 {
return fmt.Errorf("当前【业务库%s-数据库%s-表名%s】已存在", *MC.BusinessDB, *MC.Database, *MC.Table)
}
err = global.GVA_DB.Model(&model.MonitorConfig{}).Debug().Where("id = ?", MC.ID).Updates(&MC).Error
db := global.GVA_DB.Model(&model.MonitorConfig{}).Where("id = ?", MC.ID)
// 如果开始时间/结束时间的字段为空则强制更新为nil
if MC.StartTime == nil {
db.Updates(map[string]interface{}{"start_time": nil})
}
if MC.EndTime == nil {
db.Updates(map[string]interface{}{"end_time": nil})
}
go s.InitMonitor()
return err
}
// GetMonitorConfig 根据ID获取监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) GetMonitorConfig(ctx context.Context, ID string) (MC model.MonitorConfig, err error) {
err = global.GVA_DB.Where("id = ?", ID).First(&MC).Error
return
}
// GetMonitorConfigInfoList 分页获取监控配置记录
// Author [yourname](https://github.com/yourname)
func (s *MC) GetMonitorConfigInfoList(ctx context.Context, info request.MonitorConfigSearch) (list []model.MonitorConfig, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
// 创建db
db := global.GVA_DB.Model(&model.MonitorConfig{})
var MCs []model.MonitorConfig
// 如果有条件搜索 下方会自动创建搜索语句
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
}
err = db.Count(&total).Error
if err != nil {
return
}
if limit != 0 {
db = db.Limit(limit).Offset(offset)
}
err = db.Find(&MCs).Error
return MCs, total, err
}
func (s *MC) GetMonitorConfigPublic(ctx context.Context) {
}
// 初始化监控配置
func (s *MC) InitMonitor() (err error) {
var configs []model.MonitorConfig
if err = global.GVA_DB.Find(&configs).Error; err != nil {
return
}
s.refreshCache(configs)
fmt.Println("configs refreshed success")
ConfigList = configs
return nil
}
func (s *MC) refreshCache(configs []model.MonitorConfig) {
newCache := make(map[string]map[string]bool)
for _, c := range configs {
if c.IsEnable == nil || !*c.IsEnable {
continue
}
if newCache[*c.Table] == nil {
newCache[*c.Table] = make(map[string]bool)
}
for _, col := range c.Columns {
if col != "" {
newCache[*c.Table][col] = true
}
}
}
CacheMutex.Lock()
MonitorCache = newCache
CacheMutex.Unlock()
}
// IsFieldMonitored 检查字段是否被监控
func (s *MC) IsFieldMonitored(table, field string) bool {
CacheMutex.RLock()
defer CacheMutex.RUnlock()
fields, ok := MonitorCache[table]
if !ok {
return false
}
return fields[field]
}
func (s *MC) RefreshExipre() (newExpire map[string]bool) {
newExpire = make(map[string]bool)
for _, c := range ConfigList {
newExpire[*c.Table] = c.IsExpire()
}
fmt.Println("Expire refreshed success")
return
}