forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package monitorSer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stat/api/proto"
|
||||
"stat/models/monitormod"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func SearchMonitor(ctx context.Context, req *proto.MonitorSearch) (resp *proto.MonitorSearchResp, err error) {
|
||||
resp = &proto.MonitorSearchResp{}
|
||||
|
||||
filter := bson.M{}
|
||||
opt := &options.FindOptions{}
|
||||
|
||||
if req.Status != nil {
|
||||
filter[monitormod.FN.Monitor.Status] = *req.Status
|
||||
}
|
||||
if req.Name != nil {
|
||||
filter[monitormod.FN.Monitor.Name] = bson.M{"$regex": *req.Name}
|
||||
}
|
||||
if req.Category != nil {
|
||||
filter[monitormod.FN.Monitor.Category] = *req.Category
|
||||
}
|
||||
opt.SetSort(bson.M{monitormod.FN.Monitor.CreatedAt: -1})
|
||||
opt.SetSkip((req.PageNum - 1) * req.PageSize)
|
||||
opt.SetLimit(req.PageSize)
|
||||
|
||||
resp.Total, err = monitormod.CountMonitors(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Total == 0 {
|
||||
return resp, nil
|
||||
}
|
||||
MonitorList, serr := monitormod.FindManyMonitor(ctx, filter, opt)
|
||||
if serr != nil {
|
||||
return nil, serr
|
||||
}
|
||||
resp.List = MonitorList
|
||||
return
|
||||
}
|
||||
|
||||
func AddMonitor(ctx context.Context, req *proto.MonitorAddReq) error {
|
||||
now := time.Now()
|
||||
req.CreatedAt = now
|
||||
req.UpdatedAt = now
|
||||
|
||||
serr := monitormod.InsertOneMonitor(ctx, &req.Monitor)
|
||||
if serr != nil {
|
||||
return serr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func DelMonitor(ctx context.Context, id string) error {
|
||||
objectId, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
adFilter := bson.M{monitormod.FN.Monitor.ID: objectId}
|
||||
//删除
|
||||
_, serr := monitormod.DelOneMonitor(ctx, adFilter)
|
||||
if serr != nil {
|
||||
return serr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateMonitor(ctx context.Context, req *proto.MonitorUpdateReq) error {
|
||||
now := time.Now()
|
||||
req.Param.UpdatedAt = &now
|
||||
objectId, err := primitive.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
adFilter := bson.M{monitormod.FN.Monitor.ID: objectId}
|
||||
updateDoc := bson.M{"$set": req.Param}
|
||||
serr := monitormod.UpdateOneMonitor(ctx, adFilter, updateDoc)
|
||||
if serr != nil {
|
||||
return serr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package monitorSer
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"stat/common/log"
|
||||
"stat/common/statutils"
|
||||
)
|
||||
|
||||
var (
|
||||
auto *cron.Cron
|
||||
)
|
||||
|
||||
func init() {
|
||||
auto = cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(&log.Logger{}))) // 秒级定时任务
|
||||
}
|
||||
|
||||
func InitMonitor() {
|
||||
_, _ = auto.AddFunc("0 1 * * * ?", func() {
|
||||
statutils.ServerMon.RunWithName(func() error {
|
||||
return MonitorCheck()
|
||||
}, statutils.GetFuncName(MonitorCheck))
|
||||
})
|
||||
|
||||
auto.Start()
|
||||
}
|
||||
|
||||
func MonitorCheck() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user