perf: 优化日志增量获取逻辑

This commit is contained in:
RockChinQ 2024-10-13 22:33:51 +08:00
parent 7c3557e943
commit 9703fc0366
No known key found for this signature in database
GPG Key ID: 8AC0BEFE1743A015
4 changed files with 59 additions and 33 deletions

View File

@ -1,21 +0,0 @@
from __future__ import annotations
import traceback
import quart
from .....core import app
from .. import group
@group.group_class('log', '/api/v1/log')
class LogRouterGroup(group.RouterGroup):
async def initialize(self) -> None:
@self.route('', methods=['GET'])
async def _() -> str:
return self.success(
data={
"logs": self.ap.log_cache.get_all_logs()
}
)

View File

@ -0,0 +1,32 @@
from __future__ import annotations
import traceback
import quart
from .....core import app
from .. import group
@group.group_class('logs', '/api/v1/logs')
class LogsRouterGroup(group.RouterGroup):
async def initialize(self) -> None:
@self.route('', methods=['GET'])
async def _() -> str:
start_page_number = int(quart.request.args.get('start_page_number', 0))
start_offset = int(quart.request.args.get('start_offset', 0))
logs_str, end_page_number, end_offset = self.ap.log_cache.get_log_by_pointer(
start_page_number=start_page_number,
start_offset=start_offset
)
return self.success(
data={
"logs": logs_str,
"end_page_number": end_page_number,
"end_offset": end_offset
}
)

View File

@ -5,7 +5,7 @@ import asyncio
import quart
from ....core import app
from .groups import log
from .groups import logs
from . import group

View File

@ -7,12 +7,16 @@ LOG_PAGE_SIZE = 20
MAX_CACHED_PAGES = 10
class LogPage(pydantic.BaseModel):
class LogPage():
"""日志页"""
number: int
"""页码"""
cached_count: int = 0
logs: list[str]
logs: str = ""
def __init__(self, number: int):
self.number = number
self.logs = []
def add_log(self, log: str) -> bool:
"""添加日志
@ -20,9 +24,8 @@ class LogPage(pydantic.BaseModel):
Returns:
bool: 是否已满
"""
self.logs += log
self.cached_count += 1
return self.cached_count >= LOG_PAGE_SIZE
self.logs.append(log)
return len(self.logs) >= LOG_PAGE_SIZE
class LogCache:
@ -34,16 +37,28 @@ class LogCache:
def __init__(self):
self.log_pages = []
self.log_pages.append(LogPage())
self.log_pages.append(LogPage(number=0))
def add_log(self, log: str):
"""添加日志"""
if self.log_pages[-1].add_log(log):
self.log_pages.append(LogPage())
self.log_pages.append(LogPage(number=self.log_pages[-1].number + 1))
if len(self.log_pages) > MAX_CACHED_PAGES:
self.log_pages.pop(0)
def get_all_logs(self) -> str:
"""获取所有日志"""
return "".join([page.logs for page in self.log_pages])
def get_log_by_pointer(
self,
start_page_number: int,
start_offset: int,
) -> tuple[str, int, int]:
"""获取指定页码和偏移量的日志"""
final_logs_str = ""
for page in self.log_pages:
if page.number == start_page_number:
final_logs_str += "\n".join(page.logs[start_offset:])
elif page.number > start_page_number:
final_logs_str += "\n".join(page.logs)
return final_logs_str, page.number, len(page.logs)