init.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package main
  2. import (
  3. "context"
  4. "esindex/config"
  5. "fmt"
  6. es7 "github.com/olivere/elastic/v7"
  7. "go.uber.org/zap"
  8. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  10. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mysqldb"
  13. "os"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. ProjectField = make(map[string]string, 500) //项目字段
  19. ProjectListF = make(map[string]string, 200)
  20. BiddingField = make(map[string]string, 200) //bidding_processing_field, level=1 最外层字段,
  21. BiddingLevelField = make(map[string]map[string]string) //level=2 的第二层字段
  22. )
  23. // InitLog @Description
  24. // @Author J 2022/7/26 15:30
  25. func InitLog() {
  26. now := time.Now()
  27. logcfg := config.Conf.Log
  28. err := log.InitLog(
  29. log.Path(logcfg.LogPath),
  30. log.Level(logcfg.LogLevel),
  31. log.Compress(logcfg.Compress),
  32. log.MaxSize(logcfg.MaxSize),
  33. log.MaxBackups(logcfg.MaxBackups),
  34. log.MaxAge(logcfg.MaxAge),
  35. log.Format(logcfg.Format),
  36. )
  37. if err != nil {
  38. fmt.Printf("InitLog failed: %v\n", err)
  39. os.Exit(1)
  40. }
  41. log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
  42. }
  43. func InitMgo() {
  44. now := time.Now()
  45. MgoB = &mongodb.MongodbSim{
  46. MongodbAddr: config.Conf.DB.MongoB.Addr,
  47. DbName: config.Conf.DB.MongoB.Dbname,
  48. Size: config.Conf.DB.MongoB.Size,
  49. UserName: config.Conf.DB.MongoB.User,
  50. Password: config.Conf.DB.MongoB.Password,
  51. Direct: config.Conf.DB.MongoB.Direct,
  52. }
  53. MgoB.InitPool()
  54. if config.Conf.DB.MongoB.Addr == "" || config.Conf.DB.MongoB.Dbname == "" {
  55. log.Error("InitMgo", zap.String("MgoB", "地址或者数据库为空"))
  56. }
  57. if config.Conf.DB.MongoB.Coll == "" {
  58. log.Error("InitMgo", zap.String("MgoB", "查询表为空"))
  59. }
  60. log.Info("InitMgo", zap.Any("MgoB duration", time.Since(now).Seconds()))
  61. //项目信息
  62. MgoP = &mongodb.MongodbSim{
  63. MongodbAddr: config.Conf.DB.MongoP.Addr,
  64. DbName: config.Conf.DB.MongoP.Dbname,
  65. Size: config.Conf.DB.MongoP.Size,
  66. UserName: config.Conf.DB.MongoP.User,
  67. Password: config.Conf.DB.MongoP.Password,
  68. }
  69. MgoP.InitPool()
  70. if config.Conf.DB.MongoP.Addr == "" || config.Conf.DB.MongoP.Dbname == "" {
  71. log.Error("InitMgo", zap.String("MongoP", "地址或者数据库为空"))
  72. }
  73. if config.Conf.DB.MongoP.Coll == "" {
  74. log.Error("InitMgo", zap.String("MongoP", "查询表为空"))
  75. }
  76. log.Info("InitMgo", zap.Any("MgoP duration", time.Since(now).Seconds()))
  77. //中标单位定时同步
  78. MgoQ = &mongodb.MongodbSim{
  79. MongodbAddr: config.Conf.DB.MongoQ.Addr,
  80. DbName: config.Conf.DB.MongoQ.Dbname,
  81. Size: config.Conf.DB.MongoQ.Size,
  82. UserName: config.Conf.DB.MongoQ.User,
  83. Password: config.Conf.DB.MongoQ.Password,
  84. }
  85. MgoQ.InitPool()
  86. if config.Conf.DB.MongoQ.Addr == "" || config.Conf.DB.MongoQ.Dbname == "" {
  87. log.Error("InitMgo", zap.String("MongoQ", "地址或者数据库为空"))
  88. }
  89. log.Info("InitMgo", zap.Any("MgoQ duration", time.Since(now).Seconds()))
  90. //181 特殊企业,采购单位验证
  91. MgoS = &mongodb.MongodbSim{
  92. MongodbAddr: config.Conf.DB.MongoS.Addr,
  93. DbName: config.Conf.DB.MongoS.Dbname,
  94. Size: config.Conf.DB.MongoS.Size,
  95. UserName: config.Conf.DB.MongoS.User,
  96. Password: config.Conf.DB.MongoS.Password,
  97. }
  98. MgoS.InitPool()
  99. if config.Conf.DB.MongoS.Addr == "" || config.Conf.DB.MongoS.Dbname == "" {
  100. log.Error("InitMgo", zap.String("MongoS", "地址或者数据库为空"))
  101. }
  102. log.Info("InitMgo", zap.Any("MgoS duration", time.Since(now).Seconds()))
  103. }
  104. func InitMysql() {
  105. //采购单位
  106. now := time.Now()
  107. Mysql = &mysqldb.Mysql{
  108. Address: config.Conf.DB.MysqlB.Addr,
  109. DBName: config.Conf.DB.MysqlB.Dbname,
  110. UserName: config.Conf.DB.MysqlB.Username,
  111. PassWord: config.Conf.DB.MysqlB.Password,
  112. }
  113. Mysql.Init()
  114. if config.Conf.DB.MysqlB.Addr == "" || config.Conf.DB.MysqlB.Dbname == "" {
  115. log.Error("InitMysql", zap.String("Mysql", "地址或者数据库为空"))
  116. }
  117. log.Info("InitMysql", zap.Any("MysqlB duration", time.Since(now).Seconds()))
  118. }
  119. func InitEs() {
  120. now := time.Now()
  121. Es = &elastic.Elastic{
  122. S_esurl: config.Conf.DB.Es.Addr,
  123. I_size: config.Conf.DB.Es.Size,
  124. Username: config.Conf.DB.Es.Username,
  125. Password: config.Conf.DB.Es.Password,
  126. }
  127. Es.InitElasticSize()
  128. if config.Conf.DB.Es.Addr == "" {
  129. log.Error("InitEs", zap.String("ES", "地址或者数据库为空"))
  130. }
  131. if config.Conf.DB.Es.IndexB == "" {
  132. log.Error("InitEs", zap.String("IndexB", "indexb bidding 索引为空,请检查"))
  133. } else {
  134. log.Debug("InitEs", zap.String("IndexB", config.Conf.DB.Es.IndexB))
  135. }
  136. if config.Conf.DB.Es.IndexP == "" {
  137. log.Error("InitEs", zap.String("IndexB", "projectset 项目索引为空,请检查"))
  138. } else {
  139. log.Debug("InitEs", zap.String("IndexP", config.Conf.DB.Es.IndexP))
  140. }
  141. if config.Conf.DB.Es.IndexTmp == "" {
  142. log.Error("InitEs", zap.String("IndexTmp 为空", "请检查是否需要配置;该配置主要生产环境需要"))
  143. }
  144. if config.Conf.DB.Es.IndexWinner == "" {
  145. log.Error("InitEs", zap.String("IndexWinner", "中标单位 索引为空,请检查"))
  146. } else {
  147. log.Debug("InitEs", zap.String("IndexWinner", config.Conf.DB.Es.IndexWinner))
  148. }
  149. if config.Conf.DB.Es.IndexBuyer == "" {
  150. log.Error("InitEs", zap.String("IndexBuyer", "采购单位 索引为空,请检查"))
  151. } else {
  152. log.Debug("InitEs", zap.String("IndexBuyer", config.Conf.DB.Es.IndexBuyer))
  153. }
  154. Es1 = &elastic.Elastic{
  155. S_esurl: config.Conf.DB.Es.AddrP,
  156. I_size: config.Conf.DB.Es.Size,
  157. Username: config.Conf.DB.Es.Username,
  158. Password: config.Conf.DB.Es.Password,
  159. }
  160. Es1.InitElasticSize()
  161. if config.Conf.DB.Es.Addr2 != "" {
  162. Es2 = &elastic.Elastic{
  163. S_esurl: config.Conf.DB.Es.Addr2,
  164. I_size: config.Conf.DB.Es.Size,
  165. Username: config.Conf.DB.Es.Username2,
  166. Password: config.Conf.DB.Es.Password2,
  167. }
  168. Es2.InitElasticSize()
  169. log.Info("InitEs", zap.String("Addr2", config.Conf.DB.Es.Addr2))
  170. }
  171. log.Info("InitEs", zap.Any("duration", time.Since(now).Seconds()))
  172. }
  173. func InitField() {
  174. now := time.Now()
  175. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "project"}`, nil, nil, false, -1, -1)
  176. if len(*info) > 0 {
  177. for _, m := range *info {
  178. if util.IntAll(m["level"]) == 1 {
  179. ProjectField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  180. } else if util.IntAll(m["level"]) == 2 {
  181. ProjectListF[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  182. }
  183. }
  184. }
  185. log.Info("InitField", zap.Int("ProjectField", len(ProjectField)), zap.Int("ProjectListF", len(ProjectListF)))
  186. log.Info("InitField", zap.Any("duration", time.Since(now).Seconds()))
  187. }
  188. //InitEsBiddingField 初始化 bidding 索引字段
  189. func InitEsBiddingField() {
  190. now := time.Now()
  191. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "bidding"}`, nil, nil, false, -1, -1)
  192. if len(*info) > 0 {
  193. for _, m := range *info {
  194. if util.IntAll(m["level"]) == 1 {
  195. BiddingField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  196. } else if util.IntAll(m["level"]) == 2 {
  197. pfield := util.ObjToString(m["pfield"])
  198. pfieldMap := BiddingLevelField[pfield]
  199. if pfieldMap == nil {
  200. pfieldMap = make(map[string]string, 0)
  201. }
  202. pfieldMap[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  203. BiddingLevelField[pfield] = pfieldMap
  204. }
  205. }
  206. }
  207. log.Info("InitEsBiddingField", zap.Int("BiddingField es 一级字段数量", len(BiddingField)))
  208. log.Info("InitEsBiddingField", zap.Int("BiddingLevelField es 二级字段数量", len(BiddingLevelField)))
  209. log.Info("InitEsBiddingField", zap.Any("duration", time.Since(now).Seconds()))
  210. }
  211. //verifyESFields 验证es 定义字段类型和 MongoDB 数据字段
  212. func verifyESFields() {
  213. now := time.Now()
  214. log.Info("verifyESFields", zap.String("开始类型检测", ""))
  215. client, _ := es7.NewClient(
  216. es7.SetURL(config.Conf.DB.Es.Addr),
  217. es7.SetBasicAuth(config.Conf.DB.Es.Username, config.Conf.DB.Es.Password),
  218. es7.SetSniff(false),
  219. )
  220. index := config.Conf.DB.Es.IndexB //索引表 bidding
  221. // 获取 Elasticsearch 索引的 mapping 信息
  222. mapping, err := client.GetMapping().Index(index).Do(context.Background())
  223. if err != nil {
  224. log.Error("verifyESFields", zap.Any("getting Elasticsearch mapping:", err))
  225. }
  226. indexName, _ := GetIndexName(client, index)
  227. if indexName == "" || mapping == nil {
  228. log.Error("verifyESFields", zap.String("索引不存在,请检查索引", index))
  229. os.Exit(-1)
  230. return
  231. }
  232. if mapping[indexName].(map[string]interface{})["mappings"] == nil || mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"] == nil {
  233. log.Error("verifyESFields", zap.String("索引不存在或状态不对,请检查索引", index))
  234. os.Exit(-1)
  235. return
  236. }
  237. properties := mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
  238. var errField = make([]string, 0)
  239. var okField = make([]string, 0)
  240. var analyzerMap = make(map[string]string) // 分词信息
  241. var esMap = make(map[string]string) //存储es 字段类型
  242. //
  243. for field, ftype := range BiddingField {
  244. eftypeMap, _ := properties[field].(map[string]interface{})
  245. var etype string
  246. var analyzer string
  247. if fftype, ok := eftypeMap["type"]; ok {
  248. etype = fftype.(string)
  249. esMap[field] = etype
  250. }
  251. if ffanalyzer, ok := eftypeMap["analyzer"]; ok {
  252. analyzer = ffanalyzer.(string)
  253. analyzerMap[field] = analyzer
  254. }
  255. if ftype != "" {
  256. if chargeType(ftype, etype) {
  257. okField = append(okField, field)
  258. } else {
  259. errField = append(errField, field)
  260. }
  261. } else {
  262. if field == "_id" {
  263. continue
  264. } else if field == "purchasinglist" || field == "package" || field == "winnerorder" || field == "procurementlist" {
  265. if eproperties, ok := eftypeMap["properties"]; ok {
  266. if eproMap, ok := eproperties.(map[string]interface{}); ok {
  267. for k, v := range eproMap {
  268. if innerMap, ok := v.(map[string]interface{}); ok {
  269. if innerType, ok := innerMap["type"]; ok {
  270. innerLevel := BiddingLevelField[field]
  271. esMap[fmt.Sprintf("%s.%s", field, k)] = innerType.(string)
  272. if chargeType(innerLevel[k], innerType.(string)) {
  273. okField = append(okField, fmt.Sprintf("%s.%s", field, k))
  274. } else {
  275. errField = append(errField, fmt.Sprintf("%s.%s", field, k))
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. if len(errField) > 0 {
  286. log.Error("verifyESFields", zap.Int("错误字段数量", len(errField)))
  287. for _, field := range errField {
  288. if strings.Contains(field, ".") {
  289. fe := strings.Split(field, ".")
  290. log.Error(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingLevelField[fe[0]][fe[1]]), esMap[field]))
  291. } else {
  292. log.Error(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingField[field]), esMap[field]))
  293. }
  294. }
  295. os.Exit(-1)
  296. } else {
  297. log.Info("es 字段类型检测结束,", zap.Int("所有字段都符合,检测字段数量为:", len(okField)))
  298. }
  299. log.Info("verifyESFields", zap.Any("duration", time.Since(now).Seconds()))
  300. }
  301. func GetIndexName(client *es7.Client, name string) (string, error) {
  302. // 判断 name 是否为一个别名
  303. res, err := client.Aliases().Alias(name).Do(context.Background())
  304. if err != nil {
  305. // 错误处理
  306. if err.(*es7.Error).Status != 404 && err.(*es7.Error).Details != nil {
  307. return "", err
  308. }
  309. }
  310. if res != nil {
  311. for k, v := range res.Indices {
  312. for _, vv := range v.Aliases {
  313. if vv.AliasName == name {
  314. return k, nil
  315. }
  316. }
  317. }
  318. }
  319. // 判断 name 是否为一个正式索引名称
  320. resa, err := client.IndexExists(name).Do(context.Background())
  321. if err != nil {
  322. // 错误处理
  323. return "", err
  324. }
  325. if resa {
  326. return name, nil
  327. }
  328. // 如果 name 既不是别名,也不是正式索引名称,则返回空字符串
  329. return "", nil
  330. }