node.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package node
  2. import (
  3. "github.com/gogf/gf/v2/database/gdb"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "golang.org/x/net/context"
  6. "nodeMonitor/internal/dao"
  7. "nodeMonitor/internal/model"
  8. "nodeMonitor/internal/model/do"
  9. "nodeMonitor/internal/model/entity"
  10. "nodeMonitor/internal/service"
  11. )
  12. type (
  13. sNode struct{}
  14. )
  15. func init() {
  16. service.RegisterNode(New())
  17. }
  18. func New() *sNode {
  19. return &sNode{}
  20. }
  21. // Create 创建节点
  22. func (c *sNode) Create(ctx context.Context, input model.NodeCreateInput) error {
  23. return dao.Node.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  24. _, err := dao.Node.Ctx(ctx).Data(do.Node{
  25. Name: input.Name,
  26. Host: input.Host,
  27. Port: input.Port,
  28. Url: input.Url,
  29. PingType: input.PingType,
  30. }).Insert()
  31. return err
  32. })
  33. }
  34. // Del 删除节点
  35. func (c *sNode) Del(ctx context.Context, input model.NodeDelInput) error {
  36. _, err := dao.Node.Ctx(ctx).Where(g.Map{
  37. "host": input.Host,
  38. }).Delete()
  39. return err
  40. }
  41. // IsExist 节点是否存在
  42. func (c *sNode) IsExist(ctx context.Context, input model.NodeDelInput) (bool, error) {
  43. count, err := dao.Node.Ctx(ctx).Where(g.Map{
  44. "host": input.Host,
  45. }).Count()
  46. if err != nil {
  47. return false, err
  48. }
  49. return count == 0, nil
  50. }
  51. // UpdateNode 更新指定节点
  52. func (c *sNode) UpdateNode(ctx context.Context, input model.NodeCreateInput) error {
  53. _, err := dao.Node.Ctx(ctx).Where(g.Map{
  54. "host": input.Host,
  55. }).Update(g.Map{
  56. "host": input.Host,
  57. "name": input.Name,
  58. "port": input.Port,
  59. "url": input.Url,
  60. "ping_type": input.PingType,
  61. })
  62. return err
  63. }
  64. // GetNode 获取全部数据
  65. func (c *sNode) GetNode(ctx context.Context) ([]*entity.Node, error) {
  66. var nodeList []*entity.Node
  67. err := dao.Node.Ctx(ctx).Scan(&nodeList)
  68. return nodeList, err
  69. }
  70. // FindNode 获取指定节点数据
  71. func (c *sNode) FindNode(ctx context.Context, host string) (*entity.Node, error) {
  72. var nodeList *entity.Node
  73. err := dao.Node.Ctx(ctx).Where("host = ?", host).Scan(&nodeList)
  74. return nodeList, err
  75. }
  76. // UpdateNodeUrlStatus 更新节点url是否执行完成
  77. func (c *sNode) UpdateNodeUrlStatus(ctx context.Context, input model.NodeCreateInput) error {
  78. _, err := dao.Node.Ctx(ctx).Where(g.Map{
  79. "host": input.Host,
  80. }).Update(g.Map{
  81. "url_status": input.UrlStatus,
  82. })
  83. return err
  84. }