Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add model alerting_engine * heartbeat using db * reader.Client from database * fix sql
- Loading branch information
Showing
15 changed files
with
310 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type AlertingEngines struct { | ||
Id int64 `json:"id" gorm:"primaryKey"` | ||
Instance string `json:"instance"` | ||
Cluster string `json:"cluster"` // reader cluster | ||
Clock int64 `json:"clock"` | ||
} | ||
|
||
func (e *AlertingEngines) TableName() string { | ||
return "alerting_engines" | ||
} | ||
|
||
// UpdateCluster 页面上用户会给各个n9e-server分配要关联的目标集群是什么 | ||
func (e *AlertingEngines) UpdateCluster(c string) error { | ||
e.Cluster = c | ||
return DB().Model(e).Select("cluster").Updates(e).Error | ||
} | ||
|
||
// AlertingEngineGetCluster 根据实例名获取对应的集群名字 | ||
func AlertingEngineGetCluster(instance string) (string, error) { | ||
var objs []AlertingEngines | ||
err := DB().Where("instance=?", instance).Find(&objs).Error | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(objs) == 0 { | ||
return "", nil | ||
} | ||
|
||
return objs[0].Cluster, nil | ||
} | ||
|
||
// AlertingEngineGets 拉取列表数据,用户要在页面上看到所有 n9e-server 实例列表,然后为其分配 cluster | ||
func AlertingEngineGets(where string, args ...interface{}) ([]*AlertingEngines, error) { | ||
var objs []*AlertingEngines | ||
var err error | ||
session := DB().Order("instance") | ||
if where == "" { | ||
err = session.Find(&objs).Error | ||
} else { | ||
err = session.Where(where, args...).Find(&objs).Error | ||
} | ||
return objs, err | ||
} | ||
|
||
func AlertingEngineGetsInstances(where string, args ...interface{}) ([]string, error) { | ||
var arr []string | ||
var err error | ||
session := DB().Model(new(AlertingEngines)).Order("instance") | ||
if where == "" { | ||
err = session.Pluck("instance", &arr).Error | ||
} else { | ||
err = session.Where(where, args...).Pluck("instance", &arr).Error | ||
} | ||
return arr, err | ||
} | ||
|
||
func AlertingEngineHeartbeat(instance string) error { | ||
var total int64 | ||
err := DB().Model(new(AlertingEngines)).Where("instance=?", instance).Count(&total).Error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if total == 0 { | ||
// insert | ||
err = DB().Create(&AlertingEngines{ | ||
Instance: instance, | ||
Clock: time.Now().Unix(), | ||
}).Error | ||
} else { | ||
// update | ||
err = DB().Model(new(AlertingEngines)).Where("instance=?", instance).Update("clock", time.Now().Unix()).Error | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.