mirror of
https://github.com/ccfos/nightingale.git
synced 2026-03-15 04:19:10 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de8f4dd93d |
10
Makefile
10
Makefile
@@ -2,15 +2,10 @@
|
||||
|
||||
NOW = $(shell date -u '+%Y%m%d%I%M%S')
|
||||
|
||||
RELEASE_VERSION = 5.9.6
|
||||
|
||||
APP = n9e
|
||||
SERVER_BIN = $(APP)
|
||||
ROOT:=$(shell pwd -P)
|
||||
GIT_COMMIT:=$(shell git --work-tree ${ROOT} rev-parse 'HEAD^{commit}')
|
||||
_GIT_VERSION:=$(shell git --work-tree ${ROOT} describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" 2>/dev/null)
|
||||
TAG=$(shell echo "${_GIT_VERSION}" | awk -F"-" '{print $$1}')
|
||||
RELEASE_VERSION:="$(TAG)-$(GIT_COMMIT)"
|
||||
|
||||
# RELEASE_ROOT = release
|
||||
# RELEASE_SERVER = release/${APP}
|
||||
# GIT_COUNT = $(shell git rev-list --all --count)
|
||||
@@ -22,9 +17,6 @@ all: build
|
||||
build:
|
||||
go build -ldflags "-w -s -X github.com/didi/nightingale/v5/src/pkg/version.VERSION=$(RELEASE_VERSION)" -o $(SERVER_BIN) ./src
|
||||
|
||||
build-linux:
|
||||
GOOS=linux GOARCH=amd64 go build -ldflags "-w -s -X github.com/didi/nightingale/v5/src/pkg/version.VERSION=$(RELEASE_VERSION)" -o $(SERVER_BIN) ./src
|
||||
|
||||
# start:
|
||||
# @go run -ldflags "-X main.VERSION=$(RELEASE_TAG)" ./cmd/${APP}/main.go web -c ./configs/config.toml -m ./configs/model.conf --menu ./configs/menu.yaml
|
||||
run_webapi:
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
type AlertRule struct {
|
||||
Id int64 `json:"id" gorm:"primaryKey"`
|
||||
GroupId int64 `json:"group_id"` // busi group id
|
||||
Cluster string `json:"cluster"` // take effect by clusters, seperated by space
|
||||
Cluster string `json:"cluster"` // take effect by cluster
|
||||
Name string `json:"name"` // rule name
|
||||
Note string `json:"note"` // will sent in notify
|
||||
Prod string `json:"prod"` // product empty means n9e
|
||||
@@ -124,7 +124,7 @@ func (ar *AlertRule) Add() error {
|
||||
return err
|
||||
}
|
||||
|
||||
exists, err := AlertRuleExists(0, ar.GroupId, ar.Cluster, ar.Name)
|
||||
exists, err := AlertRuleExists("group_id=? and cluster=? and name=?", ar.GroupId, ar.Cluster, ar.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -142,7 +142,7 @@ func (ar *AlertRule) Add() error {
|
||||
|
||||
func (ar *AlertRule) Update(arf AlertRule) error {
|
||||
if ar.Name != arf.Name {
|
||||
exists, err := AlertRuleExists(ar.Id, ar.GroupId, ar.Cluster, arf.Name)
|
||||
exists, err := AlertRuleExists("group_id=? and cluster=? and name=? and id <> ?", ar.GroupId, ar.Cluster, arf.Name, ar.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -262,25 +262,8 @@ func AlertRuleDels(ids []int64, bgid ...int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func AlertRuleExists(id, groupId int64, cluster, name string) (bool, error) {
|
||||
session := DB().Where("id <> ? and group_id = ? and name = ?", id, groupId, name)
|
||||
|
||||
var lst []AlertRule
|
||||
err := session.Find(&lst).Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(lst) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// match cluster
|
||||
for _, r := range lst {
|
||||
if MatchCluster(r.Cluster, cluster) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
func AlertRuleExists(where string, args ...interface{}) (bool, error) {
|
||||
return Exists(DB().Model(&AlertRule{}).Where(where, args...))
|
||||
}
|
||||
|
||||
func AlertRuleGets(groupId int64) ([]AlertRule, error) {
|
||||
@@ -301,35 +284,18 @@ func AlertRuleGetsByCluster(cluster string) ([]*AlertRule, error) {
|
||||
session := DB().Where("disabled = ? and prod = ?", 0, "")
|
||||
|
||||
if cluster != "" {
|
||||
session = session.Where("(cluster like ? or cluster like ?)", "%"+cluster+"%", "%"+ClusterAll+"%")
|
||||
session = session.Where("cluster = ?", cluster)
|
||||
}
|
||||
|
||||
var lst []*AlertRule
|
||||
err := session.Find(&lst).Error
|
||||
if err != nil {
|
||||
return lst, err
|
||||
}
|
||||
|
||||
if len(lst) == 0 {
|
||||
return lst, nil
|
||||
}
|
||||
|
||||
if cluster == "" {
|
||||
if err == nil {
|
||||
for i := 0; i < len(lst); i++ {
|
||||
lst[i].DB2FE()
|
||||
}
|
||||
return lst, nil
|
||||
}
|
||||
|
||||
lr := make([]*AlertRule, 0, len(lst))
|
||||
for _, r := range lst {
|
||||
if MatchCluster(r.Cluster, cluster) {
|
||||
r.DB2FE()
|
||||
lr = append(lr, r)
|
||||
}
|
||||
}
|
||||
|
||||
return lr, err
|
||||
return lst, err
|
||||
}
|
||||
|
||||
func AlertRulesGetsBy(prods []string, query string) ([]*AlertRule, error) {
|
||||
@@ -392,8 +358,7 @@ func AlertRuleStatistics(cluster string) (*Statistics, error) {
|
||||
session := DB().Model(&AlertRule{}).Select("count(*) as total", "max(update_at) as last_updated").Where("disabled = ? and prod = ?", 0, "")
|
||||
|
||||
if cluster != "" {
|
||||
// 简略的判断,当一个clustername是另一个clustername的substring的时候,会出现stats与预期不符,不影响使用
|
||||
session = session.Where("(cluster like ? or cluster like ?)", "%"+cluster+"%", "%"+ClusterAll+"%")
|
||||
session = session.Where("cluster = ?", cluster)
|
||||
}
|
||||
|
||||
var stats []*Statistics
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/toolkits/pkg/str"
|
||||
"gorm.io/gorm"
|
||||
|
||||
@@ -11,9 +9,6 @@ import (
|
||||
|
||||
const AdminRole = "Admin"
|
||||
|
||||
// if rule's cluster field contains `ClusterAll`, means it take effect in all clusters
|
||||
const ClusterAll = "$all"
|
||||
|
||||
func DB() *gorm.DB {
|
||||
return storage.DB
|
||||
}
|
||||
@@ -47,16 +42,3 @@ type Statistics struct {
|
||||
Total int64 `gorm:"total"`
|
||||
LastUpdated int64 `gorm:"last_updated"`
|
||||
}
|
||||
|
||||
func MatchCluster(ruleCluster, targetCluster string) bool {
|
||||
if targetCluster == ClusterAll {
|
||||
return true
|
||||
}
|
||||
clusters := strings.Fields(ruleCluster)
|
||||
for _, c := range clusters {
|
||||
if c == ClusterAll || c == targetCluster {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
type RecordingRule struct {
|
||||
Id int64 `json:"id" gorm:"primaryKey"`
|
||||
GroupId int64 `json:"group_id"` // busi group id
|
||||
Cluster string `json:"cluster"` // take effect by cluster, seperated by space
|
||||
Cluster string `json:"cluster"` // take effect by cluster
|
||||
Name string `json:"name"` // new metric name
|
||||
Note string `json:"note"` // note
|
||||
Disabled int `json:"disabled"` // 0: enabled, 1: disabled
|
||||
@@ -40,7 +40,6 @@ func (re *RecordingRule) DB2FE() {
|
||||
//re.ClusterJSON = strings.Fields(re.Cluster)
|
||||
re.AppendTagsJSON = strings.Fields(re.AppendTags)
|
||||
}
|
||||
|
||||
func (re *RecordingRule) Verify() error {
|
||||
if re.GroupId < 0 {
|
||||
return fmt.Errorf("GroupId(%d) invalid", re.GroupId)
|
||||
@@ -79,7 +78,7 @@ func (re *RecordingRule) Add() error {
|
||||
return err
|
||||
}
|
||||
|
||||
exists, err := RecordingRuleExists(0, re.GroupId, re.Cluster, re.Name)
|
||||
exists, err := RecordingRuleExists("group_id=? and cluster=? and name=?", re.GroupId, re.Cluster, re.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +96,7 @@ func (re *RecordingRule) Add() error {
|
||||
|
||||
func (re *RecordingRule) Update(ref RecordingRule) error {
|
||||
if re.Name != ref.Name {
|
||||
exists, err := RecordingRuleExists(re.Id, re.GroupId, re.Cluster, ref.Name)
|
||||
exists, err := RecordingRuleExists("group_id=? and cluster=? and name=? and id <> ?", re.GroupId, re.Cluster, ref.Name, re.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -134,27 +133,9 @@ func RecordingRuleDels(ids []int64, groupId int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RecordingRuleExists(id, groupId int64, cluster, name string) (bool, error) {
|
||||
session := DB().Where("id <> ? and group_id = ? and name =? ", id, groupId, name)
|
||||
|
||||
var lst []RecordingRule
|
||||
err := session.Find(&lst).Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(lst) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// match cluster
|
||||
for _, r := range lst {
|
||||
if MatchCluster(r.Cluster, cluster) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
func RecordingRuleExists(where string, regs ...interface{}) (bool, error) {
|
||||
return Exists(DB().Model(&RecordingRule{}).Where(where, regs...))
|
||||
}
|
||||
|
||||
func RecordingRuleGets(groupId int64) ([]RecordingRule, error) {
|
||||
session := DB().Where("group_id=?", groupId).Order("name")
|
||||
|
||||
@@ -190,45 +171,26 @@ func RecordingRuleGetById(id int64) (*RecordingRule, error) {
|
||||
}
|
||||
|
||||
func RecordingRuleGetsByCluster(cluster string) ([]*RecordingRule, error) {
|
||||
session := DB().Where("disabled = ? and prod = ?", 0, "")
|
||||
|
||||
session := DB()
|
||||
if cluster != "" {
|
||||
session = session.Where("(cluster like ? or cluster like ?)", "%"+cluster+"%", "%"+ClusterAll+"%")
|
||||
session = session.Where("cluster = ?", cluster)
|
||||
}
|
||||
|
||||
var lst []*RecordingRule
|
||||
err := session.Find(&lst).Error
|
||||
if err != nil {
|
||||
return lst, err
|
||||
}
|
||||
|
||||
if len(lst) == 0 {
|
||||
return lst, nil
|
||||
}
|
||||
|
||||
if cluster == "" {
|
||||
if err == nil {
|
||||
for i := 0; i < len(lst); i++ {
|
||||
lst[i].DB2FE()
|
||||
}
|
||||
return lst, nil
|
||||
}
|
||||
|
||||
lr := make([]*RecordingRule, 0, len(lst))
|
||||
for _, r := range lst {
|
||||
if MatchCluster(r.Cluster, cluster) {
|
||||
r.DB2FE()
|
||||
lr = append(lr, r)
|
||||
}
|
||||
}
|
||||
|
||||
return lr, err
|
||||
return lst, err
|
||||
}
|
||||
|
||||
func RecordingRuleStatistics(cluster string) (*Statistics, error) {
|
||||
session := DB().Model(&RecordingRule{}).Select("count(*) as total", "max(update_at) as last_updated")
|
||||
if cluster != "" {
|
||||
// 简略的判断,当一个clustername是另一个clustername的substring的时候,会出现stats与预期不符,不影响使用
|
||||
session = session.Where("(cluster like ? or cluster like ?)", "%"+cluster+"%", "%"+ClusterAll+"%")
|
||||
session = session.Where("cluster = ?", cluster)
|
||||
}
|
||||
|
||||
var stats []*Statistics
|
||||
|
||||
Reference in New Issue
Block a user