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
|
||||
}
|
||||
Reference in New Issue
Block a user