monitor_tools.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #标准库bidding-mongo 每周统计入库数量
  122. def bidding(self):
  123. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw',collection='bidding',authuser='viewdata',authpass='viewdata')
  124. query = { "comeintime": {"$gte": start_date, "$lt": end_date}}
  125. count=MongoSentence.count(collection,query)
  126. print("标准库bidding-mongo 每周统计入库数量",count)
  127. return count
  128. #高质量库bidding-mongo 每周统计入库数量
  129. def bidding_ai(self):
  130. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw_ai',collection='bidding',authuser='viewdata',authpass='viewdata')
  131. query = { "comeintime": {"$gte": start_date, "$lt": end_date}}
  132. count=MongoSentence.count(collection,query)
  133. print("高质量库bidding-mongo 每周统计入库数量",count)
  134. return count
  135. #标准库bidding-mongo碎片化数据 每周统计入库数量
  136. def bidding_fragment(self):
  137. collection = MongoUtil.get_coon(host='127.0.0.1:27088', database='qfw',collection='bidding',authuser='viewdata',authpass='viewdata')
  138. # 定义要监控的字段值
  139. tags = [
  140. "情报_法务",
  141. "情报_财务审计",
  142. "情报_招标代理",
  143. "情报_管理咨询",
  144. "情报_保险",
  145. "情报_工程设计咨询",
  146. "情报_安防",
  147. "情报_印务商机",
  148. "情报_环境采购",
  149. "情报_家具招投标"
  150. ]
  151. data={}
  152. for tag in tags:
  153. query = {"comeintime": {"$gte": start_date, "$lt": end_date},"tag_topinformation":tag}
  154. count=MongoSentence.count(collection,query)
  155. print(f"标准库bidding-mongo{tag}每周统计入库数量",count)
  156. data[tag]=count
  157. return data
  158. #拟在建baseinfo-mysql 每周统计入库数量
  159. def nzj(self):
  160. # MySQL 数据库连接配置
  161. # mysql_db_config = {
  162. # 'host': '192.168.3.149',
  163. # 'port': 4000,
  164. # 'user': 'datagroup',
  165. # 'password': 'Dgrpdb#2024@36',
  166. # 'database': 'jianyu_subjectdb',
  167. # 'charset': 'utf8mb4'
  168. # }
  169. now = datetime.now()
  170. end_date = now.strftime("%Y-%m-%d %H:%M:%S")
  171. start_date = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S")
  172. # SQL 查询
  173. mysql_query = "SELECT COUNT(*) FROM jianyu_subjectdb.dwd_f_nzj_baseinfo WHERE createtime >= %s AND createtime <= %s"
  174. params = (start_date, end_date)
  175. conn=MysqlUtil.connect_to_mysql(host='192.168.3.149',port='4000',user='datagroup',password='Dgrpdb#2024@36',database='jianyu_subjectdb')
  176. count=MysqlUtil.execute_sql(conn,mysql_query,params)
  177. print("拟在建baseinfo-mysql每周统计入库数量", count)
  178. return count
  179. #人脉数据,每周统计入库数量
  180. def connections(self):
  181. try:
  182. query = f"SELECT COUNT(*) FROM information.transaction_info_all WHERE create_time >={start_date} AND create_time <={end_date}"
  183. # conn=ClickhouseUtil.connect_to_clickhouse(host='192.168.3.207',port='19000',user='jytop',password='pwdTopJy123',database='information')
  184. conn=ClickhouseUtil.connect_to_clickhouse(host='127.0.0.1',port='9000',user='biservice',password='Bi_top95215#',database='information')
  185. count=ClickhouseUtil.execute_sql(conn,query)
  186. result=count[0][0]
  187. print("人脉数据每周统计入库数量", result)
  188. return result
  189. except Exception as e:
  190. logger.error("An error occurred: %s", e)
  191. raise
  192. #统计结果入库
  193. def save_to_mongo(self,title,count):
  194. collection=Data_save.save_con(host='192.168.3.149',port=27180,database='data_quality',collection='statistics')
  195. now = datetime.now()
  196. timestamp = int(now.timestamp())
  197. document = {
  198. title: {
  199. "timestamp": timestamp,
  200. "count": count
  201. }
  202. }
  203. Data_save.insert_one(collection,document)
  204. monitor=monitorTools()