init.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. log.Info("InitEs", zap.String("阿里云", config.Conf.DB.Es.Addr))
  130. if config.Conf.DB.Es.Addr == "" {
  131. log.Error("InitEs", zap.String("ES", "地址或者数据库为空"))
  132. }
  133. if config.Conf.DB.Es.IndexB == "" {
  134. log.Error("InitEs", zap.String("IndexB", "indexb bidding 索引为空,请检查"))
  135. } else {
  136. log.Debug("InitEs", zap.String("IndexB", config.Conf.DB.Es.IndexB))
  137. }
  138. if config.Conf.DB.Es.IndexP == "" {
  139. log.Error("InitEs", zap.String("IndexB", "projectset 项目索引为空,请检查"))
  140. } else {
  141. log.Debug("InitEs", zap.String("IndexP", config.Conf.DB.Es.IndexP))
  142. }
  143. //if config.Conf.DB.Es.IndexTmp == "" {
  144. // log.Error("InitEs", zap.String("IndexTmp 为空", "请检查是否需要配置;该配置主要生产环境需要"))
  145. //}
  146. if config.Conf.DB.Es.IndexWinner == "" {
  147. log.Error("InitEs", zap.String("IndexWinner", "中标单位 索引为空,请检查"))
  148. } else {
  149. log.Debug("InitEs", zap.String("IndexWinner", config.Conf.DB.Es.IndexWinner))
  150. }
  151. if config.Conf.DB.Es.IndexBuyer == "" {
  152. log.Error("InitEs", zap.String("IndexBuyer", "采购单位 索引为空,请检查"))
  153. } else {
  154. log.Debug("InitEs", zap.String("IndexBuyer", config.Conf.DB.Es.IndexBuyer))
  155. }
  156. ////采集爬虫 单服务器部署的es;目前已使用华为云
  157. //Es1 = &elastic.Elastic{
  158. // S_esurl: config.Conf.DB.Es.AddrP,
  159. // I_size: config.Conf.DB.Es.Size,
  160. // Username: config.Conf.DB.Es.Username,
  161. // Password: config.Conf.DB.Es.Password,
  162. //}
  163. //Es1.InitElasticSize()
  164. //华为云 部署的es
  165. if config.Conf.DB.Es.Addr2 != "" {
  166. Es2 = &elastic.Elastic{
  167. S_esurl: config.Conf.DB.Es.Addr2,
  168. I_size: config.Conf.DB.Es.Size,
  169. Username: config.Conf.DB.Es.Username2,
  170. Password: config.Conf.DB.Es.Password2,
  171. }
  172. Es2.InitElasticSize()
  173. log.Info("InitEs", zap.String("华为云Addr2", config.Conf.DB.Es.Addr2))
  174. }
  175. log.Info("InitEs", zap.Any("duration", time.Since(now).Seconds()))
  176. }
  177. func InitField() {
  178. now := time.Now()
  179. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "project"}`, nil, nil, false, -1, -1)
  180. if len(*info) > 0 {
  181. for _, m := range *info {
  182. if util.IntAll(m["level"]) == 1 {
  183. ProjectField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  184. } else if util.IntAll(m["level"]) == 2 {
  185. ProjectListF[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  186. }
  187. }
  188. }
  189. log.Info("InitField", zap.Int("ProjectField", len(ProjectField)), zap.Int("ProjectListF", len(ProjectListF)))
  190. log.Info("InitField", zap.Any("duration", time.Since(now).Seconds()))
  191. }
  192. //InitPreProcessField 预处理阶段字段
  193. func InitPreProcessField() {
  194. now := time.Now()
  195. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "pre_process"}`, nil, nil, false, -1, -1)
  196. if len(*info) > 0 {
  197. for _, m := range *info {
  198. if util.IntAll(m["level"]) == 1 {
  199. PreProcessField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  200. }
  201. }
  202. }
  203. log.Info("InitPreProcessField", zap.Int("PreProcessField", len(ProjectField)))
  204. log.Info("InitPreProcessField", zap.Any("duration", time.Since(now).Seconds()))
  205. }
  206. //InitPreEsClient 实例化预处理 索引客户端
  207. func InitPreEsClient() {
  208. if len(config.Conf.Pre) > 0 {
  209. for k, v := range config.Conf.Pre {
  210. cli := &elastic.Elastic{
  211. S_esurl: v.Addr,
  212. I_size: 30,
  213. Username: v.Username,
  214. Password: v.Password,
  215. }
  216. cli.InitElasticSize()
  217. PreEs[k] = cli
  218. }
  219. }
  220. }
  221. //InitEsBiddingField 初始化 bidding 索引字段
  222. func InitEsBiddingField() {
  223. now := time.Now()
  224. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "bidding"}`, nil, nil, false, -1, -1)
  225. if len(*info) > 0 {
  226. for _, m := range *info {
  227. if util.IntAll(m["level"]) == 1 {
  228. BiddingField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  229. } else if util.IntAll(m["level"]) == 2 {
  230. pfield := util.ObjToString(m["pfield"])
  231. pfieldMap := BiddingLevelField[pfield]
  232. if pfieldMap == nil {
  233. pfieldMap = make(map[string]string, 0)
  234. }
  235. pfieldMap[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  236. BiddingLevelField[pfield] = pfieldMap
  237. }
  238. }
  239. }
  240. log.Info("InitEsBiddingField", zap.Int("BiddingField es 一级字段数量", len(BiddingField)))
  241. log.Info("InitEsBiddingField", zap.Int("BiddingLevelField es 二级字段数量", len(BiddingLevelField)))
  242. log.Info("InitEsBiddingField", zap.Any("duration", time.Since(now).Seconds()))
  243. }
  244. //verifyESFields 验证es 定义字段类型和 MongoDB 数据字段
  245. func verifyESFields() {
  246. now := time.Now()
  247. log.Info("verifyESFields", zap.String("开始类型检测", ""))
  248. client, _ := es7.NewClient(
  249. es7.SetURL(config.Conf.DB.Es.Addr),
  250. es7.SetBasicAuth(config.Conf.DB.Es.Username, config.Conf.DB.Es.Password),
  251. es7.SetSniff(false),
  252. )
  253. index := config.Conf.DB.Es.IndexB //索引表 bidding
  254. // 获取 Elasticsearch 索引的 mapping 信息
  255. mapping, err := client.GetMapping().Index(index).Do(context.Background())
  256. if err != nil {
  257. log.Error("verifyESFields", zap.Any("getting Elasticsearch mapping:", err))
  258. }
  259. indexName, _ := GetIndexName(client, index)
  260. if indexName == "" || mapping == nil {
  261. log.Error("verifyESFields", zap.String("索引不存在,请检查索引", index))
  262. os.Exit(-1)
  263. return
  264. }
  265. if mapping[indexName].(map[string]interface{})["mappings"] == nil || mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"] == nil {
  266. log.Error("verifyESFields", zap.String("索引不存在或状态不对,请检查索引", index))
  267. os.Exit(-1)
  268. return
  269. }
  270. properties := mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
  271. var errField = make([]string, 0)
  272. var okField = make([]string, 0)
  273. var analyzerMap = make(map[string]string) // 分词信息
  274. var esMap = make(map[string]string) //存储es 字段类型
  275. //
  276. for field, ftype := range BiddingField {
  277. eftypeMap, _ := properties[field].(map[string]interface{})
  278. var etype string
  279. var analyzer string
  280. if fftype, ok := eftypeMap["type"]; ok {
  281. etype = fftype.(string)
  282. esMap[field] = etype
  283. }
  284. if ffanalyzer, ok := eftypeMap["analyzer"]; ok {
  285. analyzer = ffanalyzer.(string)
  286. analyzerMap[field] = analyzer
  287. }
  288. if ftype != "" {
  289. if chargeType(ftype, etype) {
  290. okField = append(okField, field)
  291. } else {
  292. errField = append(errField, field)
  293. }
  294. } else {
  295. if field == "_id" {
  296. continue
  297. } else if field == "purchasinglist" || field == "package" || field == "winnerorder" || field == "procurementlist" {
  298. if eproperties, ok := eftypeMap["properties"]; ok {
  299. if eproMap, ok := eproperties.(map[string]interface{}); ok {
  300. for k, v := range eproMap {
  301. if innerMap, ok := v.(map[string]interface{}); ok {
  302. if innerType, ok := innerMap["type"]; ok {
  303. innerLevel := BiddingLevelField[field]
  304. esMap[fmt.Sprintf("%s.%s", field, k)] = innerType.(string)
  305. if chargeType(innerLevel[k], innerType.(string)) {
  306. okField = append(okField, fmt.Sprintf("%s.%s", field, k))
  307. } else {
  308. errField = append(errField, fmt.Sprintf("%s.%s", field, k))
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. if len(errField) > 0 {
  319. log.Error("verifyESFields", zap.Int("错误字段数量", len(errField)))
  320. for _, field := range errField {
  321. if strings.Contains(field, ".") {
  322. fe := strings.Split(field, ".")
  323. log.Error(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingLevelField[fe[0]][fe[1]]), esMap[field]))
  324. } else {
  325. log.Error(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingField[field]), esMap[field]))
  326. }
  327. }
  328. os.Exit(-1)
  329. } else {
  330. log.Info("es 字段类型检测结束,", zap.Int("所有字段都符合,检测字段数量为:", len(okField)))
  331. }
  332. log.Info("verifyESFields", zap.Any("duration", time.Since(now).Seconds()))
  333. }
  334. func GetIndexName(client *es7.Client, name string) (string, error) {
  335. // 判断 name 是否为一个别名
  336. res, err := client.Aliases().Alias(name).Do(context.Background())
  337. if err != nil {
  338. // 错误处理
  339. if err.(*es7.Error).Status != 404 && err.(*es7.Error).Details != nil {
  340. return "", err
  341. }
  342. }
  343. if res != nil {
  344. for k, v := range res.Indices {
  345. for _, vv := range v.Aliases {
  346. if vv.AliasName == name {
  347. return k, nil
  348. }
  349. }
  350. }
  351. }
  352. // 判断 name 是否为一个正式索引名称
  353. resa, err := client.IndexExists(name).Do(context.Background())
  354. if err != nil {
  355. // 错误处理
  356. return "", err
  357. }
  358. if resa {
  359. return name, nil
  360. }
  361. // 如果 name 既不是别名,也不是正式索引名称,则返回空字符串
  362. return "", nil
  363. }