feat: add configs service api (#1155)

* add configs service api
This commit is contained in:
Yening Qin 2022-09-16 12:06:39 +08:00 committed by GitHub
parent 7209da192f
commit 3a3ad5d9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 2 deletions

View File

@ -74,7 +74,62 @@ func ConfigsSet(ckey, cval string) error {
return err return err
} }
func ConfigsGets(ckeys []string) (map[string]string, error) { func ConfigGet(id int64) (*Configs, error) {
var objs []*Configs
err := DB().Where("id=?", id).Find(&objs).Error
if len(objs) == 0 {
return nil, nil
}
return objs[0], err
}
func ConfigsGets(prefix string, limit, offset int) ([]*Configs, error) {
var objs []*Configs
session := DB()
if prefix != "" {
session = session.Where("ckey like ?", prefix+"%")
}
err := session.Order("id desc").Limit(limit).Offset(offset).Find(&objs).Error
return objs, err
}
func (c *Configs) Add() error {
num, err := Count(DB().Model(&Configs{}).Where("ckey=?", c.Ckey))
if err != nil {
return errors.WithMessage(err, "failed to count configs")
}
if num > 0 {
return errors.WithMessage(err, "key is exists")
}
// insert
err = DB().Create(&Configs{
Ckey: c.Ckey,
Cval: c.Cval,
}).Error
return err
}
func (c *Configs) Update() error {
num, err := Count(DB().Model(&Configs{}).Where("id<>? and ckey=?", c.Id, c.Ckey))
if err != nil {
return errors.WithMessage(err, "failed to count configs")
}
if num > 0 {
return errors.WithMessage(err, "key is exists")
}
err = DB().Model(&Configs{}).Where("id=?", c.Id).Updates(c).Error
return err
}
func ConfigsDel(ids []int64) error {
return DB().Where("id in ?", ids).Delete(&Configs{}).Error
}
func ConfigsGetsByKey(ckeys []string) (map[string]string, error) {
var objs []Configs var objs []Configs
err := DB().Where("ckey in ?", ckeys).Find(&objs).Error err := DB().Where("ckey in ?", ckeys).Find(&objs).Error
if err != nil { if err != nil {

View File

@ -175,7 +175,7 @@ func (r *RuleEval) Work() {
var value model.Value var value model.Value
var err error var err error
if r.rule.Algorithm == "" && (r.rule.Cate == "" || r.rule.Cate == "prometheus") { if r.rule.Algorithm == "" && (r.rule.Cate == "" || strings.ToLower(r.rule.Cate) == "prometheus") {
var warnings prom.Warnings var warnings prom.Warnings
value, warnings, err = readerClient.Query(context.Background(), promql, time.Now()) value, warnings, err = readerClient.Query(context.Background(), promql, time.Now())
if err != nil { if err != nil {

View File

@ -324,5 +324,11 @@ func configRoute(r *gin.Engine, version string) {
service.GET("/alert-cur-events", alertCurEventsList) service.GET("/alert-cur-events", alertCurEventsList)
service.GET("/alert-his-events", alertHisEventsList) service.GET("/alert-his-events", alertHisEventsList)
service.GET("/alert-his-event/:eid", alertHisEventGet) service.GET("/alert-his-event/:eid", alertHisEventGet)
service.GET("/config/:id", configGet)
service.GET("/configs", configsGet)
service.PUT("/configs", configsPut)
service.POST("/configs", configsPost)
service.DELETE("/configs", configsDel)
} }
} }

View File

@ -0,0 +1,49 @@
package router
import (
"github.com/didi/nightingale/v5/src/models"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
)
func configsGet(c *gin.Context) {
prefix := ginx.QueryStr(c, "prefix", "")
limit := ginx.QueryInt(c, "limit", 10)
configs, err := models.ConfigsGets(prefix, limit, ginx.Offset(c, limit))
ginx.NewRender(c).Data(configs, err)
}
func configGet(c *gin.Context) {
id := ginx.UrlParamInt64(c, "id")
configs, err := models.ConfigGet(id)
ginx.NewRender(c).Data(configs, err)
}
func configsDel(c *gin.Context) {
var f idsForm
ginx.BindJSON(c, &f)
ginx.NewRender(c).Message(models.ConfigsDel(f.Ids))
}
func configsPut(c *gin.Context) {
var arr []models.Configs
ginx.BindJSON(c, &arr)
for i := 0; i < len(arr); i++ {
ginx.Dangerous(arr[i].Update())
}
ginx.NewRender(c).Message(nil)
}
func configsPost(c *gin.Context) {
var arr []models.Configs
ginx.BindJSON(c, &arr)
for i := 0; i < len(arr); i++ {
ginx.Dangerous(arr[i].Add())
}
ginx.NewRender(c).Message(nil)
}