delete no use code

This commit is contained in:
Ulric Qin 2022-05-12 10:58:28 +08:00
parent cb741a5521
commit dd5ae29f82
6 changed files with 13 additions and 355 deletions

View File

@ -1,212 +0,0 @@
package models
import (
"encoding/json"
"strings"
"time"
"github.com/pkg/errors"
"github.com/toolkits/pkg/str"
)
type CollectRule struct {
Id int64 `json:"id"`
GroupId int64 `json:"group_id"`
Cluster string `json:"cluster"`
TargetIdents string `json:"-"`
TargetIdentsJSON []string `json:"target_idents" gorm:"-"`
TargetTags string `json:"-"`
TargetTagsJSON []string `json:"target_tags" gorm:"-"`
Name string `json:"name"`
Note string `json:"note"`
Step int `json:"step"`
Type string `json:"type"`
Data string `json:"data"`
AppendTags string `json:"-"`
AppendTagsJSON []string `json:"append_tags" gorm:"-"`
CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"`
UpdateBy string `json:"update_by"`
}
type PortConfig struct {
Port int `json:"port"`
Protocol string `json:"protocol"` // tcp or udp
Timeout int `json:"timeout"` // second
}
type ProcConfig struct {
Method string `json:"method"`
Param string `json:"param"`
}
type ScriptConfig struct {
Path string `json:"path"`
Params string `json:"params"`
Stdin string `json:"stdin"`
Env map[string]string `json:"env"`
Timeout int `json:"timeout"` // second
}
type LogConfig struct {
FilePath string `json:"file_path"`
Func string `json:"func"`
Pattern string `json:"pattern"`
TagsPattern map[string]string `json:"tags_pattern"`
}
func (cr *CollectRule) TableName() string {
return "collect_rule"
}
func (cr *CollectRule) FE2DB() {
cr.TargetIdents = strings.Join(cr.TargetIdentsJSON, " ")
cr.TargetTags = strings.Join(cr.TargetTagsJSON, " ")
cr.AppendTags = strings.Join(cr.AppendTagsJSON, " ")
}
func (cr *CollectRule) DB2FE() {
cr.TargetIdentsJSON = strings.Fields(cr.TargetIdents)
cr.TargetTagsJSON = strings.Fields(cr.TargetTags)
cr.AppendTagsJSON = strings.Fields(cr.AppendTags)
}
func (cr *CollectRule) Verify() error {
if str.Dangerous(cr.Name) {
return errors.New("Name has invalid characters")
}
if cr.TargetIdents == "" && cr.TargetTags == "" {
return errors.New("target_idents and target_tags are both blank")
}
if cr.Step <= 0 {
cr.Step = 15
}
if cr.Cluster == "" {
return errors.New("cluster is blank")
}
switch cr.Type {
case "port":
var conf PortConfig
err := json.Unmarshal([]byte(cr.Data), &conf)
if err != nil {
return err
}
case "script":
var conf ScriptConfig
err := json.Unmarshal([]byte(cr.Data), &conf)
if err != nil {
return err
}
case "log":
var conf LogConfig
err := json.Unmarshal([]byte(cr.Data), &conf)
if err != nil {
return err
}
case "process":
var conf ProcConfig
err := json.Unmarshal([]byte(cr.Data), &conf)
if err != nil {
return err
}
default:
return errors.New("unsupported type")
}
return nil
}
func CollectRuleDels(ids []int64, busiGroupId int64) error {
return DB().Where("id in ? and group_id=?", ids, busiGroupId).Delete(&CollectRule{}).Error
}
func CollectRuleExists(where string, args ...interface{}) (bool, error) {
return Exists(DB().Model(&CollectRule{}).Where(where, args...))
}
func CollectRuleGets(groupId int64, typ string) ([]CollectRule, error) {
session := DB().Where("group_id=?", groupId).Order("name")
if typ != "" {
session = session.Where("type = ?", typ)
}
var lst []CollectRule
err := session.Find(&lst).Error
if err == nil {
for i := 0; i < len(lst); i++ {
lst[i].DB2FE()
}
}
return lst, err
}
func CollectRuleGet(where string, args ...interface{}) (*CollectRule, error) {
var lst []*CollectRule
err := DB().Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
lst[0].DB2FE()
return lst[0], nil
}
func CollectRuleGetById(id int64) (*CollectRule, error) {
return CollectRuleGet("id=?", id)
}
func (cr *CollectRule) Add() error {
if err := cr.Verify(); err != nil {
return err
}
exists, err := CollectRuleExists("group_id=? and type=? and name=? and cluster=?", cr.GroupId, cr.Type, cr.Name, cr.Cluster)
if err != nil {
return err
}
if exists {
return errors.New("CollectRule already exists")
}
now := time.Now().Unix()
cr.CreateAt = now
cr.UpdateAt = now
return Insert(cr)
}
func (cr *CollectRule) Update(crf CollectRule) error {
if cr.Name != crf.Name {
exists, err := CollectRuleExists("group_id=? and type=? and name=? and id <> ? and cluster=?", cr.GroupId, cr.Type, crf.Name, cr.Id, cr.Cluster)
if err != nil {
return err
}
if exists {
return errors.New("CollectRule already exists")
}
}
crf.FE2DB()
crf.Id = cr.Id
crf.GroupId = cr.GroupId
crf.Type = cr.Type
crf.CreateAt = cr.CreateAt
crf.CreateBy = cr.CreateBy
crf.UpdateAt = time.Now().Unix()
return DB().Model(cr).Select("*").Updates(crf).Error
}

View File

@ -209,17 +209,6 @@ func configRoute(r *gin.Engine, version string) {
pages.PUT("/busi-group/:id/alert-subscribes", jwtAuth(), user(), perm("/alert-subscribes/put"), bgrw(), alertSubscribePut)
pages.DELETE("/busi-group/:id/alert-subscribes", jwtAuth(), user(), perm("/alert-subscribes/del"), bgrw(), alertSubscribeDel)
// pages.GET("/busi-group/:id/collect-rules", jwtAuth(), user(), bgro(), collectRuleGets)
// pages.POST("/busi-group/:id/collect-rules", jwtAuth(), user(), bgrw(), collectRuleAdd)
// pages.DELETE("/busi-group/:id/collect-rules", jwtAuth(), user(), bgrw(), collectRuleDel)
// pages.GET("/busi-group/:id/collect-rule/:crid", jwtAuth(), user(), bgro(), collectRuleGet)
// pages.PUT("/busi-group/:id/collect-rule/:crid", jwtAuth(), user(), bgrw(), collectRulePut)
// card逻辑fe改造完之后这三个方法可以删除
pages.GET("/busi-group/:id/alert-his-events", jwtAuth(), user(), bgro(), alertHisEventGets)
pages.GET("/busi-group/:id/alert-cur-events", jwtAuth(), user(), bgro(), alertCurEventGets)
pages.DELETE("/busi-group/:id/alert-cur-events", jwtAuth(), user(), perm("/alert-cur-events/del"), alertCurEventDel)
if config.C.AnonymousAccess.AlertDetail {
pages.GET("/alert-cur-event/:eid", alertCurEventGet)
pages.GET("/alert-his-event/:eid", alertHisEventGet)

View File

@ -139,32 +139,6 @@ func alertCurEventsList(c *gin.Context) {
}, nil)
}
func alertCurEventGets(c *gin.Context) {
stime, etime := getTimeRange(c)
severity := ginx.QueryInt(c, "severity", -1)
query := ginx.QueryStr(c, "query", "")
limit := ginx.QueryInt(c, "limit", 20)
busiGroupId := ginx.UrlParamInt64(c, "id")
clusters := queryClusters(c)
total, err := models.AlertCurEventTotal(busiGroupId, stime, etime, severity, clusters, query)
ginx.Dangerous(err)
list, err := models.AlertCurEventGets(busiGroupId, stime, etime, severity, clusters, query, limit, ginx.Offset(c, limit))
ginx.Dangerous(err)
cache := make(map[int64]*models.UserGroup)
for i := 0; i < len(list); i++ {
list[i].FillNotifyGroups(cache)
}
ginx.NewRender(c).Data(gin.H{
"list": list,
"total": total,
}, nil)
}
func alertCurEventDel(c *gin.Context) {
var f idsForm
ginx.BindJSON(c, &f)

View File

@ -52,33 +52,6 @@ func alertHisEventsList(c *gin.Context) {
}, nil)
}
func alertHisEventGets(c *gin.Context) {
stime, etime := getTimeRange(c)
severity := ginx.QueryInt(c, "severity", -1)
recovered := ginx.QueryInt(c, "is_recovered", -1)
query := ginx.QueryStr(c, "query", "")
limit := ginx.QueryInt(c, "limit", 20)
busiGroupId := ginx.UrlParamInt64(c, "id")
clusters := queryClusters(c)
total, err := models.AlertHisEventTotal(busiGroupId, stime, etime, severity, recovered, clusters, query)
ginx.Dangerous(err)
list, err := models.AlertHisEventGets(busiGroupId, stime, etime, severity, recovered, clusters, query, limit, ginx.Offset(c, limit))
ginx.Dangerous(err)
cache := make(map[int64]*models.UserGroup)
for i := 0; i < len(list); i++ {
list[i].FillNotifyGroups(cache)
}
ginx.NewRender(c).Data(gin.H{
"list": list,
"total": total,
}, nil)
}
func alertHisEventGet(c *gin.Context) {
eid := ginx.UrlParamInt64(c, "eid")
event, err := models.AlertHisEventGetById(eid)

View File

@ -1,79 +0,0 @@
package router
// import (
// "net/http"
// "github.com/gin-gonic/gin"
// "github.com/toolkits/pkg/ginx"
// "github.com/didi/nightingale/v5/src/models"
// )
// func collectRuleGets(c *gin.Context) {
// busiGroupId := ginx.UrlParamInt64(c, "id")
// crs, err := models.CollectRuleGets(busiGroupId, ginx.QueryStr(c, "type", ""))
// ginx.NewRender(c).Data(crs, err)
// }
// func collectRuleAdd(c *gin.Context) {
// var lst []models.CollectRule
// ginx.BindJSON(c, &lst)
// count := len(lst)
// if count == 0 {
// ginx.Bomb(http.StatusBadRequest, "input json is empty")
// }
// username := c.MustGet("username").(string)
// bgid := ginx.UrlParamInt64(c, "id")
// // collect rule name -> error string
// reterr := make(map[string]string)
// for i := 0; i < count; i++ {
// lst[i].Id = 0
// lst[i].GroupId = bgid
// lst[i].CreateBy = username
// lst[i].UpdateBy = username
// lst[i].FE2DB()
// if err := lst[i].Add(); err != nil {
// reterr[lst[i].Name] = err.Error()
// } else {
// reterr[lst[i].Name] = ""
// }
// }
// ginx.NewRender(c).Data(reterr, nil)
// }
// func collectRuleDel(c *gin.Context) {
// var f idsForm
// ginx.BindJSON(c, &f)
// f.Verify()
// // param(busiGroupId) for protect
// ginx.NewRender(c).Message(models.CollectRuleDels(f.Ids, ginx.UrlParamInt64(c, "id")))
// }
// func collectRuleGet(c *gin.Context) {
// crid := ginx.UrlParamInt64(c, "crid")
// cr, err := models.CollectRuleGetById(crid)
// ginx.NewRender(c).Data(cr, err)
// }
// func collectRulePut(c *gin.Context) {
// var f models.CollectRule
// ginx.BindJSON(c, &f)
// crid := ginx.UrlParamInt64(c, "crid")
// cr, err := models.CollectRuleGetById(crid)
// ginx.Dangerous(err)
// if cr == nil {
// ginx.NewRender(c, http.StatusNotFound).Message("No such CollectRule")
// return
// }
// f.UpdateBy = c.MustGet("username").(string)
// ginx.NewRender(c).Message(cr.Update(f))
// }

View File

@ -140,6 +140,19 @@ func bgrwCheck(c *gin.Context, bgid int64) {
c.Set("busi_group", bg)
}
func bgrwChecks(c *gin.Context, bgids []int64) {
set := make(map[int64]struct{})
for i := 0; i < len(bgids); i++ {
if _, has := set[bgids[i]]; has {
continue
}
bgrwCheck(c, bgids[i])
set[bgids[i]] = struct{}{}
}
}
func perm(operation string) gin.HandlerFunc {
return func(c *gin.Context) {
me := c.MustGet("user").(*models.User)