Skip to content

Commit

Permalink
fix: metrics
Browse files Browse the repository at this point in the history
perf: aiocqhttp image url
  • Loading branch information
Soulter committed Aug 12, 2024
1 parent 4c8a5c3 commit 1507f54
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
9 changes: 6 additions & 3 deletions model/platform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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


async def record_metrics(self):
self.context.metrics_uploader.increment_platform_stat(self.PLATFORM_NAME)
7 changes: 5 additions & 2 deletions model/platform/qq_aiocqhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions model/platform/qq_nakuru.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion model/platform/qq_official.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions util/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1507f54

Please sign in to comment.