|
1 | | -import csv |
2 | 1 | import random |
3 | 2 | from datetime import datetime, timedelta, timezone |
4 | | -from typing import Optional |
5 | | - |
6 | 3 | import httpx |
7 | | -from fastapi import APIRouter, Depends, UploadFile, File |
| 4 | +from fastapi import APIRouter, Depends |
8 | 5 | from fastapi_cache.decorator import cache |
9 | 6 | from sqlalchemy import select |
10 | 7 |
|
@@ -78,36 +75,6 @@ async def get_system_info(_user: User = Depends(current_super_user)): |
78 | 75 | return result |
79 | 76 |
|
80 | 77 |
|
81 | | -FAKE_REQ_START_TIMESTAMP = 1672502400 # 2023-01-01 00:00:00 |
82 | | - |
83 | | - |
84 | | -def make_fake_requests_count(total=100, max=500): |
85 | | - result = {} |
86 | | - start_stage = FAKE_REQ_START_TIMESTAMP // config.stats.request_counts_interval |
87 | | - for i in range(total): |
88 | | - result[start_stage + i] = [random.randint(0, max), [1]] |
89 | | - return result |
90 | | - |
91 | | - |
92 | | -def make_fake_ask_records(total=100, days=2): |
93 | | - result = [] |
94 | | - model_names = list(api.enums.models.OpenaiWebChatModels) |
95 | | - for i in range(total): |
96 | | - ask_time = random.random() * 60 + 1 |
97 | | - total_time = ask_time + random.random() * 30 |
98 | | - result.append([ |
99 | | - [ |
100 | | - # random.randint(1, 10), # user_id |
101 | | - 1, |
102 | | - model_names[random.randint(0, len(model_names) - 1)], # model_name |
103 | | - ask_time, |
104 | | - total_time |
105 | | - ], |
106 | | - FAKE_REQ_START_TIMESTAMP + random.random() * 60 * 60 * 24 * days, # ask_time |
107 | | - ]) |
108 | | - return result |
109 | | - |
110 | | - |
111 | 78 | @router.get("/system/stats/request", tags=["system"], response_model=list[RequestLogAggregation]) |
112 | 79 | @cache(expire=30) |
113 | 80 | async def get_request_statistics( |
@@ -208,34 +175,6 @@ async def get_ask_statistics( |
208 | 175 | return result |
209 | 176 |
|
210 | 177 |
|
211 | | -def read_last_n_lines(file_path, n, exclude_key_words=None): |
212 | | - if exclude_key_words is None: |
213 | | - exclude_key_words = [] |
214 | | - try: |
215 | | - with open(file_path, "r") as f: |
216 | | - lines = f.readlines()[::-1] |
217 | | - except FileNotFoundError: |
218 | | - return [f"File not found: {file_path}"] |
219 | | - last_n_lines = [] |
220 | | - for line in lines: |
221 | | - if len(last_n_lines) >= n: |
222 | | - break |
223 | | - if any([line.find(key_word) != -1 for key_word in exclude_key_words]): |
224 | | - continue |
225 | | - last_n_lines.append(line) |
226 | | - return last_n_lines[::-1] |
227 | | - |
228 | | - |
229 | | -@router.post("/system/logs/server", tags=["system"]) |
230 | | -async def get_server_logs(_user: User = Depends(current_super_user), options: LogFilterOptions = LogFilterOptions()): |
231 | | - lines = read_last_n_lines( |
232 | | - g.server_log_filename, |
233 | | - options.max_lines, |
234 | | - options.exclude_keywords |
235 | | - ) |
236 | | - return lines |
237 | | - |
238 | | - |
239 | 178 | @router.get("/system/config", tags=["system"], response_model=ConfigModel) |
240 | 179 | async def get_config(_user: User = Depends(current_super_user)): |
241 | 180 | return config.model() |
@@ -276,54 +215,3 @@ async def sync_openai_web_conversations(_user: User = Depends(current_super_user |
276 | 215 | else: |
277 | 216 | raise exception |
278 | 217 | return None |
279 | | - |
280 | | - |
281 | | -# @router.post("/system/import-users", tags=["system"]) |
282 | | -async def import_users(file: UploadFile = File(...), _user: User = Depends(current_super_user)): |
283 | | - """ |
284 | | - 解析csv文件,导入用户 |
285 | | - csv字段: |
286 | | - """ |
287 | | - raise NotImplementedError() |
288 | | - |
289 | | - headers = ["id", "username", "nickname", "email", "active_time", "chat_status", "can_use_paid", "max_conv_count", |
290 | | - "available_ask_count", "is_superuser", "is_active", "is_verified", "hashed_password", "can_use_gpt4", |
291 | | - "available_gpt4_ask_count"] |
292 | | - content = await file.read() |
293 | | - content = content.decode("utf-8") |
294 | | - reader = csv.DictReader(content.splitlines()) |
295 | | - # check headers |
296 | | - for field in headers: |
297 | | - if field not in reader.fieldnames: |
298 | | - raise InvalidParamsException(f"Invalid csv file, missing field: {field}") |
299 | | - async with get_async_session_context() as session: |
300 | | - async with get_user_db_context(session) as user_db: |
301 | | - async with get_user_manager_context(user_db) as user_manager: |
302 | | - for row in reader: |
303 | | - user_create = UserCreate( |
304 | | - username=row["username"], |
305 | | - nickname=row["nickname"], |
306 | | - email=f"{row['username']}@example.com", |
307 | | - password="12345678", |
308 | | - remark=row["email"] |
309 | | - ) |
310 | | - await user_manager._check_username_unique(user_create.username) |
311 | | - user_dict = user_create.dict() |
312 | | - |
313 | | - del user_dict["password"] |
314 | | - user_dict["hashed_password"] = row["hashed_password"] |
315 | | - |
316 | | - user_setting = UserSettingSchema( |
317 | | - credits=0, |
318 | | - openai_web=OpenaiWebSourceSettingSchema.default(), |
319 | | - openai_api=OpenaiApiSourceSettingSchema.default(), |
320 | | - ) |
321 | | - user_setting.openai_web.available_models = ["gpt_3_5", "gpt_4"] |
322 | | - if not row["can_use_gpt4"]: |
323 | | - user_setting.openai_web.available_models = ["gpt_3_5"] |
324 | | - user_setting.openai_web.per_model_ask_count.gpt_3_5 = max( |
325 | | - int(row["available_ask_count"]) - int(row["available_gpt4_ask_count"]), 0) |
326 | | - user_setting.openai_web.per_model_ask_count.gpt_4 = int(row["available_gpt4_ask_count"]) |
327 | | - user_setting.openai_web.max_conv_count = int(row["max_conv_count"]) |
328 | | - |
329 | | - await user_manager.create_with_user_dict(user_dict, user_setting) |
0 commit comments