From 33f87ff7d7632f589d0af1486116331911ce0bf3 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Tue, 15 Apr 2025 21:08:35 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf:=20enhance=20metrics=20trac?= =?UTF-8?q?king=20with=20installation=20ID=20and=20sender=20ID=20hashing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/astr_message_event.py | 9 ++++- astrbot/core/utils/metrics.py | 41 +++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/astr_message_event.py b/astrbot/core/platform/astr_message_event.py index 96a7ad6f1..dba7f338a 100644 --- a/astrbot/core/platform/astr_message_event.py +++ b/astrbot/core/platform/astr_message_event.py @@ -1,5 +1,7 @@ import abc import asyncio +import hashlib +import uuid from dataclasses import dataclass from typing import List, Union, Optional, AsyncGenerator @@ -384,8 +386,13 @@ class AstrMessageEvent(abc.ABC): Args: message (MessageChain): 消息链,具体使用方式请参考文档。 """ + # Leverage BLAKE2 hash function to generate a non-reversible hash of the sender ID for privacy. + hash_obj = hashlib.blake2b(self.get_sender_id().encode("utf-8"), digest_size=16) + sid = str(uuid.UUID(bytes=hash_obj.digest())) asyncio.create_task( - Metric.upload(msg_event_tick=1, adapter_name=self.platform_meta.name) + Metric.upload( + msg_event_tick=1, adapter_name=self.platform_meta.name, sid=sid + ) ) self._has_send_oper = True diff --git a/astrbot/core/utils/metrics.py b/astrbot/core/utils/metrics.py index 9fc986cc0..ab4c3771d 100644 --- a/astrbot/core/utils/metrics.py +++ b/astrbot/core/utils/metrics.py @@ -1,10 +1,42 @@ import aiohttp import sys +import os +import socket +import uuid from astrbot.core.config import VERSION from astrbot.core import db_helper, logger class Metric: + _iid_cache = None + + @staticmethod + def get_installation_id(): + """获取或创建一个唯一的安装ID""" + if Metric._iid_cache is not None: + return Metric._iid_cache + + config_dir = os.path.join(os.path.expanduser("~"), ".astrbot") + id_file = os.path.join(config_dir, ".installation_id") + + if os.path.exists(id_file): + try: + with open(id_file, "r") as f: + Metric._iid_cache = f.read().strip() + return Metric._iid_cache + except Exception: + pass + try: + os.makedirs(config_dir, exist_ok=True) + installation_id = str(uuid.uuid4()) + with open(id_file, "w") as f: + f.write(installation_id) + Metric._iid_cache = installation_id + return installation_id + except Exception: + Metric._iid_cache = "null" + return "null" + @staticmethod async def upload(**kwargs): """ @@ -16,6 +48,14 @@ class Metric: kwargs["v"] = VERSION kwargs["os"] = sys.platform payload = {"metrics_data": kwargs} + try: + kwargs["hn"] = socket.gethostname() + except Exception: + pass + try: + kwargs["iid"] = Metric.get_installation_id() + except Exception: + pass try: if "adapter_name" in kwargs: db_helper.insert_platform_metrics({kwargs["adapter_name"]: 1}) @@ -24,6 +64,7 @@ class Metric: except Exception as e: logger.error(f"保存指标到数据库失败: {e}") pass + print(f"上传指标: {payload}") try: async with aiohttp.ClientSession(trust_env=True) as session: