From 1507f549341e03de84e77da8fe7ae31631f4bf99 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 12 Aug 2024 02:50:31 -0400 Subject: [PATCH] fix: metrics perf: aiocqhttp image url --- model/platform/__init__.py | 9 ++++++--- model/platform/qq_aiocqhttp.py | 7 +++++-- model/platform/qq_nakuru.py | 1 + model/platform/qq_official.py | 2 +- util/metrics.py | 3 +++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/model/platform/__init__.py b/model/platform/__init__.py index 1acac0f..37531ce 100644 --- a/model/platform/__init__.py +++ b/model/platform/__init__.py @@ -7,8 +7,9 @@ class Platform(): - def __init__(self) -> None: - pass + def __init__(self, platform_name: str, context) -> None: + self.PLATFORM_NAME = platform_name + self.context = context @abc.abstractmethod async def handle_msg(self, message: AstrBotMessage): @@ -79,4 +80,6 @@ async def convert_to_t2i_chain(self, message_result: list) -> list: else: rendered_images.append(Image.fromFileSystem(p)) return rendered_images - \ No newline at end of file + + async def record_metrics(self): + self.context.metrics_uploader.increment_platform_stat(self.PLATFORM_NAME) \ No newline at end of file diff --git a/model/platform/qq_aiocqhttp.py b/model/platform/qq_aiocqhttp.py index 31e0253..c70e6be 100644 --- a/model/platform/qq_aiocqhttp.py +++ b/model/platform/qq_aiocqhttp.py @@ -18,6 +18,7 @@ class AIOCQHTTP(Platform): def __init__(self, context: Context, message_handler: MessageHandler) -> None: + super().__init__("aiocqhttp", context) self.message_handler = message_handler self.waiting = {} self.context = context @@ -67,7 +68,9 @@ def convert_message(self, event: Event) -> AstrBotMessage: message_str += m['data']['text'].strip() abm.message.append(a) if t == 'image': - a = Image(file=m['data']['file']) + file = m['data']['file'] if 'file' in m['data'] else None + url = m['data']['url'] if 'url' in m['data'] else None + a = Image(file=file, url=url) abm.message.append(a) abm.timestamp = int(time.time()) abm.message_str = message_str @@ -195,9 +198,9 @@ async def reply_msg(self, await self._reply(message, res) async def _reply(self, message: Union[AstrBotMessage, Dict], message_chain: List[BaseMessageComponent]): + await self.record_metrics() if isinstance(message_chain, str): message_chain = [Plain(text=message_chain), ] - ret = [] image_idx = [] for idx, segment in enumerate(message_chain): diff --git a/model/platform/qq_nakuru.py b/model/platform/qq_nakuru.py index bf1b241..8b3856c 100644 --- a/model/platform/qq_nakuru.py +++ b/model/platform/qq_nakuru.py @@ -30,6 +30,7 @@ def __init__(self, type, group_id): class QQGOCQ(Platform): def __init__(self, context: Context, message_handler: MessageHandler) -> None: + super().__init__("nakuru", context) self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) diff --git a/model/platform/qq_official.py b/model/platform/qq_official.py index 6911cb3..147fd5b 100644 --- a/model/platform/qq_official.py +++ b/model/platform/qq_official.py @@ -53,7 +53,7 @@ async def on_c2c_message_create(self, message: botpy.message.C2CMessage): class QQOfficial(Platform): def __init__(self, context: Context, message_handler: MessageHandler, test_mode = False) -> None: - super().__init__() + super().__init__("qqofficial", context) self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) diff --git a/util/metrics.py b/util/metrics.py index 172dd7a..59ee066 100644 --- a/util/metrics.py +++ b/util/metrics.py @@ -65,6 +65,9 @@ async def upload_metrics(self): except BaseException as e: pass await asyncio.sleep(30*60) + + def increment_platform_stat(self, platform_name: str): + self.platform_stats[platform_name] = self.platform_stats.get(platform_name, 0) + 1 def clear(self): self.platform_stats.clear()