forked from pneymrl2f/nightingale
parent
46e2fc6ab6
commit
c557e383b6
@ -0,0 +1,95 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MetricView 在告警聚合视图查看的时候,要存储一些聚合规则
|
||||||
|
type MetricView struct {
|
||||||
|
Id int64 `json:"id" gorm:"primaryKey"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Cate int `json:"cate"`
|
||||||
|
Configs string `json:"configs"`
|
||||||
|
CreateAt int64 `json:"create_at"`
|
||||||
|
CreateBy int64 `json:"create_by"`
|
||||||
|
UpdateAt int64 `json:"update_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *MetricView) TableName() string {
|
||||||
|
return "metric_view"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *MetricView) Verify() error {
|
||||||
|
v.Name = strings.TrimSpace(v.Name)
|
||||||
|
if v.Name == "" {
|
||||||
|
return errors.New("name is blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Configs = strings.TrimSpace(v.Configs)
|
||||||
|
if v.Configs == "" {
|
||||||
|
return errors.New("configs is blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *MetricView) Add() error {
|
||||||
|
if err := v.Verify(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
v.CreateAt = now
|
||||||
|
v.UpdateAt = now
|
||||||
|
v.Cate = 1
|
||||||
|
return Insert(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *MetricView) Update(name, configs string) error {
|
||||||
|
if err := v.Verify(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
v.UpdateAt = time.Now().Unix()
|
||||||
|
v.Name = name
|
||||||
|
v.Configs = configs
|
||||||
|
|
||||||
|
return DB().Model(v).Select("name", "configs", "update_at").Updates(v).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// MetricViewDel: userid for safe delete
|
||||||
|
func MetricViewDel(ids []int64, createBy interface{}) error {
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB().Where("id in ? and create_by = ?", ids, createBy).Delete(new(MetricView)).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func MetricViewGets(createBy interface{}) ([]MetricView, error) {
|
||||||
|
var lst []MetricView
|
||||||
|
err := DB().Where("create_by = ? or cate = 0", createBy).Find(&lst).Error
|
||||||
|
if err == nil && len(lst) > 0 {
|
||||||
|
sort.Slice(lst, func(i, j int) bool {
|
||||||
|
return lst[i].Name < lst[j].Name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return lst, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func MetricViewGet(where string, args ...interface{}) (*MetricView, error) {
|
||||||
|
var lst []*MetricView
|
||||||
|
err := DB().Where(where, args...).Find(&lst).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(lst) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return lst[0], nil
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/didi/nightingale/v5/src/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/toolkits/pkg/ginx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// no param
|
||||||
|
func metricViewGets(c *gin.Context) {
|
||||||
|
lst, err := models.MetricViewGets(c.MustGet("userid"))
|
||||||
|
ginx.NewRender(c).Data(lst, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// body: name, configs
|
||||||
|
func metricViewAdd(c *gin.Context) {
|
||||||
|
var f models.MetricView
|
||||||
|
ginx.BindJSON(c, &f)
|
||||||
|
|
||||||
|
f.Id = 0
|
||||||
|
f.CreateBy = c.MustGet("userid").(int64)
|
||||||
|
|
||||||
|
ginx.NewRender(c).Message(f.Add())
|
||||||
|
}
|
||||||
|
|
||||||
|
// body: ids
|
||||||
|
func metricViewDel(c *gin.Context) {
|
||||||
|
var f idsForm
|
||||||
|
ginx.BindJSON(c, &f)
|
||||||
|
|
||||||
|
ginx.NewRender(c).Message(models.MetricViewDel(f.Ids, c.MustGet("userid")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// body: id, name, configs
|
||||||
|
func metricViewPut(c *gin.Context) {
|
||||||
|
var f models.MetricView
|
||||||
|
ginx.BindJSON(c, &f)
|
||||||
|
|
||||||
|
view, err := models.MetricViewGet("id = ?", f.Id)
|
||||||
|
ginx.Dangerous(err)
|
||||||
|
|
||||||
|
if view == nil {
|
||||||
|
ginx.NewRender(c).Message("no such item(id: %d)", f.Id)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userid := c.MustGet("userid").(int64)
|
||||||
|
if view.CreateBy != userid {
|
||||||
|
ginx.NewRender(c, http.StatusForbidden).Message("forbidden")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ginx.NewRender(c).Message(view.Update(f.Name, f.Configs))
|
||||||
|
}
|
Loading…
Reference in new issue