dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/search/conf"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/stat/prom"
  9. "go-common/library/sync/errgroup"
  10. "gopkg.in/olivere/elastic.v5"
  11. )
  12. const (
  13. _managerDep = "/x/admin/manager/users/udepts"
  14. _managerUnames = "/x/admin/manager/users/unames"
  15. _managerIP = "/x/location/infos"
  16. )
  17. // Dao .
  18. type Dao struct {
  19. c *conf.Config
  20. esPool map[string]*elastic.Client
  21. db *sql.DB
  22. client *bm.Client
  23. managerDep string
  24. managerUnames string
  25. managerIP string
  26. queryConfStmt *sql.Stmt
  27. }
  28. // New init dao
  29. func New(c *conf.Config) (d *Dao) {
  30. d = &Dao{
  31. c: c,
  32. db: sql.NewMySQL(c.DB.Search),
  33. client: bm.NewClient(c.HTTPClient),
  34. managerDep: c.Prop.Manager + _managerDep,
  35. managerUnames: c.Prop.Manager + _managerUnames,
  36. managerIP: c.Prop.API + _managerIP,
  37. }
  38. d.esPool = newEsPool(c, d)
  39. d.NewLog()
  40. go d.NewLogProcess()
  41. d.queryConfStmt = d.db.Prepared(_queryConfSQL)
  42. return
  43. }
  44. // BulkItem .
  45. type BulkItem interface {
  46. IndexName() string
  47. IndexType() string
  48. IndexID() string
  49. }
  50. // BulkMapItem .
  51. type BulkMapItem interface {
  52. IndexName() string
  53. IndexType() string
  54. IndexID() string
  55. PField() map[string]interface{}
  56. }
  57. // newEsCluster cluster action
  58. func newEsPool(c *conf.Config, d *Dao) (esCluster map[string]*elastic.Client) {
  59. esCluster = make(map[string]*elastic.Client)
  60. for esName, e := range c.Es {
  61. cof := []elastic.ClientOptionFunc{}
  62. cof = append(cof, elastic.SetURL(e.Addr...))
  63. if esName == "ops_log" {
  64. cof = append(cof, elastic.SetSniff(false))
  65. }
  66. client, err := elastic.NewClient(cof...)
  67. if err != nil {
  68. PromError("es:集群连接失败", "cluster: %s, %v", esName, err)
  69. continue
  70. }
  71. esCluster[esName] = client
  72. }
  73. return
  74. }
  75. // PromError prometheus error count.
  76. func PromError(name, format string, args ...interface{}) {
  77. prom.BusinessErrCount.Incr(name)
  78. log.Error(format, args...)
  79. }
  80. // Ping health
  81. func (d *Dao) Ping(c context.Context) (err error) {
  82. group := errgroup.Group{}
  83. group.Go(func() (err error) {
  84. err = d.db.Ping(context.Background())
  85. if err != nil {
  86. PromError("DB:Ping", "DB:Ping error(%v)", err)
  87. }
  88. return
  89. })
  90. for name, client := range d.esPool {
  91. group.Go(func() (err error) {
  92. _, _, err = client.Ping(d.c.Es[name].Addr[0]).Do(context.Background())
  93. if err != nil {
  94. PromError("Es:Ping", "%s:Ping error(%v)", name, err)
  95. }
  96. return
  97. })
  98. }
  99. return group.Wait()
  100. }