From 27b3e40dd90b8e3aa47c3a679830110068dfdea6 Mon Sep 17 00:00:00 2001 From: Shadow403 Date: Sat, 21 Dec 2024 14:45:13 +0800 Subject: [PATCH] :sparkles: update db api --- app/config/utils/_func_init_config.py | 2 +- app/database/model.py | 12 +++++++++ app/database/utils/_func_uuid.py | 3 ++- app/pusher/router/_get_db2json.py | 10 ++++---- app/pusher/router/_get_stats.py | 29 +++++++++++++++++----- app/pusher/router/main_router.py | 2 ++ app/pusher/utils/tasks/_task_livestatus.py | 2 +- app/worker/database/data_handle.py | 23 ++++++++++++----- app/worker/handle.py | 3 +-- app/worker/init.py | 3 +-- 10 files changed, 65 insertions(+), 24 deletions(-) diff --git a/app/config/utils/_func_init_config.py b/app/config/utils/_func_init_config.py index dcb44c8..0304cd4 100644 --- a/app/config/utils/_func_init_config.py +++ b/app/config/utils/_func_init_config.py @@ -13,7 +13,7 @@ def init_config(): }, "query_delay": 30, "data": { - "path": "/data" + "path": "data" }, "auth": { "sessdata": "" diff --git a/app/database/model.py b/app/database/model.py index 58abe5b..f5640fc 100644 --- a/app/database/model.py +++ b/app/database/model.py @@ -104,6 +104,12 @@ class LIVE_DATA(config_base): if_full = Column(Boolean, default=True) is_finished = Column(Boolean, default=False) + all_gift = Column(Integer, default=0) # 1002 + all_enter = Column(Integer, default=0) # 1000 + all_guard = Column(Integer, default=0) # 1003 + all_danmaku = Column(Integer, default=0) # 1001 + all_superchat = Column(Integer, default=0) # 1004 + start_timestamp = Column(Integer, default=func_timestamp()) end_timestamp = Column(Integer, default=0) @@ -128,6 +134,12 @@ def live_data_json(self): "if_full": self.if_full, "is_finished": self.is_finished, + "all_gift": self.all_gift, + "all_enter": self.all_enter, + "all_guard": self.all_guard, + "all_danmaku": self.all_danmaku, + "all_superchat": self.all_superchat, + "start_timestamp": self.start_timestamp, "end_timestamp": self.end_timestamp } diff --git a/app/database/utils/_func_uuid.py b/app/database/utils/_func_uuid.py index 3c30704..248895d 100644 --- a/app/database/utils/_func_uuid.py +++ b/app/database/utils/_func_uuid.py @@ -1,5 +1,6 @@ import uuid -def func_generate_uuid(str_value): +def func_generate_uuid(uid, live_timestamp): + str_value = f"{uid}000{live_timestamp}" UUID = str(uuid.uuid5(uuid.NAMESPACE_DNS, str_value)) return UUID diff --git a/app/pusher/router/_get_db2json.py b/app/pusher/router/_get_db2json.py index 54c6a38..37d121e 100644 --- a/app/pusher/router/_get_db2json.py +++ b/app/pusher/router/_get_db2json.py @@ -66,11 +66,11 @@ async def get_live_logs( full_data = { "live_config": { - "now": page, - "page": total_pages, - "base": live_config_data.live_data_json(), - "statistics": live_statistics_json - }, + "page_current": page, + "page_lasted": total_pages, + "base": live_config_data.live_data_json(), + "statistics": live_statistics_json + }, "live_logs": live_logs_json_chunks } diff --git a/app/pusher/router/_get_stats.py b/app/pusher/router/_get_stats.py index ce7a001..a3837ef 100644 --- a/app/pusher/router/_get_stats.py +++ b/app/pusher/router/_get_stats.py @@ -1,3 +1,4 @@ +from sqlalchemy import func from fastapi import APIRouter from config import Router @@ -7,16 +8,32 @@ from database.model import * from database.connector import get_db_config_session -from .model._model_edit_add import put_add_uid -from .model._model_del_uid import put_delete_uid - router = APIRouter(prefix=Router.stats_perfix, tags=Router.stats_tags) @router.get("/total") async def get_total_(): with get_db_config_session() as config_db_session: - total = config_db_session.query(LIVE_DATA).all() - total_list = [x.live_data_json() for x in total] + count = len(config_db_session.query(LIVE_DATA).filter(LIVE_DATA.end_timestamp != 0).all()) + + gf, et, gd, dm, sc = config_db_session.query( + func.sum(LIVE_DATA.all_gift).label('total_gift'), + func.sum(LIVE_DATA.all_enter).label('total_enter'), + func.sum(LIVE_DATA.all_guard).label('total_guard'), + func.sum(LIVE_DATA.all_danmaku).label('total_danmaku'), + func.sum(LIVE_DATA.all_superchat).label('total_superchat') + ).first() + + full_data = { + "count": count, + "total": { + "gift": gf, + "enter": et, + "guard": gd, + "danmaku": dm, + "superchat": sc + } + } - return ret_200(total_list) + return ret_200(full_data) + \ No newline at end of file diff --git a/app/pusher/router/main_router.py b/app/pusher/router/main_router.py index c12d085..7c28a82 100644 --- a/app/pusher/router/main_router.py +++ b/app/pusher/router/main_router.py @@ -3,10 +3,12 @@ from ._get_info import router as get_info_router from ._put_edit import router as put_edit_router +from ._get_stats import router as get_stats_router from ._get_db2json import router as get_db2json_router MRouter = APIRouter(prefix=config.perfix) MRouter.include_router(get_info_router) MRouter.include_router(put_edit_router) +MRouter.include_router(get_stats_router) MRouter.include_router(get_db2json_router) diff --git a/app/pusher/utils/tasks/_task_livestatus.py b/app/pusher/utils/tasks/_task_livestatus.py index 8501472..b140d0e 100644 --- a/app/pusher/utils/tasks/_task_livestatus.py +++ b/app/pusher/utils/tasks/_task_livestatus.py @@ -19,7 +19,7 @@ def live_status_inspectors(): uid = x["uid"] rid = x["room_id"] live_time = x["live_time"] - uuid = func_generate_uuid(f"{uid}000{live_time}") + uuid = func_generate_uuid(uid, live_time) new_live = LIVE_DATA( uid = uid, uuid = uuid, diff --git a/app/worker/database/data_handle.py b/app/worker/database/data_handle.py index 998fef9..80879e7 100644 --- a/app/worker/database/data_handle.py +++ b/app/worker/database/data_handle.py @@ -8,9 +8,12 @@ class data_commit_handle: def __init__(self, uid, uuid): + self.uid = uid self.uuid = uuid with get_db_worker_session(f"{uid}/{uuid}") as worker_db_session: self.db = worker_db_session + with get_db_config_session() as config_db_session: + self.cdb = config_db_session def data_commit(self, x): d = self.db.query(LIVE_STATISTICS).filter(LIVE_STATISTICS.uuid == self.uuid).first() @@ -81,9 +84,17 @@ def data_superchat(self, x): ) self.db.add(row) - def data_finish(self): - with get_db_config_session() as db: - x = db.query(LIVE_DATA).filter(LIVE_DATA.uuid == self.uuid).first() - x.is_finished = True - x.end_timestamp = func_timestamp() - db.commit() + def data_finish(self, x): + d = self.cdb.query(LIVE_DATA).filter(LIVE_DATA.uuid == self.uuid).first() + d.all_gift=x.all_gift + d.all_enter=x.all_enter + d.all_guard=x.all_guard + d.all_danmaku=x.all_danmaku + d.all_superchat=x.all_superchat + d.is_finished = True + d.end_timestamp = func_timestamp() + + c = self.cdb.query(UIDS).filter(UIDS.uid == self.uid).first() + c.is_live = False + + self.cdb.commit() diff --git a/app/worker/handle.py b/app/worker/handle.py index d76667f..f063bdc 100644 --- a/app/worker/handle.py +++ b/app/worker/handle.py @@ -4,7 +4,6 @@ from blivedm.models import web as web_models from .database.data_handle import data_commit_handle -from database.connector import get_db_worker_session class InitHandler(blivedm.BaseHandler): @@ -49,6 +48,6 @@ def _on_super_chat(M, client: blivedm.BLiveClient, message: web_models.SuperChat def _on_preparing(M, client: blivedm.BLiveClient, message: web_models.PreparingMessage): logger.warning(f"[END] [{M.room_id}] 房间下播,停止接收消息") M.db_handle.data_commit(M.revert_data) - M.db_handle.data_finish() + M.db_handle.data_finish(M.revert_data) M.work_client.stop() sys.exit() diff --git a/app/worker/init.py b/app/worker/init.py index f41b715..32d1714 100644 --- a/app/worker/init.py +++ b/app/worker/init.py @@ -8,8 +8,7 @@ def worker_db_initializer(live_uid, live_timestamp): - uuid = f"{live_uid}000{live_timestamp}" - uuid_str = func_generate_uuid(uuid) + uuid_str = func_generate_uuid(live_uid, live_timestamp) if not os.path.exists(f"{config.data_path}/{live_uid}"): os.makedirs(f"{config.data_path}/{live_uid}")