init.go 13 KB

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