monitor_tools.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Data_save
  8. # 定义一周的时间范围,转换为Unix时间戳格式
  9. end_date = int(datetime.now().timestamp())
  10. start_date = int((datetime.now() - timedelta(days=7)).timestamp())
  11. class EScount:
  12. #标准库bidding-es 每周统计入库数量
  13. def es_bidding(self):
  14. """
  15. es链接
  16. """
  17. db_config = {
  18. # es
  19. 'es_host': '127.0.0.1',
  20. 'es_port': 19800,
  21. 'es_http_auth': ('jianyuGr','we3g8glKfe#'), # 重新申请
  22. 'timeout': 10000,
  23. 'index': "bidding"
  24. }
  25. query = {"query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}}]}}}
  26. # 传入查询语句query 以及配置信息
  27. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  28. counts=esutil.get_es_count(query,**db_config)
  29. count = counts['count']
  30. print("标准库es-bidding每周入库数据量:",count)
  31. return count
  32. # 高质量库bidding-ai-es 每周统计入库数量
  33. def es_bidding_ai(self):
  34. """
  35. es链接
  36. """
  37. db_config = {
  38. # es
  39. 'es_host': '127.0.0.1',
  40. 'es_port': 19800,
  41. 'es_http_auth': ('jianyuGr','we3g8glKfe#'), # 重新申请
  42. 'timeout': 10000,
  43. 'index': "bidding_ai"
  44. }
  45. query = {"query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}}]}}}
  46. # 传入查询语句query 以及配置信息
  47. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  48. counts=esutil.get_es_count(query,**db_config)
  49. count = counts['count']
  50. print("高质量库es-bidding每周入库数据量:",count)
  51. return count
  52. # 标准库bidding-es 碎片化数据每周统计入库数量
  53. def es_bidding_fragment(self):
  54. #正式环境
  55. db_config = {
  56. # es
  57. 'es_host': '127.0.0.1',
  58. 'es_port': 19800,
  59. 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  60. 'timeout': 10000,
  61. 'index': "bidding"
  62. }
  63. # #测试环境http://192.168.3.149:9201
  64. # db_config = {
  65. # # es
  66. # 'es_host': '192.168.3.149',
  67. # 'es_port': 9201,
  68. # # 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  69. # 'timeout': 10000,
  70. # 'index': "bidding"
  71. # }
  72. # 定义要监控的字段值
  73. tags = [
  74. "情报_法务",
  75. "情报_财务审计",
  76. "情报_招标代理",
  77. "情报_管理咨询",
  78. "情报_保险",
  79. "情报_工程设计咨询",
  80. "情报_安防",
  81. "情报_印务商机",
  82. "情报_环境采购",
  83. "情报_家具招投标"
  84. ]
  85. # 初始化字典,将所有标签的计数设置为0
  86. data = {}
  87. for tag in tags:
  88. query = {
  89. "query": {"bool": {"must": [{"range": {"comeintime": {"from": f"{start_date}", "to": f"{end_date}"}}},
  90. {"term": {"tag_topinformation": tag}}]}}}
  91. count = esutil.get_es_count(query, **db_config)
  92. print(f"标准库es-bidding{tag}每周入库数据量:", count['count'])
  93. data[tag]=count['count']
  94. # 检查数据字典以确保所有标签都被更新
  95. print("数据字典内容:", data) # 打印整个数据字典
  96. return data
  97. #拟在建es数据 每周统计入库数量
  98. def es_nzj(self):
  99. """
  100. es链接
  101. """
  102. db_config = {
  103. # es
  104. 'es_host': '127.0.0.1',
  105. 'es_port': 19800,
  106. 'es_http_auth': ('jianyuGr', 'we3g8glKfe#'), # 重新申请
  107. 'timeout': 10000,
  108. 'index': "proposed_v1"
  109. }
  110. query = {
  111. "query": {"match_all": {}}}
  112. # 传入查询语句query 以及配置信息
  113. # es=esutil.get_es(db_config["es_host"], db_config["es_http_auth"], db_config["es_port"],db_config["index"])
  114. counts = esutil.get_es_count(query, **db_config)
  115. count=counts['count']
  116. print("拟在建es入库数据总量:", count)
  117. return count
  118. def save_to_mongo(self,title,count):
  119. collection=Data_save.save_con(host='192.168.3.149',port=27180,database='data_quality',collection='statistics')
  120. now = datetime.now()
  121. timestamp = int(now.timestamp())
  122. document = {
  123. title: {
  124. "timestamp": timestamp,
  125. "count": count
  126. }
  127. }
  128. Data_save.insert_one(collection,document)
  129. escount=EScount()