mng_v2.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/admin/main/search/model"
  7. "go-common/library/ecode"
  8. )
  9. // BusinessAllV2 .
  10. func (s *Service) BusinessAllV2(c context.Context) (list []*model.GFBusiness, err error) {
  11. return s.dao.BusinessAllV2(c)
  12. }
  13. // BusinessInfoV2 .
  14. func (s *Service) BusinessInfoV2(c context.Context, name string) (info *model.GFBusiness, err error) {
  15. return s.dao.BusinessInfoV2(c, name)
  16. }
  17. // BusinessAdd .
  18. func (s *Service) BusinessAdd(c context.Context, pid int64, name, description string) (id int64, err error) {
  19. return s.dao.BusinessIns(c, pid, name, description)
  20. }
  21. // BusinessUpdate .
  22. func (s *Service) BusinessUpdate(c context.Context, name, filed, value string) (id int64, err error) {
  23. allowFields := []string{"data_conf", "index_conf", "business_conf", "description", "state"}
  24. var allow bool
  25. for _, v := range allowFields {
  26. if v == filed {
  27. allow = true
  28. }
  29. }
  30. if !allow {
  31. err = ecode.AccessDenied
  32. return
  33. }
  34. return s.dao.BusinessUpdate(c, name, filed, value)
  35. }
  36. // AssetDBTables .
  37. func (s *Service) AssetDBTables(c context.Context) (list []*model.GFAsset, err error) {
  38. return s.dao.AssetDBTables(c)
  39. }
  40. // AssetDBConnect .
  41. func (s *Service) AssetDBConnect(c context.Context, host, port, user, password string) (dbNames []string, err error) {
  42. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/?charset=utf8mb4,utf8", user, password, host, port))
  43. if err != nil {
  44. return
  45. }
  46. defer db.Close()
  47. rows, err := db.Query("show databases")
  48. if err != nil {
  49. return
  50. }
  51. defer rows.Close()
  52. dbNames = make([]string, 0)
  53. for rows.Next() {
  54. var name string
  55. if err = rows.Scan(&name); err != nil {
  56. return
  57. }
  58. dbNames = append(dbNames, name)
  59. }
  60. return dbNames, rows.Err()
  61. }
  62. // AssetDBAdd .
  63. func (s *Service) AssetDBAdd(c context.Context, name, description, host, port, user, password string) (id int64, err error) {
  64. dbnames, err := s.AssetDBConnect(c, host, port, user, password)
  65. if err != nil {
  66. return
  67. }
  68. var dbExist bool
  69. for _, v := range dbnames {
  70. if v == name {
  71. dbExist = true
  72. }
  73. }
  74. if !dbExist {
  75. err = ecode.AccessDenied
  76. return
  77. }
  78. dsn := fmt.Sprintf(model.DBDsnFormat, user, password, host, port, name)
  79. return s.dao.AssetDBIns(c, name, description, dsn)
  80. }
  81. // AssetTableAdd .
  82. func (s *Service) AssetTableAdd(c context.Context, db, regex, fields, description string) (id int64, err error) {
  83. name := db + "." + regex
  84. return s.dao.AssetTableIns(c, name, db, regex, fields, description)
  85. }
  86. // UpdateAssetTable .
  87. func (s *Service) UpdateAssetTable(c context.Context, name, fields string) (id int64, err error) {
  88. return s.dao.UpdateAssetTable(c, name, fields)
  89. }
  90. // AssetInfoV2 .
  91. func (s *Service) AssetInfoV2(c context.Context, name string) (info *model.GFAsset, err error) {
  92. return s.dao.Asset(c, name)
  93. }
  94. // AssetShowTables .
  95. func (s *Service) AssetShowTables(c context.Context, dbName string) (tables []string, err error) {
  96. asset, err := s.dao.Asset(c, dbName)
  97. if err != nil {
  98. return
  99. }
  100. db, err := sql.Open("mysql", asset.DSN)
  101. if err != nil {
  102. return
  103. }
  104. defer db.Close()
  105. rows, err := db.Query("show tables")
  106. if err != nil {
  107. return
  108. }
  109. defer rows.Close()
  110. tables = make([]string, 0)
  111. for rows.Next() {
  112. var name string
  113. if err = rows.Scan(&name); err != nil {
  114. return
  115. }
  116. tables = append(tables, name)
  117. }
  118. return tables, rows.Err()
  119. }
  120. // AssetTableFields .
  121. func (s *Service) AssetTableFields(c context.Context, dbName, regex string) (fs []*model.TableField, count int, err error) {
  122. asset, err := s.dao.Asset(c, dbName)
  123. if err != nil {
  124. return
  125. }
  126. db, err := sql.Open("mysql", asset.DSN)
  127. if err != nil {
  128. return
  129. }
  130. defer db.Close()
  131. regex = fmt.Sprintf("^%s$", regex)
  132. rows, err := db.Query("SELECT COLUMN_NAME,DATA_TYPE,count(1) FROM information_schema.COLUMNS WHERE table_name REGEXP ? GROUP BY COLUMN_NAME,DATA_TYPE", regex)
  133. if err != nil {
  134. return
  135. }
  136. defer rows.Close()
  137. fs = make([]*model.TableField, 0)
  138. for rows.Next() {
  139. f := new(model.TableField)
  140. if err = rows.Scan(&f.Name, &f.Type, &f.Count); err != nil {
  141. return nil, 0, err
  142. }
  143. fs = append(fs, f)
  144. }
  145. if err = rows.Err(); err != nil {
  146. return
  147. }
  148. if len(fs) == 0 {
  149. err = ecode.NothingFound
  150. return
  151. }
  152. for _, f := range fs {
  153. if fs[0].Count != f.Count {
  154. err = ecode.NothingFound
  155. return
  156. }
  157. count = f.Count
  158. }
  159. row := db.QueryRow("SELECT COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE table_name REGEXP ? AND CONSTRAINT_NAME='PRIMARY' GROUP BY CONSTRAINT_NAME LIMIT 1", regex)
  160. var primaryCo string
  161. err = row.Scan(&primaryCo)
  162. for k, v := range fs {
  163. if v.Name == primaryCo {
  164. fs[k].Primary = true
  165. }
  166. }
  167. return fs, count, err
  168. }
  169. // ClusterOwners .
  170. func (s *Service) ClusterOwners() map[string]string {
  171. clusters := s.c.Es
  172. res := make(map[string]string)
  173. res["default"] = "guanhuaxin,daizhichen,libingqi,zhapuyu"
  174. for name, es := range clusters {
  175. if es.Owner == "" {
  176. continue
  177. }
  178. if es.Cluster != "" {
  179. name = es.Cluster
  180. }
  181. res[name] = es.Owner
  182. }
  183. return res
  184. }