Skip to content

Commit

Permalink
✨ update db api
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow403 committed Dec 21, 2024
1 parent bf16647 commit 27b3e40
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/config/utils/_func_init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def init_config():
},
"query_delay": 30,
"data": {
"path": "/data"
"path": "data"
},
"auth": {
"sessdata": ""
Expand Down
12 changes: 12 additions & 0 deletions app/database/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion app/database/utils/_func_uuid.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions app/pusher/router/_get_db2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
29 changes: 23 additions & 6 deletions app/pusher/router/_get_stats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sqlalchemy import func
from fastapi import APIRouter

from config import Router
Expand All @@ -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)

2 changes: 2 additions & 0 deletions app/pusher/router/main_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion app/pusher/utils/tasks/_task_livestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions app/worker/database/data_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
3 changes: 1 addition & 2 deletions app/worker/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
3 changes: 1 addition & 2 deletions app/worker/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 27b3e40

Please sign in to comment.