feat: 本地指标收集到数据库

This commit is contained in:
Soulter
2024-12-02 22:20:24 +08:00
parent 446c50da80
commit a01e865042
5 changed files with 17 additions and 8 deletions
+4 -1
View File
@@ -1,5 +1,8 @@
from .log import LogManager, LogBroker
from astrbot.core.utils.t2i.renderer import HtmlRenderer
from astrbot.core.db.sqlite import SQLiteDatabase
from astrbot.core.config.default import DB_PATH
html_renderer = HtmlRenderer()
logger = LogManager.GetLogger(log_name='astrbot')
logger = LogManager.GetLogger(log_name='astrbot')
db_helper = SQLiteDatabase(DB_PATH)
+1 -2
View File
@@ -15,10 +15,9 @@ class Provider(abc.ABC):
# 维护了 session_id 的上下文,不包含 system 指令
self.session_memory = defaultdict(list)
self.curr_personality = Personality(prompt=default_personality, name="")
self.db_helper = db_helper
if persistant_history:
# 读取历史记录
self.db_helper = db_helper
try:
for history in db_helper.get_llm_history():
self.session_memory[history.session_id] = json.loads(history.content)
+10
View File
@@ -2,6 +2,7 @@ import aiohttp
import sys
import logging
from astrbot.core.config import VERSION
from astrbot.core import db_helper, logger
logger = logging.getLogger("astrbot")
@@ -19,6 +20,15 @@ class Metric():
payload = {
"metrics_data": kwargs
}
try:
if 'adapter_name' in kwargs:
db_helper.insert_platform_metrics({kwargs['adapter_name']: 1})
if 'llm_name' in kwargs:
db_helper.insert_llm_metrics({kwargs['llm_name']: 1})
except Exception as e:
logger.error(f"保存指标到数据库失败: {e}")
pass
try:
async with aiohttp.ClientSession() as session:
async with session.post(base_url, json=payload, timeout=3) as response:
+2 -4
View File
@@ -7,10 +7,9 @@ import aiohttp
import zipfile
from typing import List
from astrbot.core.core_lifecycle import AstrBotCoreLifecycle
from astrbot.core.db.sqlite import SQLiteDatabase
from astrbot.core.config import DB_PATH
from astrbot.dashboard import AstrBotDashBoardLifecycle
from astrbot.core import db_helper
from astrbot.core import logger, LogManager, LogBroker
# add parent path to sys.path
@@ -92,8 +91,7 @@ if __name__ == "__main__":
# check dashboard files
asyncio.run(check_dashboard_files())
# start db
db = SQLiteDatabase(DB_PATH)
db = db_helper
# print logo
logger.info(logo_tmpl)
@@ -19,7 +19,6 @@ from dataclasses import asdict
class ProviderOpenAIOfficial(Provider):
def __init__(self, llm_config: LLMConfig, db_helper: BaseDatabase, persistant_history = True) -> None:
super().__init__(db_helper, llm_config.default_personality, persistant_history)
self.api_keys = []
self.chosen_api_key = None
self.base_url = None