Initial commit
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package monitormod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, TN.Monitor)
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Monitor.Name, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Monitor.AppId, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Monitor.Status, Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(Monitor:initMonitorIndex) Create Monitor Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneMonitor 插入一个应用监控
|
||||
func InsertOneMonitor(ctx context.Context, Monitor *Monitor) error {
|
||||
if _, err := db.InsertOne(ctx, Monitor); err != nil {
|
||||
log.Warn("Monitor:InsertOneMonitor db.InsertOne failed", log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneMonitor 查找一个应用监控
|
||||
func FindOneMonitor(ctx context.Context, filter bson.M) (
|
||||
*Monitor, error) {
|
||||
var Monitor Monitor
|
||||
if err := db.FindOne(ctx, &Monitor, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Monitor, nil
|
||||
}
|
||||
|
||||
// DelOneMonitor 删除一个应用监控
|
||||
func DelOneMonitor(ctx context.Context, filter bson.M) (
|
||||
int64, error) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("Monitor:DelOneMonitor db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneMonitor 更新一个应用监控
|
||||
func UpdateOneMonitor(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
_, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateOneMonitor db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountMonitors 统计应用监控数量
|
||||
func CountMonitors(ctx context.Context, filter bson.M) (
|
||||
int64, error) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("Monitor:CountMonitors db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateManyMonitor 更新满足条件的应用监控
|
||||
func UpdateManyMonitor(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
_, err := db.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateManyMonitor db.UpdateMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyMonitor 查找一批应用监控数据
|
||||
func FindManyMonitor(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (MonitorList, error) {
|
||||
data := make(MonitorList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("Monitor:FindManyMonitor db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FindManyMonitorWithBind 用自己定义结构查找一批应用监控数据
|
||||
func FindManyMonitorWithBind(ctx context.Context, bind interface{}, filter bson.M,
|
||||
opts ...*options.FindOptions) error {
|
||||
if err := db.Find(ctx, bind, filter, opts...); err != nil {
|
||||
log.Warn("Monitor:FindManyMonitorWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateMonitors 聚合查找一批应用监控信息
|
||||
func AggregateMonitors(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) error {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("feedback:AggregateMonitors db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package monitormod
|
||||
|
||||
type TagMonitor struct {
|
||||
ID string
|
||||
AppId string
|
||||
Name string
|
||||
Category string
|
||||
Device string
|
||||
Duration string
|
||||
Compare string
|
||||
Threshold string
|
||||
Comment string
|
||||
Status string
|
||||
TriggerCount string
|
||||
CheckCount string
|
||||
UpdatedAt string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
Monitor TagMonitor
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
Monitor string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.Monitor.ID = "_id"
|
||||
FN.Monitor.AppId = "appId"
|
||||
FN.Monitor.Name = "name"
|
||||
FN.Monitor.Category = "category"
|
||||
FN.Monitor.Device = "device"
|
||||
FN.Monitor.Duration = "duration"
|
||||
FN.Monitor.Compare = "compare"
|
||||
FN.Monitor.Threshold = "threshold"
|
||||
FN.Monitor.Comment = "comment"
|
||||
FN.Monitor.Status = "status"
|
||||
FN.Monitor.TriggerCount = "triggerCount"
|
||||
FN.Monitor.CheckCount = "checkCount"
|
||||
FN.Monitor.UpdatedAt = "updatedAt"
|
||||
FN.Monitor.CreatedAt = "createdAt"
|
||||
TN.Monitor = "monitor"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package monitormod
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"stat/common/statutils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MonitorCategory int
|
||||
|
||||
const (
|
||||
MonitorCategoryNone MonitorCategory = 0 // 占位符,无意义
|
||||
MonitorCategoryNewUser MonitorCategory = 1 // 新用户监控
|
||||
)
|
||||
|
||||
type Compare int
|
||||
|
||||
const (
|
||||
CompareNone Compare = 0
|
||||
CompareLT Compare = 1 // 小于
|
||||
CompareEQ Compare = 2 // 等于
|
||||
CompareGT Compare = 3 // 大于
|
||||
)
|
||||
|
||||
// 监控模型,数据库中的
|
||||
// @table:monitor
|
||||
type Monitor struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // ID
|
||||
Name string `bson:"name" json:"name"` // 名称
|
||||
AppId int32 `bson:"appId" json:"appId"` // appId
|
||||
Category MonitorCategory `bson:"category" json:"category"` // 分类
|
||||
Device statutils.DeviceName `bson:"device" json:"device"` // 设备
|
||||
Duration int64 `bson:"duration" json:"duration"` // 时间段,单位分钟
|
||||
Compare Compare `bson:"compare" json:"compare"` // 比较
|
||||
Threshold int64 `bson:"threshold" json:"threshold"` // 阀值
|
||||
Comment string `bson:"comment" json:"comment"` // 注释
|
||||
Status bool `bson:"status" json:"status"` // 是否启用
|
||||
TriggerCount int64 `bson:"triggerCount" json:"triggerCount"` // 触发次数
|
||||
CheckCount int64 `bson:"checkCount" json:"checkCount"` // 检测次数
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
type MonitorList []*Monitor
|
||||
Reference in New Issue
Block a user