|
|
@@ -29,8 +29,9 @@ class MongoDBTools:
|
|
|
file_name_pre = 'time_'
|
|
|
if records is not None and len(records) > 0:
|
|
|
tools.export_to_excel(records, diff_records, query_entity.goods_code, file_name_pre)
|
|
|
- print("time diff count: " + str(int(len(diff_records) /2)))
|
|
|
-
|
|
|
+ print("time diff count: " + str(int(len(diff_records) /2)))
|
|
|
+ else:
|
|
|
+ print("not records")
|
|
|
client.close()
|
|
|
except Exception as e:
|
|
|
print(f"MongoDB 查询失败:{e}")
|
|
|
@@ -80,11 +81,13 @@ class MongoDBTools:
|
|
|
query = {}
|
|
|
|
|
|
if query_entity.start_time is not None and len(query_entity.start_time) > 0 and query_entity.end_time is not None and len(query_entity.end_time) > 0:
|
|
|
+ start_time_tick = int(datetime.strptime(query_entity.start_time, "%Y-%m-%d %H:%M:%S").timestamp())
|
|
|
+ end_time_tick = int(datetime.strptime(query_entity.end_time, "%Y-%m-%d %H:%M:%S").timestamp())
|
|
|
query = {
|
|
|
"GC": query_entity.goods_code,
|
|
|
- "SAT": {
|
|
|
- "$gte": query_entity.start_time, # Greater than or equal to start_time
|
|
|
- "$lte": query_entity.end_time # Less than or equal to end_time
|
|
|
+ "AT": {
|
|
|
+ "$gte": start_time_tick, # Greater than or equal to start_time
|
|
|
+ "$lte": end_time_tick # Less than or equal to end_time
|
|
|
}
|
|
|
}
|
|
|
else:
|
|
|
@@ -92,31 +95,38 @@ class MongoDBTools:
|
|
|
"GC": query_entity.goods_code
|
|
|
}
|
|
|
|
|
|
+ # 输出当前时间
|
|
|
+ print("查询MongoDB开始:", datetime.now())
|
|
|
latest_records = None
|
|
|
# 查询记录 record_num = 0 或 none ,取所有记录
|
|
|
if query_entity.record_num is None or query_entity.record_num == 0:
|
|
|
latest_records = list(
|
|
|
- collection.find(query).sort("_id", -1)
|
|
|
+ # collection.find(query).sort("_id", -1)
|
|
|
+ collection.find(query)
|
|
|
)
|
|
|
# record_num > 0, 取最新的N条
|
|
|
if query_entity.record_num is not None and query_entity.record_num > 0:
|
|
|
latest_records = list(
|
|
|
- collection.find(query).sort("_id", -1)
|
|
|
+ # collection.find(query).sort("_id", -1)
|
|
|
+ collection.find(query)
|
|
|
.limit(query_entity.record_num) # 取最新 N 条
|
|
|
)
|
|
|
-
|
|
|
+ print("返回总记录数:", len(latest_records))
|
|
|
+ print("查询MongoDB结束:", datetime.now())
|
|
|
return latest_records
|
|
|
except Exception as e:
|
|
|
print(f"数据库操作失败:{e}")
|
|
|
|
|
|
def get_quote_data_by_type(self, client, query_entity: QuoteQueryEntity):
|
|
|
try:
|
|
|
+
|
|
|
latest_records = self.get_quote_data(client, query_entity)
|
|
|
|
|
|
if latest_records is None or len(latest_records) == 0:
|
|
|
print("no records!")
|
|
|
return None, None
|
|
|
|
|
|
+ print("处理数据开始:", datetime.now())
|
|
|
# 初始化变量
|
|
|
previous = None
|
|
|
previous_bid = None
|
|
|
@@ -131,6 +141,7 @@ class MongoDBTools:
|
|
|
current_bid = record.get("Bid")
|
|
|
current_sat = record.get("SAT")
|
|
|
record["Color"] = '0'
|
|
|
+ record["Diff"] = ''
|
|
|
if query_entity.query_type == 1:
|
|
|
# 1: 按价差(买价)
|
|
|
if current_bid is not None and previous_bid is not None:
|
|
|
@@ -138,6 +149,7 @@ class MongoDBTools:
|
|
|
if abs(difference) > query_entity.diff_value:
|
|
|
previous["Color"] = "1"
|
|
|
record["Color"] = "1"
|
|
|
+ record["Diff"] = difference
|
|
|
diff_records.append(previous)
|
|
|
diff_records.append(record)
|
|
|
elif query_entity.query_type == 2:
|
|
|
@@ -150,6 +162,7 @@ class MongoDBTools:
|
|
|
if abs(difference) > query_entity.diff_value:
|
|
|
previous["Color"] = "1"
|
|
|
record["Color"] = "1"
|
|
|
+ record["Diff"] = str(difference)
|
|
|
diff_records.append(previous)
|
|
|
diff_records.append(record)
|
|
|
except Exception as e:
|
|
|
@@ -159,6 +172,7 @@ class MongoDBTools:
|
|
|
previous_bid = current_bid
|
|
|
previous_sat = current_sat
|
|
|
|
|
|
+ print("处理数据结束:", datetime.now())
|
|
|
return latest_records, diff_records
|
|
|
except Exception as e:
|
|
|
print(f"数据库操作失败:{e}")
|
|
|
@@ -167,19 +181,21 @@ class MongoDBTools:
|
|
|
if records is None:
|
|
|
return
|
|
|
|
|
|
+ print("生成Excel开始:", datetime.now())
|
|
|
+
|
|
|
# 创建一个 Excel 文件
|
|
|
wb = openpyxl.Workbook()
|
|
|
ws = wb.active
|
|
|
ws.title = "Full Data"
|
|
|
|
|
|
# 更新样式
|
|
|
- ws = self.update_sheet_style(ws, records)
|
|
|
+ ws = self.update_sheet_style(ws, records, 0)
|
|
|
|
|
|
# 添加sheet2
|
|
|
if diff_records is not None and len(diff_records) > 0:
|
|
|
# 创建 sheet2 并填充数据
|
|
|
ws_filter = wb.create_sheet('Filter Data') # 创建新工作表 'Sheet2'
|
|
|
- ws_filter = self.update_sheet_style(ws_filter, diff_records)
|
|
|
+ ws_filter = self.update_sheet_style(ws_filter, diff_records, 1)
|
|
|
|
|
|
# 设置第二个工作表为默认激活工作表
|
|
|
wb.active = 1 # 激活 'Sheet2',index 从 0 开始,1 表示第二个工作表
|
|
|
@@ -193,12 +209,13 @@ class MongoDBTools:
|
|
|
file_name = os.path.join(dir_path, file_name_pre + goods_code + "_" + datetime.now().strftime("%Y%m%d%H%M%S") + ".xlsx")
|
|
|
wb.save(file_name)
|
|
|
|
|
|
+ print("生成Excel结束:", datetime.now())
|
|
|
print("quote date export to:" + file_name)
|
|
|
|
|
|
# 打开excel文件
|
|
|
# open_excel(file_name)
|
|
|
|
|
|
- def update_sheet_style(self, ws, records):
|
|
|
+ def update_sheet_style(self, ws, records, diff_flag = 0):
|
|
|
if ws is None:
|
|
|
return None
|
|
|
|
|
|
@@ -206,7 +223,7 @@ class MongoDBTools:
|
|
|
yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
|
|
|
|
|
|
# 写入表头
|
|
|
- ws.append(["GC", "SAT", "PE", "Bid", "Ask", "Color"])
|
|
|
+ ws.append(["GC", "SAT", "PE", "Bid", "Ask", "Color", "Diff", "_id",])
|
|
|
|
|
|
# 设置列宽
|
|
|
ws.column_dimensions['A'].width = 20 # GC 列的宽度
|
|
|
@@ -214,23 +231,34 @@ class MongoDBTools:
|
|
|
ws.column_dimensions['C'].width = 20 # PE 列的宽度
|
|
|
ws.column_dimensions['D'].width = 20 # Bid 列的宽度
|
|
|
ws.column_dimensions['E'].width = 20 # Ask 列的宽度
|
|
|
- ws.column_dimensions['F'].width = 20 # Color 列的宽度
|
|
|
+ ws.column_dimensions['F'].width = 10 # Color 列的宽度
|
|
|
+ ws.column_dimensions['G'].width = 10 # Diff 列的宽度
|
|
|
+ ws.column_dimensions['H'].width = 30 # _id 列的宽度
|
|
|
|
|
|
# 启用筛选功能
|
|
|
ws.auto_filter.ref = ws.dimensions # 激活自动筛选
|
|
|
|
|
|
# 写入数据并根据 color 属性设置行颜色
|
|
|
for row in records:
|
|
|
- row_values = [row["GC"], row["SAT"], row["PE"], row["Bid"], row["Ask"], row["Color"]]
|
|
|
+ row_values = [row["GC"], row["SAT"], row["PE"], row["Bid"], row["Ask"], row["Color"], row["Diff"], str(row["_id"])]
|
|
|
ws.append(row_values)
|
|
|
|
|
|
- # 如果 color 是 "yellow",则标记该行的颜色为黄色
|
|
|
- if row["Color"] == "1":
|
|
|
- # 获取当前行的行号
|
|
|
- row_num = ws.max_row
|
|
|
- # 为当前行的所有单元格设置背景颜色
|
|
|
- for cell in ws[row_num]:
|
|
|
- cell.fill = yellow_fill
|
|
|
+ if diff_flag == 0:
|
|
|
+ # Full Data 标签, color 是 "1",则标记该行的颜色为黄色
|
|
|
+ if row["Color"] == "1":
|
|
|
+ # 获取当前行的行号
|
|
|
+ row_num = ws.max_row
|
|
|
+ # 为当前行的所有单元格设置背景颜色
|
|
|
+ for cell in ws[row_num]:
|
|
|
+ cell.fill = yellow_fill
|
|
|
+ else:
|
|
|
+ # Filter Data 有Diff值的行颜色为黄色
|
|
|
+ if row["Diff"] is not None and row["Diff"] != '':
|
|
|
+ # 获取当前行的行号
|
|
|
+ row_num = ws.max_row
|
|
|
+ # 为当前行的所有单元格设置背景颜色
|
|
|
+ for cell in ws[row_num]:
|
|
|
+ cell.fill = yellow_fill
|
|
|
|
|
|
return ws
|
|
|
|