Update app/main.py
Browse files- app/main.py +28 -18
app/main.py
CHANGED
|
@@ -51,35 +51,45 @@ STATS_FILE = "stats.json"
|
|
| 51 |
|
| 52 |
def load_stats():
|
| 53 |
try:
|
| 54 |
-
# 使用绝对路径确保文件位置正确
|
| 55 |
stats_path = os.path.abspath(STATS_FILE)
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with open(stats_path, "r") as f:
|
| 58 |
return json.load(f)
|
| 59 |
-
except
|
| 60 |
-
|
| 61 |
-
|
| 62 |
"total_calls": 0,
|
| 63 |
"today_calls": 0,
|
| 64 |
"total_tokens": 0,
|
| 65 |
"today_tokens": 0,
|
| 66 |
"last_reset": datetime.now().isoformat()
|
| 67 |
}
|
| 68 |
-
try:
|
| 69 |
-
# 确保目录存在
|
| 70 |
-
os.makedirs(os.path.dirname(stats_path), exist_ok=True)
|
| 71 |
-
# 写入初始数据
|
| 72 |
-
with open(stats_path, "w") as f:
|
| 73 |
-
json.dump(initial_stats, f, indent=2)
|
| 74 |
-
logger.info(f"已创建初始统计文件: {stats_path}")
|
| 75 |
-
return initial_stats
|
| 76 |
-
except Exception as e:
|
| 77 |
-
logger.error(f"创建统计文件失败: {str(e)}")
|
| 78 |
-
return initial_stats
|
| 79 |
|
| 80 |
def save_stats(stats):
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
def update_stats(calls=0, tokens=0):
|
| 85 |
stats = load_stats()
|
|
|
|
| 51 |
|
| 52 |
def load_stats():
|
| 53 |
try:
|
|
|
|
| 54 |
stats_path = os.path.abspath(STATS_FILE)
|
| 55 |
+
# 确保目录存在
|
| 56 |
+
os.makedirs(os.path.dirname(stats_path), exist_ok=True)
|
| 57 |
+
|
| 58 |
+
# 如果文件不存在则创建
|
| 59 |
+
if not os.path.exists(stats_path):
|
| 60 |
+
initial_stats = {
|
| 61 |
+
"total_calls": 0,
|
| 62 |
+
"today_calls": 0,
|
| 63 |
+
"total_tokens": 0,
|
| 64 |
+
"today_tokens": 0,
|
| 65 |
+
"last_reset": datetime.now().isoformat()
|
| 66 |
+
}
|
| 67 |
+
with open(stats_path, "w") as f:
|
| 68 |
+
json.dump(initial_stats, f, indent=2)
|
| 69 |
+
logger.info(f"已创建初始统计文件: {stats_path}")
|
| 70 |
+
return initial_stats
|
| 71 |
+
|
| 72 |
+
# 读取现有文件
|
| 73 |
with open(stats_path, "r") as f:
|
| 74 |
return json.load(f)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"加载统计文件失败: {str(e)}")
|
| 77 |
+
return {
|
| 78 |
"total_calls": 0,
|
| 79 |
"today_calls": 0,
|
| 80 |
"total_tokens": 0,
|
| 81 |
"today_tokens": 0,
|
| 82 |
"last_reset": datetime.now().isoformat()
|
| 83 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def save_stats(stats):
|
| 86 |
+
try:
|
| 87 |
+
stats_path = os.path.abspath(STATS_FILE)
|
| 88 |
+
os.makedirs(os.path.dirname(stats_path), exist_ok=True)
|
| 89 |
+
with open(stats_path, "w") as f:
|
| 90 |
+
json.dump(stats, f, indent=2)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"保存统计文件失败: {str(e)}")
|
| 93 |
|
| 94 |
def update_stats(calls=0, tokens=0):
|
| 95 |
stats = load_stats()
|