1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package node
- import (
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/frame/g"
- "golang.org/x/net/context"
- "nodeMonitor/internal/dao"
- "nodeMonitor/internal/model"
- "nodeMonitor/internal/model/do"
- "nodeMonitor/internal/model/entity"
- "nodeMonitor/internal/service"
- )
- type (
- sNode struct{}
- )
- func init() {
- service.RegisterNode(New())
- }
- func New() *sNode {
- return &sNode{}
- }
- // Create 创建节点
- func (c *sNode) Create(ctx context.Context, input model.NodeCreateInput) error {
- return dao.Node.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err := dao.Node.Ctx(ctx).Data(do.Node{
- Name: input.Name,
- Host: input.Host,
- Port: input.Port,
- Url: input.Url,
- PingType: input.PingType,
- }).Insert()
- return err
- })
- }
- // Del 删除节点
- func (c *sNode) Del(ctx context.Context, input model.NodeDelInput) error {
- _, err := dao.Node.Ctx(ctx).Where(g.Map{
- "host": input.Host,
- }).Delete()
- return err
- }
- // IsExist 节点是否存在
- func (c *sNode) IsExist(ctx context.Context, input model.NodeDelInput) (bool, error) {
- count, err := dao.Node.Ctx(ctx).Where(g.Map{
- "host": input.Host,
- }).Count()
- if err != nil {
- return false, err
- }
- return count == 0, nil
- }
- // UpdateNode 更新指定节点
- func (c *sNode) UpdateNode(ctx context.Context, input model.NodeCreateInput) error {
- _, err := dao.Node.Ctx(ctx).Where(g.Map{
- "host": input.Host,
- }).Update(g.Map{
- "host": input.Host,
- "name": input.Name,
- "port": input.Port,
- "url": input.Url,
- "ping_type": input.PingType,
- })
- return err
- }
- // GetNode 获取全部数据
- func (c *sNode) GetNode(ctx context.Context) ([]*entity.Node, error) {
- var nodeList []*entity.Node
- err := dao.Node.Ctx(ctx).Scan(&nodeList)
- return nodeList, err
- }
- // FindNode 获取指定节点数据
- func (c *sNode) FindNode(ctx context.Context, host string) (*entity.Node, error) {
- var nodeList *entity.Node
- err := dao.Node.Ctx(ctx).Where("host = ?", host).Scan(&nodeList)
- return nodeList, err
- }
- // UpdateNodeUrlStatus 更新节点url是否执行完成
- func (c *sNode) UpdateNodeUrlStatus(ctx context.Context, input model.NodeCreateInput) error {
- _, err := dao.Node.Ctx(ctx).Where(g.Map{
- "host": input.Host,
- }).Update(g.Map{
- "url_status": input.UrlStatus,
- })
- return err
- }
|