monitor_tools.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # author : liumiaomiao
  4. #从es库中导出数据到测试环境mongo库
  5. from lib.es_tools import esutil
  6. from datetime import datetime, timedelta
  7. from lib.mongo_tools import MongoUtil,Data_save,MongoSentence
  8. from lib.mysql_tools import MysqlUtil
  9. from lib.clickhouse_tools import ClickhouseUtil, logger
  10. # 定义一周的时间范围,转换为Unix时间戳格式
  11. end_date = int(datetime.now().timestamp())
  12. start_date = int((datetime.now() - timedelta(days=7)).timestamp())
  13. print(f"开始时间:{start_date}--结束时间{end_date}")
  14. class monitorTools:
  15. #标准库bidding-es 每周统计入库数量
  16. def es_bidding(self):
  17. """
  18. es链接
  19. """
  20. db_config = {
  21. # es
  22. 'es_host': '127.0.0.1',
  23. 'es_port': 19800,
  24. 'es_http_auth': ('jianyuGr','we3g8glKfe#'), # 重新申请
  25. 'timeout': 10000,
  26. 'index': "bidding"
  27. }
  28. query = {"query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}}]}}}
  29. # 传入查询语句query 以及配置信息
  30. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  31. counts=esutil.get_es_count(query,**db_config)
  32. count = counts['count']
  33. print("标准库es-bidding每周入库数据量:",count)
  34. return count
  35. # 高质量库bidding-ai-es 每周统计入库数量
  36. def es_bidding_ai(self):
  37. """
  38. es链接
  39. """
  40. db_config = {
  41. # es
  42. 'es_host': '127.0.0.1',
  43. 'es_port': 19800,
  44. 'es_http_auth': ('jianyuGr','we3g8glKfe#'), # 重新申请
  45. 'timeout': 10000,
  46. 'index': "bidding_ai"
  47. }
  48. query = {"query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}}]}}}
  49. # 传入查询语句query 以及配置信息
  50. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  51. counts=esutil.get_es_count(query,**db_config)
  52. count = counts['count']
  53. print("高质量库es-bidding每周入库数据量:",count)
  54. return count
  55. # 标准库bidding-es 碎片化数据每周统计入库数量
  56. def es_bidding_fragment(self):
  57. #正式环境
  58. db_config = {
  59. # es
  60. 'es_host': '127.0.0.1',
  61. 'es_port': 19800,
  62. 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  63. 'timeout': 10000,
  64. 'index': "bidding"
  65. }
  66. # #测试环境http://192.168.3.149:9201
  67. # db_config = {
  68. # # es
  69. # 'es_host': '192.168.3.149',
  70. # 'es_port': 9201,
  71. # # 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  72. # 'timeout': 10000,
  73. # 'index': "bidding"
  74. # }
  75. # 定义要监控的字段值
  76. tags = [
  77. "情报_法务",
  78. "情报_财务审计",
  79. "情报_招标代理",
  80. "情报_管理咨询",
  81. "情报_保险",
  82. "情报_工程设计咨询",
  83. "情报_安防",
  84. "情报_印务商机",
  85. "情报_环境采购",
  86. "情报_家具招投标"
  87. ]
  88. # 初始化字典,将所有标签的计数设置为0
  89. data = {}
  90. for tag in tags:
  91. query = {
  92. "query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}},
  93. {"term": {"tag_topinformation": tag}}]}}}
  94. count = esutil.get_es_count(query, **db_config)
  95. print(f"标准库es-bidding{tag}每周入库数据量:", count['count'])
  96. data[tag]=count['count']
  97. # 检查数据字典以确保所有标签都被更新
  98. print("数据字典内容:", data) # 打印整个数据字典
  99. return data
  100. #拟在建es数据 每周统计入库数量
  101. def es_nzj(self):
  102. """
  103. es链接
  104. """
  105. db_config = {
  106. # es
  107. 'es_host': '127.0.0.1',
  108. 'es_port': 19800,
  109. 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  110. 'timeout': 10000,
  111. 'index': "proposed_v1"
  112. }
  113. query = {
  114. "query": {"match_all": {}}}
  115. # 传入查询语句query 以及配置信息
  116. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  117. counts = esutil.get_es_count(query, **db_config)
  118. count=counts['count']
  119. print("拟在建es入库数据总量:", count)
  120. return count
  121. #医械通es,每周统计入库数量
  122. def medical_es(self):
  123. """
  124. es链接
  125. """
  126. db_config = {
  127. # es
  128. 'es_host': '127.0.0.1',
  129. 'es_port': 19800,
  130. 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  131. 'timeout': 10000,
  132. 'index': "bidding"
  133. }
  134. query = {
  135. "query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}},{"term": {"bid_field": "0101"}}]}}}
  136. # 传入查询语句query 以及配置信息
  137. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  138. counts = esutil.get_es_count(query, **db_config)
  139. count = counts['count']
  140. print("医械通es每周入库数据量:", count)
  141. return count
  142. #标准库bidding-mongo 每周统计入库数量
  143. def bidding(self):
  144. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw',collection='bidding',authuser='viewdata',authpass='viewdata')
  145. query = { "comeintime": {"$gte": start_date, "$lt": end_date}}
  146. count=MongoSentence.count(collection,query)
  147. print("标准库bidding-mongo 每周统计入库数量",count)
  148. return count
  149. #高质量库bidding-mongo 每周统计入库数量
  150. def bidding_ai(self):
  151. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw_ai',collection='bidding',authuser='viewdata',authpass='viewdata')
  152. query = { "comeintime": {"$gte": start_date, "$lt": end_date}}
  153. count=MongoSentence.count(collection,query)
  154. print("高质量库bidding-mongo 每周统计入库数量",count)
  155. return count
  156. #标准库bidding-mongo碎片化数据 每周统计入库数量
  157. def bidding_fragment(self):
  158. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw',collection='bidding',authuser='viewdata',authpass='viewdata')
  159. # 定义要监控的字段值
  160. tags = [
  161. "情报_法务",
  162. "情报_财务审计",
  163. "情报_招标代理",
  164. "情报_管理咨询",
  165. "情报_保险",
  166. "情报_工程设计咨询",
  167. "情报_安防",
  168. "情报_印务商机",
  169. "情报_环境采购",
  170. "情报_家具招投标"
  171. ]
  172. # tags = [
  173. # "情报_环境采购",
  174. # "情报_家具招投标"
  175. # ]
  176. data={}
  177. for tag in tags:
  178. query = {"comeintime": {"$gte": start_date, "$lt": end_date},"tag_topinformation":tag}
  179. count=MongoSentence.count(collection,query)
  180. print(f"标准库bidding-mongo{tag}每周统计入库数量",count)
  181. data[tag]=count
  182. return data
  183. #拟在建baseinfo-mysql 每周统计入库数量
  184. def nzj(self):
  185. # MySQL 数据库连接配置
  186. # mysql_db_config = {
  187. # 'host': '192.168.3.149',
  188. # 'port': 4000,
  189. # 'user': 'datagroup',
  190. # 'password': 'Dgrpdb#2024@36',
  191. # 'database': 'jianyu_subjectdb',
  192. # 'charset': 'utf8mb4'
  193. # }
  194. now = datetime.now()
  195. end_date = now.strftime("%Y-%m-%d %H:%M:%S")
  196. start_date = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S")
  197. # SQL 查询
  198. mysql_query = "SELECT COUNT(*) FROM jianyu_subjectdb.dwd_f_nzj_baseinfo WHERE createtime >= %s AND createtime <= %s"
  199. params = (start_date, end_date)
  200. conn=MysqlUtil.connect_to_mysql(host='127.0.0.1',port='4000',user='jydev',password='JSuytest#s211',database='jianyu_subjectdb')
  201. count=MysqlUtil.execute_sql(conn,mysql_query,params)
  202. print("拟在建baseinfo-mysql每周统计入库数量", count)
  203. return count
  204. #人脉数据,每周统计入库数量
  205. def connections(self):
  206. try:
  207. query = f"SELECT COUNT(*) FROM information.transaction_info_all WHERE create_time >={start_date} AND create_time <={end_date}"
  208. # conn=ClickhouseUtil.connect_to_clickhouse(host='192.168.3.207',port='19000',user='jytop',password='pwdTopJy123',database='information')
  209. conn=ClickhouseUtil.connect_to_clickhouse(host='127.0.0.1',port='9000',user='biservice',password='Bi_top95215#',database='information')
  210. count=ClickhouseUtil.execute_sql(conn,query)
  211. result=count[0][0]
  212. print("人脉数据每周统计入库数量", result)
  213. return result
  214. except Exception as e:
  215. logger.error("An error occurred: %s", e)
  216. raise
  217. #医械通,每周统计入库数量
  218. def medical(self):
  219. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw', collection='bidding',
  220. authuser='viewdata', authpass='viewdata')
  221. query = {"comeintime": {"$gte": start_date, "$lt": end_date},"bid_field":"0101"}
  222. count = MongoSentence.count(collection, query)
  223. print("医械通每周统计入库数量", count)
  224. return count
  225. #统计结果入库
  226. def save_to_mongo(self,title,count):
  227. collection=Data_save.save_con(host='172.20.45.129',port=27002,database='data_quality',collection='statistics')
  228. now = datetime.now()
  229. timestamp = int(now.timestamp())
  230. document = {
  231. title: {
  232. "timestamp": timestamp,
  233. "count": count
  234. }
  235. }
  236. Data_save.insert_one(collection,document)
  237. monitor=monitorTools()