DataExport_forTesting.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. from pymongo import MongoClient
  2. from datetime import datetime, timedelta
  3. import pandas as pd
  4. import openpyxl
  5. # MongoDB连接配置
  6. host = '192.168.3.149'
  7. port = 27180
  8. dbname = 'data_quality'
  9. collection_name = 'statistics'
  10. # 创建MongoDB连接
  11. client = MongoClient(host, port)
  12. db = client[dbname]
  13. collection = db[collection_name]
  14. # 获取当前时间和一周前的时间
  15. end_time = datetime.now()
  16. start_time = end_time - timedelta(weeks=1)
  17. # 将datetime转换为Unix时间戳(整数类型,去掉小数部分)
  18. start_timestamp = int(start_time.timestamp())
  19. end_timestamp = int(end_time.timestamp())
  20. # 输出调试信息:检查开始时间和结束时间
  21. print("Start time:", start_time)
  22. print("End time:", end_time)
  23. print("Start timestamp:", start_timestamp)
  24. print("End timestamp:", end_timestamp)
  25. # ----------------- 第一个Sheet: 断流监控_mongo库 -------------------
  26. # 查询过去一周的数据(断流监控_mongo库)
  27. pipeline_mongo = [
  28. {
  29. "$match": {
  30. "$or": [
  31. {"bidding.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  32. {"bidding_ai.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  33. {"connections.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  34. {"nzj.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  35. {"bidding_fragment.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}}
  36. ]
  37. }
  38. },
  39. {
  40. "$limit": 5 # 限制查询返回的结果为前5条数据,便于调试
  41. }
  42. ]
  43. # 获取符合条件的数据
  44. data_mongo = list(collection.aggregate(pipeline_mongo))
  45. # 初始化MongoDB字段统计数据
  46. bidding_count = 0
  47. bidding_ai_count = 0
  48. connections_count = 0
  49. nzj_count = 0
  50. bidding_fragment_data = {
  51. "情报_法务": 0,
  52. "情报_财务审计": 0,
  53. "情报_招标代理": 0,
  54. "情报_管理咨询": 0,
  55. "情报_保险": 0,
  56. "情报_工程设计咨询": 0,
  57. "情报_安防": 0,
  58. "情报_印务商机": 0,
  59. "情报_环境采购": 0,
  60. "情报_家具招投标": 0
  61. }
  62. # 统计MongoDB数据
  63. for doc in data_mongo:
  64. if 'bidding' in doc:
  65. bidding_count += doc['bidding'].get('count', 0)
  66. if 'bidding_ai' in doc:
  67. bidding_ai_count += doc['bidding_ai'].get('count', 0)
  68. if 'connections' in doc:
  69. connections_count += doc['connections'].get('count', 0)
  70. if 'nzj' in doc:
  71. nzj_count += doc['nzj'].get('count', 0)
  72. if 'bidding_fragment' in doc:
  73. for key, value in doc['bidding_fragment'].get('count', {}).items():
  74. if key in bidding_fragment_data:
  75. bidding_fragment_data[key] += value
  76. # ----------------- 第二个Sheet: 断流监控—es -------------------
  77. # 查询过去一周的数据(断流监控—es)
  78. pipeline_es = [
  79. {
  80. "$match": {
  81. "$or": [
  82. {"es_bidding.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  83. {"es_bidding_ai.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  84. {"es_nzj.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}},
  85. {"es_bidding_fragment.timestamp": {"$gte": start_timestamp, "$lt": end_timestamp}}
  86. ]
  87. }
  88. },
  89. {
  90. "$limit": 5 # 限制查询返回的结果为前5条数据,便于调试
  91. }
  92. ]
  93. # 获取符合条件的数据
  94. data_es = list(collection.aggregate(pipeline_es))
  95. # 初始化ES字段统计数据
  96. es_bidding_count = 0
  97. es_bidding_ai_count = 0
  98. es_nzj_count = 0
  99. es_bidding_fragment_data = {
  100. "情报_法务": 0,
  101. "情报_财务审计": 0,
  102. "情报_招标代理": 0,
  103. "情报_管理咨询": 0,
  104. "情报_保险": 0,
  105. "情报_工程设计咨询": 0,
  106. "情报_安防": 0,
  107. "情报_印务商机": 0,
  108. "情报_环境采购": 0,
  109. "情报_家具招投标": 0
  110. }
  111. # 统计ES数据
  112. for doc in data_es:
  113. if 'es_bidding' in doc:
  114. es_bidding_count += doc['es_bidding'].get('count', 0)
  115. if 'es_bidding_ai' in doc:
  116. es_bidding_ai_count += doc['es_bidding_ai'].get('count', 0)
  117. if 'es_nzj' in doc:
  118. es_nzj_count += doc['es_nzj'].get('count', 0)
  119. if 'es_bidding_fragment' in doc:
  120. for key, value in doc['es_bidding_fragment'].get('count', {}).items():
  121. if key in es_bidding_fragment_data:
  122. es_bidding_fragment_data[key] += value
  123. # ----------------- 第三个Sheet: 数据时效监控 -------------------
  124. # 查询过去一周的数据(数据时效监控)
  125. pipeline_timeliness = [
  126. {
  127. "$match": {
  128. "data_timeliness.timestamp": {
  129. "$gte": start_timestamp, # 使用整数Unix时间戳
  130. "$lt": end_timestamp # 使用整数Unix时间戳
  131. }
  132. }
  133. },
  134. {
  135. "$limit": 5 # 限制查询返回的结果为前5条数据,便于调试
  136. }
  137. ]
  138. # 获取符合条件的数据
  139. data_timeliness = list(collection.aggregate(pipeline_timeliness))
  140. # 初始化字段统计数据
  141. timeliness_data = {
  142. "[0,5)分钟": 0,
  143. "[5,15)分钟": 0,
  144. "[15,30)分钟": 0,
  145. "[30,60)分钟": 0,
  146. "[1,3)小时": 0,
  147. "[3,7)小时": 0,
  148. "[7,15)小时": 0,
  149. "[15,24)小时": 0,
  150. "[1,2)天": 0,
  151. "[2,3)天": 0,
  152. "3天+": 0
  153. }
  154. # 统计数据
  155. for doc in data_timeliness:
  156. if 'data_timeliness' in doc:
  157. count_data = doc['data_timeliness'].get('count', {})
  158. timeliness_data["[0,5)分钟"] += float(count_data.get("a1", "0%").replace('%', ''))
  159. timeliness_data["[5,15)分钟"] += float(count_data.get("a2", "0%").replace('%', ''))
  160. timeliness_data["[15,30)分钟"] += float(count_data.get("a3", "0%").replace('%', ''))
  161. timeliness_data["[30,60)分钟"] += float(count_data.get("a4", "0%").replace('%', ''))
  162. timeliness_data["[1,3)小时"] += float(count_data.get("a5", "0%").replace('%', ''))
  163. timeliness_data["[3,7)小时"] += float(count_data.get("a6", "0%").replace('%', ''))
  164. timeliness_data["[7,15)小时"] += float(count_data.get("a7", "0%").replace('%', ''))
  165. timeliness_data["[15,24)小时"] += float(count_data.get("a8", "0%").replace('%', ''))
  166. timeliness_data["[1,2)天"] += float(count_data.get("a9", "0%").replace('%', ''))
  167. timeliness_data["[2,3)天"] += float(count_data.get("a10", "0%").replace('%', ''))
  168. timeliness_data["3天+"] += float(count_data.get("a11", "0%").replace('%', ''))
  169. # 获取当前时间的一周时间范围字符串
  170. date_range = f"{start_time.strftime('%Y/%m/%d')}-{end_time.strftime('%Y/%m/%d')}"
  171. # 构建Excel数据
  172. columns = ['日期', '标准库每周入库数据量', '高质量库每周入库数据量', '人脉管理数据', '拟在建数据量(全国)'] + list(bidding_fragment_data.keys())
  173. data_row_mongo = [date_range, bidding_count, bidding_ai_count, connections_count, nzj_count] + list(bidding_fragment_data.values())
  174. columns_es = ['日期', '标准库每周入库数据量', '高质量库每周数据入库量', '拟在建数据量(全国)'] + list(es_bidding_fragment_data.keys())
  175. data_row_es = [date_range, es_bidding_count, es_bidding_ai_count, es_nzj_count] + list(es_bidding_fragment_data.values())
  176. columns_timeliness = ['日期'] + list(timeliness_data.keys())
  177. data_row_timeliness = [date_range] + list(timeliness_data.values())
  178. # 创建DataFrame并写入Excel
  179. excel_file = 'mongo_data_statistics_combined.xlsx'
  180. with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:
  181. # 写入第一个sheet(断流监控_mongo库)
  182. df_mongo = pd.DataFrame([data_row_mongo], columns=columns)
  183. df_mongo.to_excel(writer, sheet_name='断流监控_mongo库(每周)', index=False)
  184. # 写入第二个sheet(断流监控—es)
  185. df_es = pd.DataFrame([data_row_es], columns=columns_es)
  186. df_es.to_excel(writer, sheet_name='断流监控-es(每周)', index=False)
  187. # 写入第三个sheet(数据时效监控)
  188. df_timeliness = pd.DataFrame([data_row_timeliness], columns=columns_timeliness)
  189. df_timeliness.to_excel(writer, sheet_name='数据时效监控(每周平均值)', index=False)
  190. print(f"统计结果已写入Excel文件: {excel_file}")