Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions agent/app/api/v2/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package v2

import (
"errors"
"net/url"
"strings"

"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/gin-gonic/gin"
)

const defaultAuditUser = "system"

// @Tags Alert
// @Summary Page alert
// @Accept json
Expand Down Expand Up @@ -54,7 +59,7 @@ func (b *BaseApi) CreateAlert(c *gin.Context) {
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
err := alertService.CreateAlert(req)
err := alertService.CreateAlert(req, loadAuditUser(c))
if err != nil {
helper.InternalServer(c, err)
return
Expand Down Expand Up @@ -98,7 +103,7 @@ func (b *BaseApi) UpdateAlert(c *gin.Context) {
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := alertService.UpdateAlert(req); err != nil {
if err := alertService.UpdateAlert(req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down Expand Up @@ -246,6 +251,30 @@ func (b *BaseApi) GetAlertConfig(c *gin.Context) {
helper.SuccessWithData(c, config)
}

// @Tags Alert
// @Summary Page alert config
// @Accept json
// @Param request body dto.PageInfo true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /alert/config/search [post]
func (b *BaseApi) PageAlertConfig(c *gin.Context) {
var req dto.PageInfo
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
total, configs, err := alertService.PageAlertConfig(req)
if err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithData(c, dto.PageResult{
Total: total,
Items: configs,
})
}

// @Tags Alert
// @Summary Update alert config
// @Accept json
Expand All @@ -260,13 +289,24 @@ func (b *BaseApi) UpdateAlertConfig(c *gin.Context) {
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
if err := alertService.UpdateAlertConfig(req); err != nil {
if err := alertService.UpdateAlertConfig(req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
helper.Success(c)
}

func loadAuditUser(c *gin.Context) string {
userName := strings.TrimSpace(c.GetHeader("X-Panel-User"))
if userName == "" {
return defaultAuditUser
}
if decoded, err := url.QueryUnescape(userName); err == nil {
return decoded
}
return userName
}

// @Tags Alert
// @Summary Delete alert config
// @Accept json
Expand Down
4 changes: 2 additions & 2 deletions agent/app/api/v2/clam.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (b *BaseApi) CreateClam(c *gin.Context) {
return
}

if err := clamService.Create(req); err != nil {
if err := clamService.Create(req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand All @@ -43,7 +43,7 @@ func (b *BaseApi) UpdateClam(c *gin.Context) {
return
}

if err := clamService.Update(req); err != nil {
if err := clamService.Update(req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down
6 changes: 3 additions & 3 deletions agent/app/api/v2/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (b *BaseApi) CreateCronjob(c *gin.Context) {
return
}

if err := cronjobService.Create(req); err != nil {
if err := cronjobService.Create(req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (b *BaseApi) ImportCronjob(c *gin.Context) {
return
}

if err := cronjobService.Import(req.Cronjobs); err != nil {
if err := cronjobService.Import(req.Cronjobs, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (b *BaseApi) UpdateCronjob(c *gin.Context) {
return
}

if err := cronjobService.Update(req.ID, req); err != nil {
if err := cronjobService.Update(req.ID, req, loadAuditUser(c)); err != nil {
helper.InternalServer(c, err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions agent/app/dto/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type AlertDTO struct {
Status string `json:"status"`
SendCount uint `json:"sendCount"`
AdvancedParams string `json:"advancedParams"`
CreateUser string `json:"createUser"`
UpdateUser string `json:"updateUser"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Expand Down
18 changes: 11 additions & 7 deletions agent/app/model/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ type Alert struct {
Count uint `gorm:"type:integer;not null" json:"count"`
Project string `gorm:"type:varchar(64)" json:"project"`
Status string `gorm:"type:varchar(64);not null" json:"status"`
Method string `gorm:"type:varchar(64);not null" json:"method"`
Method string `gorm:"type:text;not null" json:"method"`
SendCount uint `gorm:"type:integer" json:"sendCount"`
AdvancedParams string `gorm:"type:longText" json:"advancedParams"`
CreateUser string `gorm:"type:varchar(256)" json:"createUser"`
UpdateUser string `gorm:"type:varchar(256)" json:"updateUser"`
}

type AlertTask struct {
BaseModel
Type string `gorm:"type:varchar(64);not null" json:"type"`
Quota string `gorm:"type:varchar(64)" json:"quota"`
QuotaType string `gorm:"type:varchar(64)" json:"quotaType"`
Method string `gorm:"type:varchar(64);not null;default:'sms'" json:"method"`
Method string `gorm:"type:varchar(128);not null;default:'sms'" json:"method"`
}

type AlertLog struct {
Expand All @@ -34,15 +36,17 @@ type AlertLog struct {
Message string `gorm:"type:varchar(256);" json:"message"`
RecordId uint `gorm:"type:integer;" json:"recordId"`
LicenseId string `gorm:"type:varchar(256);not null;" json:"licenseId" `
Method string `gorm:"type:varchar(64);not null;default:'sms'" json:"method"`
Method string `gorm:"type:varchar(128);not null;default:'sms'" json:"method"`
}

type AlertConfig struct {
BaseModel
Type string `gorm:"type:varchar(64);not null" json:"type"`
Title string `gorm:"type:varchar(64);not null" json:"title"`
Status string `gorm:"type:varchar(64);not null" json:"status"`
Config string `gorm:"type:varchar(256);not null" json:"config"`
Type string `gorm:"type:varchar(64);not null" json:"type"`
Title string `gorm:"type:varchar(64);not null" json:"title"`
Status string `gorm:"type:varchar(64);not null" json:"status"`
Config string `gorm:"type:varchar(256);not null" json:"config"`
CreateUser string `gorm:"type:varchar(256)" json:"createUser"`
UpdateUser string `gorm:"type:varchar(256)" json:"updateUser"`
}

type LoginLog struct {
Expand Down
89 changes: 78 additions & 11 deletions agent/app/repo/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/global"
"google.golang.org/genproto/googleapis/type/date"
"gorm.io/gorm"
"strconv"
"time"
)

Expand All @@ -21,6 +22,7 @@ type IAlertRepo interface {
WithByLicenseId(licenseId string) DBOption
WithByRecordId(recordId uint) DBOption
WithByMethod(method string) DBOption
WithByMethodConfigID(id uint) DBOption

Create(alert *model.Alert) error
Get(opts ...DBOption) (model.Alert, error)
Expand All @@ -47,11 +49,15 @@ type IAlertRepo interface {
GetLicensePushCount(method string) (uint, error)

GetConfig(opts ...DBOption) (model.AlertConfig, error)
GetConfigById(id uint) (model.AlertConfig, error)
AlertConfigList(opts ...DBOption) ([]model.AlertConfig, error)
UpdateAlertConfig(maps map[string]interface{}, opts ...DBOption) error
CreateAlertConfig(config *model.AlertConfig) error
DeleteAlertConfig(opts ...DBOption) error

WithByTypeNotIn(types []string) DBOption
PageAlertConfig(page, size int, opts ...DBOption) (int64, []model.AlertConfig, error)

SyncAll(data []model.AlertConfig) error
}

Expand Down Expand Up @@ -103,7 +109,14 @@ func (a *AlertRepo) WithByRecordId(recordId uint) DBOption {

func (a *AlertRepo) WithByMethod(method string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("method = ?", method)
return g.Where("(method = ? OR method LIKE ? OR method LIKE ? OR method LIKE ?)", method, method+",%", "%,"+method, "%,"+method+",%")
}
}

func (a *AlertRepo) WithByMethodConfigID(id uint) DBOption {
method := strconv.Itoa(int(id))
return func(g *gorm.DB) *gorm.DB {
return g.Where("(method = ? OR method LIKE ? OR method LIKE ? OR method LIKE ?)", method, method+",%", "%,"+method, "%,"+method+",%")
}
}

Expand Down Expand Up @@ -306,32 +319,86 @@ func (a *AlertRepo) GetConfig(opts ...DBOption) (model.AlertConfig, error) {
return alertConfig, err
}

func (a *AlertRepo) GetConfigById(id uint) (model.AlertConfig, error) {
var config model.AlertConfig
err := global.AlertDB.First(&config, id).Error
return config, err
}

func (a *AlertRepo) WithByTypeNotIn(types []string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`type` NOT IN (?)", types)
}
}

func (a *AlertRepo) PageAlertConfig(page, size int, opts ...DBOption) (int64, []model.AlertConfig, error) {
var configs []model.AlertConfig
db := global.AlertDB.Model(&model.AlertConfig{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&configs).Error
return count, configs, err
}

var singletonTypes = map[string]bool{
constant.CommonConfig: true,
}

func (a *AlertRepo) SyncAll(data []model.AlertConfig) error {
tx := global.AlertDB.Begin()
var oldConfigs []model.AlertConfig
_ = tx.Find(&oldConfigs).Error
oldConfigMap := make(map[string]uint)
nonSingletonTypes := make(map[string]struct{})
for _, item := range oldConfigs {
oldConfigMap[item.Type] = item.ID
if singletonTypes[item.Type] {
oldConfigMap[item.Type] = item.ID
continue
}
nonSingletonTypes[item.Type] = struct{}{}
}
for _, item := range data {
if val, ok := oldConfigMap[item.Type]; ok {
item.ID = val
delete(oldConfigMap, item.Type)
} else {
item.ID = 0
if !singletonTypes[item.Type] {
nonSingletonTypes[item.Type] = struct{}{}
}
if err := tx.Model(model.AlertConfig{}).Where("id = ?", item.ID).Save(&item).Error; err != nil {
}
for itemType := range nonSingletonTypes {
if err := tx.Where("type = ?", itemType).Delete(&model.AlertConfig{}).Error; err != nil {
tx.Rollback()
return err
}
}
for _, val := range oldConfigMap {
if err := tx.Where("id = ?", val).Delete(&model.AlertConfig{}).Error; err != nil {
for _, item := range data {
if singletonTypes[item.Type] {
if val, ok := oldConfigMap[item.Type]; ok {
item.ID = val
delete(oldConfigMap, item.Type)
} else {
item.ID = 0
}
if err := tx.Model(model.AlertConfig{}).Where("id = ?", item.ID).Save(&item).Error; err != nil {
tx.Rollback()
return err
}
continue
}
item.ID = 0
if err := tx.Create(&item).Error; err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
for _, id := range oldConfigMap {
if err := tx.Where("id = ?", id).Delete(&model.AlertConfig{}).Error; err != nil {
tx.Rollback()
return err
}
}
if err := tx.Commit().Error; err != nil {
return err
}
return nil
}
Loading
Loading